typst-lsp
                                
                                
                                
                                    typst-lsp copied to clipboard
                            
                            
                            
                        Automatically Handle Root Directory Outside of A Git Directory
- I have searched the issues of this repo and believe that this is not a duplicate.
 - I have searched the discussions and believe that my question is not already covered.
 
Feature Request
Automatically Handle Root Directory Outside of A Git Directory
It has been mentioned in #38, however I didn't see it solved.
The root directory is a "Not found." if a .typ file is not opened in a git directory.
I guess the current directory would be a good default if git directory is not found.
It may not be a problem to those who know lua, but troublesome to people who just want to write their papers without learning to program.
Is this specifically a problem of typst-lsp the server, or a client?
I wonder if you are referring to this when you said "it might not be a big problem to those who know lua".
If you're talking about this, maybe I could try to make it work and create a PR for nvim-lspconfig. I'm affected by the problem too. I haven't seen anyone talking about this problem in nvim-lspconfig's repo though.
I'm running
  root_dir = function(fname) return util.path.dirname(fname) end,
in my lsp config. I'm not clear what the right config here would be, although I think typst-lsp barely uses this info for now (auto-compilation simply puts the pdf next to the source file).
I came up with
root_dir = function() return vim.fn.getcwd() end
Which works fine if file is deeper than root dir or root dir is deeper than file, or if they are in the same place. I think it should also work fine when editing multiple files, then.
getcwd() is not working for me, but on Neovim 0.10 I'm using cwd() from the libUV bindings:
root_dir = function() return vim.uv.cwd() end,
                                    
                                    
                                    
                                
…create a PR for nvim-lspconfig. … I haven't seen anyone talking about this problem in nvim-lspconfig's repo though.
After you replied, there was a PR https://github.com/neovim/nvim-lspconfig/pull/2829#issuecomment-1732400049, but rejected:
Do not add
vim.fn.cwdorutil.path.dirnameinroot_dir. A future version of lspconfig will provide emulation of a single file mode until this is formally codified in the specification.
Later https://github.com/neovim/nvim-lspconfig/pull/2873 was rejected for the same reason.
Update
I edit my init.lua to the following.
require('lspconfig').typst_lsp.setup{
  root_dir = function(fname)
    return require('lspconfig.util').root_pattern('typst.toml', '.git')(fname)
      or vim.fn.getcwd()
  end,
}
Now after opening a *.typ, I can run :LspInfo and verify root directory. (:q to close the pop-up)
Further examples:
- Project local settings · neovim/nvim-lspconfig Wiki
 - cleanup lsp config with new `root_dir` behaviour · Issue #2904 · neovim/nvim-lspconfig
 
Update 2
require('lspconfig').typst_lsp.setup{
  single_file_support = true,
}
Maybe we can make a PR like https://github.com/neovim/nvim-lspconfig/pull/2900.