aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrxi <rxi@users.noreply.github.com>2020-04-22 00:18:50 +0100
committerrxi <rxi@users.noreply.github.com>2020-04-22 00:20:20 +0100
commit7e82d2acc9d5fdd78bc4cc16d7ca3585bbd08999 (patch)
treef05184f24824e1b5b7516f5f2303b351a8be9066
parente9898390b3fed1d7450ad35ef2a0e166e4a5a5d0 (diff)
downloadlite-xl-plugins-7e82d2acc9d5fdd78bc4cc16d7ca3585bbd08999.tar.gz
lite-xl-plugins-7e82d2acc9d5fdd78bc4cc16d7ca3585bbd08999.zip
Added plugin `spellcheck`
-rw-r--r--README.md1
-rw-r--r--spellcheck.lua67
2 files changed, 68 insertions, 0 deletions
diff --git a/README.md b/README.md
index 56c17ef..92bd2e3 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,7 @@ Plugin | Description
[`language_wren`](language_wren.lua?raw=1) | Syntax for the [Wren](http://wren.io/) programming language
[`macmodkeys`](macmodkeys.lua?raw=1) | Remaps mac modkeys `command/option` to `ctrl/alt`
[`sort`](sort.lua?raw=1) | Sorts selected lines alphabetically
+[`spellcheck`](spellcheck.lua?raw=1) | [Underlines](https://user-images.githubusercontent.com/3920290/79923973-9caa7400-842e-11ea-85d4-7a196a91ca50.png) misspelt words
[`theme16`](https://github.com/monolifed/theme16) | Theme manager with base16 themes
[`titleize`](titleize.lua?raw=1) | Titleizes selected string (`hello world` => `Hello World`)
[`togglesnakecamel`](togglesnakecamel.lua?raw=1) | Toggles symbols between `snake_case` and `camelCase`
diff --git a/spellcheck.lua b/spellcheck.lua
new file mode 100644
index 0000000..b25f2ac
--- /dev/null
+++ b/spellcheck.lua
@@ -0,0 +1,67 @@
+local core = require "core"
+local style = require "core.style"
+local config = require "core.config"
+local command = require "core.command"
+local DocView = require "core.docview"
+
+config.spellcheck_files = { "%.txt$", "%.md$", "%.markdown$" }
+config.dictionary_file = "/usr/share/dict/words"
+
+local inited = false
+local words
+
+local function init_words()
+ if inited then return end
+ inited = true
+ core.add_thread(function()
+ local t = {}
+ local i = 0
+ for line in io.lines(config.dictionary_file) do
+ for word in line:gmatch("%a+") do
+ t[word:lower()] = true
+ end
+ i = i + 1
+ if i % 1000 == 0 then coroutine.yield() end
+ end
+ words = t
+ core.redraw = true
+ end)
+end
+
+
+local function matches_any(filename, ptns)
+ for _, ptn in ipairs(ptns) do
+ if filename:find(ptn) then return true end
+ end
+end
+
+
+local draw_line_text = DocView.draw_line_text
+
+function DocView:draw_line_text(idx, x, y)
+ draw_line_text(self, idx, x, y)
+
+ init_words()
+ if not words
+ or not matches_any(self.doc.filename or "", config.spellcheck_files) then
+ return
+ end
+
+ local s, e = 0, 0
+ local text = self.doc.lines[idx]
+ local l, c = self.doc:get_selection()
+
+ while true do
+ s, e = text:find("%a+", e + 1)
+ if not s then break end
+ local word = text:sub(s, e):lower()
+ if not words[word] and not (l == idx and c == 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 h = style.divider_size
+ renderer.draw_rect(x1, y + self:get_line_height() - h, x2 - x1, h, color)
+ end
+ end
+end
+