From 870fe9f83d2105dc5025f0b2895112c1d5466f13 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Thu, 2 Mar 2023 17:21:34 +0100 Subject: 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. --- spellcheck.lua | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'spellcheck.lua') 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 -- cgit v1.2.3