From 688efcf29bf3feadac93aa5de0c1be3e1fdeb8d0 Mon Sep 17 00:00:00 2001 From: web Date: Tue, 12 Aug 2025 12:44:49 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(editor):=20Enhance=20Go=20Te?= =?UTF-8?q?mplate=20Editor=20to=20support=20trimmed=20template=20tags=20an?= =?UTF-8?q?d=20improve=20range/end=20matching?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../custom-components/editor/go-template.tsx | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/ui/src/custom-components/editor/go-template.tsx b/packages/ui/src/custom-components/editor/go-template.tsx index 48624ea..721a7d6 100644 --- a/packages/ui/src/custom-components/editor/go-template.tsx +++ b/packages/ui/src/custom-components/editor/go-template.tsx @@ -708,12 +708,14 @@ export function GoTemplateEditor({ schema, enableSprig = true, ...props }: GoTem tokenizer: { root: [ [/\{\{\/\*/, 'comment', '@comment'], + [/\{\{-/, 'template-tag', '@template'], [/\{\{/, 'template-tag', '@template'], [/./, 'text'], ], template: [ [/\/\*/, 'comment', '@comment'], + [/-\}\}/, 'template-tag', '@pop'], [/\}\}/, 'template-tag', '@pop'], [/"([^"\\]|\\.)*$/, 'string.invalid'], [/"/, 'string', '@string'], @@ -756,8 +758,14 @@ export function GoTemplateEditor({ schema, enableSprig = true, ...props }: GoTem endColumn: position.column, }); - const lastOpenBrace = textUntilPosition.lastIndexOf('{{'); - const lastCloseBrace = textUntilPosition.lastIndexOf('}}'); + const lastOpenBrace = Math.max( + textUntilPosition.lastIndexOf('{{'), + textUntilPosition.lastIndexOf('{{-'), + ); + const lastCloseBrace = Math.max( + textUntilPosition.lastIndexOf('}}'), + textUntilPosition.lastIndexOf('-}}'), + ); const insideTemplate = lastOpenBrace > lastCloseBrace && lastOpenBrace !== -1; if (!insideTemplate) { @@ -768,8 +776,10 @@ export function GoTemplateEditor({ schema, enableSprig = true, ...props }: GoTem const currentPosition = model.getOffsetAt(position); const textBeforePosition = fullText.substring(0, currentPosition); - const rangeMatches = [...textBeforePosition.matchAll(/\{\{\s*range\s+([^}]+)\s*\}\}/g)]; - const endMatches = [...textBeforePosition.matchAll(/\{\{\s*end\s*\}\}/g)]; + const rangeMatches = [ + ...textBeforePosition.matchAll(/\{\{-?\s*range\s+([^}]+)\s*-?\}\}/g), + ]; + const endMatches = [...textBeforePosition.matchAll(/\{\{-?\s*end\s*-?\}\}/g)]; let activeRangeField: string | null = null; let rangeVariable: string | null = null; @@ -832,7 +842,11 @@ export function GoTemplateEditor({ schema, enableSprig = true, ...props }: GoTem } const wordStart = textUntilPosition.lastIndexOf(' ') + 1; - const templateStart = textUntilPosition.lastIndexOf('{{') + 2; + const templateStartNormal = textUntilPosition.lastIndexOf('{{'); + const templateStartTrim = textUntilPosition.lastIndexOf('{{-'); + const templateStart = + Math.max(templateStartNormal, templateStartTrim) + + (templateStartTrim > templateStartNormal ? 3 : 2); const actualStart = Math.max(wordStart, templateStart); const currentWord = textUntilPosition.substring(actualStart).trim();