jose-elias-alvarez / nvim-lsp-ts-utils

Utilities to improve the TypeScript development experience for Neovim's built-in LSP client.
The Unlicense
438 stars 18 forks source link

[FEATURE REQUEST] Only use node_modules bin #63

Closed kibs closed 3 years ago

kibs commented 3 years ago

Issues

Feature description

It would be nice to have an option to only enable formatter if the bin (prettier) exists in node_modules and not fallback to the system wide. So you only use prettier if its installed in the project.

Help

No

Implementation help

No response

jose-elias-alvarez commented 3 years ago

My plan for the future is to move formatting and diagnostics entirely to null-ls, which will handle cases like this (and pretty much anything else). For the moment, I recommend disabling the formatting integration from this plugin and using the following:

local null_ls = require("null-ls")

local local_prettier = "node_modules/.bin/prettier"
local prettier = null_ls.builtins.formatting.prettier.with({
    command = local_prettier,
    condition = function(utils)
        return utils.root_has_file(local_prettier)
    end,
})

null_ls.config({ sources = { prettier } })

Let me know if that works for you. Another alternative that I'm considering is keeping the integration here but exposing the arguments passed to with, which would let you handle this case just by defining condition.

kibs commented 3 years ago

Thank you @jose-elias-alvarez actually I already solved it with the following 😀

But good idea with moving it to null-ls it makes more sense, also because prettier can be used for other filetypes than javascript.

null-ls is very cool by the way!

local null_ls = require("null-ls")

null_ls.config({
  sources = {
    null_ls.builtins.formatting.prettier.with({
      condition = function(utils)
        return utils.root_has_file("node_modules/.bin/prettier")
      end,
      command = "node_modules/.bin/prettier",
    })
  },
})
jose-elias-alvarez commented 3 years ago

Great, glad to hear you got it solved!

phantomwhale commented 2 years ago

Gah, lost a day or two digging into why my prettier wasn't working (I mean, got to read through a tonne of the source code; nice work!) and came to this conclusion, and then found this issue saying exactly this!

One thing, the README as written indicates prettier will be picked up from the node_modules/.bin dir - https://github.com/jose-elias-alvarez/nvim-lsp-ts-utils/blob/6156cbace0d23fd87e2cc028ca2bd7e0790bb0ac/README.md?plain=1#L156-L158 - so might be worth removing those lines in the very short term

jose-elias-alvarez commented 2 years ago

@phantomwhale Hmm, the thing is it should pick up the local executable, but it seems like I broke something in a refactor (see also #66).