aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2020-08-26 01:27:23 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2020-08-26 01:42:55 +0200
commit8093caf55ba0df77fe297f682c2a327fba8ccb28 (patch)
tree4ad82076487e0eb8103d1725c0dc71bf318d83b4
parent85a97a1aab13dc9acb19715e8bc58c6f8c3bcd51 (diff)
downloadvis-spellcheck-8093caf55ba0df77fe297f682c2a327fba8ccb28.tar.gz
vis-spellcheck-8093caf55ba0df77fe297f682c2a327fba8ccb28.zip
extract generic spellchecking functionality from the full viewport function
Add get_typos(range) to retrieve a string of misspelled word in a specific file range by calling the spellchecker's list command. It return nil or a string with each misspell followed by a newline. Introduce typo_iter(text, typos, ignored) to iterate over all not ignored typos and their positions in text. It returns an statefull iterator function closure. Which will return the next typo and ist start and finish in the text, starting by 1.
-rw-r--r--spellcheck.lua53
1 files changed, 38 insertions, 15 deletions
diff --git a/spellcheck.lua b/spellcheck.lua
index 5a62b84..29718aa 100644
--- a/spellcheck.lua
+++ b/spellcheck.lua
@@ -28,8 +28,43 @@ spellcheck.check_tokens = {
[vis.lexers.COMMENT] = true
}
+
+-- Return nil or a string of misspelled word in a specific file range
+-- by calling the spellchecker's list command.
+-- The returned string consists of each misspell followed by a newline.
+local function get_typos(range)
+ local cmd = spellcheck.list_cmd:format(spellcheck.lang)
+ local ret, so, se = vis:pipe(vis.win.file, range, cmd)
+ if ret ~= 0 then
+ vis:info("calling " .. cmd .. " failed ("..ret..")")
+ return nil
+ end
+ return so
+end
+
+-- plugin global list of ignored typos
local ignored = {}
+
+-- Return an iterator over all not ignored typos and their positions in text.
+-- The returned iterator is a seelf contained statefull iterator function closure.
+-- Which will return the next typo and ist start and finish in the text, starting by 1.
+local function typo_iter(text, typos, ignored)
+ local index = 0
+ local unfiltered_iterator, iter_state = typos:gmatch("(.-)\n")
+ return function(foo, bar)
+ repeat
+ typo = unfiltered_iterator(iter_state)
+ until(not typo or not ignored[typo])
+
+ if typo then
+ local start, finish = text:find(typo, index, true)
+ index = finish
+ return typo, start, finish
+ end
+ end
+end
+
local last_viewport, last_typos = nil, ""
vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
@@ -44,23 +79,11 @@ vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
if last_viewport == viewport_text then
typos = last_typos
else
- local cmd = spellcheck.list_cmd:format(spellcheck.lang)
- local ret, so, se = vis:pipe(win.file, viewport, cmd)
- if ret ~= 0 then
- vis:message("calling " .. cmd .. " failed ("..se..")")
- return false
- end
- typos = so or ""
+ typos = get_typos(viewport) or ""
end
- local corrections_iter = typos:gmatch("(.-)\n")
- local index = 1
- for typo in corrections_iter do
- if not ignored[typo] then
- local start, finish = viewport_text:find(typo, index, true)
- win:style(42, viewport.start + start - 1, viewport.start + finish)
- index = finish
- end
+ for typo, start, finish in typo_iter(viewport_text, typos, ignored) do
+ win:style(42, viewport.start + start - 1, viewport.start + finish)
end
last_viewport = viewport_text