From a3e3823f56606f023339cf8dc1e07798d106c453 Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 10 Feb 2026 00:44:05 +0100 Subject: refactor: Improve syntax highlighting to avoid overlapping matches Co-Authored-By: Claude Sonnet 4.5 --- tools/shader_editor/index.html | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'tools/shader_editor') diff --git a/tools/shader_editor/index.html b/tools/shader_editor/index.html index 5db06de..c7feffc 100644 --- a/tools/shader_editor/index.html +++ b/tools/shader_editor/index.html @@ -682,27 +682,34 @@ const syntaxHighlight = document.getElementById('syntax-highlight'); let composeTimeout = null; function highlightWGSL(code) { - const keywords = /\b(fn|var|let|const|struct|if|else|for|while|return|break|continue|switch|case|default|loop|continuing)\b/g; - const types = /\b(f32|i32|u32|bool|vec2|vec3|vec4|mat2x2|mat3x3|mat4x4|sampler|texture_2d|array)\b/g; - const attributes = /(@\w+)/g; - const functions = /\b([a-z_]\w*)\s*(?=\()/g; - const numbers = /\b(\d+\.?\d*)\b/g; - const comments = /(\/\/.*$)/gm; - const strings = /"([^"\\]|\\.)*"/g; - let highlighted = code .replace(/&/g, '&') .replace(//g, '>'); - highlighted = highlighted - .replace(comments, '$1') - .replace(strings, '$&') - .replace(attributes, '$1') - .replace(keywords, '$1') - .replace(types, '$1') - .replace(functions, '$1') - .replace(numbers, '$1'); + // Tokenize to avoid overlapping matches + const tokens = []; + let lastIndex = 0; + + // Match comments, strings, then keywords/types/etc + const patterns = [ + { regex: /\/\/.*$/gm, className: 'hl-comment' }, + { regex: /"(?:[^"\\]|\\.)*"/g, className: 'hl-string' }, + { regex: /@\w+/g, className: 'hl-attribute' }, + { regex: /\b(?:fn|var|let|const|struct|if|else|for|while|return|break|continue|switch|case|default|loop|continuing)\b/g, className: 'hl-keyword' }, + { regex: /\b(?:f32|i32|u32|bool|vec2|vec3|vec4|mat2x2|mat3x3|mat4x4|sampler|texture_2d|array)\b/g, className: 'hl-type' }, + { regex: /\b\d+\.?\d*\b/g, className: 'hl-number' }, + { regex: /\b[a-z_]\w*(?=\s*\()/g, className: 'hl-function' }, + ]; + + // Simple sequential replacement (good enough for basic highlighting) + patterns.forEach(({ regex, className }) => { + highlighted = highlighted.replace(regex, match => { + // Don't re-highlight if already inside a span + if (/${match}`; + }); + }); return highlighted; } -- cgit v1.2.3