-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfigs.lua
289 lines (272 loc) · 10.8 KB
/
configs.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
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
local M = {}
local Defaults = {
-- print permanent url in command line
message = true,
-- highlight the linked region
highlight_duration = 500,
-- user command
command = {
name = "GitLink",
desc = "Generate git permanent link",
},
-- router bindings
router = {
browse = {
-- example: /~https://github.com/linrongbin16/gitlinker.nvim/blob/9679445c7a24783d27063cd65f525f02def5f128/lua/gitlinker.lua#L3-L4
["^github%.com"] = "/~https://github.com/"
.. "{_A.ORG}/"
.. "{_A.REPO}/blob/"
.. "{_A.REV}/"
.. "{_A.FILE}?plain=1" -- '?plain=1'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://gitlab.com/linrongbin16/test/blob/e1c498a4bae9af6e61a2f37e7ae622b2cc629319/test.lua#L3-L5
["^gitlab%.com"] = "https://gitlab.com/"
.. "{_A.ORG}/"
.. "{_A.REPO}/blob/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://bitbucket.org/gitlinkernvim/gitlinker.nvim/src/dbf3922382576391fbe50b36c55066c1768b08b6/.gitignore#lines-9:14
["^bitbucket%.org"] = "https://bitbucket.org/"
.. "{_A.ORG}/"
.. "{_A.REPO}/src/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "#lines-{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and (':' .. _A.LEND) or '')}",
-- example: https://codeberg.org/linrongbin16/gitlinker.nvim/src/commit/a570f22ff833447ee0c58268b3bae4f7197a8ad8/LICENSE#L4-L7
["^codeberg%.org"] = "https://codeberg.org/"
.. "{_A.ORG}/"
.. "{_A.REPO}/src/commit/"
.. "{_A.REV}/"
.. "{_A.FILE}?display=source" -- '?display=source'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example:
-- main repo: https://git.samba.org/?p=samba.git;a=blob;f=wscript;hb=83e8971c0f1c1db8c3574f83107190ac1ac23db0#l6
-- user repo: https://git.samba.org/?p=bbaumbach/samba.git;a=blob;f=wscript;hb=8de348e9d025d336a7985a9025fe08b7096c0394#l7
["^git%.samba%.org"] = "https://git.samba.org/?p="
.. "{string.len(_A.ORG) > 0 and (_A.ORG .. '/') or ''}" -- 'p=samba.git;' or 'p=bbaumbach/samba.git;'
.. "{_A.REPO .. '.git'};a=blob;"
.. "f={_A.FILE};"
.. "hb={_A.REV}"
.. "#l{_A.LSTART}",
},
blame = {
-- example: /~https://github.com/linrongbin16/gitlinker.nvim/blame/9679445c7a24783d27063cd65f525f02def5f128/lua/gitlinker.lua#L3-L7
["^github%.com"] = "/~https://github.com/"
.. "{_A.ORG}/"
.. "{_A.REPO}/blame/"
.. "{_A.REV}/"
.. "{_A.FILE}?plain=1" -- '?plain=1'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://gitlab.com/linrongbin16/test/blame/e1c498a4bae9af6e61a2f37e7ae622b2cc629319/test.lua#L4-8
["^gitlab%.com"] = "https://gitlab.com/"
.. "{_A.ORG}/"
.. "{_A.REPO}/blame/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://bitbucket.org/gitlinkernvim/gitlinker.nvim/annotate/dbf3922382576391fbe50b36c55066c1768b08b6/.gitignore#lines-9:14
["^bitbucket%.org"] = "https://bitbucket.org/"
.. "{_A.ORG}/"
.. "{_A.REPO}/annotate/"
.. "{_A.REV}/"
.. "{_A.FILE}"
.. "#lines-{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and (':' .. _A.LEND) or '')}",
-- example: https://codeberg.org/linrongbin16/gitlinker.nvim/blame/commit/a570f22ff833447ee0c58268b3bae4f7197a8ad8/LICENSE#L4-L7
["^codeberg%.org"] = "https://codeberg.org/"
.. "{_A.ORG}/"
.. "{_A.REPO}/blame/commit/"
.. "{_A.REV}/"
.. "{_A.FILE}?display=source" -- '?display=source'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
},
default_branch = {
-- example: /~https://github.com/linrongbin16/gitlinker.nvim/blob/master/lua/gitlinker.lua#L3-L4
["^github%.com"] = "/~https://github.com/"
.. "{_A.ORG}/"
.. "{_A.REPO}/blob/"
.. "{_A.DEFAULT_BRANCH}/"
.. "{_A.FILE}?plain=1" -- '?plain=1'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://gitlab.com/linrongbin16/test/blob/main/test.lua#L3-L4
["^gitlab%.com"] = "https://gitlab.com/"
.. "{_A.ORG}/"
.. "{_A.REPO}/blob/"
.. "{_A.DEFAULT_BRANCH}/"
.. "{_A.FILE}"
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://bitbucket.org/gitlinkernvim/gitlinker.nvim/src/master/.gitignore#lines-9:14
["^bitbucket%.org"] = "https://bitbucket.org/"
.. "{_A.ORG}/"
.. "{_A.REPO}/src/"
.. "{_A.DEFAULT_BRANCH}/"
.. "{_A.FILE}"
.. "#lines-{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and (':' .. _A.LEND) or '')}",
-- example: https://codeberg.org/linrongbin16/gitlinker.nvim/src/branch/main/LICENSE#L4-L6
["^codeberg%.org"] = "https://codeberg.org/"
.. "{_A.ORG}/"
.. "{_A.REPO}/src/branch/"
.. "{_A.DEFAULT_BRANCH}/"
.. "{_A.FILE}?display=source" -- '?display=source'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example:
-- main repo: https://git.samba.org/?p=samba.git;a=blob;f=wscript#l6
-- user repo: https://git.samba.org/?p=bbaumbach/samba.git;a=blob;f=wscript#l7
["^git%.samba%.org"] = "https://git.samba.org/?p="
.. "{string.len(_A.ORG) > 0 and (_A.ORG .. '/') or ''}" -- 'p=samba.git;' or 'p=bbaumbach/samba.git;'
.. "{_A.REPO .. '.git'};a=blob;"
.. "f={_A.FILE}"
.. "#l{_A.LSTART}",
},
current_branch = {
-- example: /~https://github.com/linrongbin16/gitlinker.nvim/blob/master/lua/gitlinker.lua#L3-L4
["^github%.com"] = "/~https://github.com/"
.. "{_A.ORG}/"
.. "{_A.REPO}/blob/"
.. "{_A.CURRENT_BRANCH}/"
.. "{_A.FILE}?plain=1" -- '?plain=1'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://gitlab.com/linrongbin16/test/blob/main/test.lua#L3-L4
["^gitlab%.com"] = "https://gitlab.com/"
.. "{_A.ORG}/"
.. "{_A.REPO}/blob/"
.. "{_A.CURRENT_BRANCH}/"
.. "{_A.FILE}"
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example: https://bitbucket.org/gitlinkernvim/gitlinker.nvim/src/master/.gitignore#lines-9:14
["^bitbucket%.org"] = "https://bitbucket.org/"
.. "{_A.ORG}/"
.. "{_A.REPO}/src/"
.. "{_A.CURRENT_BRANCH}/"
.. "{_A.FILE}"
.. "#lines-{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and (':' .. _A.LEND) or '')}",
-- example: https://codeberg.org/linrongbin16/gitlinker.nvim/src/branch/main/LICENSE#L4-L6
["^codeberg%.org"] = "https://codeberg.org/"
.. "{_A.ORG}/"
.. "{_A.REPO}/src/branch/"
.. "{_A.CURRENT_BRANCH}/"
.. "{_A.FILE}?display=source" -- '?display=source'
.. "#L{_A.LSTART}"
.. "{(_A.LEND > _A.LSTART and ('-L' .. _A.LEND) or '')}",
-- example:
-- main repo: https://git.samba.org/?p=samba.git;a=blob;f=wscript#l6
-- user repo: https://git.samba.org/?p=bbaumbach/samba.git;a=blob;f=wscript#l7
["^git%.samba%.org"] = "https://git.samba.org/?p="
.. "{string.len(_A.ORG) > 0 and (_A.ORG .. '/') or ''}" -- 'p=samba.git;' or 'p=bbaumbach/samba.git;'
.. "{_A.REPO .. '.git'};a=blob;"
.. "f={_A.FILE}"
.. "#l{_A.LSTART}",
},
},
-- enable debug
debug = false,
-- write logs to console(command line)
console_log = true,
-- write logs to file
file_log = false,
}
--- @type gitlinker.Options
local Configs = {}
--- @param opts gitlinker.Options
--- @return table<string, {list_routers:table,map_routers:table}>
M._merge_routers = function(opts)
local result = {}
-- users list
if type(opts.router) == "table" then
-- user_router_type: browse, blame, etc
for user_router_type, user_router_bindings in pairs(opts.router) do
if result[user_router_type] == nil then
result[user_router_type] = {}
result[user_router_type].list_routers = {}
result[user_router_type].map_routers = {}
end
-- list
for i, tuple in ipairs(user_router_bindings) do
if type(i) == "number" and type(tuple) == "table" and #tuple == 2 then
-- prepend to head for higher priority
table.insert(result[user_router_type].list_routers, 1, tuple)
end
end
end
end
-- default map
for default_router_type, default_router_bindings in pairs(Defaults.router) do
if result[default_router_type] == nil then
result[default_router_type] = {}
result[default_router_type].list_routers = {}
result[default_router_type].map_routers = {}
end
-- map
for pattern, route in pairs(default_router_bindings) do
if result[default_router_type].map_routers == nil then
result[default_router_type].map_routers = {}
end
if
type(pattern) == "string"
and string.len(pattern) > 0
and (type(route) == "string" or type(route) == "function")
then
result[default_router_type].map_routers[pattern] = route
end
end
end
-- default list
for default_router_type, default_router_bindings in pairs(Defaults.router) do
-- list
for i, tuple in ipairs(default_router_bindings) do
if type(i) == "number" and type(tuple) == "table" and #tuple == 2 then
table.insert(result[default_router_type].list_routers, tuple)
end
end
end
-- user map
if type(opts.router) == "table" then
for user_router_type, user_router_bindings in pairs(opts.router) do
-- map
for pattern, route in pairs(user_router_bindings) do
if result[user_router_type].map_routers == nil then
result[user_router_type].map_routers = {}
end
if
type(pattern) == "string"
and string.len(pattern) > 0
and (type(route) == "string" or type(route) == "function")
then
-- override default routers
result[user_router_type].map_routers[pattern] = route
end
end
end
end
-- logger.debug("|gitlinker._merge_routers| result:%s", vim.inspect(result))
return result
end
--- @alias gitlinker.Options table<string, any>
--- @param opts gitlinker.Options?
--- @return gitlinker.Options
M.setup = function(opts)
local merged_routers = M._merge_routers(opts or {})
Configs = vim.tbl_deep_extend("force", vim.deepcopy(Defaults), opts or {})
Configs._routers = merged_routers
return Configs
end
--- @return gitlinker.Options
M.get = function()
return Configs
end
return M