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

is there a way to beautify the cspell.json somehow? #19

Closed
NullVoxPopuli opened this issue Aug 20, 2023 · 5 comments
Closed

is there a way to beautify the cspell.json somehow? #19

NullVoxPopuli opened this issue Aug 20, 2023 · 5 comments

Comments

@NullVoxPopuli
Copy link

Was hoping I could somehow format the cspell.json to help with conflict management as I sync the config file between machines.

Looking at the README, I only see string -> table transforms, and nothing about the actual file

@davidmh
Copy link
Owner

davidmh commented Aug 21, 2023

There is, from the README:

  --- Callback after a successful execution of a code action.
  ---@param cspell_config_file_path string|nil
  ---@param params GeneratorParams
  ---@action_name 'use_suggestion'|'add_to_json'|'add_to_dictionary'
  on_success = function(cspell_config_file_path, params, action_name)
      -- For example, you can format the cspell config file after you add a word
      if action_name == 'add_to_json' then
          os.execute(
              string.format(
                  "cat %s | jq -S '.words |= sort' | tee %s > /dev/null",
                  cspell_config_file_path,
                  cspell_config_file_path
              )
          )
      end

      -- Note: The cspell_config_file_path param could be nil for the
      -- 'use_suggestion' action
  end

That example uses jq to format the file, but you could use whatever cli tool you like.

@davidmh davidmh closed this as completed Aug 22, 2023
@NullVoxPopuli
Copy link
Author

this doesn't work tho -- it creates a new cspell.json file at the current working directory, rather than the

local cspellConfig = {
  find_json = function()
    return cspellConfigFile
  end,

even if swapping out cspell_config_file_path for cspellConfigFile, it's still the local path :-\

@davidmh
Copy link
Owner

davidmh commented Aug 30, 2023

It would be easier to debug with a minimal configuration. You can take this file as an example: https://github.com/davidmh/cspell.nvim/blob/0e9c586bd7f7ab3f1f2f000a084121203e0ee62c/tests/minimal_init.lua

And you can add your own configuration right after this line:

require("null-ls.config")._set({ log = { enable = false } })

Something like:

    local null_ls = require("null-ls")
    local cspell = require("cspell")
    local sources = {
        cspell.diagnostics,
        cspell.code_actions,
    }

    null_ls.setup({ sources = sources })

Then you can start Neovim using that configuration as:

nvim -u minimal_init.lua some-other-file-to-lint.ts

@mcDevnagh
Copy link
Contributor

I'm running into the issue where cspell.json file is sometimes created in vim.loop.cwd() as well, but I think it should be a separate issue.

To beautify with a lua only solution, I created the following:

local cspell_opts = {
  config = {
    encode_json = function(cspell_tbl)
      local json = vim.json.encode(cspell_tbl) or "null"
      local buf = vim.api.nvim_create_buf(false, true)
      if buf > 0 then
        vim.api.nvim_buf_set_lines(buf, 0, -1, false, { json })
        vim.api.nvim_buf_set_option(buf, "filetype", "json")
        local conform = require("conform")
        conform.format({ bufnr = buf })
        return table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), "\n")
      end
      
      return json
    end,
  }
}

require("null-ls").setup({
  sources = {
    cspell.diagnostics.with(cspell_opts),
    cspell.code_actions.with(cspell_opts),
  }
})

It does rely on a 3rd party formatter plugin, which I have configured to use prettierd for json files, but I imagine this could also work with null-ls formatting.
This solution should be more portable than utilizing a CLI tool

@davidmh
Copy link
Owner

davidmh commented Nov 14, 2023

@NullVoxPopuli this should be fixed with #26

@davidmh davidmh closed this as completed Nov 14, 2023
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

No branches or pull requests

3 participants