Skip to content
This repository has been archived by the owner on Aug 12, 2023. It is now read-only.

Add an option to activate source only if a configuration is found in the project #1614

Open
1 task done
ndavd opened this issue Jul 3, 2023 · 4 comments
Open
1 task done
Labels
enhancement New feature or request

Comments

@ndavd
Copy link

ndavd commented Jul 3, 2023

Issues

  • I have checked existing issues and there are no existing ones with the same request.

Feature description

My current use case would be the prettier linter. I am using eslint in a project and prettier is overriding it.
So I tried doing only_local, but prettier is actually being installed in node_modules by a dependency of my project.
As a workaround I tried adding a condition:

    condition = function(utils)
      return utils.root_has_file({
        '.prettierrc',
        '.prettierrc.json',
        '.prettierrc.yml',
        '.prettierrc.yaml',
        '.prettierrc.json5',
        '.prettierrc.js',
        '.prettierrc.cjs',
        '.prettierrc.toml',
        'prettier.config.js',
        'prettier.config.cjs',
      })
    end,

And this works 99% of the cases. Thing is that prettier also recognizes an entry inside package.json...
So I feel like this would be a common thing to happen, maybe there should be an option to let the source decide whether it finds a configuration and activate it accordingly. Perhaps that's not possible, if that's the case then it would be nice to have a condition that allows me to check inside package.json for prettier (some util that let's us check inside files perhaps)

@ndavd ndavd added the enhancement New feature or request label Jul 3, 2023
@jose-elias-alvarez
Copy link
Owner

Neovim has various methods of reading files which you can use to read package.json and determine if the Prettier config key exists. Off the top of my head, you can use vim.fn.readfile() + vim.fn.json_decode() or luv + vim.json.decode() .

I wouldn't be opposed to a PR adding another helper to the utils object to facilitate this process, but I don't think I'd want to do anything beyond that as a default.

@ndavd
Copy link
Author

ndavd commented Jul 3, 2023

I see. My initial thought is that it might have been a capability of most sources (I have no idea).

@ndavd
Copy link
Author

ndavd commented Jul 3, 2023

As for a PR, I can try it. Just need to improve a bit my lua setup first

@ndavd
Copy link
Author

ndavd commented Jul 3, 2023

Modified a bit my config to achieve the desired result:

local should_load_prettier = function(condition_utils)
  -- Check for config files
  if
    condition_utils.root_has_file({
      '.prettierrc',
      '.prettierrc.json',
      '.prettierrc.yml',
      '.prettierrc.yaml',
      '.prettierrc.json5',
      '.prettierrc.js',
      '.prettierrc.cjs',
      '.prettierrc.toml',
      'prettier.config.js',
      'prettier.config.cjs',
    })
  then
    return true
  end

  -- Check for package.json config entry
  if condition_utils.root_has_file({ 'package.json' }) then
    local package_json_path = utils.get_root() .. '/package.json'
    local package_json = vim.json.decode(table.concat(vim.fn.readfile(package_json_path)))
    return package_json['prettier'] ~= nil
  end

  return false
end

Thanks for the help. I don't know how common this use case would be, I personally found it pretty useful. Would you consider adding it to the docs? Or accept a PR doing so

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants