Skip to content

Dronakurl/hunspell-lsp

Repository files navigation

hunspell-lsp

A Language Server Protocol (LSP) implementation that provides spell checking using Hunspell dictionaries.

Features

  • Real-time spell checking for text documents via LSP
  • Support for multiple languages through Hunspell dictionaries
  • Language specification via special comments in documents
  • Full synchronization with text changes
  • Code Actions: Automatic correction suggestions with quick-fix actions

Building

Prerequisites

  • Rust toolchain (2024 edition or later)
  • Hunspell library and development headers
  • Hunspell dictionaries for the languages you want to use

Installing Dependencies

Ubuntu/Debian:

# Install Hunspell development library
sudo apt install libhunspell-dev

# Install dictionaries for the languages you need
sudo apt install hunspell-en-us hunspell-de-de-frami hunspell-fr-fr hunspell-nl

Arch Linux:

sudo pacman -S hunspell hunspell-en-us hunspell-de hunspell-fr hunspell-nl

Dictionary Encoding

Some Hunspell dictionaries (particularly German, French, and Dutch) may be distributed in ISO-8859-1 encoding. While the modern hunspell-rs library handles UTF-8 properly, you may need to convert dictionaries if you see encoding issues.

If you encounter encoding problems:

# Convert German dictionaries from ISO-8859-1 to UTF-8
iconv -f ISO-8859-1 -t UTF-8 /usr/share/hunspell/de_DE_frami.aff > /tmp/de_DE_frami.aff
iconv -f ISO-8859-1 -t UTF-8 /usr/share/hunspell/de_DE_frami.dic > /tmp/de_DE_frami.dic
sudo cp /tmp/de_DE_frami.* /usr/share/hunspell/

If you need to restore original dictionaries:

sudo apt install --reinstall hunspell-de-de-frami hunspell-fr hunspell-nl

Build

cargo build

The compiled binary will be available at target/debug/hunspell-lsp.

Usage

As a Language Server

The LSP server communicates via stdin/stdout. To use it, you need to configure your editor to launch the binary:

/path/to/hunspell-lsp

Language Specification

Specify the language for spell checking by adding a lang: pattern anywhere in your document:

Format: lang: language_code

Examples:

  • lang: en_US (American English)
  • lang: de_DE (German)
  • lang: fr_FR (French)
  • lang: es_ES (Spanish)

How it works:

  • Place lang: xx_YY anywhere in your document (comments or plain text)
  • The first lang: pattern found is used
  • If no language is specified, defaults to en_US

Example usage:

In a Markdown file:

<!-- lang: de_DE -->

Dies ist ein deutscher Text.

lang: en_US

This is English text.

In a Python file:

# lang: en_GB

def hello_world():
    print("Hello World")

In plain text:

lang: de_DE
Dieser Text wird auf Deutsch geprüft.

Code Actions

The LSP server provides intelligent correction suggestions:

Smart Code Action Behavior:

  • Cursor on misspelled word → Shows corrections for that word only
  • Cursor elsewhere on line → Shows corrections for all misspelled words in the line
  • Severity → Hints (not warnings) for less intrusive editing

How it works:

  1. Misspelled words are highlighted with hints
  2. Hover over the word to see Hunspell suggestions
  3. Apply quick fixes to automatically replace misspelled words
  4. Multiple suggestions provided when available

Example diagnostic message:

Possibly misspelled: dksadf. Suggestions: does, dad, sad, dads

Code Action Examples:

  • On word: "--> 'does'" (shows only the suggestion)
  • On line: "'ihc' --> 'ich'" (shows word and suggestion)

For a JavaScript file:

// lang: en_US

function hello() {
    console.log("Hello");  // Spell checking enabled
}

Editor Configuration

Helix

Add to your ~/.config/helix/languages.toml:

[language-server.hunspell-lsp]
command = "/home/user/.cargo/bin/hunspell-lsp"

[[language]]
name = "markdown"
language-servers = ["hunspell-lsp"]

[[language]]
name = "text"
language-servers = ["hunspell-lsp"]

Using smart code actions:

  • Move cursor to misspelled word → press code action key (typically space + a in Helix)
  • Move cursor elsewhere on line → see all misspelled words in line

Dictionary Location

The server looks for Hunspell dictionaries in /usr/share/hunspell/ by default. Each language requires two files:

  • <lang>.aff (affix file)
  • <lang>.dic (dictionary file)

For example, for American English:

  • /usr/share/hunspell/en_US.aff
  • /usr/share/hunspell/en_US.dic

How It Works

  1. The LSP server receives document changes from the editor
  2. Extracts the language specification from comments
  3. Loads the appropriate Hunspell dictionary
  4. Checks each word in the document
  5. Publishes diagnostics (warnings) for misspelled words back to the editor

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

LSP server to provide spell checking with hunspell

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors