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

Make main file configurable #426

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lukasjuhrich
Copy link

Very rough fix. Not sure if I will have the time to flesh it out.

See discussion in #366 (comment).

@Myriad-Dreamin
Copy link
Collaborator

Myriad-Dreamin commented Jan 31, 2024

I commented why typst-lsp.defaultMainFile is hard to implement here, #389 (comment).

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.

To make typst-lsp.mainFile work, we should provide a canonical uri so typst-lsp could find a main source file by uri.

let full_id = workspace.full_id(uri)?;

From my simple test, simply creating a uri for main file by Url::from_str doesn't work in VSCode on windows. A uri from the language server host looks like file:https:///C:/Users/ww/work/rust/typst-book/github-pages/docs/introduction.typ, however a uri created by Url::from_str is file:https:///c%3A/Users/ww/work/rust/typst-book/github-pages/docs/introduction.typ, so the configuration will never work on windows.

Another weird behavior is that if we set a main file in the configuration and open another file, typst-lsp will fail on everything, because it tries to work on the configured main file rather the opening file.

Also note that the main file configuration may fail if one is opening a symbol linked file, because the language server host may or may not canonicalize a symbol link (which is likely not defined by the language server protocol).

@lukasjuhrich
Copy link
Author

Another weird behavior is that if we set a main file in the configuration and open another file, typst-lsp will fail on everything, because it tries to work on the configured main file rather the opening file.

@Myriad-Dreamin Can you elaborate what you mean?

If I use the typst-lsp of this PR and set

[language-server.typst-lsp.config]
mainFile = "file:https:///abs/path/to/project/dir/main.typ"
exportPdf = "onPinnedMainType"

in the (helix) LSP config, then opening helix at 00_prelim.typ (included in main.typ) and typing correctly compiles the main.typ to a PDF file.

However, I do note that references only start to be looked up correctly when I actually open the main.typ file, so that's probably what you mean.

What precisely goes wrong? Having to open the main.typ seems counterintuitive. E.g. a rust project (i.e. rust-analyzer as an LSP) also does not expect me to open the main.rs of a binary just to have meaningful diagnostics.

@Myriad-Dreamin
Copy link
Collaborator

Since it is a bit hard to explain, I want to explore it and fix some bugs in this PR for you when I have time. Currently I'm on vacation with my family :).

I also leave some notes here for future me and you all.

Path Normalization

typst-lsp doesn't do normalization on paths (URLs) so that:

  • we can only effectively compare exact URLs from LSP host (the editors).
  • some paths from user input can match exact file paths managed by typst-lsp, but some other similar paths doesn't.

This blocks us set absolute and especially relative path in the configuration easily.

Support both syntactic and semantic completion

If some users pin a file main.typ by configuration, then when they open a file that aren't touched by main.typ, typst-lsp will fail on completion. It is probably fixed by provides union of completion items:

  • syntactically: eval and run completion on the opened file.
  • semantically: compile and run completion on the pinned or opened file.
  • combines, deduplicates, and sorts the above two sources.

This prevents people from going through the external files on having set typst-lsp.mainFile.

Bad root path when some user pins an absolute path

I noticed that when I set typst-lsp.mainFile to some absolute path, typst-lsp changed its root path to the parent directory of the main file, but I would expect it always keep the workspace root as the root directory. I think of it is not expected by vscode users, but expected by nvim users tho.

@Myriad-Dreamin
Copy link
Collaborator

@nvarner This should be involved in design of the "SourceManager", to provide a more user friendly configuration to the main file, instead of a url:

mainFile = "file:https:///abs/path/to/project/dir/main.typ"

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

Successfully merging this pull request may close these issues.

2 participants