-
-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathslist.lua
More file actions
108 lines (94 loc) · 2.05 KB
/
slist.lua
File metadata and controls
108 lines (94 loc) · 2.05 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
---@class Node
---@field value NodeValue
---@field next Node
---@field prev Node
---@class NodeValue
---@field range? Range
---@field bufnr? number
---@field client_id number
---@field inlevel number
---@field line string
---@field uri string
---@field winline number
---@class Range
---@field start Position
---@field end Position
---@alias Position {character:number, line:number}
---single linked list module
local M = {}
function M.new()
return { value = nil, next = nil, prev = nil }
end
function M.tail_push(list, node)
local tmp = list
if not tmp.value then
tmp.value = node
return
end
while true do
if not tmp.next then
break
end
tmp = tmp.next
end
tmp.next = { value = node }
end
--- Finds a node in the linked list that matches the given predicate function.
---@param list Node -- The linked list to search.
---@param predicate fun(node: Node): boolean -- The predicate function to match the node.
---@return Node | nil -- The found node or nil if not found.
function M.find_node_by(list, predicate)
local current = list
while current do
if predicate(current) then
return current
end
current = current.next
end
return nil
end
function M.find_node(list, curlnum)
local tmp = list
if not tmp.value then
return
end
while tmp do
if tmp.value.winline == curlnum then
return tmp
end
tmp = tmp.next
end
end
function M.insert_node(curnode, node)
local tmp = curnode.next
curnode.next = {
value = node,
next = tmp,
}
end
function M.update_winline(node, count)
node = node.next
local total = count < 0 and math.abs(count) or 0
while node do
if total ~= 0 then
node.value.winline = -1
total = total - 1
if node.value.expand then
node.value.expand = false
end
else
if node.value.winline ~= -1 then
node.value.winline = node.value.winline + count
end
end
node = node.next
end
end
function M.list_map(list, fn)
local node = list
while node do
fn(node)
node = node.next
end
end
return M