typst-lsp
typst-lsp copied to clipboard
Optimization idea
Right, now in order to get diagnostics on the code (checking if there are any errors), the LSP uses typst::compile which also does the typesetting through typst::model::typeset. This is anyway needed when we want to export a PDF, but would not be needed otherwise.
Since I created a PR to avoid exporting a PDF on every document change (through a setting), there is unnecessary work being done. Likewise, in the future, people who choose to not use a preview will also not need the typesetting to happen.
Finally, since this LSP could be used in other applications, we cannot assume that people will always want to the fully formatted document to be generated.
The idea would be to only typst::eval::eval() the document to get the compilation errors, and then pass the returned module to typst::model::typeset() in case a preview or document export is needed.
After some testing, this will be tricky as the eval::eval function expects a Tracked<dyn World> and when I implement the necessary comemo trait on our SystemWorld, it then is not Sync anymore and there are plenty of errors in the LSP.
Needs investigation.. Here is the non-working version https://github.com/beeb/typst-lsp/tree/multistep-compilation
Judging from this piece of code, the proposed optimization seems to have been implemented since. However, I just wanted to note that this results in missing diagnostics in some cases. eval only evaluates the top-level model, there is still code running (and thus possibly errors happening) during typeset. For example, this snippet of code is clearly faulty, but Typst LSP generates no errors:
#locate(loc => {
let x = 1 + 2 + "3"
this.does.not.exist
})