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 new file mode 100644 index 0000000..6d49e59 --- /dev/null +++ b/language-configuration.json @@ -0,0 +1,35 @@ +{ + "comments": { + "blockComment": ["{% comment %}\n", "\n{% endcomment %}"] + }, + "brackets": [ + ["{", "}"], + ["[", "]"], + ["<", ">"] + ], + "autoClosingPairs": [ + { "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": [ + ["-", "-"], + ["{", "}"], + ["[", "]"], + ["(", ")"], + ["'", "'"], + ["\"", "\""], + ["`", "`"] + ], + "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)); 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" } ] },