-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconfig.vim
More file actions
263 lines (249 loc) · 8.59 KB
/
config.vim
File metadata and controls
263 lines (249 loc) · 8.59 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
" vim: noet
let s:configFileName = 'vim-node-config.json'
function! s:removeSessionKeys(session,...)
for uvar in a:000
if has_key(a:session, uvar)
call remove(a:session, uvar)
endif
endfor
endfunction
" configuration defualts, for parameters which might or might not appear in
" the configuration.
function! nodeinspect#config#SetConfigurationDefaults(session)
let a:session["restart"] = 0
let a:session["configUsed"] = 0
let a:session["cwd"] = getcwd()
let a:session["envFile"] = ""
let a:session["env"] = ""
let a:session["runtimeExecutable"] = ""
let a:session["runtimeArgs"] = []
let a:session["exJob"] = -1
endfunction
" replace macros for a string. current recognizable macros (well, one):
" ${workspaceFolder} = pwd
function! s:ReplaceMacros(str)
let replaced = a:str
if match(replaced, "${workspaceFolder}") != -1
let currentDirectory = getcwd()
let replaced = substitute(a:str, "${workspaceFolder}", currentDirectory ,"")
endif
return replaced
endfunction
" find the config file path. if its not in the currend working directory, try
" going up from the current buffer directory. Returns the directory or empty
" string if failed to find the config file.
function s:GetConfigFilePath()
let configFilePath = getcwd() . '/' . s:configFileName
if filereadable(configFilePath)
return configFilePath
endif
" if the file is not found in pwd and the script is a decedant, try going up
let expandString = '%:p:h'
let traverseDir = expand(expandString)
while stridx(traverseDir, getcwd()) != -1
let configFilePath = traverseDir . '/' . s:configFileName
if filereadable(configFilePath)
return configFilePath
endif
let expandString = expandString . ':h'
let traverseDir = expand(expandString)
endwhile
return ''
endfunction
" try and load the config file; it migth not exist, in this case use the
" defaults. returns 0 on success, !0 on failure.
function! nodeinspect#config#LoadConfigFile(session)
let configuration = {}
let configFilePath = s:GetConfigFilePath()
let fullFile = ''
" clear previous sessoin config
call s:removeSessionKeys(a:session,"localRoot","remoteRoot")
if configFilePath != ''
" indicate this configuration is from file
let a:session["configUsed"] = 1
"read file
let lines = readfile(configFilePath)
for line in lines
let fullFile = fullFile . line
endfor
" loaded the entire file, parse it to object
" configPtr holds the used configuration
let configObj = json_decode(fullFile)
let configPtr = v:null
if type(configObj) == v:t_dict
" test wherever its a multi configuration
if has_key(a:session, "configName") == 1 || has_key(configObj,"configurations") == 1
" validation, the first arg must be present and be the config name
if len(a:session["args"]) < 1
echom "Config name must be specified (only it). Use 'args' for paramters"
return 1
endif
" set the config name. other args are irrelevant - should be set from
" the arg configuration
let a:session["configName"] = a:session["args"][0]
if has_key(configObj,"configurations") == 1 && type(configObj["configurations"]) == v:t_list
" loop over configurations, get the relevant one
for configItem in configObj["configurations"]
if type(configItem) == v:t_dict && has_key(configItem, "name") && configItem["name"] == a:session["configName"]
let configPtr = configItem
break
endif
endfor
endif
if type(configPtr) != v:t_dict
echom "vim-node-inspect - can't find configuration error"
return 1
endif
else
let configPtr = configObj
endif
if has_key(configPtr,"localRoot") == 1 && has_key(configPtr,"remoteRoot") == 1
let configuration["localRoot"] = s:ReplaceMacros(configPtr["localRoot"])
let configuration["remoteRoot"] = configPtr["remoteRoot"]
" add trailing backslash if not present. it will normalize both inputs
" in case the user add one with and one without
if configuration["localRoot"][-1:-1] != '/'
let configuration["localRoot"] = configuration["localRoot"] . '/'
endif
if configuration["remoteRoot"][-1:-1] != '/'
let configuration["remoteRoot"] = configuration["remoteRoot"] . '/'
endif
endif
if has_key(configPtr,"request") == 1
if configPtr["request"] == 'attach' || configPtr["request"] == 'launch'
let configuration["request"] = configPtr["request"]
else
echom "error reading launch in vim-node-inspect"
return 1
endif
endif
if has_key(configPtr,"program") == 1
if type(configPtr["program"]) == 1
let configuration["program"] = s:ReplaceMacros(configPtr["program"])
else
echom "error reading program in vim-node-inspect"
return 1
endif
endif
if has_key(configPtr,"address") == 1
if type(configPtr["address"]) == 1
let configuration["address"] = configPtr["address"]
else
echom "error reading address in vim-node-inspect"
return 1
endif
endif
if has_key(configPtr,"port") == 1
if type(configPtr["port"]) == 0
let configuration["port"] = configPtr["port"]
else
echom "error reading port in vim-node-inspect"
return 1
endif
endif
if has_key(configPtr,"restart") == 1
if configPtr["restart"] == v:true || configPtr["restart"] == 1
let a:session["restart"] = 1
else
let a:session["restart"] = 0
endif
endif
if has_key(configPtr,"cwd") == 1
if type(configPtr["cwd"]) == 1
let a:session["cwd"] = s:ReplaceMacros(configPtr["cwd"])
else
echom "error reading cwd in vim-node-inspect"
return 1
endif
endif
if has_key(configPtr,"envfile") == 1
if type(configPtr["envfile"]) == 1
let envfile = s:replacemacros(configPtr["envfile"])
if filereadable(expand(envfile))
let a:session["envfile"] = envfile
else
echom "error reading envfile in vim-node-inspect"
return 1
end
else
echom "error reading envfile in vim-node-inspect"
return 1
endif
endif
if has_key(configPtr,"env") == 1
if type(configPtr["env"]) == 4
let a:session["env"] = json_encode(configPtr["env"])
else
echom "error reading envs in vim-node-inspect"
return 1
endif
endif
if has_key(configPtr,"runtimeExecutable") == 1
if type(configPtr["runtimeExecutable"]) == 1
let a:session["runtimeExecutable"] = configPtr["runtimeExecutable"]
else
echom "error reading runtimeExecutable in vim-node-inspect"
return 1
endif
endif
if has_key(configPtr,"runtimeArgs") == 1
if type(configPtr["runtimeArgs"]) == 3
let a:session["runtimeArgs"] = configPtr["runtimeArgs"]
else
echom "error reading runtimeArgs in vim-node-inspect"
return 1
endif
endif
" validate config and setup session
if has_key(configuration, "request") == 1
if configuration["request"] == 'attach'
if has_key(configuration, "port") == 0
echom "vim-node-inspect config error, attach without a port"
return 1
else
let a:session["request"] = configuration["request"]
let a:session["port"] = configuration["port"]
" address defaults to localhost
if has_key(configuration, "address")
let a:session["address"] = configuration["address"]
else
let a:session["address"] = "127.0.0.1"
endif
endif
endif
if configuration["request"] == 'launch'
if has_key(configuration, "restart") == 1
echom "vim-node-inspect config error, restart in invalid in launch mode"
return 1
endif
if has_key(configuration, "program") == 0 && has_key(configuration, "runtimeExecutable") == 0
echom "vim-node-inspect config error, launch without a program or runtimeExecutable"
return 1
else
let a:session["request"] = configuration["request"]
let a:session["script"] = configuration["program"]
endif
endif
endif
if (has_key(configuration, "localRoot") == 1 || has_key(configuration, "remoteRoot") == 1)
if ((has_key(configuration, "localRoot") == 1 && has_key(configuration, "remoteRoot") == 0) || (has_key(configuration, "localRoot") == 0 && has_key(configuration, "remoteRoot") == 1))
echom 'vim-node-inspect directories set error'
return 1
else
let a:session["localRoot"] = configuration["localRoot"]
let a:session["remoteRoot"] = configuration["remoteRoot"]
endif
endif
" read each line of args in order to alter the value
let a:session["args"] = []
if has_key(configPtr,"args") == 1
for singleArg in configPtr["args"]
call add(a:session["args"] ,s:ReplaceMacros(singleArg))
endfor
endif
else
echom 'error reading vim-node-config.json, not a valid json'
return 1
endif
endif
endfunction