typst-lsp icon indicating copy to clipboard operation
typst-lsp copied to clipboard

Automatically Handle Root Directory Outside of A Git Directory

Open lace-wing opened this issue 2 years ago • 5 comments

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

lace-wing avatar Aug 23 '23 11:08 lace-wing

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.

leana8959 avatar Sep 17 '23 13:09 leana8959

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

KillTheMule avatar Sep 17 '23 15:09 KillTheMule

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.

Andrew15-5 avatar Oct 09 '23 22:10 Andrew15-5

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,

dustypomerleau avatar Dec 15 '23 06:12 dustypomerleau

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

YDX-2147483647 avatar Dec 15 '23 07:12 YDX-2147483647