@@ -9,180 +9,162 @@ M.name = 'resolver'
99M .description = ' Collection of resolvers for dynamic filetype detection'
1010
1111M .config = {
12- sources = {
13- nestjs = false ,
14- toggleterm = false ,
15- oil = false ,
16- },
12+ sources = {
13+ nestjs = false ,
14+ toggleterm = false ,
15+ oil = false ,
16+ },
1717}
1818
1919local cache = {
20- nestjs = {},
20+ nestjs = {},
2121}
2222
23- local function has_file (dir , filename )
24- return vim .fs .root (dir , filename ) ~= nil
25- end
23+ local function has_file (dir , filename ) return vim .fs .root (dir , filename ) ~= nil end
2624
2725local sources = {
28- nestjs = {
29- event = { ' pre_activity' },
30- match = {
31- filetype = {
32- ' angular' ,
33- ' javascript' ,
34- ' typescript'
35- }
36- },
37- run = function (opts )
38- local ws = opts .workspace_dir or uv .cwd ()
39- local cached = cache .nestjs [ws ]
40-
41- if cached == nil then
42- -- `0` for current buffer
43- cached = has_file (0 , ' nest-cli.json' )
44- cache .nestjs [ws ] = cached
45- end
46-
47- if cached then
48- opts .force_filetype = ' nest'
49- end
50- end ,
51- },
52- toggleterm = {
53- event = { ' pre_activity' },
54- match = { filetype = ' toggleterm' },
55- run = function (opts )
56- local cmd = opts .filename :match ' ^([^%s;]+)'
57- if cmd and cmd ~= ' ' then
58- local asset = config .assets [cmd ]
59- local fallback = mappings .filetype_mappings [cmd ]
60- local name
61- if asset then
62- name = asset .tooltip
63- if type (name ) == ' function' then
64- name = name (opts )
65- end
66-
67- if name == nil then
68- name = fallback and fallback [3 ] or cmd
69- end
70- elseif fallback then
71- name = fallback [3 ]
72- else
73- name = cmd
74- end
75-
76- opts .filename = name
77- opts .force_filetype = cmd
78- end
79- end ,
26+ nestjs = {
27+ event = { ' pre_activity' },
28+ match = {
29+ filetype = {
30+ ' angular' ,
31+ ' javascript' ,
32+ ' typescript' ,
33+ },
8034 },
81- oil = {
82- event = { ' workspace_change' },
83- match = {
84- workspace = ' .' ,
85- filetype = { ' oil' , ' oil_preview' , ' oil_progress' },
86- },
87- run = function (opts )
88- local path = vim .fn .expand ' %:p:h'
89- if path :sub (1 , 7 ) == ' oil:///' then
90- local stripped = path :sub (7 )
91- local cached = opts .manager .workspace :get (stripped )
92-
93- local info = {
94- dir = stripped ,
95- name = vim .fn .fnamemodify (stripped , ' :t' ),
96- repo_url = cached and cached .repo_url or nil ,
97- }
98-
99- opts .manager .workspace :set (path , info )
100- opts .manager .workspace :set_current (info )
101-
102- opts .workspace = info .name
103- opts .workspace_dir = info .dir
104- opts .repo_url = info .repo_url
105- end
106- end ,
35+ run = function (opts )
36+ local ws = opts .workspace_dir or uv .cwd ()
37+ local cached = cache .nestjs [ws ]
38+
39+ if cached == nil then
40+ -- `0` for current buffer
41+ cached = has_file (0 , ' nest-cli.json' )
42+ cache .nestjs [ws ] = cached
43+ end
44+
45+ if cached then opts .force_filetype = ' nest' end
46+ end ,
47+ },
48+ toggleterm = {
49+ event = { ' pre_activity' },
50+ match = { filetype = ' toggleterm' },
51+ run = function (opts )
52+ local cmd = opts .filename :match ' ^([^%s;]+)'
53+ if cmd and cmd ~= ' ' then
54+ local asset = config .assets [cmd ]
55+ local fallback = mappings .filetype_mappings [cmd ]
56+ local name
57+ if asset then
58+ name = asset .tooltip
59+ if type (name ) == ' function' then name = name (opts ) end
60+
61+ if name == nil then name = fallback and fallback [3 ] or cmd end
62+ elseif fallback then
63+ name = fallback [3 ]
64+ else
65+ name = cmd
66+ end
67+
68+ opts .filename = name
69+ opts .force_filetype = cmd
70+ end
71+ end ,
72+ },
73+ oil = {
74+ event = { ' workspace_change' },
75+ match = {
76+ workspace = ' .' ,
77+ filetype = { ' oil' , ' oil_preview' , ' oil_progress' },
10778 },
79+ run = function (opts )
80+ local path = vim .fn .expand ' %:p:h'
81+ if path :sub (1 , 7 ) == ' oil:///' then
82+ local stripped = path :sub (7 )
83+ local cached = opts .manager .workspace :get (stripped )
84+
85+ local info = {
86+ dir = stripped ,
87+ name = vim .fn .fnamemodify (stripped , ' :t' ),
88+ repo_url = cached and cached .repo_url or nil ,
89+ }
90+
91+ opts .manager .workspace :set (path , info )
92+ opts .manager .workspace :set_current (info )
93+
94+ opts .workspace = info .name
95+ opts .workspace_dir = info .dir
96+ opts .repo_url = info .repo_url
97+ end
98+ end ,
99+ },
108100}
109101
110102local active_resolvers = {}
111103
112104local function check_match (match , opts )
113- if not match then
114- return true
115- end
105+ if not match then return true end
116106
117- if type (match ) == ' function' then
118- return match (opts )
119- end
107+ if type (match ) == ' function' then return match (opts ) end
120108
121- for k , v in pairs (match ) do
122- if type (v ) == ' table' then
123- if not vim .tbl_contains (v , opts [k ]) then
124- return false
125- end
126- elseif opts [k ] ~= v then
127- return false
128- end
109+ for k , v in pairs (match ) do
110+ if type (v ) == ' table' then
111+ if not vim .tbl_contains (v , opts [k ]) then return false end
112+ elseif opts [k ] ~= v then
113+ return false
129114 end
115+ end
130116
131- return true
117+ return true
132118end
133119
134120function M .setup (config )
135- if config then
136- M .config = vim .tbl_deep_extend (' force' , M .config , config )
121+ if config then M .config = vim .tbl_deep_extend (' force' , M .config , config ) end
122+
123+ local user_resolvers = config and config .resolvers
124+ if type (user_resolvers ) == ' boolean' then
125+ user_resolvers = { user_resolvers }
126+ elseif type (user_resolvers ) ~= ' table' then
127+ user_resolvers = {}
128+ end
129+ local global_toggle = user_resolvers [1 ]
130+
131+ for name , source in pairs (sources ) do
132+ local enabled = user_resolvers [name ]
133+ if enabled == nil then
134+ if global_toggle ~= nil then
135+ enabled = global_toggle
136+ else
137+ enabled = M .config .sources [name ]
138+ end
137139 end
138140
139- local user_resolvers = config and config .resolvers
140- if type (user_resolvers ) == ' boolean' then
141- user_resolvers = { user_resolvers }
142- elseif type (user_resolvers ) ~= ' table' then
143- user_resolvers = {}
144- end
145- local global_toggle = user_resolvers [1 ]
146-
147- for name , source in pairs (sources ) do
148- local enabled = user_resolvers [name ]
149- if enabled == nil then
150- if global_toggle ~= nil then
151- enabled = global_toggle
152- else
153- enabled = M .config .sources [name ]
154- end
155- end
141+ if enabled then
142+ for _ , event in ipairs (source .event ) do
143+ if not active_resolvers [event ] then active_resolvers [event ] = {} end
144+ table.insert (active_resolvers [event ], source )
145+ end
156146
157- if enabled then
158- for _ , event in ipairs (source .event ) do
159- if not active_resolvers [event ] then
160- active_resolvers [event ] = {}
161- end
162- table.insert (active_resolvers [event ], source )
163- end
164-
165- logger .debug (' Resolver: Registered resolver ' .. name )
166- end
167- end
168-
169- local priorities = require (' cord.internal.hooks' ).PRIORITY
170- M .hooks = {}
171- for event , resolvers in pairs (active_resolvers ) do
172- M .hooks [event ] = {
173- fun = function (opts )
174- for name , resolver in pairs (resolvers ) do
175- if check_match (resolver .match , opts ) then
176- logger .debug (' Resolver: Running resolver ' .. name )
177- resolver .run (opts )
178- end
179- end
180- end ,
181- priority = priorities .HIGHEST ,
182- }
147+ logger .debug (' Resolver: Registered resolver ' .. name )
183148 end
149+ end
150+
151+ local priorities = require (' cord.internal.hooks' ).PRIORITY
152+ M .hooks = {}
153+ for event , resolvers in pairs (active_resolvers ) do
154+ M .hooks [event ] = {
155+ fun = function (opts )
156+ for name , resolver in pairs (resolvers ) do
157+ if check_match (resolver .match , opts ) then
158+ logger .debug (' Resolver: Running resolver ' .. name )
159+ resolver .run (opts )
160+ end
161+ end
162+ end ,
163+ priority = priorities .HIGHEST ,
164+ }
165+ end
184166
185- return M
167+ return M
186168end
187169
188170return M
0 commit comments