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

Programmatic way of getting spelling errors #29167

Open
ravibrock opened this issue Jun 3, 2024 · 10 comments
Open

Programmatic way of getting spelling errors #29167

ravibrock opened this issue Jun 3, 2024 · 10 comments
Labels
enhancement feature request has:workaround issue is not fixed but can be circumvented until then spell spellcheck
Milestone

Comments

@ravibrock
Copy link

Problem

The neovim spell checker is very useful, but there doesn't seem to be any straightforward way to get all spelling mistakes in a buffer with Lua without iterating through them with ]s. Ideally, there would be a function like vim.fn.get_spelling_errors(bufnr) that would return all errors in a buffer as a table (while preserving things like Treesitter context-dependence that are ignored by vim.spell.check().

Expected behavior

The new feature would look like a function that returns the same result that would be provided by iterating through the buffer with ]s and writing down each mistake found. This would solve the problem, and it would be useful because it would (probably) be faster than iterating through, would definitely be more elegant, and would be simpler to run in the background as it wouldn't require moving and then resetting the cursor. Something like:

{
  {
    word = "tset",
    severity = "bad",
    linenr = 20,
    colnr = 10,
  },
  {
    word = "grey",
    severity = "local",
    linenr = 15,
    colnr = 5,
  },
}
@ravibrock ravibrock added the enhancement feature request label Jun 3, 2024
@zeertzjq zeertzjq added the spell spellcheck label Jun 3, 2024
@lewis6991
Copy link
Member

You can use vim.spell.check and pass in each line of the buffer manually. I don't think we need to provide a first class function for this.

@ravibrock
Copy link
Author

Thanks for the reply. I was thinking it could be useful to have something that would integrate with things like Treesitter automatically (whereas vim.spell.check is strictly strings), but it might be too niche.

@lewis6991
Copy link
Member

I guess the main point is that Neovim already exposes everything needed for this to be implemented so there isn't anything that can't be done via a custom function or plugin.

@ravibrock
Copy link
Author

ravibrock commented Jun 3, 2024

How does it expose something like "get next spelling error"? I went through the source code and it looks like ]s is hardcoded in C--there isn't an API for getting the location of the next spelling error. I guess my suggestion is to expose whatever method(s) Neovim uses internally to jump to the next spelling error in such a way that they can be called directly in Lua, since presumably they exist in the C codebase (and, since they're implemented in C, are probably faster than comparable Lua implementations). But I'm not 100% sure how complicated that would be.

@lewis6991
Copy link
Member

  • You can get the buffer text
  • You can iterate words using a pattern.
  • You can check if a certain position has a @spell capture
  • You can check the spelling of a string.

It isn't exactly the same as what the C codebase does, but I'm not sure it needs to be. And I'm not sure it's even feasible given how it is implemented in C. Last time I checked I remember being quite ad-hoc.

We can, if need be, improve the API for any one of these points.

@ravibrock
Copy link
Author

That makes sense, thanks. I'll try to implement iteration through @spell groups tomorrow and see how it goes. If I can get that working it would probably be as close to an ideal solution as is necessary.

@asmodeus812

This comment has been minimized.

@lewis6991

This comment has been minimized.

@asmodeus812

This comment has been minimized.

@lewis6991

This comment has been minimized.

@justinmk justinmk added this to the unplanned milestone Jun 4, 2024
@justinmk justinmk added the has:workaround issue is not fixed but can be circumvented until then label Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement feature request has:workaround issue is not fixed but can be circumvented until then spell spellcheck
Projects
None yet
Development

No branches or pull requests

5 participants