-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtreeview.lua
209 lines (181 loc) · 6.08 KB
/
treeview.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
local core = require "core"
local config = require "core.config"
-- Delay loading of TreeView changes to properly check if the plugin is enabled.
core.add_thread(function()
-- Load treeview if enabled to add menu entries
---@module 'plugins.treeview'
local TreeView
---@module 'core.contextmenu'
local TreeViewMenu
if config.plugins.treeview ~= false then
TreeView = require "plugins.treeview"
TreeViewMenu = TreeView.contextmenu
else
return
end
local scm = require "plugins.scm"
local command = require "core.command"
--------------------------------------------------------------------------------
-- Override treeview to change color of files depending on status
--------------------------------------------------------------------------------
local treeview_get_item_text = TreeView.get_item_text
function TreeView:get_item_text(item, active, hovered)
local text, font, color = treeview_get_item_text(self, item, active, hovered)
local path = item.abs_filename
local status = scm.get_path_changes(path)
if status then
if status.text then text = status.text end
color = status.color
end
return text, font, color
end
--------------------------------------------------------------------------------
-- Add entries to treeview contextmenu
--------------------------------------------------------------------------------
-- Entries when right clicking project root
TreeViewMenu:register(
function()
return TreeView.hovered_item
and scm.is_scm_project(TreeView.hovered_item.abs_filename)
end,
{
TreeViewMenu.DIVIDER,
{ text = "Pull From Remote", command = "treeview:scm-pull" },
{ text = "View Changes Diff", command = "treeview:scm-global-diff" },
{ text = "View Project Status", command = "treeview:scm-project-status" },
{ text = "View Commits History", command = "treeview:scm-commits-history" }
}
)
TreeViewMenu:register(function()
return TreeView.hovered_item
and scm.get_path_backend(TreeView.hovered_item.abs_filename)
end, {
TreeViewMenu.DIVIDER,
{ text = "Add to Repo", command = "treeview:scm-path-add" },
{ text = "Remove from Repo", command = "treeview:scm-path-remove" },
{ text = "Add to Staging", command = "treeview:scm-staging-add" },
{ text = "Remove from Staging", command = "treeview:scm-staging-remove" },
{ text = "Revert Changes", command = "treeview:scm-file-revert" },
{ text = "View Changes Diff", command = "treeview:scm-file-diff" },
{ text = "View Commits History", command = "treeview:scm-commits-history" }
})
--------------------------------------------------------------------------------
-- Register treeview commands
--------------------------------------------------------------------------------
command.add(
function()
return TreeView.hovered_item
and scm.is_scm_project(TreeView.hovered_item.abs_filename)
end, {
["treeview:scm-pull"] = function()
scm.pull(TreeView.hovered_item.abs_filename)
end,
["treeview:scm-global-diff"] = function()
scm.open_diff(TreeView.hovered_item.abs_filename)
end,
["treeview:scm-project-status"] = function()
scm.open_project_status(TreeView.hovered_item.abs_filename)
end,
["treeview:scm-commits-history"] = function()
scm.open_commit_history(TreeView.hovered_item.abs_filename)
end
})
command.add(
function()
return TreeView.hovered_item
and (
scm.is_scm_project(TreeView.hovered_item.abs_filename)
or
scm.get_path_backend(TreeView.hovered_item.abs_filename, false, true)
)
end, {
["treeview:scm-commits-history"] = function()
scm.open_commit_history(TreeView.hovered_item.abs_filename)
end
})
command.add(
function()
return TreeView.hovered_item
and scm.get_path_backend(TreeView.hovered_item.abs_filename)
and scm.get_path_status(TreeView.hovered_item.abs_filename) == "untracked"
end, {
["treeview:scm-path-add"] = function()
scm.add_path(TreeView.hovered_item.abs_filename)
end
})
command.add(
function()
return TreeView.hovered_item
and scm.get_path_status(TreeView.hovered_item.abs_filename) == "unchanged"
end, {
["treeview:scm-path-remove"] = function()
scm.remove_path(TreeView.hovered_item.abs_filename)
end
})
command.add(
function()
if TreeView.hovered_item then
local path = TreeView.hovered_item.abs_filename
local status = scm.get_path_status(path)
if status == "edited" and not scm.is_staged(path) then
local backend = scm.get_path_backend(path)
if backend and backend:has_staging() then
return true
end
end
end
return false
end, {
["treeview:scm-staging-add"] = function()
scm.stage_file(TreeView.hovered_item.abs_filename)
end
})
command.add(
function()
if TreeView.hovered_item then
local backend = scm.get_path_backend(TreeView.hovered_item.abs_filename)
if backend and backend:has_staging() then
if scm.is_staged(TreeView.hovered_item.abs_filename) then
return true
end
end
end
return false
end, {
["treeview:scm-staging-remove"] = function()
scm.unstage_file(TreeView.hovered_item.abs_filename)
end
})
command.add(
function()
if TreeView.hovered_item then
local path = TreeView.hovered_item.abs_filename
local status = scm.get_path_status(path)
local backend = scm.get_path_backend(path)
if backend and backend:has_staging() then
if status == "edited" and not scm.is_staged(path) then
return true
end
elseif backend then
return scm.get_path_backend(
TreeView.hovered_item.abs_filename, true, true
)
end
end
return false
end, {
["treeview:scm-file-revert"] = function()
scm.revert_file(TreeView.hovered_item.abs_filename)
end,
})
command.add(
function()
return TreeView.hovered_item
and scm.get_path_status(TreeView.hovered_item.abs_filename) == "edited"
and not scm.is_staged(TreeView.hovered_item.abs_filename)
end, {
["treeview:scm-file-diff"] = function()
scm.open_path_diff(TreeView.hovered_item.abs_filename)
end
})
end) -- end initialization coroutine