From 655dafe59e35a2bc7bd0785c342c47e3ea860c70 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Mon, 27 Dec 2021 20:46:39 +0100 Subject: try multiple json implementations We try 'json', 'cjson' and 'dkjson' in that order before using our bundled fallback implementation. --- README.md | 1 + init.lua | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c6c7aff..3d5213f 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Currently only clangd and pyls are configured by default and somewhat tested. * Optional: the json implementation of your choice * must be usable by calling `require('json')` * must provide `json.encode, json.decode` + * vis-lspc tries to find a suitable json implementation using those candidates: json, cjson, dkjson ## Installation diff --git a/init.lua b/init.lua index c8e0f04..e2dc90e 100644 --- a/init.lua +++ b/init.lua @@ -11,19 +11,27 @@ if not vis.communicate then return {} end -local function load_fallback_json() - return dofile(source_path .. 'json.lua') -end +local json +local json_impls = {'json', 'cjson', 'dkjson'} -- find a suitable json implementation -local json -if vis:module_exist('json') then - json = require('json') - if not json.encode or not json.decode then - json = load_fallback_json() +for _, json_impl in ipairs(json_impls) do + if vis:module_exist(json_impl) then + json = require(json_impl) + if not json.encode or not json.decode then + json = nil + end + + -- found a usable json implementation + if json then + break + end end -else - json = load_fallback_json() +end + +-- We found no suitable implementation in json_impls -> use our fallback +if not json then + json = dofile(source_path .. 'json.lua') end local jsonrpc = {} -- cgit v1.2.3