Skip to content

Commit de119c7

Browse files
committed
Adapt plugin to 1.1.0 related analysis logic
1 parent 3badd83 commit de119c7

6 files changed

Lines changed: 206 additions & 126 deletions

File tree

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22

33
## master
44

5+
**Improvements:**
6+
7+
- Add support for the new `rescript` npm package. This version will handle both, `bs-platform` and `rescript` projects.
8+
- Tip: Running `:RescriptInfo` will show you if the plugin is running in `legacy` (`bsc.exe` / `bsb.exe`) or `modern` (`rescript.exe`) mode
9+
- Updated vendored rescript-vscode to `1.1.0` (see changes [here](https://github.com/rescript-lang/rescript-vscode/blob/master/CHANGELOG.md#110))
10+
11+
**Breaking Changes:**
12+
13+
We slimmed down our vendored LSP code and changed the binary paths. Within your `CocConfig`, change your server path accordingly:
14+
15+
```diff
16+
"languageserver": {
17+
"rescript": {
18+
"enable": true,
19+
- "module": "~/.config/nvim/plugged/vim-rescript/rescript-vscode/extension/server/out/server.js",
20+
+ "module": "~/.config/nvim/plugged/vim-rescript/server/out/server.js",
21+
"args": ["--node-ipc"],
22+
"filetypes": ["rescript"],
23+
"rootPatterns": ["bsconfig.json"]
24+
}
25+
}
26+
```
27+
28+
We also renamed the following plugin commands:
29+
30+
- `:RescriptBuildWorld` to `:RescriptBuildWithDeps`
31+
- `:RescriptCleanWorld` to `:RescriptBuildWithDeps`
32+
533
## 1.4.0
634

735
**Improvements:**

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ After the installation, open your coc config (`:CocConfig`) and add the followin
6969
"languageserver": {
7070
"rescript": {
7171
"enable": true,
72-
"module": "~/.config/nvim/plugged/vim-rescript/rescript-vscode/extension/server/out/server.js",
72+
"module": "~/.config/nvim/plugged/vim-rescript/server/out/server.js",
7373
"args": ["--node-ipc"],
7474
"filetypes": ["rescript"],
7575
"rootPatterns": ["bsconfig.json"]

autoload/rescript.vim

Lines changed: 131 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,22 @@ function! rescript#UpdateProjectEnv()
2828
" Looks for the nearest node_modules directory
2929
let l:res_bin_dir = finddir('node_modules/rescript/', ".;") . s:rescript_arch
3030

31+
" If 1, we are running with the old bsb / bsc binaries
32+
let g:rescript_legacy_mode = 0
33+
3134
if l:res_bin_dir == s:rescript_arch
3235
" look for bs-platform if rescript node module not found
3336
let l:res_bin_dir = finddir('node_modules/bs-platform/', ".;") . s:rescript_arch
37+
let g:rescript_legacy_mode = 1
38+
else
39+
" Here we are handling a project that is based on the rescript npm package
40+
" This package only uses a rescript.exe, no bsc, nor bsb
41+
let g:rescript_exe = getcwd() . "/" . l:res_bin_dir . "/rescript.exe"
3442
endif
3543

36-
"if !exists("g:rescript_compile_exe")
44+
" These variables are only used in legacy mode (bs-platform based projects)
3745
let g:rescript_compile_exe = getcwd() . "/" . l:res_bin_dir . "/bsc.exe"
38-
"endif
39-
40-
"if !exists("g:rescript_build_exe")
4146
let g:rescript_build_exe = getcwd() . "/" . l:res_bin_dir . "/bsb.exe"
42-
"endif
4347

4448
" Note that this doesn't find bsconfig when the editor was started without a
4549
" specific file within the project
@@ -71,8 +75,8 @@ function! rescript#Init()
7175
let s:got_format_err = 0
7276
let s:got_build_err = 0
7377

74-
if !exists("g:rescript_editor_support_exe")
75-
let g:rescript_editor_support_exe = s:rescript_plugin_dir . "/rescript-vscode/extension/server/" . s:rescript_arch . "/rescript-editor-support.exe"
78+
if !exists("g:rescript_analysis_exe")
79+
let g:rescript_analysis_exe = s:rescript_plugin_dir . "/server/analysis_binaries/" . s:rescript_arch . "-run.exe"
7680
endif
7781

7882
call rescript#UpdateProjectEnv()
@@ -83,7 +87,7 @@ function! s:DeleteLines(start, end) abort
8387
endfunction
8488

8589
function! rescript#GetRescriptVscodeVersion()
86-
let l:out = readfile(s:rescript_plugin_dir . "/rescript-vscode/extension/package.json")
90+
let l:out = readfile(s:rescript_plugin_dir . "/server/package.json")
8791

8892
try
8993
let l:json = json_decode(l:out)
@@ -197,7 +201,10 @@ function! rescript#TypeHint()
197201
write
198202
endif
199203

200-
let l:command = g:rescript_editor_support_exe . " dump " . @%
204+
let c_line = line(".")
205+
let c_col = col(".")
206+
207+
let l:command = g:rescript_analysis_exe . " hover " . @% . " " . (c_line - 1) . " " . (c_col - 1)
201208

202209
let out = system(l:command)
203210

@@ -210,57 +217,37 @@ function! rescript#TypeHint()
210217
try
211218
let l:json = json_decode(out)
212219
catch /.*/
213-
echo "No type info due to build error"
220+
echo "No type info (couldn't decode analysis result)"
214221
return
215222
endtry
216223

217-
let c_line = line(".")
218-
let c_col = col(".")
224+
let l:match = l:json
219225

220-
for item in l:json
221-
let start_line = item.range.start.line + 1
222-
let end_line = item.range.end.line + 1
223-
let start_col = item.range.start.character + 1
224-
let end_col = item.range.end.character
225-
226-
if c_line >= start_line && c_line <= end_line
227-
if c_col >= start_col && c_col <= end_col
228-
let l:match = item
229-
break
230-
endif
231-
endif
232-
endfor
233-
234-
if exists("l:match")
235-
" In case our hover involves a reference, resolve that first
236-
let l:matches = matchlist(get(l:match, "hover", ""), '#\(\d\+\)')
237-
if len(l:matches) > 0
238-
let l:index = l:matches[1]
239-
let l:match = get(l:json, l:index)
240-
endif
241-
242-
if get(l:match, "hover", "") != ""
243-
let md_content = l:match.hover
226+
if get(l:match, "contents", "") != ""
227+
let md_content = l:match.contents
244228

245-
let text = split(md_content, "\n")
246-
let lines = extend(["Pos (l:c): " . c_line . ":" . c_col], text)
229+
let text = split(md_content, "\n")
230+
let lines = extend(["Pos (l:c): " . c_line . ":" . c_col], text)
247231

248-
call s:ShowInPreview("type-preview", "markdown", lines)
232+
call s:ShowInPreview("type-preview", "markdown", lines)
249233

250-
" calculate pos for matchadd
251-
let start_line = item.range.start.line + 1
252-
let end_line = item.range.end.line + 1
253-
let start_col = item.range.start.character + 1
254-
let end_col = item.range.end.character
234+
"TODO: Add back the highlighting at some point
235+
"----
236+
" calculate pos for matchadd
237+
"let start_line = item.range.start.line + 1
238+
"
239+
"let end_line = item.range.end.line + 1
240+
"let start_col = item.range.start.character + 1
241+
"let end_col = item.range.end.character
255242

256-
let startPos = [start_line, start_col, end_col - start_col]
257-
let endPos = [end_line, end_col]
243+
"let startPos = [start_line, start_col, end_col - start_col]
244+
"let endPos = [end_line, end_col]
258245

259-
let pos = [startPos, endPos]
246+
"let pos = [startPos, endPos]
260247

261-
call rescript#highlight#HighlightWord(pos)
262-
return
263-
endif
248+
"call rescript#highlight#HighlightWord(pos)
249+
"------
250+
return
264251
endif
265252
echo "No type info"
266253
endfunction
@@ -274,65 +261,53 @@ function! rescript#JumpToDefinition()
274261
write
275262
endif
276263

277-
let l:command = g:rescript_editor_support_exe . " dump " . @%
264+
let c_line = line(".")
265+
let c_col = col(".")
266+
267+
let l:command = g:rescript_analysis_exe . " definition " . @% . " " . (c_line - 1) . " " . (c_col - 1)
278268

279269
let out = system(l:command)
280270

281271
if v:shell_error != 0
282-
echohl Error | echomsg "Type Info failed with exit code '" . v:shell_error . "'" | echohl None
272+
echohl Error | echomsg "Analysis binary failed with exit code '" . v:shell_error . "'" | echohl None
283273
return
284274
endif
285275

286276
let l:json = []
287277
try
288278
let l:json = json_decode(out)
289279
catch /.*/
290-
echo "No type info due to build error"
280+
echo "No definition available (couldn't decode analysis result)"
291281
return
292282
endtry
293283

294284
let c_line = line(".")
295285
let c_col = col(".")
296286

297-
for item in l:json
298-
let start_line = item.range.start.line + 1
299-
let end_line = item.range.end.line + 1
300-
let start_col = item.range.start.character + 1
301-
let end_col = item.range.end.character
302-
303-
if c_line >= start_line && c_line <= end_line
304-
if c_col >= start_col && c_col <= end_col
305-
let l:match = item
306-
break
307-
endif
308-
endif
309-
endfor
310-
311-
if exists("l:match")
312-
if has_key(l:match, "definition")
313-
let l:def = l:match.definition
314-
315-
if has_key(l:def, "uri")
316-
execute ":e " . l:def.uri
317-
endif
287+
" Prevents errors when v:null was returned
288+
if type(l:json) == v:t_dict
289+
let l:def = l:json
318290

319-
let start_line = l:def.range.start.line + 1
320-
let start_col = l:def.range.start.character + 1
321-
let end_line = l:def.range.end.line + 1
322-
let end_col = l:def.range.end.character
323-
call cursor(start_line, start_col)
291+
if has_key(l:def, "uri")
292+
execute ":e " . l:def.uri
293+
endif
324294

325-
let startPos = [start_line, start_col, end_col - start_col]
326-
let endPos = [end_line, end_col]
327-
let pos = [startPos, endPos]
295+
let start_line = l:def.range.start.line + 1
296+
let start_col = l:def.range.start.character + 1
297+
let end_line = l:def.range.end.line + 1
298+
let end_col = l:def.range.end.character
299+
call cursor(start_line, start_col)
328300

329-
" If pos doesn't point to start: 0,0 and end: 0,0
330-
if pos != [[1, 1, -1], [1, 0]]
331-
call rescript#highlight#HighlightWord(pos)
332-
endif
301+
let startPos = [start_line, start_col, end_col - start_col]
302+
let endPos = [end_line, end_col]
303+
let pos = [startPos, endPos]
333304

334-
return
305+
" If pos doesn't point to start: 0,0 and end: 0,0
306+
if pos != [[1, 1, -1], [1, 0]]
307+
call rescript#highlight#HighlightWord(pos)
335308
endif
309+
310+
return
336311
endif
337312
echo "No definition found"
338313
endfunction
@@ -367,7 +342,7 @@ function! rescript#Complete(findstart, base)
367342
let l:tmpname = tempname() . "." . l:ext
368343
call writefile(getline(1, '$'), l:tmpname)
369344

370-
let l:command = g:rescript_editor_support_exe . " complete " . @% . ":" . ( c_line - 1) . ":" . (c_col - 1) . " " . l:tmpname
345+
let l:command = g:rescript_analysis_exe . " complete " . @% . " " . ( c_line - 1) . " " . (c_col - 1) . " " . l:tmpname
371346

372347
let out = system(l:command)
373348

@@ -399,12 +374,60 @@ function! rescript#Complete(findstart, base)
399374
return l:ret
400375
endfunction
401376

402-
function! rescript#BuildProject(...)
377+
" with_deps: bool ... if 1, clean deps as well
378+
function! rescript#Clean(...)
379+
let l:with_deps = get(a:, 0, 0)
380+
381+
if g:rescript_legacy_mode ==? 1
382+
if l:with_deps ==? 1
383+
" bsb -clean-world
384+
let l:cmd = g:rescript_build_exe . " -clean-world"
385+
else
386+
" bsb -clean
387+
let l:cmd = g:rescript_build_exe . " -clean"
388+
endif
389+
else
390+
if l:with_deps ==? 1
391+
" rescript clean -with-deps
392+
let l:cmd = g:rescript_exe . " clean -with-deps"
393+
else
394+
" rescript clean
395+
let l:cmd = g:rescript_exe . " clean"
396+
endif
397+
endif
398+
399+
exe "lcd " . g:rescript_project_root
400+
let out = system(l:cmd)
401+
exe "lcd -"
402+
403+
if v:shell_error ==? 0
404+
echo "Cleaning successful"
405+
else
406+
echo out
407+
endif
408+
409+
return v:shell_error
410+
endfunction
411+
412+
" with_deps: bool ... if 1, clean deps as well
413+
function! rescript#Build(...)
414+
let with_deps = get(a:, 0, 0)
415+
403416
call rescript#UpdateProjectEnv()
404417

405-
let l:cmd = g:rescript_build_exe
406-
if a:0 ==? 1
407-
let l:cmd = g:rescript_build_exe . " " . a:1
418+
" If in legacy mode, run bsb
419+
if g:rescript_legacy_mode ==? 1
420+
let l:cmd = g:rescript_build_exe
421+
if l:with_deps ==? 1
422+
" bsb -make-world
423+
let l:cmd = g:rescript_build_exe . " -make-world"
424+
endif
425+
else
426+
" Otherwise we are in modern mode and use rescript.exe
427+
let l:cmd = g:rescript_exe
428+
if l:with_deps ==? 1
429+
let l:cmd = g:rescript_exe . " -with-deps"
430+
endif
408431
endif
409432

410433
exe "lcd " . g:rescript_project_root
@@ -508,10 +531,23 @@ function! rescript#Info()
508531
let l:version = "ReScript version: " . rescript#DetectVersion()
509532

510533
echo l:version
534+
535+
if g:rescript_legacy_mode ==? 1
536+
echo "Editor mode: legacy (using bsc / bsb instead of rescript)"
537+
else
538+
echo "Editor mode: modern (using rescript.exe instead of bsc / bsb)"
539+
endif
540+
511541
echo "Detected Config File: " . g:rescript_project_config
512542
echo "Detected Project Root: " . g:rescript_project_root
513-
echo "Detected rescript_editor_support_exe: " . g:rescript_editor_support_exe
514-
echo "Detected rescript_compile_exe: " . g:rescript_compile_exe
515-
echo "Detected rescript_build_exe: " . g:rescript_build_exe
543+
echo "Detected rescript_analysis_exe: " . g:rescript_analysis_exe
544+
545+
if g:rescript_legacy_mode ==? 1
546+
echo "Detected rescript_compile_exe: " . g:rescript_compile_exe
547+
echo "Detected rescript_build_exe: " . g:rescript_build_exe
548+
else
549+
echo "Detected rescript_exe: " . g:rescript_exe
550+
endif
551+
516552
echo "Bundled rescript-vscode extension: " . rescript#GetRescriptVscodeVersion()
517553
endfunction

autoload/rescript/parsing.vim

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
" Links to the reference implementation:
2-
" - https://github.com/rescript-lang/rescript-vscode/blob/master/server/src/utils.ts
3-
" - https://github.com/rescript-lang/rescript-vscode/blob/master/CONTRIBUTING.md
4-
"
1+
" Link to the reference implementation:
2+
" https://github.com/rescript-lang/rescript-vscode/blob/0dbf2eb9cdb0bd6d95be1aee88b73830feecb5cc/server/src/utils.ts#L129-L329
53

64

75
" Parses all syntax errors from the output of the ReScript compiler /

0 commit comments

Comments
 (0)