BuckleScript friendly vim
+ official ocaml-lsp setup for Reason/OCaml
Prerequisite: Install esy 0.6.0
.
- Check
esy version
(it has to be>0.6.0
) - Create
esy.json
with the following contents next topackage.json
:
{
"dependencies": { "ocaml": "4.6.x" },
"devDependencies": {
"@opam/ocaml-lsp-server": "ocaml/ocaml-lsp:ocaml-lsp-server.opam#007a1af",
"@opam/reason": "*"
},
"resolutions": {
"@opam/reason": "facebook/reason#b555665"
}
}
- Run
esy
in that directory - Configure your vim LSP client accordingly. ALE example:
let g:ale_fixers = {
\ 'ocaml': ['ocamlformat'
\}
let g:ale_linters = {
\ 'reason': ['ocaml-lsp'],
\ 'ocaml': ['ocaml-lsp'],
\}
" OCaml/Reason
au BufRead,BufNewFile *.re set filetype=reason
au BufRead,BufNewFile *.rei set filetype=reason
function! s:fix_refmt(buffer) abort
let ext = expand('#' . a:buffer . ':e')
if ext ==# 'rei'
return {
\ 'command': 'esy refmt --interface true'
\}
else
return {
\ 'command': 'esy refmt'
\}
endif
endfunction
let g:ale_fixers.reason = [function('s:fix_refmt')]
function! s:executable_callback(buffer) abort
return 'true'
endfunction
function! s:get_command(buffer) abort
return 'esy exec-command --include-current-env ocamllsp'
endfunction
function! s:get_language(buffer) abort
return getbufvar(a:buffer, '&filetype')
endfunction
function! s:get_project_root(buffer) abort
let l:merlin_file = ale#path#FindNearestFile(a:buffer, 'esy.json')
if empty(l:merlin_file)
let l:merlin_file = ale#path#FindNearestFile(a:buffer, 'package.json')
endif
return !empty(l:merlin_file) ? fnamemodify(l:merlin_file, ':h') : ''
endfunction
call ale#linter#Define('ocaml', {
\ 'name': 'ocaml-lsp',
\ 'lsp': 'stdio',
\ 'executable': function('s:executable_callback'),
\ 'command': function('s:get_command'),
\ 'language': function('s:get_language'),
\ 'project_root': function('s:get_project_root')
\})
call ale#linter#Define('reason', {
\ 'name': 'ocaml-lsp',
\ 'lsp': 'stdio',
\ 'executable': function('s:executable_callback'),
\ 'command': function('s:get_command'),
\ 'language': function('s:get_language'),
\ 'project_root': function('s:get_project_root')
\})
ocaml-lsp
is the official LSP server for OCaml and Reason. It's well maintained and based on the state of the art OCaml/Reason editor intelligence engine - merlin
.