diff options
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/common_lsp.lua | 7 | ||||
| -rw-r--r-- | lua/common_lsp/skeleton.lua | 104 | ||||
| -rw-r--r-- | lua/common_lsp/texlab.lua | 41 | ||||
| -rw-r--r-- | lua/common_lsp/util.lua | 15 |
4 files changed, 139 insertions, 28 deletions
diff --git a/lua/common_lsp.lua b/lua/common_lsp.lua new file mode 100644 index 00000000..d412bb15 --- /dev/null +++ b/lua/common_lsp.lua @@ -0,0 +1,7 @@ +local M = { + texlab = require 'common_lsp/texlab'; +} + + +return M +-- vim:et ts=2 sw=2 diff --git a/lua/common_lsp/skeleton.lua b/lua/common_lsp/skeleton.lua new file mode 100644 index 00000000..b08307ca --- /dev/null +++ b/lua/common_lsp/skeleton.lua @@ -0,0 +1,104 @@ +local api = vim.api +local validate = vim.validate +local util = require 'common_lsp/util' +local lsp = vim.lsp + +local M = {} + +local default_config +default_config = { + name = "SKELETON"; + cmd = {"SKELETON"}; + filetype = {"c", "cpp"}; + SKELETON_log_level = lsp.protocol.MessageType.Warning; + SKELETON_settings = {}; +} + +local function setup_callbacks(config) + config.callbacks = config.callbacks or {} + + config.callbacks["window/logMessage"] = function(err, method, params, client_id) + if params and params.type <= config.texlab_log_level then + lsp.builtin_callbacks[method](err, method, params, client_id) + end + end + + -- TODO use existing callback? + config.callbacks["workspace/configuration"] = function(err, method, params, client_id) + if err then error(tostring(err)) end + if not params.items then + return {} + end + + local result = {} + for _, item in ipairs(params.items) do + if item.section then + local value = util.lookup_section(config, item.section) or vim.NIL + print(string.format("config[%q] = %s", item.section, vim.inspect(value))) + table.insert(result, value) + end + end + return result + end +end + +-- A function to set up SKELETON easier. +-- +-- Additionally, it sets up the following commands: +-- - SKELETONCommand +-- +-- {config} is the same as |vim.lsp.add_filetype_config()|, but with some +-- additions and changes: +-- +-- {SKELETON_log_level} +-- controls the level of logs to show from build processes and other +-- window/logMessage events. By default it is set to +-- vim.lsp.protocol.MessageType.Warning instead of +-- vim.lsp.protocol.MessageType.Log. +-- +-- {SKELETON_settings} +-- This is a table, and the keys are case sensitive. +-- Example: `SKELETON_settings = { }` +-- +-- {filetype} +-- Defaults to {"c", "cpp"} +-- +-- {cmd} +-- Defaults to {"SKELETON"} +-- +-- {name} +-- Defaults to "SKELETON" +function M.setup(config) + config = vim.tbl_extend("keep", config, default_config) + + util.tbl_deep_extend(config.settings, default_config.settings) + + config.capabilities = config.capabilities or vim.lsp.protocol.make_client_capabilities() + util.tbl_deep_extend(config.capabilities, { + workspace = { + configuration = true; + } + }) + + setup_callbacks(config) + + config.on_attach = util.add_hook_after(config.on_attach, function(client, bufnr) + if bufnr == api.nvim_get_current_buf() then + M._setup_buffer() + else + util.nvim_multiline_command(string.format("autocmd BufEnter <buffer=%d> ++once lua require'common_lsp/SKELETON'._setup_buffer()", bufnr)) + end + end) + + -- TODO should this find the project root instead? + lsp.add_filetype_config(config) +end + +function M._setup_buffer() + util.nvim_multiline_command [[ + ]] +end + +return M +-- vim:et ts=2 sw=2 + diff --git a/lua/common_lsp/texlab.lua b/lua/common_lsp/texlab.lua index 44f468d8..a24393e1 100644 --- a/lua/common_lsp/texlab.lua +++ b/lua/common_lsp/texlab.lua @@ -5,23 +5,8 @@ local lsp = vim.lsp local M = {} -local texlab_build_status = vim.tbl_add_reverse_lookup { - Success = 0; - Error = 1; - Failure = 2; - Cancelled = 3; -} - -- TODO support more of https://github.com/microsoft/vscode-languageserver-node/blob/master/protocol/src/protocol.progress.proposed.md -local function lookup_configuration(config, section) - local settings = config.texlab_settings - for part in vim.gsplit(section, '.', true) do - settings = settings[part] - end - return settings -end - local default_config default_config = { name = "texlab"; @@ -39,13 +24,6 @@ default_config = { } } -local function nvim_command(command) - validate { command = { command, 's' } } - for line in vim.gsplit(command, "\n", true) do - api.nvim_command(line) - end -end - local function setup_callbacks(config) config.callbacks = config.callbacks or {} @@ -65,7 +43,7 @@ local function setup_callbacks(config) local result = {} for _, item in ipairs(params.items) do if item.section then - local value = lookup_configuration(config, item.section) or vim.NIL + local value = util.lookup_section(config.texlab_settings, item.section) or vim.NIL print(string.format("config[%q] = %s", item.section, vim.inspect(value))) table.insert(result, value) end @@ -101,7 +79,7 @@ end -- -- {name} -- Defaults to "texlab" -function M.texlab(config) +function M.setup(config) config = vim.tbl_extend("keep", config, default_config) util.tbl_deep_extend(config.texlab_settings, default_config.texlab_settings) @@ -117,20 +95,27 @@ function M.texlab(config) config.on_attach = util.add_hook_after(config.on_attach, function(client, bufnr) if bufnr == api.nvim_get_current_buf() then - M.texlab_setup_commands() + M._setup_buffer() else - nvim_command(string.format("autocmd BufEnter <buffer=%d> ++once lua require'common_lsp/texlab'.texlab_setup_commands()", bufnr)) + util.nvim_multiline_command(string.format("autocmd BufEnter <buffer=%d> ++once lua require'common_lsp/texlab'._setup_buffer()", bufnr)) end end) lsp.add_filetype_config(config) end -function M.texlab_setup_commands() - nvim_command [[ +function M._setup_buffer() + util.nvim_multiline_command [[ command! TexlabBuild lua require'common_lsp/texlab'.texlab_buf_build(0) ]] end +local texlab_build_status = vim.tbl_add_reverse_lookup { + Success = 0; + Error = 1; + Failure = 2; + Cancelled = 3; +} + function M.texlab_buf_build(bufnr) bufnr = util.validate_bufnr(bufnr) local params = { textDocument = { uri = vim.uri_from_bufnr(bufnr) } } diff --git a/lua/common_lsp/util.lua b/lua/common_lsp/util.lua index 2de92447..ee48b5d7 100644 --- a/lua/common_lsp/util.lua +++ b/lua/common_lsp/util.lua @@ -54,5 +54,20 @@ function M.tbl_deep_extend(dst, ...) return dst end +function M.nvim_multiline_command(command) + validate { command = { command, 's' } } + for line in vim.gsplit(command, "\n", true) do + api.nvim_command(line) + end +end + +function M.lookup_section(settings, section) + for part in vim.gsplit(section, '.', true) do + settings = settings[part] + end + return settings +end + + return M -- vim:et ts=2 sw=2 |
