Repository navigation
Quote the right word after inline code at the start of a paragraph - #227
Conversation
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.
Simpler and fasterBesides 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.
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. |
|
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.
It fixes my case. Thanks for the quick fix! |
Fixes #226.
Problem
A word in
ltex.dictionaryis still flagged as a spelling mistake when inline code comes before it at the start of a paragraph. Example, withfrobnitzin the dictionary:The server reports
'runs frobn': Possible spelling mistake found.onfrobnitz. 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
Dummy0as 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 frobninstead offrobnitz, comparedruns frobnwith the dictionary, found no match, and kept the diagnostic.Fix
Not changed
Testing
AnnotatedTextFragmentTestchecks 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.ltex.language = "en-GB". Before the fix, the reporter's file gave'uns auto'with the bundled LanguageTool, and thefrobnitzfile 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:autouse)'autouse'frobnitz)'frobnitz'frobnitz)'frobnitz'frobnitz)'frobnitz'