aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-03-02 17:21:34 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2023-03-02 17:21:34 +0100
commit870fe9f83d2105dc5025f0b2895112c1d5466f13 (patch)
treedf4b810fd722aeef12c65e5a28276b7cce974b6b
parent40f20dfde4a5702b0b02b2d89ad746a93d851c27 (diff)
downloadvis-spellcheck-870fe9f83d2105dc5025f0b2895112c1d5466f13.tar.gz
fix typo detection at file start
If a file starts with a typo our pattern [%A]typo[%A] can not find it. However if the same typo appears a second type in the text our pattern happily finds the second occurence skipping the first and all typos in between. In the text typo1 Correct text typo2 ... typo1 our pattern will match the second occurence of 'typo1' skipping 'typo2'. Since we update our search index 'typo2' will never be found and vis-spellcheck will show the user an information prompt. Fix this behaviour by adding a special case for the first typo which may start the text.
-rw-r--r--spellcheck.lua37
1 files changed, 19 insertions, 18 deletions
diff --git a/spellcheck.lua b/spellcheck.lua
index a8dd37d..1d0d6c7 100644
--- a/spellcheck.lua
+++ b/spellcheck.lua
@@ -125,32 +125,33 @@ local function typo_iter(text, typos, ignored) -- luacheck: ignore ignored
until (not typo or (typo ~= '' and not ignored[typo]))
if typo then
- -- to prevent typos from being found in correct words before them
- -- ("stuff stuf", "broken ok", ...)
- -- we match typos only when they are enclosed in non-letter characters.
- local start, finish = text:find('[%A]' .. escape_lua_pattern(typo) ..
- '[%A]', index)
- -- typo was not found by our pattern this means it must be either
- -- the first or last word in the text
- if not start then
- -- check start of text
+ local start, finish
+ -- special case for typos at the beginning of the text
+ -- Leading typos are not found by our used pattern [%A]typo[%A].
+ -- To prevent typos to be skipped if the leading typo accours an
+ -- additional time in the text we need this special case
+ if index == 1 and text:sub(1, #typo) == typo then
start = 1
finish = #typo
- -- typo is not the beginning must be the end of text
- if text:sub(start, finish) ~= typo then
+ else
+ -- to prevent typos from being found in correct words before them
+ -- ("stuff stuf", "broken ok", ...)
+ -- we match typos only when they are enclosed in non-letter characters.
+ local pattern = '[%A]' .. escape_lua_pattern(typo) .. '[%A]'
+ start, finish = text:find(pattern, index)
+ if start then -- our pattern [%A]typo[%A] found it
+ start = start + 1 -- ignore leading non letter char
+ finish = finish - 1 -- ignore trailing non letter char
+ else -- typo was not found by our pattern this means it must be the last word
start = #text - #typo + 1
finish = start + #typo - 1
- end
- if text:sub(start, finish) ~= typo then
- vis:info(string.format(
+ if text:sub(start, finish) ~= typo then
+ vis:info(string.format(
'can\'t find typo %s after %d. Please report this bug.',
typo, index))
+ end
end
- -- our pettern [%A]typo[%A] found it
- else
- start = start + 1 -- ignore leading non letter char
- finish = finish - 1 -- ignore trailing non letter char
end
index = finish