Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the [djlinter](https://www.djlint.com/) #4909

Merged
merged 6 commits into from
Mar 8, 2025

Conversation

vds2212
Copy link
Contributor

@vds2212 vds2212 commented Feb 16, 2025

This code aim to propose a linter for the djlinter from: djlinter

Copy link
Contributor

@hsanson hsanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR in incomplete. Please ensure to:

  • Add linter tests (example)
  • Add handler tests (example)
  • Add entry to supported-tools.md (example)
  • Add entry to doc/ale.txt (example)
  • Add entry to ale-supported-languages-and-tools.txt (example)
  • Add entry to ale-html.txt (example)
  • Ensure all above entries are added in alphabetical order.

@vds2212
Copy link
Contributor Author

vds2212 commented Feb 21, 2025

I have complete the tasks indicated. Let me know if you want me to do something else.

@vds2212 vds2212 requested a review from hsanson February 24, 2025 05:18
@vds2212
Copy link
Contributor Author

vds2212 commented Feb 24, 2025

I found an obvious mistake :-|
Is there a way I can run the test myself before requesting GitHub to re-run them?

@hsanson
Copy link
Contributor

hsanson commented Feb 24, 2025

If you have docker installed you can run tests with ./run-tests. For windows tests I have no idea and rely on Github to run those tests.

@vds2212
Copy link
Contributor Author

vds2212 commented Feb 24, 2025

I'm running on Windows :-| I believe I have fixed the problem (although I'm not yet able to run the tests).
If it is not too much work for you maybe it would be worth to trigger the test on GitHub (can I do it myself?).

@vds2212
Copy link
Contributor Author

vds2212 commented Feb 28, 2025

Should I do something to trigger the 5 pending checks?

@hsanson
Copy link
Contributor

hsanson commented Mar 7, 2025

I recommend you add ale_custom_linting_rules linter to the list of enabled linters for vim files. This would show you any linter errors.

@AdrianVollmer
Copy link

AdrianVollmer commented Mar 7, 2025

Hi, thanks for implementing this linter. I'd also like to see this in ale.

This patch should fix the linting issues (save to file and apply with git apply <filename>):

From 3e202bbc002b7c377743bb8a88a481b9dd6b6366 Mon Sep 17 00:00:00 2001
From: Adrian Vollmer
Date: Fri, 7 Mar 2025 13:25:47 +0100
Subject: [PATCH] Fix linting

---
 ale_linters/html/djlint.vim | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/ale_linters/html/djlint.vim b/ale_linters/html/djlint.vim
index 9e12fa02..a185dfea 100644
--- a/ale_linters/html/djlint.vim
+++ b/ale_linters/html/djlint.vim
@@ -13,34 +13,35 @@ function! ale_linters#html#djlint#GetCommand(buffer) abort
 
     let l:options = ale#Var(a:buffer, 'html_djlint_options')
 
-    return ale#Escape(l:executable)
-                \ . (!empty(l:options) ? ' ' . l:options : '') . ' %s'
+    return ale#Escape(l:executable) . (!empty(l:options) ? ' ' . l:options : '') . ' %s'
 endfunction
 
-function! ale_linters#html#djlint#Handle(buffer, lines)
+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',
-                    \}
+        \   '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',
-            \})
+\   'name': 'djlint',
+\   'executable': function('ale_linters#html#djlint#GetExecutable'),
+\   'command': function('ale_linters#html#djlint#GetCommand'),
+\   'callback': 'ale_linters#html#djlint#Handle',
+\})
 
 " vim:ts=4:sw=4:et:
-- 
2.47.2

@vds2212
Copy link
Contributor Author

vds2212 commented Mar 7, 2025

I recommend you add ale_custom_linting_rules linter to the list of enabled linters for vim files. This would show you any linter errors.

Unfortunately I'm on Windows and the custom_linting_rules seems to be a bash script :-| based on sed and grep

@vds2212
Copy link
Contributor Author

vds2212 commented Mar 7, 2025

Hi, thanks for implementing this linter. I'd also like to see this in ale.

This patch should fix the linting issues (save to file and apply with git apply <filename>):

From 3e202bbc002b7c377743bb8a88a481b9dd6b6366 Mon Sep 17 00:00:00 2001
From: Adrian Vollmer <[email protected]>
Date: Fri, 7 Mar 2025 13:25:47 +0100
Subject: [PATCH] Fix linting

---
 ale_linters/html/djlint.vim | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/ale_linters/html/djlint.vim b/ale_linters/html/djlint.vim
index 9e12fa02..a185dfea 100644
--- a/ale_linters/html/djlint.vim
+++ b/ale_linters/html/djlint.vim
@@ -13,34 +13,35 @@ function! ale_linters#html#djlint#GetCommand(buffer) abort
 
     let l:options = ale#Var(a:buffer, 'html_djlint_options')
 
-    return ale#Escape(l:executable)
-                \ . (!empty(l:options) ? ' ' . l:options : '') . ' %s'
+    return ale#Escape(l:executable) . (!empty(l:options) ? ' ' . l:options : '') . ' %s'
 endfunction
 
-function! ale_linters#html#djlint#Handle(buffer, lines)
+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',
-                    \}
+        \   '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',
-            \})
+\   'name': 'djlint',
+\   'executable': function('ale_linters#html#djlint#GetExecutable'),
+\   'command': function('ale_linters#html#djlint#GetCommand'),
+\   'callback': 'ale_linters#html#djlint#Handle',
+\})
 
 " vim:ts=4:sw=4:et:
-- 
2.47.2

Thanks for your help!
Unfortunately I saw your proposal too late.
I'm new to this pull request process. I'm sure I'll still need your help to make it through :-)

Copy link
Contributor

@hsanson hsanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the work and patience.

@hsanson hsanson merged commit 3611c32 into dense-analysis:master Mar 8, 2025
7 checks passed
@vds2212
Copy link
Contributor Author

vds2212 commented Mar 8, 2025

Thanks for your patience! And thank you for the very helpfu ALE project :-)

@AdrianVollmer
Copy link

AdrianVollmer commented Mar 9, 2025

Thanks for the effort! However, I have a question. Does this work for you as is? Because my vim instance recognizes Django HTML templates as htmldjango, not html, so I'd have to register this linter as let g:ale_linters = {'htmldjango': ['djlint']}

I'm not entirely sure how the internals of ALE work and if we can simply link the linter to the htmldjango file type as well. I suppose it also has value when linting pure HTML files without any Django template tags in it.

Edit: I suppose we might as well support *.jinja files and perhaps more, as djlint can lint those when supplying --profile jinja, for example. @hsanson how do you suggest to structure the code when very similar functions can use almost the same command to lint (or fix) a bunch of file types? Looks like the way ALE is doing things requires a lot of code to be copy-pasted.

Edit2: djlint supports these file types for linting and reformatting: django, jinja, nunjucks, handlebars, golang, angular, html [default: html]. (I'm using it for Django templates FWIW.) I'll have to find out how vim calls all these file types. Writing an individual linter for all of these just for a different value of the --profile argument seems a bit tedious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants