70160
Ce
cel-tui / files
cel-tui / packages / clew / src / shared.ts
4.1 KB
1import type { ClewOutput, ClewToken } from "./model.js"; 2 3export function requiredAt<T>( 4 items: readonly T[], 5 index: number, 6 description: string, 7): T { 8 const item = items[index]; 9 if (item === undefined) { 10 throw new Error(`Missing ${description} at index ${index}`); 11 } 12 return item; 13} 14 15export function createToken( 16 text: string, 17 start: number, 18 end: number, 19 type: string, 20 scopes?: readonly string[], 21 path?: readonly string[], 22): ClewToken { 23 return { 24 text, 25 start, 26 end, 27 type, 28 path, 29 scopes: scopes ?? [type], 30 }; 31} 32 33export function pushTokenRange( 34 tokens: ClewToken[], 35 content: string, 36 start: number, 37 end: number, 38 type: string, 39 scopes?: readonly string[], 40 path?: readonly string[], 41): void { 42 if (end <= start) { 43 return; 44 } 45 46 tokens.push( 47 createToken(content.slice(start, end), start, end, type, scopes, path), 48 ); 49} 50 51export function cloneToken(token: ClewToken): ClewToken { 52 return { 53 text: token.text, 54 start: token.start, 55 end: token.end, 56 type: token.type, 57 path: token.path ? [...token.path] : undefined, 58 scopes: token.scopes ? [...token.scopes] : undefined, 59 }; 60} 61 62export function cloneTokens(tokens: readonly ClewToken[]): ClewToken[] { 63 return tokens.map(cloneToken); 64} 65 66export function cloneOutput(output: ClewOutput): ClewOutput { 67 return { tokens: cloneTokens(output.tokens) }; 68} 69 70function sameStringArray( 71 a: readonly string[] | undefined, 72 b: readonly string[] | undefined, 73): boolean { 74 if (a === b) { 75 return true; 76 } 77 if (!a || !b || a.length !== b.length) { 78 return false; 79 } 80 for (let i = 0; i < a.length; i++) { 81 if (a[i] !== b[i]) { 82 return false; 83 } 84 } 85 return true; 86} 87 88export function sameToken(a: ClewToken, b: ClewToken): boolean { 89 return ( 90 a.text === b.text && 91 a.start === b.start && 92 a.end === b.end && 93 a.type === b.type && 94 sameStringArray(a.path, b.path) && 95 sameStringArray(a.scopes, b.scopes) 96 ); 97} 98 99export function commonPrefixLength( 100 previous: readonly ClewToken[], 101 next: readonly ClewToken[], 102): number { 103 const max = Math.min(previous.length, next.length); 104 let index = 0; 105 while (index < max && sameToken(previous[index], next[index])) { 106 index++; 107 } 108 return index; 109} 110 111export function tokensFrom( 112 tokens: readonly ClewToken[], 113 start: number, 114): ClewToken[] { 115 return tokens.filter((token) => token.end > start).map(cloneToken); 116} 117 118export function tokensStartingAtOrAfter( 119 tokens: readonly ClewToken[], 120 start: number, 121): ClewToken[] { 122 return tokens.filter((token) => token.start >= start).map(cloneToken); 123} 124 125export function splitLines(content: string): Array<{ 126 delimiter: string; 127 start: number; 128 text: string; 129}> { 130 const lines: Array<{ delimiter: string; start: number; text: string }> = []; 131 let lineStart = 0; 132 let index = 0; 133 134 while (index < content.length) { 135 while ( 136 index < content.length && 137 content[index] !== "\n" && 138 content[index] !== "\r" 139 ) { 140 index++; 141 } 142 143 const textEnd = index; 144 let delimiter = ""; 145 146 if (index < content.length) { 147 if (content[index] === "\r" && content[index + 1] === "\n") { 148 delimiter = "\r\n"; 149 index += 2; 150 } else { 151 delimiter = content[index] ?? ""; 152 index += 1; 153 } 154 } 155 156 lines.push({ 157 delimiter, 158 start: lineStart, 159 text: content.slice(lineStart, textEnd), 160 }); 161 lineStart = index; 162 } 163 164 return lines; 165} 166 167export function stableLineBoundary(content: string, ended: boolean): number { 168 if (ended) { 169 return content.length; 170 } 171 172 const lf = content.lastIndexOf("\n"); 173 if (lf !== -1) { 174 return lf + 1; 175 } 176 177 const cr = content.lastIndexOf("\r"); 178 if (cr !== -1) { 179 return cr + 1; 180 } 181 182 return 0; 183} 184 185export function outputAtBoundary( 186 output: ClewOutput, 187 boundary: number, 188): ClewOutput { 189 return { 190 tokens: output.tokens 191 .filter((token) => token.end <= boundary) 192 .map(cloneToken), 193 }; 194} 195 196/** Last source offset fully represented by an output's ordered tokens. */ 197export function outputBoundary(output: ClewOutput): number { 198 return output.tokens.at(-1)?.end ?? 0; 199}