|
| 1 | +// Custom C# Language Definition - Fixes string highlighting issues with URLs |
| 2 | +// Specifically addresses the problem where https:// inside strings affects .Dump() highlighting |
| 3 | + |
| 4 | +export function overrideCSharpLanguage() { |
| 5 | + // Enhanced C# language definition that fixes URL highlighting in strings |
| 6 | + const enhancedCSharpLanguage = { |
| 7 | + defaultToken: '', |
| 8 | + tokenPostfix: '.cs', |
| 9 | + |
| 10 | + keywords: [ |
| 11 | + 'extern', 'alias', 'using', 'bool', 'decimal', 'sbyte', 'byte', 'short', |
| 12 | + 'ushort', 'int', 'uint', 'long', 'ulong', 'char', 'float', 'double', |
| 13 | + 'object', 'dynamic', 'string', 'assembly', 'is', 'as', 'ref', 'out', |
| 14 | + 'this', 'base', 'new', 'typeof', 'void', 'checked', 'unchecked', |
| 15 | + 'default', 'delegate', 'var', 'const', 'if', 'else', 'switch', 'case', |
| 16 | + 'while', 'do', 'for', 'foreach', 'in', 'break', 'continue', 'goto', |
| 17 | + 'return', 'throw', 'try', 'catch', 'finally', 'lock', 'yield', 'from', |
| 18 | + 'let', 'where', 'join', 'on', 'equals', 'into', 'orderby', 'ascending', |
| 19 | + 'descending', 'select', 'group', 'by', 'namespace', 'partial', 'class', |
| 20 | + 'field', 'event', 'method', 'param', 'public', 'protected', 'internal', |
| 21 | + 'private', 'abstract', 'sealed', 'static', 'struct', 'readonly', |
| 22 | + 'volatile', 'virtual', 'override', 'params', 'get', 'set', 'add', |
| 23 | + 'remove', 'operator', 'true', 'false', 'implicit', 'explicit', |
| 24 | + 'interface', 'enum', 'null', 'async', 'await', 'fixed', 'sizeof', |
| 25 | + 'stackalloc', 'unsafe', 'nameof', 'when' |
| 26 | + ], |
| 27 | + |
| 28 | + namespaceFollows: ['namespace', 'using'], |
| 29 | + parenFollows: ['if', 'for', 'while', 'switch', 'foreach', 'using', 'catch', 'when'], |
| 30 | + operators: [ |
| 31 | + '=', '??', '||', '&&', '|', '^', '&', '==', '!=', '<=', '>=', '<<', |
| 32 | + '+', '-', '*', '/', '%', '!', '~', '++', '--', '+=', '-=', '*=', |
| 33 | + '/=', '%=', '&=', '|=', '^=', '<<=', '>>=', '>>', '=>' |
| 34 | + ], |
| 35 | + |
| 36 | + symbols: /[=><!~?:&|+\-*\/\^%]+/, |
| 37 | + escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/, |
| 38 | + |
| 39 | + tokenizer: { |
| 40 | + root: [ |
| 41 | + // String literals MUST be processed FIRST to prevent // inside strings from being treated as comments |
| 42 | + [/"/, { token: 'string.quote', next: '@string' }], |
| 43 | + [/\$\@"/, { token: 'string.quote', next: '@litinterpstring' }], |
| 44 | + [/\@"/, { token: 'string.quote', next: '@litstring' }], |
| 45 | + [/\$"/, { token: 'string.quote', next: '@interpolatedstring' }], |
| 46 | + |
| 47 | + // Character literals |
| 48 | + [/'[^\\']'/, 'string'], |
| 49 | + [/(')(@escapes)(')/, ['string', 'string.escape', 'string']], |
| 50 | + [/'/, 'string.invalid'], |
| 51 | + |
| 52 | + // Identifiers and keywords (including method names) |
| 53 | + [/\@?[a-zA-Z_]\w*/, { |
| 54 | + cases: { |
| 55 | + '@namespaceFollows': { token: 'keyword.$0', next: '@namespace' }, |
| 56 | + '@keywords': { token: 'keyword.$0', next: '@qualified' }, |
| 57 | + '@default': { token: 'identifier', next: '@qualified' } |
| 58 | + } |
| 59 | + }], |
| 60 | + |
| 61 | + // Whitespace (includes comments - but only when NOT inside strings) |
| 62 | + { include: '@whitespace' }, |
| 63 | + |
| 64 | + // Brackets |
| 65 | + [/}/, { |
| 66 | + cases: { |
| 67 | + '$S2==interpolatedstring': { token: 'string.quote', next: '@pop' }, |
| 68 | + '$S2==litinterpstring': { token: 'string.quote', next: '@pop' }, |
| 69 | + '@default': '@brackets' |
| 70 | + } |
| 71 | + }], |
| 72 | + [/[{}()\[\]]/, '@brackets'], |
| 73 | + [/[<>](?!@symbols)/, '@brackets'], |
| 74 | + |
| 75 | + // Operators |
| 76 | + [/@symbols/, { |
| 77 | + cases: { |
| 78 | + '@operators': 'delimiter', |
| 79 | + '@default': '' |
| 80 | + } |
| 81 | + }], |
| 82 | + |
| 83 | + // Numbers |
| 84 | + [/[0-9_]*\.[0-9_]+([eE][\-+]?\d+)?[fFdD]?/, 'number.float'], |
| 85 | + [/0[xX][0-9a-fA-F_]+/, 'number.hex'], |
| 86 | + [/0[bB][01_]+/, 'number.hex'], |
| 87 | + [/[0-9_]+/, 'number'], |
| 88 | + |
| 89 | + // Delimiters |
| 90 | + [/[;,.]/, 'delimiter'] |
| 91 | + ], |
| 92 | + |
| 93 | + qualified: [ |
| 94 | + [/[a-zA-Z_][\w]*/, { |
| 95 | + cases: { |
| 96 | + '@keywords': { token: 'keyword.$0' }, |
| 97 | + '@default': 'identifier' |
| 98 | + } |
| 99 | + }], |
| 100 | + [/\./, 'delimiter'], |
| 101 | + ['', '', '@pop'] |
| 102 | + ], |
| 103 | + |
| 104 | + namespace: [ |
| 105 | + { include: '@whitespace' }, |
| 106 | + [/[A-Z]\w*/, 'namespace'], |
| 107 | + [/[\.=]/, 'delimiter'], |
| 108 | + ['', '', '@pop'] |
| 109 | + ], |
| 110 | + |
| 111 | + comment: [ |
| 112 | + [/[^\/*]+/, 'comment'], |
| 113 | + ['\\*/', 'comment', '@pop'], |
| 114 | + [/[\/*]/, 'comment'] |
| 115 | + ], |
| 116 | + |
| 117 | + // String state - NO comment processing allowed here |
| 118 | + string: [ |
| 119 | + [/[^\\"]+/, 'string'], // Everything inside string is just string content |
| 120 | + [/@escapes/, 'string.escape'], |
| 121 | + [/\\./, 'string.escape.invalid'], |
| 122 | + [/"/, { token: 'string.quote', next: '@pop' }] |
| 123 | + ], |
| 124 | + |
| 125 | + litstring: [ |
| 126 | + [/[^"]+/, 'string'], // Everything inside literal string is just string content |
| 127 | + [/""/, 'string.escape'], |
| 128 | + [/"/, { token: 'string.quote', next: '@pop' }] |
| 129 | + ], |
| 130 | + |
| 131 | + litinterpstring: [ |
| 132 | + [/[^"{]+/, 'string'], |
| 133 | + [/""/, 'string.escape'], |
| 134 | + [/{{/, 'string.escape'], |
| 135 | + [/}}/, 'string.escape'], |
| 136 | + [/{/, { token: 'string.quote', next: 'root.litinterpstring' }], |
| 137 | + [/"/, { token: 'string.quote', next: '@pop' }] |
| 138 | + ], |
| 139 | + |
| 140 | + interpolatedstring: [ |
| 141 | + [/[^\\"{]+/, 'string'], |
| 142 | + [/@escapes/, 'string.escape'], |
| 143 | + [/\\./, 'string.escape.invalid'], |
| 144 | + [/{{/, 'string.escape'], |
| 145 | + [/}}/, 'string.escape'], |
| 146 | + [/{/, { token: 'string.quote', next: 'root.interpolatedstring' }], |
| 147 | + [/"/, { token: 'string.quote', next: '@pop' }] |
| 148 | + ], |
| 149 | + |
| 150 | + // Whitespace handling - comments only processed when not in string context |
| 151 | + whitespace: [ |
| 152 | + [/^[ \t\v\f]*#((r)|(load))(?=\s)/, 'directive.csx'], |
| 153 | + [/^[ \t\v\f]*#\w.*$/, 'namespace.cpp'], |
| 154 | + [/[ \t\v\f\r\n]+/, ''], |
| 155 | + [/\/\*/, 'comment', '@comment'], |
| 156 | + [/\/\/.*$/, 'comment'] |
| 157 | + ] |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + // Override the existing C# language tokenizer |
| 162 | + const languageId = 'csharp'; |
| 163 | + let overrideApplied = false; |
| 164 | + |
| 165 | + const applyOverride = () => { |
| 166 | + if (!globalThis.monaco?.languages?.setMonarchTokensProvider) { |
| 167 | + return false; |
| 168 | + } |
| 169 | + |
| 170 | + try { |
| 171 | + globalThis.monaco.languages.setMonarchTokensProvider(languageId, enhancedCSharpLanguage); |
| 172 | + if (!overrideApplied) { |
| 173 | + overrideApplied = true; |
| 174 | + console.log('C# language tokenizer successfully overridden - fixed string URL highlighting'); |
| 175 | + } |
| 176 | + return true; |
| 177 | + } catch (error) { |
| 178 | + console.error('Failed to override C# language tokenizer:', error); |
| 179 | + return false; |
| 180 | + } |
| 181 | + }; |
| 182 | + |
| 183 | + const scheduleOverride = (delay = 0, attemptsLeft = 10) => { |
| 184 | + if (attemptsLeft <= 0) { |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + setTimeout(() => { |
| 189 | + const applied = applyOverride(); |
| 190 | + if (!applied) { |
| 191 | + scheduleOverride(Math.min(delay + 50, 250), attemptsLeft - 1); |
| 192 | + } |
| 193 | + }, delay); |
| 194 | + }; |
| 195 | + |
| 196 | + try { |
| 197 | + if (globalThis.monaco?.languages?.getLanguages?.().some(lang => lang.id === languageId)) { |
| 198 | + scheduleOverride(); |
| 199 | + } else { |
| 200 | + scheduleOverride(25); |
| 201 | + } |
| 202 | + } catch (error) { |
| 203 | + console.warn('Unable to verify Monaco languages for override immediately:', error); |
| 204 | + scheduleOverride(); |
| 205 | + } |
| 206 | + |
| 207 | + if (globalThis.monaco?.languages?.onLanguage) { |
| 208 | + globalThis.monaco.languages.onLanguage(languageId, () => { |
| 209 | + scheduleOverride(); |
| 210 | + scheduleOverride(50); |
| 211 | + scheduleOverride(200); |
| 212 | + }); |
| 213 | + } else { |
| 214 | + // Fallback: keep trying until Monaco exposes the language APIs |
| 215 | + scheduleOverride(); |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | + |
0 commit comments