Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Add capability to pin a main file #389

Merged
merged 3 commits into from
Dec 29, 2023
Merged

Conversation

Myriad-Dreamin
Copy link
Collaborator

@Myriad-Dreamin Myriad-Dreamin commented Dec 12, 2023

This PR proposes an abstraction corresponding to "Preview this file" in official webapp. In conclusion, this PR adds a "typst-lsp.doPinMain" command to lsp program which could then get used by:

  • user manually runs command "typst-lsp.pinMainToCurrent" or "typst-lsp.unpinMain" in VSCode.
  • user writes some lua script to automate the pin action.
  • some other vscode extension (such as typst-preview) pins a previewing file automatically.

image


Usage 1: improve mode of exportPDF by adding "onPinnedMain{Save,Type}".
Currently, automatically exportPDF on save or on type is annoying since typst-lsp cannot identify the source files that has empty content. This PR can allow users only exporting PDF "onPinnedMain{Save,Type}", close #11, may close #184, may close #285.

image


Usage 2: make hover tip and autocompletion according to the pinned file.

Before, hover variable in template.typ doesn't work at all:

image

After, it shows result correctly:

image


Usage 3: show diagnostics according to compilation of the pinned file when editing other files, close #153.

Before, the errors in thesis.typ fly away if the user doesn't focus thesis.typ:

image

After, lsp will show diagnostics according to compilation of the pinned file when editing template.typ:

image


Usage 3.5: eliminate false errors in a project with multiple files.

Note that lsp may also produces false errors, such as errors on bib things. Once this PR is ready, editors could pin the main file in a project with multiple files, may close #64, may close #366.

@PgBiel
Copy link
Contributor

PgBiel commented Dec 13, 2023

Great work! I wonder, is there a way to add a context menu button to .typ files so that you can right-click them and select "Pin file" or something? That would be interesting... (But also not strictly necessary for this PR)

@Myriad-Dreamin
Copy link
Collaborator Author

@PgBiel I think this abstraction is mostly used by editor scripts. Therefore, you don't have to pin them manually, but with some configuration on your favorite editor. For example, I made a branch in typst-preview, which invokes "typst-lsp.pinMainToCurrent" when it starts to preview some file, https://github.com/Enter-tainer/typst-preview/tree/pin-lsp. There is also a configurable switch to enable/disable it, determined by users. I believe neovim users may invent other luascript like it.

Your idea also looks great to me, since it is much more convenient than keystroking ctrl+shift+p and running "typst-lsp.pinMainToCurrent" command. But honestly, we may leave it for another PR to reduce code changes, because I'm dumb and currently don't know how to bind it 😃.

I want to say that it is expected to leave many space for user customization. And we may discuss and equip lsp with more smart behaviors in future.

@PgBiel
Copy link
Contributor

PgBiel commented Dec 14, 2023

Awesome. We can definitely investigate that in the future. Thanks for the great work 👍

@SimonRask
Copy link

Looks great! I think this would solve #366.

I personally think it would be great to expose a configuration option such as typst-lsp.defaultMainFile, which would allow setting a default main file at the workspace level. It does not seem to be possible to test this in Helix currently, since I haven't found a way to pass the URI argument when running the typst-lsp.pinMainToCurrent command.

@Myriad-Dreamin
Copy link
Collaborator Author

@SimonRask Note that the typst-lsp program provides typst-lsp.doPinMain, and the vscode extension provides typst-lsp.pinMainToCurrent. typst-lsp.doPinMain receives an uri argument while typst-lsp.pinMainToCurrent doesn't. I don't know how we can get a such uri for main file in some helix extension, but I hope the above clarification could help you.

Since I don't use helix, it would be appreciate if you can verify it that current PR can work in helix by some way.


typst-lsp.defaultMainFile looks good to me. To make typst-lsp.defaultMainFile correct, we may need to canonicalize path and convert it to a file url for comparison. Since current lsp doesn't canonicalize paths or urls at all, it is not quite easy and would need extra efforts to ensure correctness. So I think it could an optional feature in scope of this PR.

Now we may have two extra features in summary:

  • In vscode or more editors, right-click them and select "Pin file" or something.
  • For all editors, provide a typst-lsp.defaultMainFile configuration.

@SimonRask
Copy link

@Myriad-Dreamin

Note that the typst-lsp program provides typst-lsp.doPinMain, and the vscode extension provides typst-lsp.pinMainToCurrent. typst-lsp.doPinMain receives an uri argument while typst-lsp.pinMainToCurrent doesn't.

Sorry for the confusion. I meant typst-lsp.doPinMain instead of typst-lsp.pinMainToCurrent.

Since I don't use helix, it would be appreciate if you can verify it that current PR can work in helix by some way.

I will try to see if I can somehow pass arguments to typst-lsp.doPinMain. Note that this is not isolated to this PR, as it is also the case for typst-lsp.doPdfExport, for example.

Since current lsp doesn't canonicalize paths or urls at all, it is not quite easy and would need extra efforts to ensure correctness. So I think it could an optional feature in scope of this PR.

Agreed

@nvarner
Copy link
Owner

nvarner commented Dec 29, 2023

I'm not fully satisfied with this solution, but this is a big improvement over the status quo. I'll merge for now. Thank you!

@nvarner nvarner merged commit 2ac37c0 into nvarner:master Dec 29, 2023
6 checks passed
@Myriad-Dreamin Myriad-Dreamin deleted the pin-main branch December 30, 2023 06:44
@Myriad-Dreamin
Copy link
Collaborator Author

@nvarner should we create a tracking issue? For discussion on other perfect solutions or improving this solution.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
4 participants