Skip to content

Commit e6face6

Browse files
committed
astgrep: support its LSP
Fixes: #4974
1 parent 3d68ec7 commit e6face6

File tree

8 files changed

+109
-0
lines changed

8 files changed

+109
-0
lines changed

autoload/ale/astgrep.vim

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
" Author: Ben Boeckel <[email protected]>
2+
" Description: A CLI tool for code structural search, lint and rewriting
3+
4+
call ale#Set('astgrep_executable', 'ast-grep')
5+
6+
function! ale#astgrep#GetCommand(buffer) abort
7+
return '%e lsp'
8+
endfunction
9+
10+
function! ale#astgrep#GetProjectRoot(buffer) abort
11+
" Try to find nearest sgconfig.yml
12+
let l:sgconfig_file = ale#path#FindNearestFile(a:buffer, 'sgconfig.yml')
13+
14+
if !empty(l:sgconfig_file)
15+
return fnamemodify(l:sgconfig_file . '/', ':p:h:h')
16+
endif
17+
18+
return ''
19+
endfunction
20+
21+
function! ale#astgrep#Define(lang) abort
22+
call ale#linter#Define(a:lang, {
23+
\ 'name': 'astgrep',
24+
\ 'lsp': 'stdio',
25+
\ 'executable': {b -> ale#Var(b, 'astgrep_executable')},
26+
\ 'command': function('ale#astgrep#GetCommand'),
27+
\ 'project_root': function('ale#astgrep#GetProjectRoot'),
28+
\})
29+
endfunction
30+
31+
function! ale#astgrep#ApplyLanguages() abort
32+
for l:lang in g:ale_astgrep_linter_languages
33+
call ale#astgrep#Define(l:lang)
34+
endfor
35+
endfunction
36+
37+
let g:ale_astgrep_linter_languages = get(g:, 'ale_astgrep_linter_languages', [])
38+
call ale#astgrep#ApplyLanguages()

doc/ale-generic.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
===============================================================================
2+
ALE Generic Integration *ale-generic-options*
3+
*ale-integration-generic*
4+
5+
===============================================================================
6+
Integration Information
7+
8+
Some tools are not language specific. This document collects information on
9+
ale's support for these tools.
10+
11+
12+
===============================================================================
13+
astgrep *ale-generic-astgrep*
14+
15+
`'ast-grep'` is a tool which can detect code patterns and offer fixes based
16+
on various languages' abstract syntax trees (ASTs) using tree-sitter.
17+
18+
*ale-options.astgrep_executable*
19+
*g:ale_astgrep_executable*
20+
*b:ale_astgrep_executable*
21+
astgrep_executable
22+
g:ale_astgrep_executable
23+
Type: |String|
24+
Default: `'ast-grep'`
25+
26+
This variable can be modified to change the executable path for
27+
`ast-grep`.
28+
29+
30+
*ale-options.astgrep_linter_languages*
31+
*g:ale_astgrep_linter_languages*
32+
astgrep_linter_languages
33+
g:ale_astgrep_linter_languages
34+
Type: |List|
35+
Default: `'[]'`
36+
37+
This variable can be set to a list of languages for which to enable the
38+
`ast-grep` linter.
39+
40+
41+
===============================================================================
42+
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

doc/ale-supported-languages-and-tools.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ Notes:
211211
* `proselint`
212212
* FusionScript
213213
* `fusion-lint`
214+
* Generic
215+
* `ast-grep`
214216
* Git Commit Messages
215217
* `gitlint`
216218
* Gleam

doc/ale.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,6 +3485,8 @@ documented in additional help files.
34853485
fountain................................|ale-fountain-options|
34863486
fusionscript............................|ale-fuse-options|
34873487
fusion-lint...........................|ale-fuse-fusionlint|
3488+
generic.................................|ale-generic-options|
3489+
astgrep...............................|ale-generic-astgrep|
34883490
git commit..............................|ale-gitcommit-options|
34893491
gitlint...............................|ale-gitcommit-gitlint|
34903492
gleam...................................|ale-gleam-options|

supported-tools.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ formatting.
220220
* [proselint](http://proselint.com/)
221221
* FusionScript
222222
* [fusion-lint](https://github.com/RyanSquared/fusionscript)
223+
* Generic
224+
* [ast-grep](https://ast-grep.github.io/)
223225
* Git Commit Messages
224226
* [gitlint](https://github.com/jorisroovers/gitlint)
225227
* Gleam
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Before:
2+
" Load the dummy linter; tests will register as needed.
3+
call ale#assert#SetUpLinterTest('testft', 'testlinter')
4+
" Replace the dummy linter with a defined linter.
5+
call ale#linter#Reset()
6+
call ale#linter#PreventLoading('python')
7+
call ale#astgrep#Define('python')
8+
9+
After:
10+
call ale#assert#TearDownLinterTest()
11+
12+
Execute(The default executable path should be correct):
13+
AssertLinter 'ast-grep', ale#Escape('ast-grep') . ' lsp'
14+
15+
Execute(The project root should be detected correctly with a configuration file):
16+
call ale#test#SetFilename('../test-files/generic/astgrep/subdir/file.py')
17+
18+
AssertLSPProject ale#path#Simplify(g:dir . '/../test-files/generic/astgrep')
19+
20+
Execute(The project root should be empty when no project files can be detected):
21+
call ale#test#SetFilename('../test-files/dummy')
22+
23+
AssertLSPProject ''

test/test-files/generic/astgrep/sgconfig.yml

Whitespace-only changes.

test/test-files/generic/astgrep/subdir/file.py

Whitespace-only changes.

0 commit comments

Comments
 (0)