diff options
author | jgmdev <jgmdev@gmail.com> | 2022-11-01 20:57:07 -0400 |
---|---|---|
committer | jgmdev <jgmdev@gmail.com> | 2022-11-01 20:57:07 -0400 |
commit | 49139e03398c9d0ecb347267a4882a4eb3f7ee23 (patch) | |
tree | 6ac890a209172a29e09ad47ccd0156e7f7114382 /plugins/spellcheck.lua | |
parent | 380f6ef5fe9f8af19cd1f6b4c043eede51cbfcae (diff) | |
parent | 0971a7a686a4e18ee31b576c460966a5ec20ff01 (diff) | |
download | lite-xl-plugins-49139e03398c9d0ecb347267a4882a4eb3f7ee23.tar.gz lite-xl-plugins-49139e03398c9d0ecb347267a4882a4eb3f7ee23.zip |
Merge branch '2.1'
Diffstat (limited to 'plugins/spellcheck.lua')
-rw-r--r-- | plugins/spellcheck.lua | 133 |
1 files changed, 97 insertions, 36 deletions
diff --git a/plugins/spellcheck.lua b/plugins/spellcheck.lua index 7b0ba2b..f55cf1f 100644 --- a/plugins/spellcheck.lua +++ b/plugins/spellcheck.lua @@ -1,4 +1,4 @@ --- mod-version:2 -- lite-xl 2.0 +-- mod-version:3 local core = require "core" local style = require "core.style" local config = require "core.config" @@ -7,33 +7,42 @@ local common = require "core.common" local DocView = require "core.docview" local Doc = require "core.doc" -config.plugins.spellcheck = {} -config.plugins.spellcheck.files = { "%.txt$", "%.md$", "%.markdown$" } +local platform_dictionary_file if PLATFORM == "Windows" then - config.plugins.spellcheck.dictionary_file = EXEDIR .. "/words.txt" + platform_dictionary_file = EXEDIR .. "/words.txt" else - config.plugins.spellcheck.dictionary_file = "/usr/share/dict/words" + platform_dictionary_file = "/usr/share/dict/words" end +config.plugins.spellcheck = common.merge({ + enabled = true, + files = { "%.txt$", "%.md$", "%.markdown$" }, + dictionary_file = platform_dictionary_file +}, config.plugins.spellcheck) local last_input_time = 0 local word_pattern = "%a+" local words -core.add_thread(function() - local t = {} - local i = 0 - for line in io.lines(config.plugins.spellcheck.dictionary_file) do - for word in line:gmatch(word_pattern) do - t[word:lower()] = true +local function load_dictionary() + core.add_thread(function() + local t = {} + local i = 0 + for line in io.lines(config.plugins.spellcheck.dictionary_file) do + for word in line:gmatch(word_pattern) do + t[word:lower()] = true + end + i = i + 1 + if i % 1000 == 0 then coroutine.yield() end end - i = i + 1 - if i % 1000 == 0 then coroutine.yield() end - end - words = t - core.redraw = true - core.log_quiet("Finished loading dictionary file: \"%s\"", config.plugins.spellcheck.dictionary_file) -end) + words = t + core.redraw = true + core.log_quiet( + "Finished loading dictionary file: \"%s\"", + config.plugins.spellcheck.dictionary_file + ) + end) +end local function matches_any(filename, ptns) @@ -62,11 +71,16 @@ end local draw_line_text = DocView.draw_line_text function DocView:draw_line_text(idx, x, y) - draw_line_text(self, idx, x, y) - - if not words - or not matches_any(self.doc.filename or "", config.plugins.spellcheck.files) then - return + local lh = draw_line_text(self, idx, x, y) + + if + not config.plugins.spellcheck.enabled + or + not words + or + not matches_any(self.doc.filename or "", config.plugins.spellcheck.files) + then + return lh end local s, e = 0, 0 @@ -78,12 +92,13 @@ function DocView:draw_line_text(idx, x, y) local word = text:sub(s, e):lower() if not words[word] and not active_word(self.doc, idx, e + 1) then local color = style.spellcheck_error or style.syntax.keyword2 - local x1 = x + self:get_col_x_offset(idx, s) - local x2 = x + self:get_col_x_offset(idx, e + 1) + local x1, y1 = self:get_line_screen_position(idx, s) + local x2, y2 = self:get_line_screen_position(idx, e + 1) local h = math.ceil(1 * SCALE) - renderer.draw_rect(x1, y + self:get_line_height() - h, x2 - x1, h, color) + renderer.draw_rect(x1, y1 + self:get_line_height() - h, x2 - x1, h, color) end end + return lh end @@ -112,8 +127,44 @@ local function compare_words(word1, word2) end +-- The config specification used by the settings gui +config.plugins.spellcheck.config_spec = { + name = "Spell Check", + { + label = "Enabled", + description = "Disable or enable spell checking.", + path = "enabled", + type = "toggle", + default = true + }, + { + label = "Files", + description = "List of Lua patterns matching files to spell check.", + path = "files", + type = "list_strings", + default = { "%.txt$", "%.md$", "%.markdown$" } + }, + { + label = "Dictionary File", + description = "Path to a text file that contains a list of dictionary words.", + path = "dictionary_file", + type = "file", + exists = true, + default = platform_dictionary_file, + on_apply = function() + load_dictionary() + end + } +} + +load_dictionary() + command.add("core.docview", { + ["spell-check:toggle"] = function() + config.plugins.spellcheck.enabled = not config.plugins.spellcheck.enabled + end, + ["spell-check:add-to-dictionary"] = function() local word = get_word_at_caret() if words[word] then @@ -164,18 +215,28 @@ command.add("core.docview", { -- select word and init replacement selector local label = string.format("Replace \"%s\" With", word) doc:set_selection(line, e + 1, line, s) - core.command_view:enter(label, function(text, item) - text = item and item.text or text - doc:replace(function() return text end) - end, function(text) - local t = {} - for _, w in ipairs(suggestions) do - if w:lower():find(text:lower(), 1, true) then - table.insert(t, w) + core.command_view:enter(label, { + submit = function(text, item) + text = item and item.text or text + doc:replace(function() return text end) + end, + suggest = function(text) + local t = {} + for _, w in ipairs(suggestions) do + if w:lower():find(text:lower(), 1, true) then + table.insert(t, w) + end end + return t end - return t - end) + }) end, }) + +local contextmenu = require "plugins.contextmenu" +contextmenu:register("core.docview", { + contextmenu.DIVIDER, + { text = "View Suggestions", command = "spell-check:replace" }, + { text = "Add to Dictionary", command = "spell-check:add-to-dictionary" } +}) |