From d6f1a47647a75b314668170c520f994800928ada Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Wed, 13 Aug 2025 07:30:46 -0400 Subject: [PATCH] tombi: support its LSP (#5022) * tombi: support its LSP * tombi: support linting and formatting --- ale_linters/toml/tombi.vim | 45 ++++++++++++++++ autoload/ale/fix/registry.vim | 10 ++++ autoload/ale/fixers/tombi_format.vim | 16 ++++++ autoload/ale/fixers/tombi_lint.vim | 16 ++++++ doc/ale-supported-languages-and-tools.txt | 1 + doc/ale-toml.txt | 53 +++++++++++++++++++ doc/ale.txt | 1 + supported-tools.md | 1 + .../test_tombi_format_fixer_callback.vader | 33 ++++++++++++ .../test_tombi_lint_fixer_callback.vader | 33 ++++++++++++ test/linter/test_toml_tombi.vader | 23 ++++++++ .../toml/tombi/pyprojecttoml/pyproject.toml | 0 .../toml/tombi/pyprojecttoml/subdir/file.toml | 0 .../toml/tombi/tombitoml/subdir/file.toml | 0 .../toml/tombi/tombitoml/tombi.toml | 0 15 files changed, 232 insertions(+) create mode 100644 ale_linters/toml/tombi.vim create mode 100644 autoload/ale/fixers/tombi_format.vim create mode 100644 autoload/ale/fixers/tombi_lint.vim create mode 100644 test/fixers/test_tombi_format_fixer_callback.vader create mode 100644 test/fixers/test_tombi_lint_fixer_callback.vader create mode 100644 test/linter/test_toml_tombi.vader create mode 100644 test/test-files/toml/tombi/pyprojecttoml/pyproject.toml create mode 100644 test/test-files/toml/tombi/pyprojecttoml/subdir/file.toml create mode 100644 test/test-files/toml/tombi/tombitoml/subdir/file.toml create mode 100644 test/test-files/toml/tombi/tombitoml/tombi.toml diff --git a/ale_linters/toml/tombi.vim b/ale_linters/toml/tombi.vim new file mode 100644 index 00000000..59c8da40 --- /dev/null +++ b/ale_linters/toml/tombi.vim @@ -0,0 +1,45 @@ +" Author: Ben Boeckel +" Description: TOML Formatter / Linter / Language Server + +call ale#Set('toml_tombi_executable', 'tombi') +call ale#Set('toml_tombi_lsp_options', '') + +function! ale_linters#toml#tombi#GetCommand(buffer) abort + let l:options = ale#Var(a:buffer, 'toml_tombi_lsp_options') + + return '%e lsp' + \ . (empty(l:options) ? '' : ' ' . l:options) +endfunction + +function! ale_linters#toml#tombi#GetProjectRoot(buffer) abort + " Try to find nearest tombi.toml + let l:tombiconfig_file = ale#path#FindNearestFile(a:buffer, 'tombi.toml') + + if !empty(l:tombiconfig_file) + return fnamemodify(l:tombiconfig_file . '/', ':p:h:h') + endif + + " Try to find nearest pyproject.toml + let l:pyproject_file = ale#path#FindNearestFile(a:buffer, 'pyproject.toml') + + if !empty(l:pyproject_file) + return fnamemodify(l:pyproject_file . '/', ':p:h:h') + endif + + " Try to find nearest `git` directory + let l:gitdir = ale#path#FindNearestFile(a:buffer, '.git') + + if !empty(l:gitdir) + return fnamemodify(l:gitdir . '/', ':p:h:h') + endif + + return '' +endfunction + +call ale#linter#Define('toml', { +\ 'name': 'tombi', +\ 'lsp': 'stdio', +\ 'executable': {b -> ale#Var(b, 'toml_tombi_executable')}, +\ 'command': function('ale_linters#toml#tombi#GetCommand'), +\ 'project_root': function('ale_linters#toml#tombi#GetProjectRoot'), +\}) diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index ee26079f..03df1a83 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -732,6 +732,16 @@ let s:default_registry = { \ 'suggested_filetypes': ['roc'], \ 'description': 'Annotates all top-level definitions in Roc files.', \ }, +\ 'tombi_format': { +\ 'function': 'ale#fixers#tombi_format#Fix', +\ 'suggested_filetypes': ['toml'], +\ 'description': 'Formats TOML files', +\ }, +\ 'tombi_lint': { +\ 'function': 'ale#fixers#tombi_lint#Fix', +\ 'suggested_filetypes': ['toml'], +\ 'description': 'Lints TOML files', +\ }, \} " Reset the function registry to the default entries. diff --git a/autoload/ale/fixers/tombi_format.vim b/autoload/ale/fixers/tombi_format.vim new file mode 100644 index 00000000..730032fa --- /dev/null +++ b/autoload/ale/fixers/tombi_format.vim @@ -0,0 +1,16 @@ +" Author: Ben Boeckel +" Description: Integration of tombi formatting with ALE. + +call ale#Set('toml_tombi_executable', 'tombi') +call ale#Set('toml_tombi_format_options', '') + +function! ale#fixers#tombi_format#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'toml_tombi_executable') + let l:options = ale#Var(a:buffer, 'toml_tombi_format_options') + + return { + \ 'command': ale#Escape(l:executable) + \ . ' format' + \ . (empty(l:options) ? '' : ' ' . l:options), + \} +endfunction diff --git a/autoload/ale/fixers/tombi_lint.vim b/autoload/ale/fixers/tombi_lint.vim new file mode 100644 index 00000000..06d90ebf --- /dev/null +++ b/autoload/ale/fixers/tombi_lint.vim @@ -0,0 +1,16 @@ +" Author: Ben Boeckel +" Description: Integration of tombi linting with ALE. + +call ale#Set('toml_tombi_executable', 'tombi') +call ale#Set('toml_tombi_lint_options', '') + +function! ale#fixers#tombi_lint#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'toml_tombi_executable') + let l:options = ale#Var(a:buffer, 'toml_tombi_lint_options') + + return { + \ 'command': ale#Escape(l:executable) + \ . ' lint' + \ . (empty(l:options) ? '' : ' ' . l:options), + \} +endfunction diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index 7f03e10a..363d4ff5 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -698,6 +698,7 @@ Notes: * `thriftcheck` * TOML * `dprint` + * `tombi` * TypeScript * `biome` * `cspell` diff --git a/doc/ale-toml.txt b/doc/ale-toml.txt index 222a91f4..86c788a9 100644 --- a/doc/ale-toml.txt +++ b/doc/ale-toml.txt @@ -8,5 +8,58 @@ dprint *ale-toml-dprint* See |ale-dprint-options| and https://dprint.dev/plugins/toml +=============================================================================== +tombi *ale-toml-tombi* + + `'tombi'` is a TOML formatter, linter, and language server. + + *ale-options.toml_tombi_executable* + *g:ale_toml_tombi_executable* + *b:ale_toml_tombi_executable* +toml_tombi_executable +g:ale_toml_tombi_executable + Type: |String| + Default: `'tombi'` + + This variable can be modified to change the executable path for + `tombi`. + + + *ale-options.toml_tombi_lsp_options* + *g:ale_toml_tombi_lsp_options* + *b:ale_toml_tombi_lsp_options* +toml_tombi_lsp_options +g:ale_toml_tombi_lsp_options + Type: |String| + Default: `''` + + This variable can be modified to provide options when using `tombi` as an + LSP server. + + + *ale-options.toml_tombi_format_options* + *g:ale_toml_tombi_format_options* + *b:ale_toml_tombi_format_options* +toml_tombi_format_options +g:ale_toml_tombi_format_options + Type: |String| + Default: `''` + + This variable can be modified to provide options when using `tombi` as a + formatter. + + + *ale-options.toml_tombi_lint_options* + *g:ale_toml_tombi_lint_options* + *b:ale_toml_tombi_lint_options* +toml_tombi_lint_options +g:ale_toml_tombi_lint_options + Type: |String| + Default: `''` + + This variable can be modified to provide options when using `tombi` as a + linter. + + =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index e508f769..24ab0274 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -3928,6 +3928,7 @@ documented in additional help files. thriftcheck...........................|ale-thrift-thriftcheck| toml....................................|ale-toml-options| dprint................................|ale-toml-dprint| + tombi.................................|ale-toml-tombi| typescript..............................|ale-typescript-options| biome.................................|ale-typescript-biome| cspell................................|ale-typescript-cspell| diff --git a/supported-tools.md b/supported-tools.md index 137883be..69960dea 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -707,6 +707,7 @@ formatting. * [thriftcheck](https://github.com/pinterest/thriftcheck) * TOML * [dprint](https://dprint.dev) + * [tombi](https://tombi-toml.github.io/tombi/) * TypeScript * [biome](https://biomejs.dev/) * [cspell](https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell) diff --git a/test/fixers/test_tombi_format_fixer_callback.vader b/test/fixers/test_tombi_format_fixer_callback.vader new file mode 100644 index 00000000..0807d354 --- /dev/null +++ b/test/fixers/test_tombi_format_fixer_callback.vader @@ -0,0 +1,33 @@ +Before: + Save g:ale_toml_tombi_executable + Save g:ale_toml_tombi_format_options + + " Use an invalid global executable, so we don't match it. + let g:ale_toml_tombi_executable = 'xxxinvalid' + let g:ale_toml_tombi_format_options = '' + + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(The tombi format callback should return the correct default values): + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') . ' format', + \ }, + \ ale#fixers#tombi_format#Fix(bufnr('')) + +Execute(The tombi format callback should include custom options): + let g:ale_toml_tombi_format_options = "--offline" + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') + \ . ' format' + \ . ' ' . g:ale_toml_tombi_format_options + \ }, + \ ale#fixers#tombi_format#Fix(bufnr('')) diff --git a/test/fixers/test_tombi_lint_fixer_callback.vader b/test/fixers/test_tombi_lint_fixer_callback.vader new file mode 100644 index 00000000..c3d6763e --- /dev/null +++ b/test/fixers/test_tombi_lint_fixer_callback.vader @@ -0,0 +1,33 @@ +Before: + Save g:ale_toml_tombi_executable + Save g:ale_toml_tombi_lint_options + + " Use an invalid global executable, so we don't match it. + let g:ale_toml_tombi_executable = 'xxxinvalid' + let g:ale_toml_tombi_lint_options = '' + + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(The tombi lint callback should return the correct default values): + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') . ' lint', + \ }, + \ ale#fixers#tombi_lint#Fix(bufnr('')) + +Execute(The tombi lint callback should include custom options): + let g:ale_toml_tombi_lint_options = "--offline" + + AssertEqual + \ { + \ 'command': ale#Escape('xxxinvalid') + \ . ' lint' + \ . ' ' . g:ale_toml_tombi_lint_options + \ }, + \ ale#fixers#tombi_lint#Fix(bufnr('')) diff --git a/test/linter/test_toml_tombi.vader b/test/linter/test_toml_tombi.vader new file mode 100644 index 00000000..70fa1d00 --- /dev/null +++ b/test/linter/test_toml_tombi.vader @@ -0,0 +1,23 @@ +Before: + call ale#assert#SetUpLinterTest('toml', 'tombi') + +After: + call ale#assert#TearDownLinterTest() + +Execute(The default executable path should be correct): + AssertLinter 'tombi', ale#Escape('tombi') . ' lsp' + +Execute(The project root should be detected correctly with a configuration file): + call ale#test#SetFilename('../test-files/toml/tombi/tombitoml/subdir/file.ext') + + AssertLSPProject ale#path#Simplify(g:dir . '/../test-files/toml/tombi/tombitoml') + +Execute(The project root should be detected correctly in Python projects): + call ale#test#SetFilename('../test-files/toml/tombi/pyprojecttoml/subdir/file.ext') + + AssertLSPProject ale#path#Simplify(g:dir . '/../test-files/toml/tombi/pyprojecttoml') + +Execute(The project root should be empty when no project files can be detected): + call ale#test#SetFilename('../test-files/dummy') + + AssertLSPProject '' diff --git a/test/test-files/toml/tombi/pyprojecttoml/pyproject.toml b/test/test-files/toml/tombi/pyprojecttoml/pyproject.toml new file mode 100644 index 00000000..e69de29b diff --git a/test/test-files/toml/tombi/pyprojecttoml/subdir/file.toml b/test/test-files/toml/tombi/pyprojecttoml/subdir/file.toml new file mode 100644 index 00000000..e69de29b diff --git a/test/test-files/toml/tombi/tombitoml/subdir/file.toml b/test/test-files/toml/tombi/tombitoml/subdir/file.toml new file mode 100644 index 00000000..e69de29b diff --git a/test/test-files/toml/tombi/tombitoml/tombi.toml b/test/test-files/toml/tombi/tombitoml/tombi.toml new file mode 100644 index 00000000..e69de29b