diff options
| author | Lorenzo Bellina <59364991+TheRealLorenz@users.noreply.github.com> | 2025-04-13 00:15:43 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-12 15:15:43 -0700 |
| commit | 81a570f58b2113cc2d538efae743ad38d6ab564f (patch) | |
| tree | 74f9691bbb1b303cd7cfaab7b543f82cb0dc6b46 /lsp/tinymist.lua | |
| parent | refactor: replace vim.loop with vim.uv #3703 (diff) | |
| download | nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar.gz nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar.bz2 nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar.lz nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar.xz nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar.zst nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.zip | |
feat: migrate to vim.lsp.config #3659
Problem:
Nvim 0.11 has vim.lsp.config, which mostly replaces the legacy
nvim-lspconfig "framework".
Solution:
Migrate all configs to `lsp/*` variants. The old configs in
`lua/lspconfig/` are "frozen".
The new configs include these changes:
- `commands` field became raw calls to
`vim.api.nvim_buf_create_user_command` inside `on_attach`.
- `root_dir` became:
- `root_markers` whenever the file list was simple didn't need to mach `*`
- if the logic was complicated, or needed to match something like
'\*.c', it was defined as a vim.lsp.Config `root_dir` callback.
- `on_config_change` became `before_init`. I don't actually know if this
is the correct approach, but looking around the documentation of
`nvim-lspconfig` a saw that it was defined as the function that gets
called as soon as the config have `root_dir`, and so I thought
`before_init` might be the closest alternative.
- `docs.description` became a luadoc `@brief` docstring.
- `single_file_support = false`?
Co-authored-by: Aliou Diallo <aliou@users.noreply.github.com>
Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
Diffstat (limited to 'lsp/tinymist.lua')
| -rw-r--r-- | lsp/tinymist.lua | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/lsp/tinymist.lua b/lsp/tinymist.lua new file mode 100644 index 00000000..369b7a30 --- /dev/null +++ b/lsp/tinymist.lua @@ -0,0 +1,79 @@ +---@param command_name string +---@return fun():nil run_tinymist_command, string cmd_name, string cmd_desc +local function create_tinymist_command(command_name) + local export_type = command_name:match 'tinymist%.export(%w+)' + local info_type = command_name:match 'tinymist%.(%w+)' + if info_type and info_type:match '^get' then + info_type = info_type:gsub('^get', 'Get') + end + local cmd_display = export_type or info_type + ---Execute the Tinymist command, supporting both 0.10 and 0.11 exec methods + ---@return nil + local function run_tinymist_command() + local bufnr = vim.api.nvim_get_current_buf() + local client = vim.lsp.get_clients({ name = 'tinymist', buffer = bufnr })[1] + if not client then + return vim.notify('No Tinymist client attached to the current buffer', vim.log.levels.ERROR) + end + local arguments = { vim.api.nvim_buf_get_name(bufnr) } + local title_str = export_type and ('Export ' .. cmd_display) or cmd_display + ---@type lsp.Handler + local function handler(err, res) + if err then + return vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR) + end + -- If exporting, show the string result; else, show the table for inspection + vim.notify(export_type and res or vim.inspect(res), vim.log.levels.INFO) + end + if vim.fn.has 'nvim-0.11' == 1 then + -- For Neovim 0.11+ + return client:exec_cmd({ + title = title_str, + command = command_name, + arguments = arguments, + }, { bufnr = bufnr }, handler) + else + return vim.notify('Tinymist commands require Neovim 0.11+', vim.log.levels.WARN) + end + end + -- Construct a readable command name/desc + local cmd_name = export_type and ('LspTinymistExport' .. cmd_display) or ('LspTinymist' .. cmd_display) ---@type string + local cmd_desc = export_type and ('Export to ' .. cmd_display) or ('Get ' .. cmd_display) ---@type string + return run_tinymist_command, cmd_name, cmd_desc +end + +---@brief +--- +---https://github.com/Myriad-Dreamin/tinymist +-- An integrated language service for Typst [taɪpst]. You can also call it "微霭" [wēi ǎi] in Chinese. +-- +-- Currently some of Tinymist's workspace commands are supported, namely: +-- `LspTinymistExportSvg`, `LspTinymistExportPng`, `LspTinymistExportPdf +-- `LspTinymistExportMarkdown`, `LspTinymistExportText`, `LspTinymistExportQuery`, +-- `LspTinymistExportAnsiHighlight`, `LspTinymistGetServerInfo`, +-- `LspTinymistGetDocumentTrace`, `LspTinymistGetWorkspaceLabels`, and +-- `LspTinymistGetDocumentMetrics`. +return { + cmd = { 'tinymist' }, + filetypes = { 'typst' }, + root_markers = { '.git' }, + on_attach = function(_) + for _, command in ipairs { + 'tinymist.exportSvg', + 'tinymist.exportPng', + 'tinymist.exportPdf', + -- 'tinymist.exportHtml', -- Use typst 0.13 + 'tinymist.exportMarkdown', + 'tinymist.exportText', + 'tinymist.exportQuery', + 'tinymist.exportAnsiHighlight', + 'tinymist.getServerInfo', + 'tinymist.getDocumentTrace', + 'tinymist.getWorkspaceLabels', + 'tinymist.getDocumentMetrics', + } do + local cmd_func, cmd_name, cmd_desc = create_tinymist_command(command) + vim.api.nvim_create_user_command(cmd_name, cmd_func, { nargs = 0, desc = cmd_desc }) + end + end, +} |
