-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathengine.vim
More file actions
406 lines (325 loc) · 9.39 KB
/
Copy pathengine.vim
File metadata and controls
406 lines (325 loc) · 9.39 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
let s:config = ctrlspace#context#Configuration()
let s:modes = ctrlspace#modes#Modes()
let s:resonators = ['.', '/', '_', '-', ' ']
if has("win32")
call add(s:resonators, '\')
endif
" returns [patterns, indices, size, text]
function! ctrlspace#engine#Content()
if !empty(s:config.FileEngine) && s:modes.File.Enabled
return s:contentFromFileEngine()
endif
let items = s:contentSource()
if !empty(s:modes.Search.Data.Letters)
let items = s:computeLowestNoises(items)
call sort(items, function("ctrlspace#engine#CompareByNoiseAndText"))
else
if len(items) > 500
let items = items[0:499]
endif
if s:modes.Tab.Enabled
call sort(items, function("ctrlspace#engine#CompareByIndex"))
else
call sort(items, function("ctrlspace#engine#CompareByText"))
endif
endif
" trim the list in search mode
if s:modes.Search.Enabled
let maxHeight = ctrlspace#window#MaxHeight()
if len(items) > maxHeight
let items = items[-maxHeight : -1]
endif
endif
return s:prepareContent(items)
endfunction
function! s:contentFromFileEngine()
call ctrlspace#files#CollectFiles()
let context = '{"Query":"' . join(s:modes.Search.Data.Letters, "") . '","Columns":' . &columns .
\ ',"Limit":' . (s:modes.Search.Enabled ? ctrlspace#window#MaxHeight() : 0) .
\ ',"Source":"' . escape(fnamemodify(ctrlspace#util#FilesCache(), ":p"), '\"') .
\ '","Dots":"' . s:config.Symbols.Dots . '","DotsSize":' . ctrlspace#context#SymbolSizes().Dots . '}'
let results = split(system(s:config.FileEngine, context), "\n")
let patterns = eval(results[0])
let indices = eval(results[1])
let size = str2nr(results[2])
let text = join(results[3:], "\n")
return [patterns, indices, size, text]
endfunction
function! ctrlspace#engine#CompareByText(a, b)
if a:a.text < a:b.text
return -1
elseif a:a.text > a:b.text
return 1
else
return 0
endif
endfunction
function! ctrlspace#engine#CompareByIndex(a, b)
if a:a.index < a:b.index
return -1
elseif a:a.index > a:b.index
return 1
else
return 0
endif
endfunction
function! ctrlspace#engine#CompareByNoiseAndText(a, b)
if a:a.noise < a:b.noise
return 1
elseif a:a.noise > a:b.noise
return -1
elseif a:a.smallnoise < a:b.smallnoise
return 1
elseif a:a.smallnoise > a:b.smallnoise
return -1
elseif strlen(a:a.text) < strlen(a:b.text)
return 1
elseif strlen(a:a.text) > strlen(a:b.text)
return -1
elseif a:a.text < a:b.text
return -1
elseif a:a.text > a:b.text
return 1
else
return 0
endif
endfunction
function! s:computeLowestNoises(source)
let results = []
let noises = []
let resultsCount = 0
for index in range(len(a:source))
let item = a:source[index]
let [noise, smallnoise, pattern] = s:findLowestSearchNoise(item.text)
if noise == -1
continue
else
let item.noise = noise
let item.smallnoise = smallnoise
let item.pattern = pattern
if resultsCount < 200
let resultsCount += 1
call add(results, item)
call add(noises, noise)
else
let maxIndex = index(noises, max(noises))
if noises[maxIndex] > noise
call remove(noises, maxIndex)
call insert(noises, noise, maxIndex)
call remove(results, maxIndex)
call insert(results, item, maxIndex)
endif
endif
endif
endfor
return results
endfunction
function! s:contentSource()
let clv = ctrlspace#modes#CurrentListView()
if clv.Name ==# "Buffer"
return s:bufferListContent(clv)
elseif clv.Name ==# "File"
return s:fileListContent(clv)
elseif clv.Name ==# "Tab"
return s:tabContent(clv)
elseif clv.Name ==# "Workspace"
return s:workspaceListContent(clv)
elseif clv.Name ==# "Bookmark"
return s:bookmarkListContent(clv)
endif
endfunction
function! s:bookmarkListContent(clv)
let content = []
let bookmarks = ctrlspace#bookmarks#Bookmarks()
for i in range(len(bookmarks))
let indicators = ""
if !empty(a:clv.Data.Active) && (bookmarks[i].Directory ==# a:clv.Data.Active.Directory)
let indicators .= s:config.Symbols.IA
endif
call add(content, { "index": i, "text": bookmarks[i].Name, "indicators": indicators })
endfor
return content
endfunction
function! s:workspaceListContent(clv)
let content = []
let workspaces = ctrlspace#workspaces#Workspaces()
let active = ctrlspace#workspaces#ActiveWorkspace()
for i in range(len(workspaces))
let name = workspaces[i]
let indicators = ""
if name ==# active.Name && active.Status
if active.Status == 2
let indicators .= s:config.Symbols.IM
endif
let indicators .= s:config.Symbols.IA
elseif name ==# a:clv.Data.LastActive
let indicators .= s:config.Symbols.IV
endif
call add(content, { "index": i, "text": name, "indicators": indicators })
endfor
return content
endfunction
function! s:tabContent(clv)
let content = []
let currentTab = tabpagenr()
for i in range(1, tabpagenr("$"))
let winnr = tabpagewinnr(i)
let buflist = tabpagebuflist(i)
let bufnr = buflist[winnr - 1]
let bufname = bufname(bufnr)
let tabBufsNumber = ctrlspace#api#TabBuffersNumber(i)
let title = ctrlspace#api#TabTitle(i, bufnr, bufname)
if !s:config.UseUnicode && !empty(tabBufsNumber)
let tabBufsNumber = ":" . tabBufsNumber
endif
let indicators = ""
if ctrlspace#api#TabModified(i)
let indicators .= s:config.Symbols.IM
endif
if i == currentTab
let indicators .= s:config.Symbols.IA
endif
call add(content, { "index": i, "text": string(i) . tabBufsNumber . " " . title, "indicators": indicators })
endfor
return content
endfunction
function! s:fileListContent(clv)
call ctrlspace#files#CollectFiles()
return deepcopy(ctrlspace#files#Items())
endfunction
function! s:bufferListContent(clv)
let content = []
if a:clv.Data.SubMode ==# "single"
let buffers = map(keys(ctrlspace#buffers#Buffers(tabpagenr())), "str2nr(v:val)")
elseif a:clv.Data.SubMode ==# "all"
let buffers = map(keys(ctrlspace#buffers#Buffers(0)), "str2nr(v:val)")
elseif a:clv.Data.SubMode ==# "visible"
let buffers = filter(map(keys(ctrlspace#buffers#Buffers(tabpagenr())), "str2nr(v:val)"), "bufwinnr(v:val) != -1")
endif
for i in buffers
let entry = s:bufferEntry(i)
if !empty(entry)
call add(content, entry)
endif
endfor
return content
endfunction
function! s:bufferEntry(bufnr)
let bufname = fnamemodify(bufname(a:bufnr), ":.")
let modified = getbufvar(a:bufnr, "&modified")
let winnr = bufwinnr(a:bufnr)
if !strlen(bufname) && (modified || (winnr != -1))
let bufname = "[" . a:bufnr . "*No Name]"
endif
if strlen(bufname)
let indicators = ""
if modified
let indicators .= s:config.Symbols.IM
endif
if winnr == t:CtrlSpaceStartWindow
let indicators .= s:config.Symbols.IA
elseif winnr != -1
let indicators .= s:config.Symbols.IV
endif
return { "index": a:bufnr, "text": bufname, "indicators": indicators }
else
return {}
endif
endfunction
function! s:findSubsequence(text, offset)
let positions = []
let noise = 0
let currentOffset = a:offset
for letter in s:modes.Search.Data.Letters
let matchedPosition = match(a:text, "\\m\\c" . letter, currentOffset)
if matchedPosition == -1
return [-1, []]
else
if !empty(positions)
let noise += abs(matchedPosition - positions[-1]) - 1
endif
call add(positions, matchedPosition)
let currentOffset = matchedPosition + 1
endif
endfor
return [noise, positions]
endfunction
function! s:findLowestSearchNoise(text)
let noise = -1
let smallnoise = 0
let matchedString = ""
let ltrLen = len(s:modes.Search.Data.Letters)
let textLen = strlen(a:text)
if ltrLen == 1
let noise = match(a:text, "\\m\\c" . s:modes.Search.Data.Letters[0])
if noise > -1
let matchedString = s:modes.Search.Data.Letters[0]
endif
else
let offset = 0
let positions = []
while ltrLen <= textLen - offset
let subseq = s:findSubsequence(a:text, offset)
if subseq[0] == -1
break
elseif (noise == -1) || (subseq[0] < noise)
let [noise, positions] = subseq
let offset = positions[0] + 1
else
let offset += 1
endif
endwhile
if noise > -1
let matchedString = a:text[positions[0]:positions[-1]]
let smallnoise = 0
if positions[0] != 0
let smallnoise += 1
if index(s:resonators, a:text[positions[0] - 1]) == -1
let smallnoise += 1
endif
endif
if positions[-1] != textLen - 1
let smallnoise += 1
if index(s:resonators, a:text[positions[-1] + 1]) == -1
let smallnoise += 1
endif
endif
endif
endif
let pattern = ""
if (noise > -1) && !empty(matchedString)
let pattern = matchedString
endif
return [noise, smallnoise, pattern]
endfunction
function! s:prepareContent(items)
let sizes = ctrlspace#context#SymbolSizes()
if s:modes.File.Enabled
let itemSpace = 5
elseif s:modes.Bookmark.Enabled
let itemSpace = 5 + sizes.IAV
else
let itemSpace = 5 + sizes.IAV + sizes.IM
endif
let content = ""
let patterns = {}
let indices = []
for item in a:items
let line = item.text
if strwidth(line) + itemSpace > &columns
let line = s:config.Symbols.Dots . strpart(line, strwidth(line) - &columns + itemSpace + sizes.Dots)
endif
if !empty(item.indicators)
let line .= " " . item.indicators
endif
while strwidth(line) < &columns
let line .= " "
endwhile
let content .= " " . line . "\n"
if has_key(item, "pattern")
let patterns[item.pattern] = 1
endif
call add(indices, item.index)
endfor
return [keys(patterns), indices, len(a:items), content]
endfunction