-
Notifications
You must be signed in to change notification settings - Fork 1
/
suggestions.lua
93 lines (82 loc) · 3.17 KB
/
suggestions.lua
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
local backend = require("textadept-spellchecker.backend")
local check = require("textadept-spellchecker.check")
---------------------------
-- Indicator click handler
---------------------------
local SUGGESTION_LIST = 4242 -- List id
-- autocomplete handler will be placed here to avoid presence of
-- many anonymous handlers in event table
local current_autocomplete_handler = false
-- Word beginning and length for correct substitution of suggestion
local g_word_start = 0
local g_word_length = 0
-- only this autocomplete handler presents in event table
local function on_answer(word, suggestion)
-- Handles autocompletion answer if it is necessary
-- then removes handler
if current_autocomplete_handler then
current_autocomplete_handler(word, suggestion or "")
end
-- remove handler to avoid displaying suggestion again without click
current_autocomplete_handler = false
end
local function on_suggestion_click(list_id, selection, pos)
-- Handles click on item in suggestion list and replaces mistake
if list_id == SUGGESTION_LIST then
if selection == _L["DICT_ADD"] then
-- TODO: addition to the dictionary
local checker = backend.get_checker()
local word = buffer:text_range(g_word_start, g_word_start+g_word_length)
checker:write("* "..word.."\n")
checker:write("#\n")
elseif selection == _L["IGNORE"] then
local checker = backend.get_checker()
local word = buffer:text_range(g_word_start, g_word_start+g_word_length)
checker:write("@ "..word.."\n")
else
buffer:delete_range(g_word_start, g_word_length)
buffer:insert_text(buffer.current_pos, selection)
end
check.frame()
end
end
local function on_indicator_click(pos, mod)
-- Handles click on indicator and shows suggestion menu
if mod ~= 0 then return end -- no modificators should be pressed
local word_start = buffer:word_start_position(pos, false)
local word_end = buffer:word_end_position(pos, false)
g_word_start = word_start
local word = buffer:text_range(word_start, word_end)
current_autocomplete_handler = function(origin_word, suggestions)
g_word_length = origin_word:len()
local old_separator = buffer.auto_c_separator
buffer.auto_c_separator = string.byte(",")
local suggestions_list = _L["DICT_ADD"]..
",".._L["IGNORE"]
if suggestions and suggestions:len() > 0 then
suggestions_list = suggestions_list..","..
suggestions:gsub(", ",",")
end
buffer:user_list_show(SUGGESTION_LIST,
suggestions_list
)
buffer.auto_c_separator = old_separator
end
local checker = backend.get_checker()
checker:write(word.."\n")
end
local _M = {}
local function shutdown()
events.disconnect(events.INDICATOR_CLICK, on_indicator_click)
events.disconnect(backend.ANSWER, on_answer)
events.disconnect(events.USER_LIST_SELECTION, on_suggestion_click)
events.disconnect(events.RESET_BEFORE, shutdown)
end
function _M.init()
events.connect(events.INDICATOR_CLICK, on_indicator_click)
events.connect(backend.ANSWER, on_answer)
events.connect(events.USER_LIST_SELECTION, on_suggestion_click)
events.connect(events.QUIT, shutdown)
-- events.connect(events.RESET_BEFORE, shutdown)
end
return _M