aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-11-21 20:08:58 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2023-11-21 20:19:38 +0100
commitaf541188335dda55594d0c7de92b823e181049de (patch)
treec6d5dfaa0749bef825edea60e66528c70d4d1fdb
parent391e0d3345910d1c51e39f7080f806c9f2170003 (diff)
downloadvis-lspc-af541188335dda55594d0c7de92b823e181049de.tar.gz
vis-lspc-af541188335dda55594d0c7de92b823e181049de.zip
include some of the ls_* methods in the language server's table
Including methods in the language server table allows them to be used outside of vis-lspc. Additionally, I think this makes more sense to use them as methods with the ls:method(...) syntax instead of calling them as ls_*(ls, ...).
-rw-r--r--init.lua102
1 files changed, 59 insertions, 43 deletions
diff --git a/init.lua b/init.lua
index 675488f..90e7ddc 100644
--- a/init.lua
+++ b/init.lua
@@ -1180,46 +1180,12 @@ end
-- Initiate the shutdown of a language server
-- Sending the exit notification and closing the file handle are done in
-- the shutdown response handler.
-local function ls_shutdown_server(ls)
+local function ls_shutdown(ls)
ls_call_method(ls, 'shutdown')
end
-local function ls_start_server(syntax)
- local ls_conf = lspc.ls_map[syntax]
- if not ls_conf then
- return nil, 'No language server available for ' .. syntax
- end
-
- local exe = ls_conf.cmd:gmatch('%S+')()
- if not os.execute('type ' .. exe .. '>/dev/null 2>/dev/null') then
- -- remove the configured language server
- lspc.ls_map[syntax] = nil
- local msg = string.format('Language server for %s configured but %s not found', syntax, exe)
- -- the warning will be visual if the language server was automatically startet
- -- if the user tried to start teh server manually they will see msg as error
- lspc_warn(msg)
- return nil, msg
-
- end
-
- if lspc.running[ls_conf.name] then
- return nil, 'Already a language server running for ' .. syntax
- end
-
- local ls = {
- name = ls_conf.name,
- settings = ls_conf.settings,
- formatting_options = ls_conf.formatting_options,
- initialized = false,
- id = 0,
- inflight = {},
- open_files = {},
- parser = parser.Parser(),
- }
-
- ls.fd = vis:communicate(ls_conf.name, ls_conf.cmd)
-
- lspc.running[ls_conf.name] = ls
+local function ls_start(ls, init_options)
+ ls.fd = vis:communicate(ls.name, ls.cmd)
-- register the response handler
vis.events.subscribe(vis.events.PROCESS_RESPONSE, function(name, event, code, msg)
@@ -1253,11 +1219,61 @@ local function ls_start_server(syntax)
capabilities = lspc.client_capabilites,
}
- if ls_conf.init_options then
- params.initializationOptions = ls_conf.init_options
+ if init_options then
+ params.initializationOptions = init_options
+ end
+
+ ls:call_method('initialize', params)
+end
+
+local function new_ls(ls_conf)
+ local ls = {
+ name = ls_conf.name,
+ cmd = ls_conf.cmd,
+ settings = ls_conf.settings,
+ initialized_hook = ls_conf.initialized_hook,
+ file_open_hook = ls_conf.file_open_hook,
+ formatting_options = ls_conf.formatting_options,
+ initialized = false,
+ id = 0,
+ inflight = {},
+ open_files = {},
+ parser = parser.Parser(),
+
+ -- exported methods of a language server
+ send_notification = ls_send_notification,
+ send_did_change = ls_send_did_change,
+ call_method = ls_call_method,
+ shutdown = ls_shutdown,
+ }
+
+ return ls
+end
+
+local function lspc_start_server(syntax)
+ local ls_conf = lspc.ls_map[syntax]
+ if not ls_conf then
+ return nil, 'No language server available for ' .. syntax
+ end
+
+ local exe = ls_conf.cmd:gmatch('%S+')()
+ if not os.execute('type ' .. exe .. '>/dev/null 2>/dev/null') then
+ -- remove the configured language server
+ lspc.ls_map[syntax] = nil
+ local msg = string.format('Language server for %s configured but %s not found', syntax, exe)
+ -- the warning will be visual if the language server was automatically startet
+ -- if the user tried to start teh server manually they will see msg as error
+ lspc_warn(msg)
+ return nil, msg
+ end
+
+ if lspc.running[ls_conf.name] then
+ return nil, 'Already a language server running for ' .. syntax
end
- ls_call_method(ls, 'initialize', params)
+ local ls = new_ls(ls_conf)
+ lspc.running[ls_conf.name] = ls
+ ls_start(ls, ls_conf.init_options)
return ls
end
@@ -1558,7 +1574,7 @@ vis:command_register('lspc-start-server', function(argv, _, win)
lspc_err('no language specified')
end
- local _, err = ls_start_server(syntax)
+ local _, err = lspc_start_server(syntax)
if err then
lspc_err(err)
end
@@ -1571,7 +1587,7 @@ vis:command_register('lspc-shutdown-server', function(argv, _, win)
return
end
- ls_shutdown_server(ls)
+ ls:shutdown()
end)
vis:command_register('lspc-close', function(_, _, win)
@@ -1620,7 +1636,7 @@ end)
vis.events.subscribe(vis.events.WIN_OPEN, function(win)
if lspc.autostart and win.syntax then
- ls_start_server(win.syntax)
+ lspc_start_server(win.syntax)
end
assert(win:style_define(lspc.diagnostic_style_id, lspc.diagnostic_style))