nvarner / typst-lsp

[Deprecated] An early language server for Typst, plus a VS Code extension
MIT License
1.21k stars 78 forks source link

Automatically Handle Root Directory Outside of A Git Directory #263

Open lace-wing opened 1 year ago

lace-wing commented 1 year ago

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.

leana8959 commented 1 year ago

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.

KillTheMule commented 1 year ago

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).

Andrew15-5 commented 1 year ago

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.

dustypomerleau commented 11 months ago

getcwd() is not working for me, but on Neovim 0.10 I'm using [cwd()](https://neovim.io/doc/user/luvref.html#uv.cwd()) from the libUV bindings:

root_dir = function() return vim.uv.cwd() end,
YDX-2147483647 commented 11 months ago

…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.cwd or util.path.dirname in root_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:

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.