Skip to content

Commit 9c1993a

Browse files
feat(tinymist): implement simple workspace commands
1 parent 84e0cd5 commit 9c1993a

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

lua/lspconfig/configs/tinymist.lua

+71
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,73 @@
1+
---@param command_name string
2+
---@return fun():nil run_tinymist_command, string cmd_name, string cmd_desc
3+
local function create_tinymist_command(command_name)
4+
local export_type = command_name:match 'tinymist%.export(%w+)'
5+
local info_type = command_name:match 'tinymist%.(%w+)'
6+
if info_type and info_type:match '^get' then
7+
info_type = info_type:gsub('^get', 'Get')
8+
end
9+
local cmd_display = export_type or info_type
10+
---Execute the Tinymist command, supporting both 0.10 and 0.11 exec methods
11+
---@return nil
12+
local function run_tinymist_command()
13+
local bufnr = vim.api.nvim_get_current_buf()
14+
local client = require 'lspconfig.util'.get_active_client_by_name(bufnr, 'tinymist')
15+
if not client then
16+
return vim.notify('No Tinymist client attached to the current buffer', vim.log.levels.ERROR)
17+
end
18+
local arguments = { vim.uri_to_fname(vim.uri_from_bufnr(bufnr)) }
19+
local title_str = export_type and ('Export ' .. cmd_display) or cmd_display
20+
---@type lsp.Handler
21+
local function handler(err, res)
22+
if err then
23+
return vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR)
24+
end
25+
-- If exporting, show the string result; else, show the table for inspection
26+
vim.notify(export_type and res or vim.inspect(res), vim.log.levels.INFO)
27+
end
28+
if vim.fn.has 'nvim-0.11' == 1 then
29+
-- For Neovim 0.11+
30+
return client:exec_cmd({
31+
title = title_str,
32+
command = command_name,
33+
arguments = arguments,
34+
}, { bufnr = bufnr }, handler)
35+
else
36+
-- For Neovim < 0.11
37+
return client.request('workspace/executeCommand', {
38+
command = command_name,
39+
arguments = arguments,
40+
}, handler, bufnr)
41+
end
42+
end
43+
-- Construct a readable command name/desc
44+
local cmd_name = export_type and ('TinymistExport' .. cmd_display) or ('Tinymist' .. cmd_display) ---@type string
45+
local cmd_desc = export_type and ('Export to ' .. cmd_display) or ('Get ' .. cmd_display) ---@type string
46+
return run_tinymist_command, cmd_name, cmd_desc
47+
end
48+
49+
local tinymist_commands = {}
50+
for _, cmd_val in ipairs({
51+
'tinymist.exportSvg',
52+
'tinymist.exportPng',
53+
'tinymist.exportPdf',
54+
-- 'tinymist.exportHtml', -- Use typst 0.13
55+
'tinymist.exportMarkdown',
56+
'tinymist.exportText',
57+
'tinymist.exportQuery',
58+
'tinymist.exportAnsiHighlight',
59+
'tinymist.getServerInfo',
60+
'tinymist.getDocumentTrace',
61+
'tinymist.getWorkspaceLabels',
62+
'tinymist.getDocumentMetrics',
63+
}) do
64+
local run_cmd, name, desc = create_tinymist_command(cmd_val)
65+
tinymist_commands[name] = {
66+
run_cmd,
67+
description = desc,
68+
}
69+
end
70+
171
return {
272
default_config = {
373
cmd = { 'tinymist' },
@@ -7,6 +77,7 @@ return {
777
end,
878
single_file_support = true,
979
},
80+
commands = tinymist_commands,
1081
docs = {
1182
description = [[
1283
https://github.com/Myriad-Dreamin/tinymist

0 commit comments

Comments
 (0)