From 8c71c6c5edfa447aaa867858e2e913340ea964b7 Mon Sep 17 00:00:00 2001 From: Pham Huy Hoang Date: Mon, 2 Jan 2023 16:28:40 +0900 Subject: [PATCH] fix(html): fix wrong indents for script/style tags --- queries/html_tags/indents.scm | 27 ++++++++++++++++++++++-- tests/indent/html/issue-3986.html | 4 ++++ tests/indent/html/script_style.html | 12 +++++++++++ tests/indent/html/self_closing_tag.html | 14 +++++++++++++ tests/indent/html/start_tag.html | 8 +++++++ tests/indent/html_spec.lua | 28 +++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/indent/html/issue-3986.html create mode 100644 tests/indent/html/script_style.html create mode 100644 tests/indent/html/self_closing_tag.html create mode 100644 tests/indent/html/start_tag.html create mode 100644 tests/indent/html_spec.lua diff --git a/queries/html_tags/indents.scm b/queries/html_tags/indents.scm index 7b9d6afe1..a9e0794f5 100644 --- a/queries/html_tags/indents.scm +++ b/queries/html_tags/indents.scm @@ -1,9 +1,32 @@ [ - (element) + ( + (element + (start_tag + (tag_name) @_not_special) + ) + (#not-any-of? @_not_special "meta" "link") + ) + (element (self_closing_tag)) ] @indent +; These tags are usually written one-lined and doesnt use self-closing tags so special-cased them +; but add indent to the tag to make sure attributes inside them are still indented if written multi-lined +( + (start_tag + (tag_name) @_special) + (#any-of? @_special "meta" "link") +) @indent + + +; These are the nodes that will be captured when we do `normal o` +; But last element has already been ended, so capturing this +; to mark end of last element +(element (end_tag [">"] @indent_end)) +(element (self_closing_tag "/>" @indent_end)) + +; Script/style elements aren't indented, so only branch the end tag of other elements +(element (end_tag) @branch) [ - (end_tag) ">" "/>" ] @branch diff --git a/tests/indent/html/issue-3986.html b/tests/indent/html/issue-3986.html new file mode 100644 index 000000000..2e6d9f0d6 --- /dev/null +++ b/tests/indent/html/issue-3986.html @@ -0,0 +1,4 @@ +
+
+
+
diff --git a/tests/indent/html/script_style.html b/tests/indent/html/script_style.html new file mode 100644 index 000000000..b00f3f40f --- /dev/null +++ b/tests/indent/html/script_style.html @@ -0,0 +1,12 @@ + + + + + + diff --git a/tests/indent/html/self_closing_tag.html b/tests/indent/html/self_closing_tag.html new file mode 100644 index 000000000..79376b83d --- /dev/null +++ b/tests/indent/html/self_closing_tag.html @@ -0,0 +1,14 @@ + + + + + + + + + +
+