summaryrefslogtreecommitdiff
path: root/script/provider/spell.lua
blob: b315b3aae703a18fe80ec0ac89aa081b153954b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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