`line` with its comments and strings blanked, given the block `state` it starts in.
(line: string, syntax: Syntax, state: { close: string | null })
| 192 | |
| 193 | /** `line` with its comments and strings blanked, given the block `state` it starts in. */ |
| 194 | function codeOf(line: string, syntax: Syntax, state: { close: string | null }): string { |
| 195 | let out = ''; |
| 196 | let i = 0; |
| 197 | scan: while (i < line.length) { |
| 198 | if (state.close !== null) { |
| 199 | const end = line.indexOf(state.close, i); |
| 200 | if (end < 0) return out; |
| 201 | i = end + state.close.length; |
| 202 | state.close = null; |
| 203 | out += ' '; |
| 204 | continue; |
| 205 | } |
| 206 | for (const [open, close] of syntax.block) { |
| 207 | if (line.startsWith(open, i)) { state.close = close; i += open.length; continue scan; } |
| 208 | } |
| 209 | for (const opener of syntax.line) if (line.startsWith(opener, i)) return out; |
| 210 | const ch = line[i]!; |
| 211 | if (syntax.quotes.includes(ch)) { |
| 212 | let j = i + 1; |
| 213 | while (j < line.length && line[j] !== ch) j += line[j] === '\\' ? 2 : 1; |
| 214 | if (j >= line.length) return out; // an unclosed string runs to the end of the line |
| 215 | i = j + 1; |
| 216 | out += ' '; |
| 217 | continue; |
| 218 | } |
| 219 | out += ch; |
| 220 | i++; |
| 221 | } |
| 222 | return out; |
| 223 | } |
| 224 | |
| 225 | /** `name` as a whole word: not inside a longer identifier, in any script. */ |
| 226 | export function wholeNamePattern(name: string): RegExp { |