From d3019b57aee89422600cdec6854403d645c90a06 Mon Sep 17 00:00:00 2001 From: Ashkan Kiani Date: Wed, 13 Nov 2019 13:23:32 -0800 Subject: Improve skeleton --- lua/common_lsp/skeleton.lua | 51 ++++++++++++++++------------ lua/common_lsp/texlab.lua | 82 +++++++++++++++++++++++++-------------------- lua/common_lsp/util.lua | 21 ++++++++++++ 3 files changed, 97 insertions(+), 57 deletions(-) (limited to 'lua') diff --git a/lua/common_lsp/skeleton.lua b/lua/common_lsp/skeleton.lua index 72828474..f030030a 100644 --- a/lua/common_lsp/skeleton.lua +++ b/lua/common_lsp/skeleton.lua @@ -4,9 +4,11 @@ local inspect = vim.inspect local M = {} +M.name = "SKELETON" + local default_config default_config = { - name = "SKELETON"; + name = M.name; cmd = {"SKELETON"}; filetype = {"SKELETON"}; log_level = lsp.protocol.MessageType.Warning; @@ -32,7 +34,8 @@ local function setup_callbacks(config) for _, item in ipairs(params.items) do if item.section then local value = util.lookup_section(config.settings, item.section) or vim.NIL - print(string.format("config[%q] = %s", item.section, inspect(value))) + -- Uncomment this to debug. + -- print(string.format("config[%q] = %s", item.section, inspect(value))) table.insert(result, value) end end @@ -40,13 +43,6 @@ local function setup_callbacks(config) end end -M.name = "SKELETON" - -local function module_fn(fn_body) - validate { fn_body = {fn_body, 's'} } - return string.format("lua require 'common_lsp/%s'.%s", M.name, fn_body) -end - -- A function to set up SKELETON easier. -- -- Additionally, it sets up the following commands: @@ -93,7 +89,8 @@ function M.setup(config) M._setup_buffer() else api.nvim_command(string.format( - "autocmd BufEnter ++once lua require'common_lsp/SKELETON'._setup_buffer()", + "autocmd BufEnter ++once lua require'common_lsp/%s'._setup_buffer()", + M.name, bufnr)) end end) @@ -101,22 +98,34 @@ function M.setup(config) lsp.add_filetype_config(config) end +-- Declare any commands here. You can use additional modifiers like "-range" +-- which will be added as command options. All of these commands are buffer +-- level by default. +M.commands = { + SKELETON_FORMAT = { + function() + M.buf_SPOOKY_FUNCTION(0) + end; + "-range"; + }; + SKELETON_SPOOKY_COMMAND = { + function() + local bufnr = util.validate_bufnr(0) + print("SPOOKY COMMAND STUFF!", bufnr) + end; + }; +} + function M._setup_buffer() - local commands = { - "command! SKELETON_SPOOKY_COMMAND -buffer "..module_fn("buf_SPOOKY_FUNCTION(0)"); - "command! SKELETON_OTHER_COMMAND -buffer "..module_fn("buf_OTHER_FUNCTION(0)"); - } - for _, command in ipairs(commands) do - api.nvim_command(command) - end -end + -- Do other setup here if you want. -function M.buf_SPOOKY_FUNCTION(bufnr) - bufnr = util.validate_bufnr(bufnr) + -- Create the module commands + util.create_module_commands(M.name, M.commands) end -function M.buf_OTHER_FUNCTION(bufnr) +function M.buf_SPOOKY_FUNCTION(bufnr) bufnr = util.validate_bufnr(bufnr) + print("SPOOKY FUNCTION STUFF!", bufnr) end return M diff --git a/lua/common_lsp/texlab.lua b/lua/common_lsp/texlab.lua index 2fd74c81..592e0499 100644 --- a/lua/common_lsp/texlab.lua +++ b/lua/common_lsp/texlab.lua @@ -1,19 +1,19 @@ -local api = vim.api -local validate = vim.validate local util = require 'common_lsp/util' -local lsp = vim.lsp +local api, lsp = vim.api, vim.lsp local M = {} +M.name = "texlab" + -- TODO support more of https://github.com/microsoft/vscode-languageserver-node/blob/master/protocol/src/protocol.progress.proposed.md local default_config default_config = { - name = "texlab"; + name = M.name; cmd = {"texlab"}; filetype = {"tex", "bib"}; - texlab_log_level = lsp.protocol.MessageType.Warning; - texlab_settings = { + log_level = lsp.protocol.MessageType.Warning; + settings = { latex = { build = { args = {"-pdf", "-interaction=nonstopmode", "-synctex=1"}; @@ -28,23 +28,20 @@ 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 + if params and params.type <= config.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.texlab_settings, item.section) or vim.NIL - print(string.format("config[%q] = %s", item.section, vim.inspect(value))) + local value = util.lookup_section(config.settings, item.section) or vim.NIL table.insert(result, value) end end @@ -60,29 +57,29 @@ end -- {config} is the same as |vim.lsp.add_filetype_config()|, but with some -- additions and changes: -- --- {texlab_log_level} +-- {name} +-- Defaults to "texlab" +-- +-- {cmd} +-- Defaults to {"texlab"} +-- +-- {filetype} +-- Defaults to {"tex", "bib"} +-- +-- {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. -- --- {texlab_settings} +-- {settings} -- The settings specified here https://texlab.netlify.com/docs/reference/configuration. -- This is a table, and the keys are case sensitive. --- Example: `texlab_settings = { latex = { build = { executable = "latexmk" } } }` --- --- {filetype} --- Defaults to {"tex", "bib"} --- --- {cmd} --- Defaults to {"texlab"} --- --- {name} --- Defaults to "texlab" +-- Example: `settings = { latex = { build = { onSave = true; } } }` function M.setup(config) config = vim.tbl_extend("keep", config, default_config) - util.tbl_deep_extend(config.texlab_settings, default_config.texlab_settings) + 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, { @@ -97,16 +94,30 @@ function M.setup(config) if bufnr == api.nvim_get_current_buf() then M._setup_buffer() else - util.nvim_multiline_command(string.format("autocmd BufEnter ++once lua require'common_lsp/texlab'._setup_buffer()", bufnr)) + api.nvim_command(string.format( + "autocmd BufEnter ++once lua require'common_lsp/%s'._setup_buffer()", + M.name, + bufnr)) end end) + lsp.add_filetype_config(config) end +-- Declare any commands here. You can use additional modifiers like "-range" +-- which will be added as command options. All of these commands are buffer +-- level by default. +M.commands = { + TexlabBuild = { + function() + M.buf_build(0) + end; + }; +} + function M._setup_buffer() - util.nvim_multiline_command [[ - command! TexlabBuild -buffer lua require'common_lsp/texlab'.texlab_buf_build(0) - ]] + -- Create the module commands + util.create_module_commands(M.name, M.commands) end local texlab_build_status = vim.tbl_add_reverse_lookup { @@ -116,21 +127,20 @@ local texlab_build_status = vim.tbl_add_reverse_lookup { Cancelled = 3; } -function M.texlab_buf_build(bufnr) +function M.buf_build(bufnr) bufnr = util.validate_bufnr(bufnr) local params = { textDocument = { uri = vim.uri_from_bufnr(bufnr) } } - lsp.buf_request(bufnr, 'textDocument/build', params, function(err, method, result, client_id) - if err then error(tostring(err)) end - print("Build "..texlab_build_status[result.status]) - end) + lsp.buf_request(bufnr, 'textDocument/build', params, + function(err, _, result, _) + if err then error(tostring(err)) end + print("Build "..texlab_build_status[result.status]) + end) end -M.buf_build = M.texlab_buf_build - -- bufnr isn't actually required here, but we need a valid buffer in order to -- be able to find the client for buf_request. -- TODO find a client by looking through buffers for a valid client? -function M.texlab_build_cancel_all(bufnr) +function M.build_cancel_all(bufnr) bufnr = util.validate_bufnr(bufnr) local params = { token = "texlab-build-*" } lsp.buf_request(bufnr, 'window/progress/cancel', params, function(err, method, result, client_id) diff --git a/lua/common_lsp/util.lua b/lua/common_lsp/util.lua index ee48b5d7..1aebbfb6 100644 --- a/lua/common_lsp/util.lua +++ b/lua/common_lsp/util.lua @@ -68,6 +68,27 @@ function M.lookup_section(settings, section) return settings end +function M.create_module_commands(module_name, commands) + for command_name, def in pairs(commands) do + local parts = {"command!"} + -- Insert attributes. + for k, v in pairs(def) do + if type(k) == 'string' and type(v) == 'boolean' and v then + table.insert(parts, "-"..k) + elseif type(k) == 'number' and type(v) == 'string' and v:match("^%-") then + table.insert(parts, v) + end + end + table.insert(parts, command_name) + -- The command definition. + table.insert(parts, + string.format("lua require'common_lsp/%s'.commands[%q][1]()", module_name, command_name)) + local command = table.concat(parts, " ") + print(command) + api.nvim_command(command) + end +end + return M -- vim:et ts=2 sw=2 -- cgit v1.2.3-70-g09d2