Skip to content

Quote the right word after inline code at the start of a paragraph - #227

Merged
spitzerd merged 2 commits into
ltex-plus:developfrom
alberti42:fix-quote-after-inline-code
Nov 26, 2026
Merged

spitzerd merged 2 commits into
ltex-plus:developfrom
alberti42:fix-quote-after-inline-code

Conversation

@alberti42

Copy link
Copy Markdown
Member

Fixes #226.

Problem

A word in ltex.dictionary is still flagged as a spelling mistake when inline code comes before it at the start of a paragraph. Example, with frobnitz in the dictionary:

The weather is nice and the team works well together today.

`Service.disable_all_features()` runs frobnitz per class.

The server reports 'runs frobn': Possible spelling mistake found. on frobnitz. The underline is in the right place; the word in quotes is not.

This happens in Markdown, Org and reStructuredText, with every backend: the bundled LanguageTool, a LanguageTool HTTP server and LanguageTool Premium. It happens when the paragraph is checked in the same request as the paragraph before it, for example when a file is opened. LaTeX, Typst and HTML did not show it in the same test.

Cause

Before checking, the server turns the document into two texts side by side: the source, and the plain text that LanguageTool reads. Inline code becomes a placeholder in the plain text, such as Dummy0. LanguageTool reports a match as a range in the source. To show the misspelled word in the message, and to compare it with the dictionary, the server converts that range back to a range in the plain text.

The server splits a document into paragraphs, so that it can cache the results per paragraph, and sends several paragraphs in one request. In the three formats above, the blank line before a paragraph and the inline code that starts it end up in one piece of markup. Splitting the paragraphs cuts that piece in two, and leaves Dummy0 as a piece with no source at all.

The conversion back to plain text estimated positions between known points, and LanguageTool's bookkeeping records no known point for a piece with no source. In the reporter's file, between the two known points around the misspelled word, the source grows by 24 characters and the plain text by 30, so the estimate was off by a few characters. In the example above, the server then quoted runs frobn instead of frobnitz, compared runs frobn with the dictionary, found no match, and kept the diagnostic.

Fix

  1. Exact conversion. The server now walks through the pieces of the text in order. A position inside prose maps one to one. A position inside markup maps to the start or the end of the plain text that markup became. A piece with no source is skipped. Nothing is estimated.
  2. Less code. The old conversion read a private field of LanguageTool by reflection. That code is removed.
  3. Comment. A comment in the paragraph splitter said that a cut never falls inside a piece of markup. It now says when it does.

Not changed

  • Paragraph splitting. The splitter still cuts inside the piece of markup. The ranges LanguageTool reports were already correct; only the conversion back to plain text was wrong.
  • "Add to dictionary". This action takes the word from the single paragraph, not from the request, and already took the right word.

Testing

  • New tests. AnnotatedTextFragmentTest checks the example above in Markdown, Org and reStructuredText: the message must quote 'frobnitz', and the dictionary entry must remove the diagnostic. On the old code this test fails with 'runs frobn': Possible spelling mistake found. It also checks the conversion position by position, for prose, inline math and markup with no plain text.
  • Full suite. 423 tests pass, with the Premium credentials loaded and nothing skipped. ktlint and detekt pass.
  • Over LSP. A script started the server, sent the file, and printed the diagnostics, with ltex.language = "en-GB". Before the fix, the reporter's file gave 'uns auto' with the bundled LanguageTool, and the frobnitz file gave 'runs frobn' with both the bundled LanguageTool and Premium, in each case also with the word in the dictionary. After the fix, with Premium:
File Word not in dictionary Word in dictionary
reporter's Markdown (autouse) 'autouse' no diagnostic
Markdown (frobnitz) 'frobnitz' no diagnostic
Org (frobnitz) 'frobnitz' no diagnostic
reStructuredText (frobnitz) 'frobnitz' no diagnostic

When a paragraph starts with inline code, the Markdown, Org and reStructuredText builders put the blank line before it and the code into one markup part (source "\n\n`code`", plain text "\n\nDummy0"). AnnotatedTextSlicer cuts the paragraph inside that part and leaves "Dummy0" as a markup part with no source. When paragraphs are checked in one request, getSubstringOfPlainText mapped source positions to plain-text positions by interpolating between the mapping points of an inverted AnnotatedText. LanguageTool keeps one mapping point per position, so the part with no source added none, and the word after it was quoted from the wrong place ('runs frobn' instead of 'frobnitz'). The dictionary check compares its entries against that quoted word, so the entry never matched and the word stayed flagged.

getPlainTextPosition now walks the parts in order and maps each position exactly. The reflection on LanguageTool's private mapping field is removed with the old helpers.

Fixes ltex-plus#226.
getSubstringOfPlainText rebuilt the list of pieces on each of its two calls to getPlainTextPosition. The fragment now builds it on the first call and keeps it, as it keeps its plain text.
@alberti42

Copy link
Copy Markdown
Member Author

Simpler and faster

Besides fixing the bug, the new code is shorter and faster than the code it replaces.

What this code does. When LanguageTool reports a spelling mistake, the server looks up the misspelled word in the text, to quote it in the message and to compare it with the dictionary. It does this once or twice for every spelling mistake.

Simpler. The old code made an inverted copy of the text, read a private field of LanguageTool to find a few known positions, sorted them, and estimated the position of the word between two of them. The new code walks through the pieces of the text (prose and markup) from the start, adding up their lengths until it reaches the word. The file went from 226 lines to 158.

Faster. I timed the lookup on a Markdown document whose paragraphs have links, emphasis and inline code, checked in one request as when a file is opened. Each time includes setting up the text and then looking up the given number of words. Times are the median of 2000 runs (50 for the slowest cases), after warm-up rounds, on a Mac.

Document Words looked up Before After
20 paragraphs 1 43 µs 3 µs
20 paragraphs 10 182 µs 5 µs
20 paragraphs 62 958 µs 13 µs
200 paragraphs 1 473 µs 25 µs
200 paragraphs 10 2.0 ms 37 µs
200 paragraphs 620 103 ms 0.85 ms

The old code redid its search and sorting for every word, so its time grew with the length of the document times the number of words. The new code prepares the list of pieces once per request, and then each word costs one pass over that list.

For most documents the difference is small next to the time LanguageTool takes to check the text. It matters for a long document with many spelling mistakes: in the last row, 620 words took 0.1 s before and 0.85 ms now.

@ColinTenaguillo

Copy link
Copy Markdown

I built this PR (fac85af) and compared it with 19.0.0 on the same files: the minimal example from #226, a file of edge cases, and two real Markdown documents (about 700 lines in all), with a local LanguageTool 6.8 HTTP server and my en-GB dictionary.

19.0.0 #227
Spelling diagnostics 64 59
Wrong word quoted in the message 5 0
Dictionary word still flagged 5 0
  • The 5 diagnostics that are gone are exactly the 5 misquoted dictionary words: inline code at the start of a paragraph (including the first one after a heading) and at the start of a list item. The other 59 diagnostics are identical.
  • A real misspelling right after inline code is still flagged.
  • Inline code in the middle of a sentence was already quoted correctly in 19.0.0, and still is.

It fixes my case. Thanks for the quick fix!

@spitzerd
spitzerd merged commit b841628 into ltex-plus:develop Nov 26, 2026
7 checks passed
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

Successfully merging this pull request may close these issues.

Misspelling quoted from the wrong slice after inline code (HTTP backend), so dictionary entries stay flagged

3 participants