diff options
Diffstat (limited to 'script')
-rw-r--r-- | script/config/config.lua | 1 | ||||
-rw-r--r-- | script/core/code-action.lua | 47 | ||||
-rw-r--r-- | script/core/diagnostics/spell-check.lua | 34 | ||||
-rw-r--r-- | script/proto/define.lua | 4 | ||||
-rw-r--r-- | script/provider/spell.lua | 56 |
5 files changed, 141 insertions, 1 deletions
diff --git a/script/config/config.lua b/script/config/config.lua index dc4b9330..46e80994 100644 --- a/script/config/config.lua +++ b/script/config/config.lua @@ -220,6 +220,7 @@ local Template = { ['Lua.format.enable'] = Type.Boolean >> true, ['Lua.format.defaultConfig'] = Type.Hash(Type.String, Type.String) >> {}, + ['Lua.spell.dict'] = Type.Array(Type.String), ['Lua.telemetry.enable'] = Type.Or(Type.Boolean >> false, Type.Nil) >> nil, -- VSCode diff --git a/script/core/code-action.lua b/script/core/code-action.lua index 29c63a41..116fff24 100644 --- a/script/core/code-action.lua +++ b/script/core/code-action.lua @@ -352,6 +352,51 @@ local function solveAwaitInSync(uri, diag, results) } end +local function solveSpell(uri, diag, results) + local spell = require 'provider.spell' + local word = diag.data + if word == nil then + return + end + + results[#results+1] = { + title = lang.script('ACTION_ADD_DICT', word), + kind = 'quickfix', + command = { + title = lang.script.COMMAND_ADD_DICT, + command = 'lua.setConfig', + arguments = { + { + key = 'Lua.spell.dict', + action = 'add', + value = word, + uri = uri, + } + } + } + } + + local suggests = spell.getSpellSuggest(word) + for _, suggest in ipairs(suggests) do + results[#results+1] = { + title = suggest, + kind = 'quickfix', + edit = { + changes = { + [uri] = { + { + start = converter.unpackPosition(uri, diag.range.start), + finish = converter.unpackPosition(uri, diag.range["end"]), + newText = suggest + } + } + } + } + } + end + +end + local function solveDiagnostic(uri, diag, start, results) if diag.source == lang.script.DIAG_SYNTAX_CHECK then solveSyntax(uri, diag, results) @@ -372,6 +417,8 @@ local function solveDiagnostic(uri, diag, start, results) solveTrailingSpace(uri, diag, results) elseif diag.code == 'await-in-sync' then solveAwaitInSync(uri, diag, results) + elseif diag.code == 'spell-check' then + solveSpell(uri, diag, results) end disableDiagnostic(uri, diag.code, start, results) end diff --git a/script/core/diagnostics/spell-check.lua b/script/core/diagnostics/spell-check.lua new file mode 100644 index 00000000..ebdb0245 --- /dev/null +++ b/script/core/diagnostics/spell-check.lua @@ -0,0 +1,34 @@ +local files = require 'files' +local converter = require 'proto.converter' +local log = require 'log' +local spell = require 'provider.spell' + + +---@async +return function(uri, callback) + local text = files.getText(uri) + if not text then + return + end + + local status, diagnosticInfos = spell.spellCheck(uri, text) + + if not status then + if diagnosticInfos ~= nil then + log.error(diagnosticInfos) + end + + return + end + + if diagnosticInfos then + for _, diagnosticInfo in ipairs(diagnosticInfos) do + callback { + start = converter.unpackPosition(uri, diagnosticInfo.range.start), + finish = converter.unpackPosition(uri, diagnosticInfo.range["end"]), + message = diagnosticInfo.message, + data = diagnosticInfo.data + } + end + end +end diff --git a/script/proto/define.lua b/script/proto/define.lua index d6e25052..52006992 100644 --- a/script/proto/define.lua +++ b/script/proto/define.lua @@ -61,7 +61,8 @@ m.DiagnosticDefaultSeverity = { ['duplicate-doc-field'] = 'Warning', ['unknown-diag-code'] = 'Warning', - ['codestyle-check'] = "Warning", + ['codestyle-check'] = 'Warning', + ['spell-check'] = 'Information', } ---@alias DiagnosticDefaultNeededFileStatus @@ -123,6 +124,7 @@ m.DiagnosticDefaultNeededFileStatus = { ['unknown-diag-code'] = 'Any', ['codestyle-check'] = 'None', + ['spell-check'] = 'None', } --- 诊断报告标签 diff --git a/script/provider/spell.lua b/script/provider/spell.lua new file mode 100644 index 00000000..b315b3aa --- /dev/null +++ b/script/provider/spell.lua @@ -0,0 +1,56 @@ +local suc, codeFormat = pcall(require, 'code_format') +if not suc then + return +end + +local fs = require 'bee.filesystem' +local config = require 'config' +local diagnostics = require 'provider.diagnostic' + +local m = {} + +function m.loadDictionaryFromFile(filePath) + return codeFormat.spell_load_dictionary_from_path(filePath) +end + +function m.loadDictionaryFromBuffer(buffer) + return codeFormat.spell_load_dictionary_from_buffer(buffer) +end + +function m.addWord(word) + return codeFormat.spell_load_dictionary_from_buffer(word) +end + +function m.spellCheck(uri, text) + if not m._dictionaryLoaded then + m.initDictionary() + m._dictionaryLoaded = true + end + + local tempDict = config.get(uri, 'Lua.spell.dict') + + return codeFormat.spell_analysis(uri, text, tempDict) +end + +function m.getSpellSuggest(word) + local status, result = codeFormat.spell_suggest(word) + if status then + return result + end +end + +function m.initDictionary() + local basicDictionary = fs.path(METAPATH) / "spell/dictionary.txt" + local luaDictionary = fs.path(METAPATH) / "spell/lua_dict.txt" + + m.loadDictionaryFromFile(basicDictionary:string()) + m.loadDictionaryFromFile(luaDictionary:string()) +end + +config.watch(function (uri, key, value, oldValue) + if key == 'Lua.spell.dict' and uri ~= nil then + diagnostics.refresh(uri) + end +end) + +return m |