Add linting support for all formats supported by djlint (#4920)
Some checks are pending
CI / build_image (push) Waiting to run
CI / test_ale (--linters-only) (push) Blocked by required conditions
CI / test_ale (--neovim-07-only) (push) Blocked by required conditions
CI / test_ale (--neovim-08-only) (push) Blocked by required conditions
CI / test_ale (--vim-80-only) (push) Blocked by required conditions
CI / test_ale (--vim-90-only) (push) Blocked by required conditions

* Refactor djlint linter code

This patch moves the code to the `autoload` directory, so it's available
when it's needed by a specific linter. This avoids redundant code when
another format supported by djlint is added.

* Add linting support for all formats supported by djlint

So far, the `djlint` linter in ALE only supported `html`, which is only
one of several file types supported by `djlint`.

This patch adds support for the following file types:

* gohtmltmpl
* handlebars
* htmlangular
* htmldjango
* jinja
* nunjucks

* Add djlint fixer for various HTML template formats

* Supported formats:
    - html
    - htmlangular
    - htmldjango
    - jinja
    - handlebars
    - nunjucks
    - gohtmltmpl

* Add doc entries

* Add vader tests

---------

Co-authored-by: Adrian Vollmer <computerfluesterer@protonmail.com>
This commit is contained in:
Adrian Vollmer
2025-03-17 23:56:32 +01:00
committed by GitHub
parent fe6a91fb92
commit 47f1f49655
23 changed files with 448 additions and 38 deletions

View File

@@ -0,0 +1,12 @@
" Author: Adrian Vollmer <computerfluesterer@protonmail.com>
" Description: djlint for Django HTML template files
call ale#Set('html_djlint_executable', 'djlint')
call ale#Set('html_djlint_options', '')
call ale#linter#Define('gohtmltmpl', {
\ 'name': 'djlint',
\ 'executable': function('ale#handlers#djlint#GetExecutable'),
\ 'command': function('ale#handlers#djlint#GetCommand'),
\ 'callback': 'ale#handlers#djlint#Handle',
\})

View File

@@ -0,0 +1,12 @@
" Author: Adrian Vollmer <computerfluesterer@protonmail.com>
" Description: djlint for Django HTML template files
call ale#Set('html_djlint_executable', 'djlint')
call ale#Set('html_djlint_options', '')
call ale#linter#Define('handlebars', {
\ 'name': 'djlint',
\ 'executable': function('ale#handlers#djlint#GetExecutable'),
\ 'command': function('ale#handlers#djlint#GetCommand'),
\ 'callback': 'ale#handlers#djlint#Handle',
\})

View File

@@ -4,45 +4,11 @@
call ale#Set('html_djlint_executable', 'djlint')
call ale#Set('html_djlint_options', '')
function! ale_linters#html#djlint#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'html_djlint_executable')
endfunction
function! ale_linters#html#djlint#GetCommand(buffer) abort
let l:executable = ale_linters#html#djlint#GetExecutable(a:buffer)
let l:options = ale#Var(a:buffer, 'html_djlint_options')
return ale#Escape(l:executable)
\ . (!empty(l:options) ? ' ' . l:options : '') . ' %s'
endfunction
function! ale_linters#html#djlint#Handle(buffer, lines) abort
let l:output = []
let l:pattern = '\v^([A-Z]\d+) (\d+):(\d+) (.*)$'
let l:i = 0
for l:match in ale#util#GetMatches(a:lines, l:pattern)
let l:i += 1
let l:item = {
\ 'lnum': l:match[2] + 0,
\ 'col': l:match[3] + 0,
\ 'vcol': 1,
\ 'text': l:match[4],
\ 'code': l:match[1],
\ 'type': 'W',
\}
call add(l:output, l:item)
endfor
return l:output
endfunction
call ale#linter#Define('html', {
\ 'name': 'djlint',
\ 'executable': function('ale_linters#html#djlint#GetExecutable'),
\ 'command': function('ale_linters#html#djlint#GetCommand'),
\ 'callback': 'ale_linters#html#djlint#Handle',
\ 'executable': function('ale#handlers#djlint#GetExecutable'),
\ 'command': function('ale#handlers#djlint#GetCommand'),
\ 'callback': 'ale#handlers#djlint#Handle',
\})
" vim:ts=4:sw=4:et:

View File

@@ -0,0 +1,12 @@
" Author: Adrian Vollmer <computerfluesterer@protonmail.com>
" Description: djlint for Django HTML template files
call ale#Set('html_djlint_executable', 'djlint')
call ale#Set('html_djlint_options', '')
call ale#linter#Define('htmlangular', {
\ 'name': 'djlint',
\ 'executable': function('ale#handlers#djlint#GetExecutable'),
\ 'command': function('ale#handlers#djlint#GetCommand'),
\ 'callback': 'ale#handlers#djlint#Handle',
\})

View File

@@ -0,0 +1,12 @@
" Author: Adrian Vollmer <computerfluesterer@protonmail.com>
" Description: djlint for Django HTML template files
call ale#Set('html_djlint_executable', 'djlint')
call ale#Set('html_djlint_options', '')
call ale#linter#Define('htmldjango', {
\ 'name': 'djlint',
\ 'executable': function('ale#handlers#djlint#GetExecutable'),
\ 'command': function('ale#handlers#djlint#GetCommand'),
\ 'callback': 'ale#handlers#djlint#Handle',
\})

View File

@@ -0,0 +1,12 @@
" Author: Adrian Vollmer <computerfluesterer@protonmail.com>
" Description: djlint for Django HTML template files
call ale#Set('html_djlint_executable', 'djlint')
call ale#Set('html_djlint_options', '')
call ale#linter#Define('jinja', {
\ 'name': 'djlint',
\ 'executable': function('ale#handlers#djlint#GetExecutable'),
\ 'command': function('ale#handlers#djlint#GetCommand'),
\ 'callback': 'ale#handlers#djlint#Handle',
\})

View File

@@ -0,0 +1,12 @@
" Author: Adrian Vollmer <computerfluesterer@protonmail.com>
" Description: djlint for Django HTML template files
call ale#Set('html_djlint_executable', 'djlint')
call ale#Set('html_djlint_options', '')
call ale#linter#Define('nunjucks', {
\ 'name': 'djlint',
\ 'executable': function('ale#handlers#djlint#GetExecutable'),
\ 'command': function('ale#handlers#djlint#GetCommand'),
\ 'callback': 'ale#handlers#djlint#Handle',
\})