tekumara / typos-lsp

Source code spell checker for Visual Studio Code, Neovim and other LSP clients
https://marketplace.visualstudio.com/items?itemName=tekumara.typos-vscode
MIT License
225 stars 6 forks source link

Show misspellings only at definitions (typed/compiled languages only) #28

Closed peter-lyons-kehl closed 9 months ago

peter-lyons-kehl commented 9 months ago

This is a feature request for typed/compiled languages only (examples below are in Rust). I admit I don't know how this works with/in VS Code, so this may not be easy/feasible.

Since typos-vscode mentions LSP, I hope that it could offer an option, when it would check with LSP (rust-analyzer for Rust) whether a symbol is being defined (rather than used), and it would check the spelling only at definition points. (This would add more CPU load, but it would also reduce CPU load.)

The simple examples:

// DO underline the misspelling on the following line: definition
let mut mispelled = true;

// Do NOT underline the misspelling on the following line: use only
mispelled = false;

// DO underline: definition
type MispelledAlias = bool;
// Do NOT underline: use only
let good_name: MispelledAlias = true;

Side note: None of all VS Code spell checkers that I've tried can do this (because they don't work with LSP; the maximum they allow is custom regex filters, but that can't cover nested {...{...}...} blocks, plus bracket characters { and } in string/char constants, comments....).

That would minimize repeated reports, and also eliminate reports of misspellings from 3rd party (std/imported Rust crates).

Example for Rust projects:

If the above is too complex to do language-agnostic, could typos-vscode do it and specialize on per-LSP basis? For example, with Rust, could it talk to rust-analyzer (since it's the only mainstream Rust LSP, and RLS is now deprecated)?

And/or, could rust-analyzer somehow check with typos-vscode instead?

peter-lyons-kehl commented 9 months ago

For anyone new to this: https://github.com/crate-ci/typos/issues/263.

tekumara commented 9 months ago

AFAICT there isn't a way for language servers to talk to each other (when connected to the same client). Nor can I see a way to receive events from the client about the syntax tree.

So unfortunately that leaves:

  1. Language-specific servers implement spell-checking. Since they already build a syntax tree, they can use that to narrow the scope of spell-checking. eg: for rust-analyzer, see https://github.com/rust-lang/rust-analyzer/issues/14597 and the linked discussion which cites the same motivation as you have (only checking misspellings at definitions). The disadvantage is this isn't language agnostic.
  2. typos-lsp implements parsers for each language to generate a concrete syntax tree it can use to narrow type-checking. The disadvantage is that the source code file needs to be parsed twice ie: once by the native LSP server (eg: rust-analyzer) and again by typos-lsp.
peter-lyons-kehl commented 9 months ago

Thank you so much. I'm subscribing to that rust-analyzer feature issue & discussion. Happy New Year.