Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax highlighting in popups, returned by 3rd party language server, not work #2472

Open
istudyatuni opened this issue May 7, 2024 · 9 comments

Comments

@istudyatuni
Copy link
Contributor

istudyatuni commented May 7, 2024

and I'm not sure, how to fix it

Describe the bug

Tinymist language server returns something like this on hover:

{'contents': '```typc\n...\n```\nsome text'}

I tried to add new file_extension typc to syntax definition, but this doesn't help

To Reproduce

Steps to reproduce the behavior:

  1. Download server binary from releases and install "Typst" extension
  2. Add this to LSP settings:
"tinymist-lsp": {
  "enabled": true,
  "command": [
    "path/to/tinymist"
  ],
  "selector": "text.typst"
}
  1. Create .typ file with this content:
#let test() = [test]
  1. Hover over test()

Expected behavior

Code block with definition is highlighted

Screenshots

Second screenshot from vscode

image
image

Logs

tinymist-lsp: [2024-05-07T18:41:41Z INFO  tinymist::harness] handling textDocument/hover - (3) at Instant { tv_sec: 4646, tv_nsec: 450974903 }
tinymist-lsp: [2024-05-07T18:41:41Z INFO  tinymist_query::analysis::global] def_use_lexical_hierarchy([..]/test.typ): compute
tinymist-lsp: [2024-05-07T18:41:41Z INFO  tinymist_query::syntax::lexical_hierarchy] lexical hierarchy analysis took 18.886µs
tinymist-lsp: [2024-05-07T18:41:41Z INFO  tinymist_query::analysis::global] import(2): compute
tinymist-lsp: [2024-05-07T18:41:41Z INFO  tinymist_query::analysis::global] def_use((2, 0)): compute
tinymist-lsp: [2024-05-07T18:41:41Z INFO  tinymist_query::analysis::linked_def] def_name for function: Ident: "test"
tinymist-lsp: [2024-05-07T18:41:41Z INFO  tinymist_query::analysis::linked_def] okay for function: Some((Func(test), None))
tinymist-lsp: [2024-05-07T18:41:41Z INFO  tinymist::harness] handled  textDocument/hover - (3) in 2.91ms
:: [21:41:41.514] --> tinymist-lsp textDocument/hover (3): {'textDocument': {'uri': '[..]/test.typ'}, 'position': {'line': 0, 'character': 7}}
tinymist-lsp: [2024-05-07T18:41:41Z INFO  tinymist::harness] handling textDocument/codeAction - (4) at Instant { tv_sec: 4646, tv_nsec: 482253701 }
tinymist-lsp: [2024-05-07T18:41:41Z INFO  tinymist::harness] handled  textDocument/codeAction - (4) in 146.34µs
:: [21:41:41.545] --> tinymist-lsp textDocument/codeAction (4): {'textDocument': {'uri': '[..]/test.typ'}, 'range': {'start': {'line': 0, 'character': 7}, 'end': {'line': 0, 'character': 7}}, 'context': {'diagnostics': [], 'triggerKind': 2}}
:: [21:41:41.549] <<< tinymist-lsp (3) (duration: 34ms): {'contents': '```typc\nlet test();\n```', 'range': {'end': {'character': 9, 'line': 0}, 'start': {'character': 5, 'line': 0}}}
:: [21:41:41.674] <<< tinymist-lsp (4) (duration: 128ms): None

Paths are redacted

Environment (please complete the following information):

  • OS: Archlinux
  • Sublime Text version: 4169
  • LSP version: 2.0.1
  • Language servers used: ..
@jfcherng
Copy link
Contributor

jfcherng commented May 7, 2024

You can either override this file: https://github.com/sublimelsp/LSP/blob/main/language-ids.sublime-settings or combining other issues you filed, invent a LSP-tinymist plugin.

@jwortmann
Copy link
Member

jwortmann commented May 7, 2024

You can either override this file: https://github.com/sublimelsp/LSP/blob/main/language-ids.sublime-settings

No, this is not used for the syntax highlighting in popus.

You can either add

    "mdpopups.sublime_user_lang_map": {
        "typst": [["typst", "typc"], ["Typst/Typst"]]
    }

to you Preferences.sublime-settings user settings, or better provide a PR upstream at https://github.com/facelessuser/sublime-markdown-popups/blob/master/st3/mdpopups/st_mapping.py so that other users can also benefit from Typst syntax highlighting in popups.

See https://facelessuser.github.io/sublime-markdown-popups/settings/#mdpopupssublime_user_lang_map

It is also possible to extend the language map from a LSP-tinymist helper package via

def markdown_language_id_to_st_syntax_map(cls) -> MarkdownLangMap | None:
but imo this is not the best solution.

@Myriad-Dreamin
Copy link

Note that typc is not typst in markup mode but that in code mode.

image

@istudyatuni
Copy link
Contributor Author

I'm not sure, how to handle this, so, if I understand correctly, there are some possible solutions:

  • Detect in tinymist, when client is sublime text, and change mode to typ (because syntax definition is defined in single file, and default context is markup), like ```typ #let ... ```
    • Client info looks like 'clientInfo': {'name': 'Sublime Text LSP', 'version': '2.0.0'}
  • Probably it's possible to set default syntax context in mdpopups to script-statement
  • If not, extracting scripts context to separate file should help

@predragnikolic
Copy link
Member

@istudyatuni this comment gives the solution on how to add syntax highlighting in popups for Typst
-> #2472 (comment)

From the command palette select Preferences: Settings and paste:

{
	"mdpopups.sublime_user_lang_map": {
		"typst": [["typst", "typc" ], ["Typst/Typst" ] ]
	}
}

Restart ST, and now hover over the variables.

Screenshot 2024-05-20 at 18 20 41

You should see syntax highlighting.


LSP uses mdpopus(sublime-markdown-popups) to render the markdown in popups.

A better way would be to submit a PR to mdpopus to add support for Typst. (here is a PR example of how to do that)
that way someone in the future will not have to configure it manually.

If you have any additional questions feel free to ask.

@predragnikolic
Copy link
Member

I will close the issue because these 1, 2 comments are pointing to the solution.

@istudyatuni
Copy link
Contributor Author

Strangely, but this doesn't work for me. Probably after the LSP update it will work, but I can't check it now

@istudyatuni
Copy link
Contributor Author

@predragnikolic I tested it in clean installation, and it still don't work for me

Preferences

{
	"mdpopups.sublime_user_lang_map": {
		"typst": [["typst", "typc"], ["Typst/Typst"]],
	},
	"font_size": 12,
}

LSP Preferences

// Settings in here override those in "LSP/LSP.sublime-settings"
{
    "clients": {
        "tinymist-lsp": {
            "enabled": true,
            "command": [
                "/usr/bin/tinymist"
            ],
            "selector": "text.typst",
            "auto_complete_selector": "text | markup",
            "initializationOptions": {
                "exportPdf": "never",
            }
        },
    },
}

  • tinymist v0.11.9
  • LSP v2.1.0

Installed packages: LSP, Typst

Also popup differs from you screenshot - there is no # at the start:

image

@jwortmann
Copy link
Member

jwortmann commented May 25, 2024

It doesn't work because the let test(); expression is implicitly considered to be within a code context, so by itself it is just not highlighted by the Typst syntax highlighter.

What you would need to do is to create a separate syntax definition for typc code block contents (could be derived from the regular Typst syntax definition) and then in your user settings use something like

	"mdpopups.sublime_user_lang_map": {
		"typst": [["typst"], ["Typst/Typst"]],
		"typc": [["typc"], ["User/MyTypcSyntax"]],
	},

or include the syntax into a LSP-tinymist package (see https://github.com/sublimelsp/LSP-pyright/blob/32ab2159cb79088b52684f068dcd23586ddecdc9/syntaxes/pyright.sublime-syntax and https://github.com/sublimelsp/LSP-pyright/blob/32ab2159cb79088b52684f068dcd23586ddecdc9/plugin.py#L88-L90 for an example in LSP-pyright, which similarly works around Pyright using non-standard syntax for its popups).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants