forked from eugenpt/lite-xl-vibe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.lua
229 lines (191 loc) · 6.84 KB
/
translate.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
local core = require "core"
local command = require "core.command"
local common = require "core.common"
local config = require "core.config"
local translate = require "core.doc.translate"
local kb = require "plugins.vibe.keyboard"
local misc = require "plugins.vibe.misc"
local function dv()
return core.active_view
end
local function doc()
return core.active_view.doc
end
-------------------------------------------------------------------------------
local translations = {}
-------------------------------------------------------------------------------
-- Translations --
-------------------------------------------------------------------------------
local function is_non_word(char)
return config.non_word_chars:find(char, nil, true)
end
function translate.next_word_start(doc, line, col)
local prev
local end_line, end_col = translate.end_of_doc(doc, line, col)
while line < end_line or col < end_col do
prev = doc:get_char(line, col)
local line2, col2 = doc:position_offset(line, col, 1)
local char = doc:get_char(line2, col2)
line, col = line2, col2
if is_non_word(prev) and not is_non_word(char)
-- or line == line2 and col == col2
then
break
end
end
return line, col
end
translations["next-word-start"] = translate.next_word_start
-------------------------------------------------------------------------------
-- WORDs
-------------------------------------------------------------------------------
local function is_non_WORD(char)
return config.non_WORD_chars:find(char, nil, true)
end
function translate.previous_WORD_start(doc, line, col)
local prev
while line > 1 or col > 1 do
local l, c = doc:position_offset(line, col, -1)
local char = doc:get_char(l, c)
if prev and prev ~= char or not is_non_WORD(char) then
break
end
prev, line, col = char, l, c
end
return translate.start_of_WORD(doc, line, col)
end
function translate.next_WORD_end(doc, line, col)
local prev
local end_line, end_col = translate.end_of_doc(doc, line, col)
while line < end_line or col < end_col do
local char = doc:get_char(line, col)
if prev and prev ~= char or not is_non_WORD(char) then
break
end
line, col = doc:position_offset(line, col, 1)
prev = char
end
return translate.end_of_WORD(doc, line, col)
end
function translate.start_of_WORD(doc, line, col)
while true do
local line2, col2 = doc:position_offset(line, col, -1)
local char = doc:get_char(line2, col2)
if is_non_WORD(char)
or line == line2 and col == col2 then
break
end
line, col = line2, col2
end
return line, col
end
function translate.end_of_WORD(doc, line, col)
while true do
local line2, col2 = doc:position_offset(line, col, 1)
local char = doc:get_char(line, col)
if is_non_WORD(char)
or line == line2 and col == col2 then
break
end
line, col = line2, col2
end
return line, col
end
function translate.next_WORD_start(doc, line, col)
local prev
local end_line, end_col = translate.end_of_doc(doc, line, col)
while line < end_line or col < end_col do
prev = doc:get_char(line, col)
local line2, col2 = doc:position_offset(line, col, 1)
local char = doc:get_char(line2, col2)
line, col = line2, col2
if is_non_WORD(prev) and not is_non_WORD(char)
-- or line == line2 and col == col2
then
break
end
end
return line, col
end
translations["previous-WORD-start"] = translate.previous_WORD_start
translations["next-WORD-end"] = translate.next_WORD_end
translations["next-WORD-start"] = translate.next_WORD_start
function translate.first_line_char(doc, line, col)
return translate.next_WORD_start(doc, line, 1)
end
translations["first-line-character"] = translate.first_line_char
-------------------------------------------------------------------------------
-- symbols
-------------------------------------------------------------------------------
for _,i in ipairs(kb.all_typed_symbols) do
translations['next-symbol-'..i] = function(doc,line,col)
return misc.find_in_line(i, false, true, doc,line,col)
end
translations['previous-symbol-'..i] = function(doc,line,col)
return misc.find_in_line(i, true, true, doc,line,col)
end
translations['next-symbol-excluded-'..i] = function(doc,line,col)
return misc.find_in_line(i, false, false, doc,line,col)
end
translations['previous-symbol-excluded-'..i] = function(doc,line,col)
return misc.find_in_line(i, true, false, doc,line,col)
end
end
-- matching
for _,objects in pairs(misc.matching_objectss) do
local symbol = objects[1]
local symbol_match = objects[2]
local object_name = symbol..symbol_match
for include=0,1 do
local include_suffix = include==1 and '-inluded' or ''
translations['previous-'..object_name..'-start'..include_suffix] = function(doc, line, col)
return misc.find_in_line_unmatched(symbol, symbol_match, true, include==1, doc, line, col)
end
translations['next-'..object_name..'-end'..include_suffix] = function(doc, line, col)
return misc.find_in_line_unmatched(symbol_match, symbol, false, include==1, doc, line, col)
end
end
end
-------------------------------------------------------------------------------
local commands = {}
for name, fn in pairs(translations) do
commands["doc:move-to-" .. name] = function() doc():move_to(fn, dv()) end
commands["doc:select-to-" .. name] = function() doc():select_to(fn, dv()) end
commands["doc:delete-to-" .. name] = function() doc():delete_to(fn, dv()) end
end
command.add("core.docview", commands)
--
command.add(misc.has_selection, {
["doc:move-to-selection-start"] = function()
local line,col,line2,col2 = doc():get_selection(true) -- true for sort
doc():set_selection(line,col,line2,col2)
end,
["doc:move-to-selection-end"] = function()
local line,col,line2,col2 = doc():get_selection(true) -- true for sort
doc():set_selection(line2,col2,line,col)
end,
["doc:move-to-other-edge-of-selection"] = function()
local line,col,line2,col2 = doc():get_selection() -- true for sort
doc():set_selection(line2,col2,line,col)
end,
})
--
local objects = misc.keys(misc.matching_objectss)
-- objects[#objects+1] = 'word' -- this one exists already
objects[#objects+1] = 'block' -- Why doesn't this one exist?
objects[#objects+1] = 'WORD'
for _, obj in ipairs(objects) do
command.add("core.docview",{
["doc:select-"..obj] = function()
-- in case of selection, go to start of it
if misc.has_selection() then
-- repeated backwards search needs to be jump-started
command.perform("doc:move-to-selection-start")
command.perform("doc:select-to-previous-char")
end
command.perform("doc:select-to-previous-"..obj.."-start")
command.perform("doc:move-to-selection-end")
command.perform("doc:select-to-next-"..obj.."-end")
end,
})
end