From 3e3186f6f770120b339a5c34525ac84558066d22 Mon Sep 17 00:00:00 2001 From: "Charles-P. Clermont" Date: Fri, 26 Feb 2021 14:05:06 -0500 Subject: [PATCH 1/2] Add bracket closing rules Fixes #12 --- language-configuration.json | 39 +++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 language-configuration.json diff --git a/language-configuration.json b/language-configuration.json new file mode 100644 index 0000000..ea31976 --- /dev/null +++ b/language-configuration.json @@ -0,0 +1,39 @@ +{ + "comments": { + "lineComment": ["{% # ", "{%}"], + "blockComment": ["{% comment %}\n", "\n{% endcomment %}"] + }, + "brackets": [ + ["{", "}"], + ["[", "]"], + ["<", ">"], + ["-", "-"], + ["%", "%"] + ], + "autoClosingPairs": [ + { "open": "{", "close": "}" }, + { "open": "{%", "close": "%}" }, + { "open": "{% ", "close": " %}" }, + { "open": "{%-", "close": "-%}" }, + { "open": "{%- ", "close": " -%}" }, + { "open": "{{", "close": "}}" }, + { "open": "{{ ", "close": " }}" }, + { "open": "{{-", "close": "-}}" }, + { "open": "{{- ", "close": " -}}" }, + { "open": "<", "close": ">" }, + { "open": "[", "close": "]" }, + { "open": "'", "close": "'", "notIn": ["string", "comment"] }, + { "open": "\"", "close": "\"", "notIn": ["string"] } + ], + "autoCloseBefore": ":.,=}])>` \n\t", + "surroundingPairs": [ + ["-", "-"], + ["{", "}"], + ["%", "%"], + ["[", "]"], + ["(", ")"], + ["'", "'"], + ["\"", "\""], + ["`", "`"] + ] +} diff --git a/package.json b/package.json index 1c3b692..87c0913 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,8 @@ ], "extensions": [ ".liquid" - ] + ], + "configuration": "./language-configuration.json" } ] }, From da50d0a73d5b6ae75fab67b4eb4d55874de3c0c2 Mon Sep 17 00:00:00 2001 From: "Charles-P. Clermont" Date: Mon, 15 Mar 2021 09:28:51 -0400 Subject: [PATCH 2/2] Add indentation rules and fix closing brackets --- extension.js | 3 +- language-configuration.json | 18 +++---- make-identation-rules-regex.js | 85 ++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 make-identation-rules-regex.js diff --git a/extension.js b/extension.js index ffe59c3..80403d1 100644 --- a/extension.js +++ b/extension.js @@ -10,6 +10,7 @@ const { TransportKind, } = require('vscode-languageclient'); + let client; async function getThemeCheckExecutable() { @@ -77,13 +78,13 @@ async function restartServer() { if (client) await stopServer(); await startServer(); } +/** */ function onConfigChange(event) { if (event.affectsConfiguration('themeCheck.languageServerPath')) { restartServer(); } } - async function activate(context) { context.subscriptions.push( vscode.commands.registerCommand( diff --git a/language-configuration.json b/language-configuration.json index ea31976..6d49e59 100644 --- a/language-configuration.json +++ b/language-configuration.json @@ -1,39 +1,35 @@ { "comments": { - "lineComment": ["{% # ", "{%}"], "blockComment": ["{% comment %}\n", "\n{% endcomment %}"] }, "brackets": [ ["{", "}"], ["[", "]"], - ["<", ">"], - ["-", "-"], - ["%", "%"] + ["<", ">"] ], "autoClosingPairs": [ { "open": "{", "close": "}" }, { "open": "{%", "close": "%}" }, - { "open": "{% ", "close": " %}" }, { "open": "{%-", "close": "-%}" }, - { "open": "{%- ", "close": " -%}" }, { "open": "{{", "close": "}}" }, - { "open": "{{ ", "close": " }}" }, { "open": "{{-", "close": "-}}" }, - { "open": "{{- ", "close": " -}}" }, { "open": "<", "close": ">" }, { "open": "[", "close": "]" }, { "open": "'", "close": "'", "notIn": ["string", "comment"] }, { "open": "\"", "close": "\"", "notIn": ["string"] } ], - "autoCloseBefore": ":.,=}])>` \n\t", + "autoCloseBefore": "%-:.,=}])>` \n\t", "surroundingPairs": [ ["-", "-"], ["{", "}"], - ["%", "%"], ["[", "]"], ["(", ")"], ["'", "'"], ["\"", "\""], ["`", "`"] - ] + ], + "indentationRules": { + "increaseIndentPattern": "(<(?!\\/|area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)[^>]+>|{%-?\\s+(?:if|form|comment|case|when|for|unless).*?-?%}|{%(?:(?!%}).)*|{{(?:(?!}}).)*)$", + "decreaseIndentPattern": "^\\s*(<\\/[^>]+>|{%-?\\s+(?:endif|endform|endcomment|endcase|endwhen|endfor|endunless).*?-?%}|%}|}}|>)" + } } diff --git a/make-identation-rules-regex.js b/make-identation-rules-regex.js new file mode 100644 index 0000000..82426e3 --- /dev/null +++ b/make-identation-rules-regex.js @@ -0,0 +1,85 @@ +// These HTML elements do not require to be closed (either via or ) +const voidElements = [ + 'area', + 'base', + 'br', + 'col', + 'embed', + 'hr', + 'img', + 'input', + 'keygen', + 'link', + 'menuitem', + 'meta', + 'param', + 'source', + 'track', + 'wbr', +]; + +const openingLiquidTags = [ + 'if', + 'form', + 'comment', + 'case', + 'when', + 'for', + 'unless', +]; + +const closingLiquidTags = openingLiquidTags.map( + (name) => `end${name}`, +); + +// https://regex101.com/r/pl3GPU/1 +function increaseIndentPattern() { + const patterns = [ + // Opening HTML tags that are not self closing. Here we use a negative + // lookahead (?!) to make sure that the next character after < is not / + // or one of the void elements. + String.raw`<(?!\/|${voidElements.join('|')})[^>]+>`, + + // Opening liquid tags that have a corresponding end$name tag. + String.raw`{%-?\s+(?:${openingLiquidTags.join('|')}).*?-?%}`, // opening liquid tags + + // Tag start not closed + String.raw`{%(?:(?!%}).)*`, + + // Variable start not closed + String.raw`{{(?:(?!}}).)*`, + ]; + + // The line must end by one of those patterns + return String.raw`(${patterns.join('|')})$`; +} + +function decreaseIndentPattern() { + const patterns = [ + // Closing HTML tags + String.raw`<\/[^>]+>`, + + // Closing liquid tags + String.raw`{%-?\s+(?:${closingLiquidTags.join('|')}).*?-?%}`, // opening liquid tags + + // Multiline tag closed + String.raw`%}`, + + // Multiline variable closed + String.raw`}}`, + + // Multiline HTML tag closed + String.raw`>`, + ]; + + // The line must start by one of those patterns + return String.raw`^\s*(${patterns.join('|')})`; +} + +const indentationRules = { + indentationRules: { + increaseIndentPattern: increaseIndentPattern(), + decreaseIndentPattern: decreaseIndentPattern(), + } +} +console.log(JSON.stringify(indentationRules, null, 2));