forked from iGio90/filemanager-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual.lua
More file actions
222 lines (182 loc) · 5.59 KB
/
virtual.lua
File metadata and controls
222 lines (182 loc) · 5.59 KB
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
local micro = import('micro')
local config = import('micro/config')
local utils = dofile(config.ConfigDir .. '/plug/filemanager/utils.lua')
local Settings = dofile(config.ConfigDir .. '/plug/filemanager/settings.lua')
local Virtual = {}
Virtual.__index = Virtual
function Virtual:new(bp)
local instance = setmetatable({}, Virtual)
instance.bp = bp
instance.cursor = self.Cursor:new(bp)
instance.selected_lines = {}
instance.last_line_interact = nil
return instance
end
function Virtual:click_event()
self.last_line_interact = nil
self.selected_lines = {self.cursor:get_line_num()}
self.cursor:save_current_loc()
self.bp:Deselect()
self.cursor:select_all()
end
function Virtual:unselect_all()
self.bp:Deselect()
end
function Virtual:drag_event()
local hovered_line = self.cursor:get_loc_y()
local start_click_line = self.cursor.last_click_loc.Y
local previous_interacted_line = self.last_line_interact or start_click_line
local is_up_direction = hovered_line < previous_interacted_line
local is_hovered_line_selected = self:is_line_selected(hovered_line)
self.last_line_interact = hovered_line
if hovered_line == start_click_line then
self.bp:RemoveAllMultiCursors()
self.selected_lines = {start_click_line}
self:refresh()
return
end
if hovered_line == previous_interacted_line then
self:refresh()
return
end
if is_hovered_line_selected then
self.bp:RemoveMultiCursor()
table.remove(self.selected_lines)
else
table.insert(self.selected_lines, hovered_line)
self.cursor:restore_loc()
self.bp:SpawnMultiCursorUpN(is_up_direction and 1 or -1 )
end
self:refresh()
end
function Virtual:is_line_selected(line_number)
for _, line in ipairs(self.selected_lines) do
if line == line_number then
return true
end
end
return false
end
function Virtual:select_line_on_cursor()
self.selected_lines = {self.cursor:get_loc().Y}
self.bp.Cursor:SelectLine()
end
function Virtual:refresh()
self.cursor:restore_loc()
self.cursor:select_all()
end
function Virtual:move_cursor_and_select_line(line_num)
self.bp.Cursor.Y = line_num
self.bp.Cursor:SelectLine()
end
function Virtual:move_cursor_and_select_first_line()
self:move_cursor_and_select_line(Settings.Const.previousDirectoryLine + 1)
self:adjust()
end
function Virtual:move_cursor_and_select_last_line()
self:move_cursor_and_select_line(self.bp.Buf:LinesNum() - 1)
self:adjust()
end
function Virtual:move_cursor(line_num)
self.selected_lines = {self.cursor:get_line_num()}
self.bp.Cursor.Y = line_num
end
function Virtual:adjust()
self.bp:Center()
end
Virtual.Cursor = {}
function Virtual.Cursor:new(bp)
local self = setmetatable({}, { __index = Virtual.Cursor })
self.bp = bp
self.cursor_loc_tmp = nil
self.last_click_loc = {}
return self
end
function Virtual.Cursor:select_all()
local cursors = self.bp.Buf:GetCursors()
-- self:restore_loc()
for i = 1, #cursors do
cursors[i]:SelectLine()
end
end
function Virtual.Cursor:get_line_text()
return self.bp.Buf:line(self:get_line_num())
end
function Virtual.Cursor:get_line_text_len()
return #self.bp.Buf:line(self:get_line_num()) + 3
end
function Virtual.Cursor:move_to_file_name_start()
local line_string = self:get_line_text()
local first_char_loc = utils.first_char_loc(line_string)
self.bp:Deselect()
self:set_loc_x(first_char_loc)
end
function Virtual.Cursor:move_to_end()
self.bp:Deselect()
self.bp.Cursor:End()
end
-- Moves the cursor to the first character of file_name,
-- Selects everything to end of the line
function Virtual.Cursor:select_file_name()
self:move_to_file_name_start()
self.bp:SelectToEndOfLine()
end
-- Selects the entire file name, if it's an extension file
-- unselect till the first dot
function Virtual.Cursor:select_file_name_no_extension()
self:select_file_name()
local line_string = self:get_line_text()
local dot_loc = utils.get_dot_location(line_string)
if dot_loc then
for i = 1, #line_string - dot_loc + 1 do
self.bp:SelectLeft()
end
end
-- This makes get_can_move_right return true after selecting
self.bp.Cursor:Left()
end
function Virtual.Cursor:restore_loc()
self.bp:Deselect()
self:set_loc(self.last_click_loc)
end
function Virtual.Cursor:adjust()
local first_char_loc = utils.first_char_loc(self:get_line_text())
if self:get_loc_x() < first_char_loc then
self:set_loc_x(first_char_loc)
end
end
function Virtual.Cursor:save_current_loc()
--self.cursor_loc_tmp = self.bp.Cursor.Loc seems to pass a reference not a value
self.last_click_loc.X = self.bp.Cursor.Loc.X
self.last_click_loc.Y = self.bp.Cursor.Loc.Y
end
function Virtual.Cursor:get_can_move_right()
-- -3 because line text has an icon which has more than 1 byte
local current_line_len = #self:get_line_text() - 3
return self:get_loc_x() <= current_line_len
end
function Virtual.Cursor:get_can_move_left()
return self:get_loc_x() > utils.first_char_loc(self:get_line_text())
end
function Virtual.Cursor:get_loc()
return self.bp.Cursor.Loc
end
function Virtual.Cursor:get_loc_x()
return self.bp.Cursor.Loc.X
end
function Virtual.Cursor:get_loc_y()
return self.bp.Cursor.Loc.Y
end
function Virtual.Cursor:get_line_num()
return self.bp.Cursor.Loc.Y
end
function Virtual.Cursor:set_loc(loc)
self.bp.Cursor.Loc = loc
end
function Virtual.Cursor:set_loc_x(loc_x)
self.bp.Cursor.Loc.X = loc_x
end
function Virtual.Cursor:ser_loc_zero_zero()
self:set_loc({X = 0, Y = 0})
end
return Virtual