🐛 fix(editor): Enhance Go Template Editor to support trimmed template tags and improve range/end matching

This commit is contained in:
web 2025-08-12 12:44:49 -07:00 committed by speakeloudest
parent d76c3e06c3
commit 688efcf29b

View File

@ -708,12 +708,14 @@ export function GoTemplateEditor({ schema, enableSprig = true, ...props }: GoTem
tokenizer: { tokenizer: {
root: [ root: [
[/\{\{\/\*/, 'comment', '@comment'], [/\{\{\/\*/, 'comment', '@comment'],
[/\{\{-/, 'template-tag', '@template'],
[/\{\{/, 'template-tag', '@template'], [/\{\{/, 'template-tag', '@template'],
[/./, 'text'], [/./, 'text'],
], ],
template: [ template: [
[/\/\*/, 'comment', '@comment'], [/\/\*/, 'comment', '@comment'],
[/-\}\}/, 'template-tag', '@pop'],
[/\}\}/, 'template-tag', '@pop'], [/\}\}/, 'template-tag', '@pop'],
[/"([^"\\]|\\.)*$/, 'string.invalid'], [/"([^"\\]|\\.)*$/, 'string.invalid'],
[/"/, 'string', '@string'], [/"/, 'string', '@string'],
@ -756,8 +758,14 @@ export function GoTemplateEditor({ schema, enableSprig = true, ...props }: GoTem
endColumn: position.column, endColumn: position.column,
}); });
const lastOpenBrace = textUntilPosition.lastIndexOf('{{'); const lastOpenBrace = Math.max(
const lastCloseBrace = textUntilPosition.lastIndexOf('}}'); textUntilPosition.lastIndexOf('{{'),
textUntilPosition.lastIndexOf('{{-'),
);
const lastCloseBrace = Math.max(
textUntilPosition.lastIndexOf('}}'),
textUntilPosition.lastIndexOf('-}}'),
);
const insideTemplate = lastOpenBrace > lastCloseBrace && lastOpenBrace !== -1; const insideTemplate = lastOpenBrace > lastCloseBrace && lastOpenBrace !== -1;
if (!insideTemplate) { if (!insideTemplate) {
@ -768,8 +776,10 @@ export function GoTemplateEditor({ schema, enableSprig = true, ...props }: GoTem
const currentPosition = model.getOffsetAt(position); const currentPosition = model.getOffsetAt(position);
const textBeforePosition = fullText.substring(0, currentPosition); const textBeforePosition = fullText.substring(0, currentPosition);
const rangeMatches = [...textBeforePosition.matchAll(/\{\{\s*range\s+([^}]+)\s*\}\}/g)]; const rangeMatches = [
const endMatches = [...textBeforePosition.matchAll(/\{\{\s*end\s*\}\}/g)]; ...textBeforePosition.matchAll(/\{\{-?\s*range\s+([^}]+)\s*-?\}\}/g),
];
const endMatches = [...textBeforePosition.matchAll(/\{\{-?\s*end\s*-?\}\}/g)];
let activeRangeField: string | null = null; let activeRangeField: string | null = null;
let rangeVariable: 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 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 actualStart = Math.max(wordStart, templateStart);
const currentWord = textUntilPosition.substring(actualStart).trim(); const currentWord = textUntilPosition.substring(actualStart).trim();