Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce "shebangs" config flag #2708

Closed
wants to merge 2 commits into from
Closed

Conversation

sigmaSd
Copy link
Contributor

@sigmaSd sigmaSd commented Jul 9, 2023

This is useful for running one off scripts with the lsp

see sigmaSd/deno-nvim#2 for prior discussion

Also for precedence helix also has this feature

example of usage

require("deno-nvim").setup {
  server = {
    on_attach = require("lvim.lsp").common_on_attach,
    on_init = require("lvim.lsp").common_on_init,
    root_dir = require "lspconfig.util".root_pattern('deno.json', 'deno.jsonc', 'denonvim.tag'),
    -- single_file_support = true,
    shebangs = { "deno" }, -- new part here

a.ts

#!/usr/bin/env deno
Deno

now the script a.ts will automatically trigger deno lsp

@williamboman
Copy link
Contributor

This can easily be solved by adding a filetype detection instead and not adding a bespoke filetype detection implementation to nvim-lspconfig:

vim.filetype.add {
  pattern = {
    [".*"] = {
      priority = -math.huge,
      function(path, bufnr)
        local content = vim.filetype.getlines(bufnr, 1)
        if vim.filetype.matchregex(content, [[^#!/usr/bin/env deno]]) then
          return "javascript"
        end
      end,
    },
  },
}

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Jul 12, 2023

The problem is I need to hook to the internals of lspconfig in order to start the lsp server in single file mode in case of a match, or do you have an example on how to do it externally?

Also I'm not really sure of the point of the file detection because I still use the ts and js extension (so file type is detected already) as that's what this PR assumes

@williamboman
Copy link
Contributor

The problem is I need to hook to the internals of lspconfig in order to start the lsp server in single file mode in case of a match, or do you have an example on how to do it externally?

Also I'm not really sure of the point of the file detection because I still use the ts and js extension (so file type is detected already) as that's what this PR assumes

Ah if the language server supports single file support the flag could be enabled for it. This could either be done manually or be part of the default server config:

lspconfig.denols.setup {
  single_file_support = true,
}

@sigmaSd
Copy link
Contributor Author

sigmaSd commented Jul 13, 2023

But the point is to enable it only when the shebang exists

@johnpyp
Copy link

johnpyp commented Jul 30, 2023

Hey, following up on this - like @sigmaSd mentioned, the usecase mentioned here about disambiguating when to use tsserver or denols, when filetypes aren't enough.

For example:

  • Node should apply when there's a package.json root file
  • Deno should apply when there's a deno.json root file
  • Node should apply when the hashbang calls node
  • Deno should apply when the hashbang calls deno
  • In the case of a tie, the user should be able to pick which one applies

Assuming this was implemented flexibly, you could imagine something like a "buffer rule" that determines whether or not the lsp should be attached after the rest of the existing filetype/root file rules already apply. This would also enable additional better custom heuristics like:

  • Deno applies if any import fetches from a url that contains deno.land (a common import url)
  • Deno applies if the Deno global is used in the buffer
    ... etc.

I'm not sure what the best way to implement this would be that's flexible enough

@justinmk justinmk changed the title add shebangs to config introduce "shebangs" config flag Oct 1, 2024
@justinmk
Copy link
Member

justinmk commented Oct 1, 2024

Instead of this, would it make sense for :LspStart foo to force the "foo" lsp client to attach to the current buffer? Maybe :LspStart! foo. #3331

Expecting users to fiddle with another flag for scenarios that can't really be properly detected in the first place, is the wrong approach.

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

Successfully merging this pull request may close these issues.

4 participants