Merge pull request #3533 from motato1/master

Deno support for LSP and fixer
This commit is contained in:
Horacio Sanson 2021-01-23 11:04:52 +09:00 committed by GitHub
commit c374736301
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 214 additions and 0 deletions

View file

@ -0,0 +1,25 @@
" Author: Mohammed Chelouti - https://github.com/motato1
" Description: Deno lsp linter for TypeScript files.
call ale#linter#Define('typescript', {
\ 'name': 'deno',
\ 'lsp': 'stdio',
\ 'executable': function('ale#handlers#deno#GetExecutable'),
\ 'command': '%e lsp',
\ 'project_root': function('ale#handlers#deno#GetProjectRoot'),
\ 'initialization_options': function('ale_linters#typescript#deno#GetInitializationOptions'),
\})
function! ale_linters#typescript#deno#GetInitializationOptions(buffer) abort
let l:options = {
\ 'enable': v:true,
\ 'lint': v:true,
\ 'unstable': v:false,
\ }
if ale#Var(a:buffer, 'deno_unstable')
let l:options.unstable = v:true
endif
return l:options
endfunction

View file

@ -32,6 +32,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['python'], \ 'suggested_filetypes': ['python'],
\ 'description': 'Fix PEP8 issues with black.', \ 'description': 'Fix PEP8 issues with black.',
\ }, \ },
\ 'deno': {
\ 'function': 'ale#fixers#deno#Fix',
\ 'suggested_filetypes': ['typescript'],
\ 'description': 'Fix TypeScript using deno fmt.',
\ },
\ 'dfmt': { \ 'dfmt': {
\ 'function': 'ale#fixers#dfmt#Fix', \ 'function': 'ale#fixers#dfmt#Fix',
\ 'suggested_filetypes': ['d'], \ 'suggested_filetypes': ['d'],

View file

@ -0,0 +1,17 @@
function! ale#fixers#deno#Fix(buffer) abort
let l:executable = ale#handlers#deno#GetExecutable(a:buffer)
if !executable(l:executable)
return 0
endif
let l:options = ' fmt -'
if ale#Var(a:buffer, 'deno_unstable')
let l:options = l:options . ' --unstable'
endif
return {
\ 'command': ale#Escape(l:executable) . l:options
\}
endfunction

View file

@ -0,0 +1,52 @@
" Author: Mohammed Chelouti - https://github.com/motato1
" Description: Handler functions for Deno.
call ale#Set('deno_executable', 'deno')
call ale#Set('deno_unstable', 0)
call ale#Set('deno_lsp_project_root', '')
function! ale#handlers#deno#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'deno_executable')
endfunction
" Find project root for Deno's language server.
"
" Deno projects do not require a project or configuration file at the project root.
" This means the root directory has to be guessed,
" unless it is explicitly specified by the user.
"
" The project root is determined by ...
" 1. using a user-specified value from deno_lsp_project_root
" 2. looking for common top-level files/dirs
" 3. using the buffer's directory
function! ale#handlers#deno#GetProjectRoot(buffer) abort
let l:project_root = ale#Var(a:buffer, 'deno_lsp_project_root')
if !empty(l:project_root)
return l:project_root
endif
let l:possible_project_roots = [
\ 'tsconfig.json',
\ '.git',
\ bufname(a:buffer),
\]
for l:possible_root in l:possible_project_roots
let l:project_root = ale#path#FindNearestFile(a:buffer, l:possible_root)
if empty(l:project_root)
let l:project_root = ale#path#FindNearestDirectory(a:buffer, l:possible_root)
endif
if !empty(l:project_root)
" dir:p expands to /full/path/to/dir/ whereas
" file:p expands to /full/path/to/file (no trailing slash)
" Appending '/' ensures that :h:h removes the path's last segment
" regardless of whether it is a directory or not.
return fnamemodify(l:project_root . '/', ':p:h:h')
endif
endfor
return ''
endfunction

View file

@ -498,6 +498,7 @@ Notes:
* Thrift * Thrift
* `thrift` * `thrift`
* TypeScript * TypeScript
* `deno`
* `eslint` * `eslint`
* `fecs` * `fecs`
* `prettier` * `prettier`

View file

@ -2,6 +2,39 @@
ALE TypeScript Integration *ale-typescript-options* ALE TypeScript Integration *ale-typescript-options*
===============================================================================
deno *ale-typescript-deno*
Starting from version 1.6.0, Deno comes with its own language server. Earlier
versions are not supported.
g:ale_deno_executable *g:ale_deno_executable*
*b:ale_deno_executable*
Type: |String|
Default: `'deno'`
g:ale_deno_lsp_project_root *g:ale_deno_lsp_project_root*
*b:ale_deno_lsp_project_root*
Type: |String|
Default: `''`
If this variable is left unset, ALE will try to find the project root by
executing the following steps in the given order:
1. Find an ancestor directory containing a tsconfig.json.
2. Find an ancestory irectory containing a .git folder.
3. Use the directory of the current buffer (if the buffer was opened from
a file).
g:ale_deno_unstable *g:ale_deno_unstable*
*b:ale_deno_unstable*
Type: |Number|
Default: `0`
Enable or disable unstable Deno features and APIs.
=============================================================================== ===============================================================================
eslint *ale-typescript-eslint* eslint *ale-typescript-eslint*

View file

@ -2994,6 +2994,7 @@ documented in additional help files.
thrift..................................|ale-thrift-options| thrift..................................|ale-thrift-options|
thrift................................|ale-thrift-thrift| thrift................................|ale-thrift-thrift|
typescript..............................|ale-typescript-options| typescript..............................|ale-typescript-options|
deno..................................|ale-typescript-deno|
eslint................................|ale-typescript-eslint| eslint................................|ale-typescript-eslint|
prettier..............................|ale-typescript-prettier| prettier..............................|ale-typescript-prettier|
standard..............................|ale-typescript-standard| standard..............................|ale-typescript-standard|

View file

@ -507,6 +507,7 @@ formatting.
* Thrift * Thrift
* [thrift](http://thrift.apache.org/) * [thrift](http://thrift.apache.org/)
* TypeScript * TypeScript
* [deno](https://deno.land/)
* [eslint](http://eslint.org/) * [eslint](http://eslint.org/)
* [fecs](http://fecs.baidu.com/) * [fecs](http://fecs.baidu.com/)
* [prettier](https://github.com/prettier/prettier) * [prettier](https://github.com/prettier/prettier)

View file

@ -0,0 +1,60 @@
Before:
let g:ale_deno_unstable = 0
let g:ale_deno_executable = 'deno'
let g:ale_deno_project_root = ''
runtime autoload/ale/handlers/deno.vim
call ale#assert#SetUpLinterTest('typescript', 'deno')
After:
call ale#assert#TearDownLinterTest()
Execute(Should set deno lsp for TypeScript projects using stable Deno API):
AssertLSPLanguage 'typescript'
AssertLSPConfig {}
AssertLSPProject ale#path#Simplify(g:dir . '/../..')
AssertLSPOptions {
\ 'enable': v:true,
\ 'lint': v:true,
\ 'unstable': v:false
\}
Execute(Should set deno lsp using unstable Deno API if enabled by user):
let g:ale_deno_unstable = 1
AssertLSPLanguage 'typescript'
AssertLSPConfig {}
AssertLSPProject ale#path#Simplify(g:dir . '/../..')
AssertLSPOptions {
\ 'enable': v:true,
\ 'lint': v:true,
\ 'unstable': v:true
\}
Execute(Should find project root containing tsconfig.json):
call ale#test#SetFilename('../typescript/test.ts')
AssertLSPLanguage 'typescript'
AssertLSPConfig {}
AssertLSPProject ale#path#Simplify(g:dir . '/../typescript')
AssertLSPOptions {
\ 'enable': v:true,
\ 'lint': v:true,
\ 'unstable': v:false
\}
Execute(Should use user-specified project root):
let g:ale_deno_lsp_project_root = '/'
call ale#test#SetFilename('../typescript/test.ts')
AssertLSPLanguage 'typescript'
AssertLSPConfig {}
AssertLSPProject '/'
AssertLSPOptions {
\ 'enable': v:true,
\ 'lint': v:true,
\ 'unstable': v:false
\}
Execute(Check Deno LSP command):
AssertLinter 'deno', [
\ ale#Escape('deno') . ' lsp',
\]

View file

@ -0,0 +1,19 @@
Before:
runtime autoload/ale/handlers/deno.vim
After:
unlet! g:ale_deno_executable
call ale#linter#Reset()
Execute(Default executable should be detected correctly):
AssertEqual
\ 'deno',
\ ale#handlers#deno#GetExecutable(bufnr(''))
Execute(User specified executable should override default):
let g:ale_deno_executable = '/path/to/deno-bin'
AssertEqual
\ '/path/to/deno-bin',
\ ale#handlers#deno#GetExecutable(bufnr(''))

0
test/typescript/test.ts Normal file
View file

View file