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
+
1
45
return {
2
46
default_config = {
3
47
cmd = { ' tinymist' },
@@ -6,11 +50,37 @@ return {
6
50
return vim .fs .dirname (vim .fs .find (' .git' , { path = fname , upward = true })[1 ])
7
51
end ,
8
52
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 ,
9
72
},
10
73
docs = {
11
74
description = [[
12
75
https://github.com/Myriad-Dreamin/tinymist
13
76
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:
79
+ `LspTinymistExportSvg`, `LspTinymistExportPng`, `LspTinymistExportPdf
80
+ `LspTinymistExportMarkdown`, `LspTinymistExportText`, `LspTinymistExportQuery`,
81
+ `LspTinymistExportAnsiHighlight`, `LspTinymistGetServerInfo`,
82
+ `LspTinymistGetDocumentTrace`, `LspTinymistGetWorkspaceLabels`, and
83
+ `LspTinymistGetDocumentMetrics`.
14
84
]] ,
15
85
},
16
86
}
0 commit comments