grace/dots

dotfiles/nix configs

git clone https://git.gae.moe/grace/dots

Grace Yoderhx and vcs changesddf0c79

vulcan
2.9 KiB77 linesraw
1---@brief
2---
3--- https://github.com/Myriad-Dreamin/tinymist
4--- An integrated language service for Typst [taɪpst]. You can also call it "微霭" [wēi ǎi] in Chinese.
5---
6--- Currently some of Tinymist's workspace commands are supported, namely:
7--- `LspTinymistExportSvg`, `LspTinymistExportPng`, `LspTinymistExportPdf
8--- `LspTinymistExportMarkdown`, `LspTinymistExportText`, `LspTinymistExportQuery`,
9--- `LspTinymistExportAnsiHighlight`, `LspTinymistGetServerInfo`,
10--- `LspTinymistGetDocumentTrace`, `LspTinymistGetWorkspaceLabels`, and
11--- `LspTinymistGetDocumentMetrics`.
12
13---@param command_name string
14---@param client vim.lsp.Client
15---@param bufnr integer
16---@return fun():nil run_tinymist_command, string cmd_name, string cmd_desc
17local function create_tinymist_command(command_name, client, bufnr)
18  local export_type = command_name:match 'tinymist%.export(%w+)'
19  local info_type = command_name:match 'tinymist%.(%w+)'
20  if info_type and info_type:match '^get' then
21    info_type = info_type:gsub('^get', 'Get')
22  end
23  local cmd_display = export_type or info_type
24  ---@return nil
25  local function run_tinymist_command()
26    local arguments = { vim.api.nvim_buf_get_name(bufnr) }
27    local title_str = export_type and ('Export ' .. cmd_display) or cmd_display
28    ---@type lsp.Handler
29    local function handler(err, res)
30      if err then
31        return vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR)
32      end
33      -- If exporting, show the string result; else, show the table for inspection
34      vim.notify(export_type and res or vim.inspect(res), vim.log.levels.INFO)
35    end
36    return client:exec_cmd({
37      title = title_str,
38      command = command_name,
39      arguments = arguments,
40    }, { bufnr = bufnr }, handler)
41  end
42  -- Construct a readable command name/desc
43  local cmd_name = export_type and ('TinymistExport' .. cmd_display) or ('Tinymist' .. cmd_display) ---@type string
44  local cmd_desc = export_type and ('Export to ' .. cmd_display) or ('Get ' .. cmd_display) ---@type string
45  return run_tinymist_command, cmd_name, cmd_desc
46end
47
48---@type vim.lsp.Config
49return {
50  cmd = { 'tinymist' },
51  filetypes = { 'typst' },
52  root_markers = { '.git', '.jj' },
53  on_attach = function(client, bufnr)
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, client, bufnr)
69      vim.api.nvim_buf_create_user_command(bufnr, 'Lsp' .. cmd_name, cmd_func, { nargs = 0, desc = cmd_desc })
70    end
71  end,
72  settings = {
73    ["tinymist"] = {
74      formatterMode = "typstyle"
75    }
76  }
77}