Skip to content

Commit 456f775

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

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

lua/lspconfig/configs/tinymist.lua

+66
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
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 = vim.lsp.get_clients({ name = 'tinymist', buffer = bufnr })[1]
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.api.nvim_buf_get_name(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+
return vim.notify('Tinymist commands require Neovim 0.11+', vim.log.levels.WARN)
37+
end
38+
end
39+
-- Construct a readable command name/desc
40+
local cmd_name = export_type and ('LspTinymistExport' .. cmd_display) or ('LspTinymist' .. cmd_display) ---@type string
41+
local cmd_desc = export_type and ('Export to ' .. cmd_display) or ('Get ' .. cmd_display) ---@type string
42+
return run_tinymist_command, cmd_name, cmd_desc
43+
end
44+
145
return {
246
default_config = {
347
cmd = { 'tinymist' },
@@ -6,11 +50,33 @@ return {
650
return vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1])
751
end,
852
single_file_support = true,
53+
on_attach = function(_)
54+
for _, command in ipairs {
55+
'tinymist.exportSvg',
56+
'tinymist.exportPng',
57+
'tinymist.exportPdf',
58+
-- 'tinymist.exportHtml', -- Use typst 0.13
59+
'tinymist.exportMarkdown',
60+
'tinymist.exportText',
61+
'tinymist.exportQuery',
62+
'tinymist.exportAnsiHighlight',
63+
'tinymist.getServerInfo',
64+
'tinymist.getDocumentTrace',
65+
'tinymist.getWorkspaceLabels',
66+
'tinymist.getDocumentMetrics',
67+
} do
68+
local cmd_func, cmd_name, cmd_desc = create_tinymist_command(command)
69+
vim.api.nvim_create_user_command(cmd_name, cmd_func, { nargs = 0, desc = cmd_desc })
70+
end
71+
end,
972
},
1073
docs = {
1174
description = [[
1275
https://github.com/Myriad-Dreamin/tinymist
1376
An integrated language service for Typst [taɪpst]. You can also call it "微霭" [wēi ǎi] in Chinese.
77+
78+
Currently some of Tinymist's workspace commands are supported, namely: exportSvg, exportPng, exportPdf, exportMarkdown,
79+
exportText, exportQuery, exportAnsiHighlight, getServerInfo, getDocumentTrace, getWorkspaceLabels and getDocumentMetrics
1480
]],
1581
},
1682
}

0 commit comments

Comments
 (0)