Changesets · Branches · Tags · Compare
| Changeset | 0f17595b5c72172b2479e25b3313d053d6d7073cc4fe628319f210ff2e8df43e |
|---|---|
| Author | Jacob Kim <jacob@continue.dev> · 2025-07-15 13:09 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-07-15 13:09 -07:00 |
| Parents | f8f9f2f3beb8, d891dd383c10 |
8 files changed
modified core/codeRenderer/CodeRenderer.ts +94 −74
| @@ -9,9 +9,7 @@ | ||
| 9 | 9 | * creating a render of it. |
| 10 | 10 | */ |
| 11 | 11 | import { |
| 12 | - transformerMetaHighlight, | |
| 13 | 12 | transformerNotationDiff, |
| 14 | - transformerNotationFocus, | |
| 15 | 13 | transformerNotationHighlight, |
| 16 | 14 | } from "@shikijs/transformers"; |
| 17 | 15 | import { JSDOM } from "jsdom"; |
| @@ -21,6 +19,7 @@ | ||
| 21 | 19 | getSingletonHighlighter, |
| 22 | 20 | Highlighter, |
| 23 | 21 | } from "shiki"; |
| 22 | +import { DiffLine } from ".."; | |
| 24 | 23 | import { escapeForSVG, kebabOfStr } from "../util/text"; |
| 25 | 24 | |
| 26 | 25 | interface CodeRendererOptions { |
| @@ -37,6 +36,10 @@ | ||
| 37 | 36 | interface ConversionOptions extends HTMLOptions { |
| 38 | 37 | transparent?: boolean; |
| 39 | 38 | imageType: "svg"; |
| 39 | + fontSize: number; | |
| 40 | + fontFamily: string; | |
| 41 | + dimensions: Dimensions; | |
| 42 | + lineHeight: number; | |
| 40 | 43 | } |
| 41 | 44 | |
| 42 | 45 | interface Dimensions { |
| @@ -166,83 +169,102 @@ | ||
| 166 | 169 | code: string, |
| 167 | 170 | language: string = "javascript", |
| 168 | 171 | currLineOffsetFromTop: number, |
| 172 | + newDiffLines: DiffLine[], | |
| 169 | 173 | ): Promise<string> { |
| 170 | - const annotatedCode = code | |
| 171 | - .split("\n") | |
| 172 | - .map((line, i) => | |
| 173 | - i === currLineOffsetFromTop | |
| 174 | - ? line + " \/\/ \[\!code highlight\]" | |
| 175 | - : line, | |
| 176 | - ) | |
| 177 | - .join("\n"); | |
| 174 | + const lines = code.split("\n"); | |
| 175 | + const newDiffLineMap = new Set(); | |
| 176 | + | |
| 177 | + if (newDiffLines) { | |
| 178 | + newDiffLines.forEach((diffLine) => { | |
| 179 | + if (diffLine.type === "new") { | |
| 180 | + newDiffLineMap.add(diffLine.line); | |
| 181 | + } | |
| 182 | + }); | |
| 183 | + } | |
| 178 | 184 | |
| 179 | - await this.highlighter!.loadLanguage(language as BundledLanguage); | |
| 185 | + const annotatedLines = []; | |
| 186 | + | |
| 187 | + // NOTE: Shiki's preprocessor deletes transformer annotations when applied to an empty line. | |
| 188 | + // If you are transforming an empty line, make sure that | |
| 189 | + // the transformation is applied to a non-empty line first. | |
| 190 | + for (let i = 0; i < lines.length; i++) { | |
| 191 | + const line = lines[i]; | |
| 192 | + | |
| 193 | + // Add highlight comment before target line. | |
| 194 | + if (i + 1 === currLineOffsetFromTop && currLineOffsetFromTop >= 0) { | |
| 195 | + annotatedLines.push("// [!code highlight:1]"); | |
| 196 | + } | |
| 197 | + | |
| 198 | + // Handle diff lines | |
| 199 | + if (newDiffLineMap.has(line)) { | |
| 200 | + if (line.trim() === "") { | |
| 201 | + // For empty lines, add the magic comment on a separate line before. | |
| 202 | + annotatedLines.push("// [!code ++]"); | |
| 203 | + annotatedLines.push(line); // The empty line itself. | |
| 204 | + } else { | |
| 205 | + // For non-empty lines, append the magic comment. | |
| 206 | + annotatedLines.push(line + "// [!code ++]"); | |
| 207 | + } | |
| 208 | + newDiffLineMap.delete(line); | |
| 209 | + } else { | |
| 210 | + annotatedLines.push(line); | |
| 211 | + } | |
| 212 | + } | |
| 180 | 213 | |
| 214 | + const annotatedCode = annotatedLines.join("\n"); | |
| 215 | + | |
| 216 | + await this.highlighter!.loadLanguage(language as BundledLanguage); | |
| 181 | 217 | return this.highlighter!.codeToHtml(annotatedCode, { |
| 182 | 218 | lang: language, |
| 183 | 219 | theme: this.currentTheme, |
| 184 | - transformers: [ | |
| 185 | - // transformerColorizedBrackets(), | |
| 186 | - transformerMetaHighlight(), | |
| 187 | - transformerNotationHighlight(), | |
| 188 | - transformerNotationDiff(), | |
| 189 | - transformerNotationFocus(), | |
| 190 | - ], | |
| 220 | + transformers: [transformerNotationHighlight(), transformerNotationDiff()], | |
| 191 | 221 | }); |
| 192 | 222 | } |
| 193 | 223 | |
| 194 | 224 | async convertToSVG( |
| 195 | 225 | code: string, |
| 196 | 226 | language: string = "javascript", |
| 197 | - fontSize: number, | |
| 198 | - fontFamily: string, | |
| 199 | - dimensions: Dimensions, | |
| 200 | - lineHeight: number, | |
| 201 | 227 | options: ConversionOptions, |
| 202 | 228 | currLineOffsetFromTop: number, |
| 229 | + newDiffLines: DiffLine[], | |
| 203 | 230 | ): Promise<Buffer> { |
| 204 | 231 | const strokeWidth = 1; |
| 205 | 232 | const highlightedCodeHtml = await this.highlightCode( |
| 206 | 233 | code, |
| 207 | 234 | language, |
| 208 | 235 | currLineOffsetFromTop, |
| 236 | + newDiffLines, | |
| 209 | 237 | ); |
| 210 | 238 | // console.log(highlightedCodeHtml); |
| 211 | 239 | |
| 212 | 240 | const { guts, lineBackgrounds } = this.convertShikiHtmlToSvgGut( |
| 213 | 241 | highlightedCodeHtml, |
| 214 | - fontSize, | |
| 215 | - fontFamily, | |
| 216 | - lineHeight, | |
| 217 | - dimensions, | |
| 242 | + options, | |
| 218 | 243 | ); |
| 219 | 244 | const backgroundColor = this.getBackgroundColor(highlightedCodeHtml); |
| 220 | 245 | |
| 221 | - const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${dimensions.width}" height="${dimensions.height}" shape-rendering="crispEdges"> | |
| 222 | - <style> | |
| 223 | - :root { | |
| 224 | - --purple: rgb(112, 114, 209); | |
| 225 | - --green: rgb(136, 194, 163); | |
| 226 | - --blue: rgb(107, 166, 205); | |
| 227 | - } | |
| 228 | - </style> | |
| 229 | - <g> | |
| 230 | - <rect x="0" y="0" rx="10" ry="10" width="${dimensions.width}" height="${dimensions.height}" fill="${this.editorBackground}" shape-rendering="crispEdges" /> | |
| 231 | - ${lineBackgrounds} | |
| 232 | - ${guts} | |
| 233 | - </g> | |
| 234 | -</svg>`; | |
| 235 | - console.log(svg); | |
| 246 | + const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${options.dimensions.width}" height="${options.dimensions.height}" shape-rendering="crispEdges"> | |
| 247 | + <style> | |
| 248 | + :root { | |
| 249 | + --purple: rgb(112, 114, 209); | |
| 250 | + --green: rgb(136, 194, 163); | |
| 251 | + --blue: rgb(107, 166, 205); | |
| 252 | + } | |
| 253 | + </style> | |
| 254 | + <g> | |
| 255 | + <rect x="0" y="0" rx="10" ry="10" width="${options.dimensions.width}" height="${options.dimensions.height}" fill="${this.editorBackground}" shape-rendering="crispEdges" /> | |
| 256 | + ${lineBackgrounds} | |
| 257 | + ${guts} | |
| 258 | + </g> | |
| 259 | + </svg>`; | |
| 260 | + // console.log(svg); | |
| 236 | 261 | |
| 237 | 262 | return Buffer.from(svg, "utf8"); |
| 238 | 263 | } |
| 239 | 264 | |
| 240 | 265 | convertShikiHtmlToSvgGut( |
| 241 | 266 | shikiHtml: string, |
| 242 | - fontSize: number, | |
| 243 | - fontFamily: string, | |
| 244 | - lineHeight: number, | |
| 245 | - dimensions: Dimensions, | |
| 267 | + options: ConversionOptions, | |
| 246 | 268 | ): { guts: string; lineBackgrounds: string } { |
| 247 | 269 | const dom = new JSDOM(shikiHtml); |
| 248 | 270 | const document = dom.window.document; |
| @@ -263,6 +285,7 @@ | ||
| 263 | 285 | if (classes.includes("highlighted")) { |
| 264 | 286 | fill = ` fill="${this.editorLineHighlight}"`; |
| 265 | 287 | } |
| 288 | + | |
| 266 | 289 | const content = el.textContent || ""; |
| 267 | 290 | return `<tspan xml:space="preserve"${fill}>${escapeForSVG(content)}</tspan>`; |
| 268 | 291 | }) |
| @@ -278,8 +301,8 @@ | ||
| 278 | 301 | // The first step is to add lineHeight / 2 to move the axis down. |
| 279 | 302 | // The second step is to add 'dominant-baseline="central"' to vertically center the text. |
| 280 | 303 | // Note that we choose "central" over "middle". "middle" will center the text too perfectly, which is actually undesirable! |
| 281 | - const y = index * lineHeight + lineHeight / 2; | |
| 282 | - return `<text x="0" y="${y}" font-family="${fontFamily}" font-size="${fontSize.toString()}" xml:space="preserve" dominant-baseline="central" shape-rendering="crispEdges">${spans}</text>`; | |
| 304 | + const y = index * options.lineHeight + options.lineHeight / 2; | |
| 305 | + return `<text x="0" y="${y}" font-family="${options.fontFamily}" font-size="${options.fontSize.toString()}" xml:space="preserve" dominant-baseline="central" shape-rendering="crispEdges">${spans}</text>`; | |
| 283 | 306 | }); |
| 284 | 307 | |
| 285 | 308 | const lineBackgrounds = lines |
| @@ -287,8 +310,11 @@ | ||
| 287 | 310 | const classes = line?.getAttribute("class") || ""; |
| 288 | 311 | const bgColor = classes.includes("highlighted") |
| 289 | 312 | ? this.editorLineHighlight |
| 290 | - : this.editorBackground; | |
| 291 | - const y = index * lineHeight; | |
| 313 | + : classes.includes("diff add") | |
| 314 | + ? "rgba(255, 255, 0, 0.2)" | |
| 315 | + : this.editorBackground; | |
| 316 | + | |
| 317 | + const y = index * options.lineHeight; | |
| 292 | 318 | const isFirst = index === 0; |
| 293 | 319 | const isLast = index === lines.length - 1; |
| 294 | 320 | const radius = 10; |
| @@ -297,24 +323,24 @@ | ||
| 297 | 323 | // This is undesirable in our case because pixel-perfect alignment of these rectangles will introduce thin gaps. |
| 298 | 324 | // Turning it off with 'shape-rendering="crispEdges"' solves the issue. |
| 299 | 325 | return isFirst |
| 300 | - ? `<path d="M ${0} ${y + lineHeight} | |
| 301 | - L ${0} ${y + radius} | |
| 302 | - Q ${0} ${y} ${radius} ${y} | |
| 303 | - L ${dimensions.width - radius} ${y} | |
| 304 | - Q ${dimensions.width} ${y} ${dimensions.width} ${y + radius} | |
| 305 | - L ${dimensions.width} ${y + lineHeight} | |
| 306 | - Z" | |
| 307 | - fill="${bgColor}" />` | |
| 326 | + ? `<path d="M ${0} ${y + options.lineHeight} | |
| 327 | + L ${0} ${y + radius} | |
| 328 | + Q ${0} ${y} ${radius} ${y} | |
| 329 | + L ${options.dimensions.width - radius} ${y} | |
| 330 | + Q ${options.dimensions.width} ${y} ${options.dimensions.width} ${y + radius} | |
| 331 | + L ${options.dimensions.width} ${y + options.lineHeight} | |
| 332 | + Z" | |
| 333 | + fill="${bgColor}" />` | |
| 308 | 334 | : isLast |
| 309 | - ? `<path d="M ${0} ${y} | |
| 310 | - L ${0} ${y + lineHeight - radius} | |
| 311 | - Q ${0} ${y + lineHeight} ${radius} ${y + lineHeight} | |
| 312 | - L ${dimensions.width - radius} ${y + lineHeight} | |
| 313 | - Q ${dimensions.width} ${y + lineHeight} ${dimensions.width} ${y + lineHeight - 10} | |
| 314 | - L ${dimensions.width} ${y} | |
| 315 | - Z" | |
| 316 | - fill="${bgColor}" />` | |
| 317 | - : `<rect x="0" y="${y}" rx="${radius}" ry="${radius}" width="100%" height="${lineHeight}" fill="${bgColor}" shape-rendering="crispEdges" />`; | |
| 335 | + ? `<path d="M ${0} ${y} | |
| 336 | + L ${0} ${y + options.lineHeight - radius} | |
| 337 | + Q ${0} ${y + options.lineHeight} ${radius} ${y + options.lineHeight} | |
| 338 | + L ${options.dimensions.width - radius} ${y + options.lineHeight} | |
| 339 | + Q ${options.dimensions.width} ${y + options.lineHeight} ${options.dimensions.width} ${y + options.lineHeight - 10} | |
| 340 | + L ${options.dimensions.width} ${y} | |
| 341 | + Z" | |
| 342 | + fill="${bgColor}" />` | |
| 343 | + : `<rect x="0" y="${y}" width="100%" height="${options.lineHeight}" fill="${bgColor}" shape-rendering="crispEdges" />`; | |
| 318 | 344 | }) |
| 319 | 345 | .join("\n"); |
| 320 | 346 | |
| @@ -344,12 +370,9 @@ | ||
| 344 | 370 | async getDataUri( |
| 345 | 371 | code: string, |
| 346 | 372 | language: string = "javascript", |
| 347 | - fontSize: number, | |
| 348 | - fontFamily: string, | |
| 349 | - dimensions: Dimensions, | |
| 350 | - lineHeight: number, | |
| 351 | 373 | options: ConversionOptions, |
| 352 | 374 | currLineOffsetFromTop: number, |
| 375 | + newDiffLines: DiffLine[], | |
| 353 | 376 | ): Promise<DataUri> { |
| 354 | 377 | switch (options.imageType) { |
| 355 | 378 | // case "png": |
| @@ -366,12 +389,9 @@ | ||
| 366 | 389 | const svgBuffer = await this.convertToSVG( |
| 367 | 390 | code, |
| 368 | 391 | language, |
| 369 | - fontSize, | |
| 370 | - fontFamily, | |
| 371 | - dimensions, | |
| 372 | - lineHeight, | |
| 373 | 392 | options, |
| 374 | 393 | currLineOffsetFromTop, |
| 394 | + newDiffLines, | |
| 375 | 395 | ); |
| 376 | 396 | return `data:image/svg+xml;base64,${svgBuffer.toString("base64")}`; |
| 377 | 397 | } |
modified core/diff/myers.ts +156 −2
| @@ -1,6 +1,6 @@ | ||
| 1 | -import { diffLines, type Change } from "diff"; | |
| 1 | +import { diffChars, diffLines, type Change } from "diff"; | |
| 2 | 2 | |
| 3 | -import { DiffLine } from ".."; | |
| 3 | +import { DiffChar, DiffLine } from ".."; | |
| 4 | 4 | |
| 5 | 5 | export function convertMyersChangeToDiffLines(change: Change): DiffLine[] { |
| 6 | 6 | const type: DiffLine["type"] = change.added |
| @@ -55,3 +55,157 @@ | ||
| 55 | 55 | |
| 56 | 56 | return ourFormat; |
| 57 | 57 | } |
| 58 | + | |
| 59 | +export function myersCharDiff( | |
| 60 | + oldContent: string, | |
| 61 | + newContent: string, | |
| 62 | +): DiffChar[] { | |
| 63 | + // Process the content character by character. | |
| 64 | + // We will handle newlines separately, | |
| 65 | + // because diffChars does not have an option to ignore eol newlines. | |
| 66 | + const theirFormat = diffChars(oldContent, newContent); | |
| 67 | + | |
| 68 | + // Track indices as we process the diff. | |
| 69 | + let oldIndex = 0; | |
| 70 | + let newIndex = 0; | |
| 71 | + let oldLineIndex = 0; | |
| 72 | + let newLineIndex = 0; | |
| 73 | + let oldCharIndexInLine = 0; | |
| 74 | + let newCharIndexInLine = 0; | |
| 75 | + | |
| 76 | + const result: DiffChar[] = []; | |
| 77 | + | |
| 78 | + for (const change of theirFormat) { | |
| 79 | + // Split the change value by newlines to handle them separately. | |
| 80 | + if (change.value.includes("\n")) { | |
| 81 | + const parts = change.value.split(/(\n)/g); // This keeps the newlines as separate entries. | |
| 82 | + | |
| 83 | + for (let i = 0; i < parts.length; i++) { | |
| 84 | + const part = parts[i]; | |
| 85 | + if (part === "") continue; | |
| 86 | + | |
| 87 | + if (part === "\n") { | |
| 88 | + // Handle newline. | |
| 89 | + if (change.added) { | |
| 90 | + result.push({ | |
| 91 | + type: "new", | |
| 92 | + char: part, | |
| 93 | + newIndex: newIndex, | |
| 94 | + newLineIndex: newLineIndex, | |
| 95 | + newCharIndexInLine: newCharIndexInLine, | |
| 96 | + }); | |
| 97 | + newIndex += part.length; | |
| 98 | + newLineIndex++; | |
| 99 | + newCharIndexInLine = 0; // Reset when moving to a new line. | |
| 100 | + } else if (change.removed) { | |
| 101 | + result.push({ | |
| 102 | + type: "old", | |
| 103 | + char: part, | |
| 104 | + oldIndex: oldIndex, | |
| 105 | + oldLineIndex: oldLineIndex, | |
| 106 | + oldCharIndexInLine: oldCharIndexInLine, | |
| 107 | + }); | |
| 108 | + oldIndex += part.length; | |
| 109 | + oldLineIndex++; | |
| 110 | + oldCharIndexInLine = 0; // Reset when moving to a new line. | |
| 111 | + } else { | |
| 112 | + result.push({ | |
| 113 | + type: "same", | |
| 114 | + char: part, | |
| 115 | + oldIndex: oldIndex, | |
| 116 | + newIndex: newIndex, | |
| 117 | + oldLineIndex: oldLineIndex, | |
| 118 | + newLineIndex: newLineIndex, | |
| 119 | + oldCharIndexInLine: oldCharIndexInLine, | |
| 120 | + newCharIndexInLine: newCharIndexInLine, | |
| 121 | + }); | |
| 122 | + oldIndex += part.length; | |
| 123 | + newIndex += part.length; | |
| 124 | + oldLineIndex++; | |
| 125 | + newLineIndex++; | |
| 126 | + oldCharIndexInLine = 0; | |
| 127 | + newCharIndexInLine = 0; | |
| 128 | + } | |
| 129 | + } else { | |
| 130 | + // Handle regular text. | |
| 131 | + if (change.added) { | |
| 132 | + result.push({ | |
| 133 | + type: "new", | |
| 134 | + char: part, | |
| 135 | + newIndex: newIndex, | |
| 136 | + newLineIndex: newLineIndex, | |
| 137 | + newCharIndexInLine: newCharIndexInLine, | |
| 138 | + }); | |
| 139 | + newIndex += part.length; | |
| 140 | + newCharIndexInLine += part.length; | |
| 141 | + } else if (change.removed) { | |
| 142 | + result.push({ | |
| 143 | + type: "old", | |
| 144 | + char: part, | |
| 145 | + oldIndex: oldIndex, | |
| 146 | + oldLineIndex: oldLineIndex, | |
| 147 | + oldCharIndexInLine: oldCharIndexInLine, | |
| 148 | + }); | |
| 149 | + oldIndex += part.length; | |
| 150 | + oldCharIndexInLine += part.length; | |
| 151 | + } else { | |
| 152 | + result.push({ | |
| 153 | + type: "same", | |
| 154 | + char: part, | |
| 155 | + oldIndex: oldIndex, | |
| 156 | + newIndex: newIndex, | |
| 157 | + oldLineIndex: oldLineIndex, | |
| 158 | + newLineIndex: newLineIndex, | |
| 159 | + oldCharIndexInLine: oldCharIndexInLine, | |
| 160 | + newCharIndexInLine: newCharIndexInLine, | |
| 161 | + }); | |
| 162 | + oldIndex += part.length; | |
| 163 | + newIndex += part.length; | |
| 164 | + oldCharIndexInLine += part.length; | |
| 165 | + newCharIndexInLine += part.length; | |
| 166 | + } | |
| 167 | + } | |
| 168 | + } | |
| 169 | + } else { | |
| 170 | + // No newlines, handle as a simple change. | |
| 171 | + if (change.added) { | |
| 172 | + result.push({ | |
| 173 | + type: "new", | |
| 174 | + char: change.value, | |
| 175 | + newIndex: newIndex, | |
| 176 | + newLineIndex: newLineIndex, | |
| 177 | + newCharIndexInLine: newCharIndexInLine, | |
| 178 | + }); | |
| 179 | + newIndex += change.value.length; | |
| 180 | + newCharIndexInLine += change.value.length; | |
| 181 | + } else if (change.removed) { | |
| 182 | + result.push({ | |
| 183 | + type: "old", | |
| 184 | + char: change.value, | |
| 185 | + oldIndex: oldIndex, | |
| 186 | + oldLineIndex: oldLineIndex, | |
| 187 | + oldCharIndexInLine: oldCharIndexInLine, | |
| 188 | + }); | |
| 189 | + oldIndex += change.value.length; | |
| 190 | + oldCharIndexInLine += change.value.length; | |
| 191 | + } else { | |
| 192 | + result.push({ | |
| 193 | + type: "same", | |
| 194 | + char: change.value, | |
| 195 | + oldIndex: oldIndex, | |
| 196 | + newIndex: newIndex, | |
| 197 | + oldLineIndex: oldLineIndex, | |
| 198 | + newLineIndex: newLineIndex, | |
| 199 | + oldCharIndexInLine: oldCharIndexInLine, | |
| 200 | + newCharIndexInLine: newCharIndexInLine, | |
| 201 | + }); | |
| 202 | + oldIndex += change.value.length; | |
| 203 | + newIndex += change.value.length; | |
| 204 | + oldCharIndexInLine += change.value.length; | |
| 205 | + newCharIndexInLine += change.value.length; | |
| 206 | + } | |
| 207 | + } | |
| 208 | + } | |
| 209 | + | |
| 210 | + return result; | |
| 211 | +} |
modified core/diff/myers.vitest.ts +587 −1
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | import { dedent } from "../util"; |
| 4 | 4 | |
| 5 | -import { myersDiff } from "./myers"; | |
| 5 | +import { myersCharDiff, myersDiff } from "./myers"; | |
| 6 | 6 | |
| 7 | 7 | describe("Test myers diff function", () => { |
| 8 | 8 | test("should ...", () => { |
| @@ -56,3 +56,589 @@ | ||
| 56 | 56 | ]); |
| 57 | 57 | }); |
| 58 | 58 | }); |
| 59 | + | |
| 60 | +describe("Test myersCharDiff function on the same line", () => { | |
| 61 | + test("should differentiate character changes", () => { | |
| 62 | + const oldContent = "hello world"; | |
| 63 | + const newContent = "hello earth"; | |
| 64 | + | |
| 65 | + const diffChars = myersCharDiff(oldContent, newContent); | |
| 66 | + expect(diffChars).toEqual([ | |
| 67 | + { | |
| 68 | + type: "same", | |
| 69 | + char: "hello ", | |
| 70 | + oldIndex: 0, | |
| 71 | + newIndex: 0, | |
| 72 | + oldCharIndexInLine: 0, | |
| 73 | + newCharIndexInLine: 0, | |
| 74 | + oldLineIndex: 0, | |
| 75 | + newLineIndex: 0, | |
| 76 | + }, | |
| 77 | + { | |
| 78 | + type: "old", | |
| 79 | + char: "wo", | |
| 80 | + oldIndex: 6, | |
| 81 | + oldCharIndexInLine: 6, | |
| 82 | + oldLineIndex: 0, | |
| 83 | + }, | |
| 84 | + { | |
| 85 | + type: "new", | |
| 86 | + char: "ea", | |
| 87 | + newIndex: 6, | |
| 88 | + newCharIndexInLine: 6, | |
| 89 | + newLineIndex: 0, | |
| 90 | + }, | |
| 91 | + { | |
| 92 | + type: "same", | |
| 93 | + char: "r", | |
| 94 | + oldIndex: 8, | |
| 95 | + newIndex: 8, | |
| 96 | + oldCharIndexInLine: 8, | |
| 97 | + newCharIndexInLine: 8, | |
| 98 | + oldLineIndex: 0, | |
| 99 | + newLineIndex: 0, | |
| 100 | + }, | |
| 101 | + { | |
| 102 | + type: "old", | |
| 103 | + char: "ld", | |
| 104 | + oldIndex: 9, | |
| 105 | + oldCharIndexInLine: 9, | |
| 106 | + oldLineIndex: 0, | |
| 107 | + }, | |
| 108 | + { | |
| 109 | + type: "new", | |
| 110 | + char: "th", | |
| 111 | + newIndex: 9, | |
| 112 | + newCharIndexInLine: 9, | |
| 113 | + newLineIndex: 0, | |
| 114 | + }, | |
| 115 | + ]); | |
| 116 | + }); | |
| 117 | + | |
| 118 | + test("should handle insertions", () => { | |
| 119 | + const oldContent = "abc"; | |
| 120 | + const newContent = "abxyzc"; | |
| 121 | + | |
| 122 | + const diffChars = myersCharDiff(oldContent, newContent); | |
| 123 | + expect(diffChars).toEqual([ | |
| 124 | + { | |
| 125 | + type: "same", | |
| 126 | + char: "ab", | |
| 127 | + oldIndex: 0, | |
| 128 | + newIndex: 0, | |
| 129 | + oldCharIndexInLine: 0, | |
| 130 | + newCharIndexInLine: 0, | |
| 131 | + oldLineIndex: 0, | |
| 132 | + newLineIndex: 0, | |
| 133 | + }, | |
| 134 | + { | |
| 135 | + type: "new", | |
| 136 | + char: "xyz", | |
| 137 | + newIndex: 2, | |
| 138 | + newCharIndexInLine: 2, | |
| 139 | + newLineIndex: 0, | |
| 140 | + }, | |
| 141 | + { | |
| 142 | + type: "same", | |
| 143 | + char: "c", | |
| 144 | + oldIndex: 2, | |
| 145 | + newIndex: 5, | |
| 146 | + oldCharIndexInLine: 2, | |
| 147 | + newCharIndexInLine: 5, | |
| 148 | + oldLineIndex: 0, | |
| 149 | + newLineIndex: 0, | |
| 150 | + }, | |
| 151 | + ]); | |
| 152 | + }); | |
| 153 | + | |
| 154 | + test("should handle deletions", () => { | |
| 155 | + const oldContent = "abxyzc"; | |
| 156 | + const newContent = "abc"; | |
| 157 | + | |
| 158 | + const diffChars = myersCharDiff(oldContent, newContent); | |
| 159 | + expect(diffChars).toEqual([ | |
| 160 | + { | |
| 161 | + type: "same", | |
| 162 | + char: "ab", | |
| 163 | + oldIndex: 0, | |
| 164 | + newIndex: 0, | |
| 165 | + oldCharIndexInLine: 0, | |
| 166 | + newCharIndexInLine: 0, | |
| 167 | + oldLineIndex: 0, | |
| 168 | + newLineIndex: 0, | |
| 169 | + }, | |
| 170 | + { | |
| 171 | + type: "old", | |
| 172 | + char: "xyz", | |
| 173 | + oldIndex: 2, | |
| 174 | + oldCharIndexInLine: 2, | |
| 175 | + oldLineIndex: 0, | |
| 176 | + }, | |
| 177 | + { | |
| 178 | + type: "same", | |
| 179 | + char: "c", | |
| 180 | + oldIndex: 5, | |
| 181 | + newIndex: 2, | |
| 182 | + oldCharIndexInLine: 5, | |
| 183 | + newCharIndexInLine: 2, | |
| 184 | + oldLineIndex: 0, | |
| 185 | + newLineIndex: 0, | |
| 186 | + }, | |
| 187 | + ]); | |
| 188 | + }); | |
| 189 | + | |
| 190 | + test("should handle empty strings", () => { | |
| 191 | + const oldContent = ""; | |
| 192 | + const newContent = "abc"; | |
| 193 | + | |
| 194 | + const diffChars = myersCharDiff(oldContent, newContent); | |
| 195 | + expect(diffChars).toEqual([ | |
| 196 | + { | |
| 197 | + type: "new", | |
| 198 | + char: "abc", | |
| 199 | + newIndex: 0, | |
| 200 | + newCharIndexInLine: 0, | |
| 201 | + newLineIndex: 0, | |
| 202 | + }, | |
| 203 | + ]); | |
| 204 | + }); | |
| 205 | + | |
| 206 | + test("should handle identical strings", () => { | |
| 207 | + const content = "no changes here"; | |
| 208 | + | |
| 209 | + const diffChars = myersCharDiff(content, content); | |
| 210 | + expect(diffChars).toEqual([ | |
| 211 | + { | |
| 212 | + type: "same", | |
| 213 | + char: "no changes here", | |
| 214 | + oldIndex: 0, | |
| 215 | + newIndex: 0, | |
| 216 | + oldCharIndexInLine: 0, | |
| 217 | + newCharIndexInLine: 0, | |
| 218 | + oldLineIndex: 0, | |
| 219 | + newLineIndex: 0, | |
| 220 | + }, | |
| 221 | + ]); | |
| 222 | + }); | |
| 223 | + | |
| 224 | + test("should handle whitespace changes", () => { | |
| 225 | + const oldContent = "hello world"; | |
| 226 | + const newContent = "hello world"; | |
| 227 | + | |
| 228 | + const diffChars = myersCharDiff(oldContent, newContent); | |
| 229 | + expect(diffChars).toEqual([ | |
| 230 | + { | |
| 231 | + type: "same", | |
| 232 | + char: "hello ", | |
| 233 | + oldIndex: 0, | |
| 234 | + newIndex: 0, | |
| 235 | + oldCharIndexInLine: 0, | |
| 236 | + newCharIndexInLine: 0, | |
| 237 | + oldLineIndex: 0, | |
| 238 | + newLineIndex: 0, | |
| 239 | + }, | |
| 240 | + { | |
| 241 | + type: "new", | |
| 242 | + char: " ", | |
| 243 | + newIndex: 6, | |
| 244 | + newCharIndexInLine: 6, | |
| 245 | + newLineIndex: 0, | |
| 246 | + }, | |
| 247 | + { | |
| 248 | + type: "same", | |
| 249 | + char: "world", | |
| 250 | + oldIndex: 6, | |
| 251 | + newIndex: 7, | |
| 252 | + oldCharIndexInLine: 6, | |
| 253 | + newCharIndexInLine: 7, | |
| 254 | + oldLineIndex: 0, | |
| 255 | + newLineIndex: 0, | |
| 256 | + }, | |
| 257 | + ]); | |
| 258 | + }); | |
| 259 | + | |
| 260 | + test("should handle complex changes", () => { | |
| 261 | + const oldContent = "The quick brown fox jumps over the lazy dog"; | |
| 262 | + const newContent = "The fast brown fox leaps over the sleeping dog"; | |
| 263 | + | |
| 264 | + const diffChars = myersCharDiff(oldContent, newContent); | |
| 265 | + expect(diffChars).toEqual([ | |
| 266 | + { | |
| 267 | + type: "same", | |
| 268 | + char: "The ", | |
| 269 | + oldIndex: 0, | |
| 270 | + newIndex: 0, | |
| 271 | + oldCharIndexInLine: 0, | |
| 272 | + newCharIndexInLine: 0, | |
| 273 | + oldLineIndex: 0, | |
| 274 | + newLineIndex: 0, | |
| 275 | + }, | |
| 276 | + { | |
| 277 | + type: "old", | |
| 278 | + char: "quick", | |
| 279 | + oldIndex: 4, | |
| 280 | + oldCharIndexInLine: 4, | |
| 281 | + oldLineIndex: 0, | |
| 282 | + }, | |
| 283 | + { | |
| 284 | + type: "new", | |
| 285 | + char: "fast", | |
| 286 | + newIndex: 4, | |
| 287 | + newCharIndexInLine: 4, | |
| 288 | + newLineIndex: 0, | |
| 289 | + }, | |
| 290 | + { | |
| 291 | + type: "same", | |
| 292 | + char: " brown fox ", | |
| 293 | + oldIndex: 9, | |
| 294 | + newIndex: 8, | |
| 295 | + oldCharIndexInLine: 9, | |
| 296 | + newCharIndexInLine: 8, | |
| 297 | + oldLineIndex: 0, | |
| 298 | + newLineIndex: 0, | |
| 299 | + }, | |
| 300 | + { | |
| 301 | + type: "old", | |
| 302 | + char: "jum", | |
| 303 | + oldIndex: 20, | |
| 304 | + oldCharIndexInLine: 20, | |
| 305 | + oldLineIndex: 0, | |
| 306 | + }, | |
| 307 | + { | |
| 308 | + type: "new", | |
| 309 | + char: "lea", | |
| 310 | + newIndex: 19, | |
| 311 | + newCharIndexInLine: 19, | |
| 312 | + newLineIndex: 0, | |
| 313 | + }, | |
| 314 | + { | |
| 315 | + type: "same", | |
| 316 | + char: "ps over the ", | |
| 317 | + oldIndex: 23, | |
| 318 | + newIndex: 22, | |
| 319 | + oldCharIndexInLine: 23, | |
| 320 | + newCharIndexInLine: 22, | |
| 321 | + oldLineIndex: 0, | |
| 322 | + newLineIndex: 0, | |
| 323 | + }, | |
| 324 | + { | |
| 325 | + type: "new", | |
| 326 | + char: "s", | |
| 327 | + newIndex: 34, | |
| 328 | + newCharIndexInLine: 34, | |
| 329 | + newLineIndex: 0, | |
| 330 | + }, | |
| 331 | + { | |
| 332 | + type: "same", | |
| 333 | + char: "l", | |
| 334 | + oldIndex: 35, | |
| 335 | + newIndex: 35, | |
| 336 | + oldCharIndexInLine: 35, | |
| 337 | + newCharIndexInLine: 35, | |
| 338 | + oldLineIndex: 0, | |
| 339 | + newLineIndex: 0, | |
| 340 | + }, | |
| 341 | + { | |
| 342 | + type: "old", | |
| 343 | + char: "azy", | |
| 344 | + oldIndex: 36, | |
| 345 | + oldCharIndexInLine: 36, | |
| 346 | + oldLineIndex: 0, | |
| 347 | + }, | |
| 348 | + { | |
| 349 | + type: "new", | |
| 350 | + char: "eeping", | |
| 351 | + newIndex: 36, | |
| 352 | + newCharIndexInLine: 36, | |
| 353 | + newLineIndex: 0, | |
| 354 | + }, | |
| 355 | + { | |
| 356 | + type: "same", | |
| 357 | + char: " dog", | |
| 358 | + oldIndex: 39, | |
| 359 | + newIndex: 42, | |
| 360 | + oldCharIndexInLine: 39, | |
| 361 | + newCharIndexInLine: 42, | |
| 362 | + oldLineIndex: 0, | |
| 363 | + newLineIndex: 0, | |
| 364 | + }, | |
| 365 | + ]); | |
| 366 | + }); | |
| 367 | +}); | |
| 368 | + | |
| 369 | +describe("Test myersCharDiff function on different lines", () => { | |
| 370 | + test("should track line indices for multi-line changes", () => { | |
| 371 | + const oldContent = ["Line one", "Line two", "Line three"].join("\n"); | |
| 372 | + | |
| 373 | + const newContent = ["Line one", "Modified line", "Line three"].join("\n"); | |
| 374 | + | |
| 375 | + const diffChars = myersCharDiff(oldContent, newContent); | |
| 376 | + expect(diffChars).toEqual([ | |
| 377 | + { | |
| 378 | + type: "same", | |
| 379 | + char: "Line one", | |
| 380 | + oldIndex: 0, | |
| 381 | + newIndex: 0, | |
| 382 | + oldCharIndexInLine: 0, | |
| 383 | + newCharIndexInLine: 0, | |
| 384 | + oldLineIndex: 0, | |
| 385 | + newLineIndex: 0, | |
| 386 | + }, | |
| 387 | + { | |
| 388 | + type: "same", | |
| 389 | + char: "\n", | |
| 390 | + oldIndex: 8, | |
| 391 | + newIndex: 8, | |
| 392 | + oldCharIndexInLine: 8, | |
| 393 | + newCharIndexInLine: 8, | |
| 394 | + oldLineIndex: 0, | |
| 395 | + newLineIndex: 0, | |
| 396 | + }, | |
| 397 | + { | |
| 398 | + type: "old", | |
| 399 | + char: "L", | |
| 400 | + oldIndex: 9, | |
| 401 | + oldCharIndexInLine: 0, | |
| 402 | + oldLineIndex: 1, | |
| 403 | + }, | |
| 404 | + { | |
| 405 | + type: "new", | |
| 406 | + char: "Mod", | |
| 407 | + newIndex: 9, | |
| 408 | + newCharIndexInLine: 0, | |
| 409 | + newLineIndex: 1, | |
| 410 | + }, | |
| 411 | + { | |
| 412 | + char: "i", | |
| 413 | + newCharIndexInLine: 3, | |
| 414 | + newIndex: 12, | |
| 415 | + newLineIndex: 1, | |
| 416 | + oldCharIndexInLine: 1, | |
| 417 | + oldIndex: 10, | |
| 418 | + oldLineIndex: 1, | |
| 419 | + type: "same", | |
| 420 | + }, | |
| 421 | + { | |
| 422 | + char: "n", | |
| 423 | + oldCharIndexInLine: 2, | |
| 424 | + oldIndex: 11, | |
| 425 | + oldLineIndex: 1, | |
| 426 | + type: "old", | |
| 427 | + }, | |
| 428 | + { | |
| 429 | + char: "fi", | |
| 430 | + newCharIndexInLine: 4, | |
| 431 | + newIndex: 13, | |
| 432 | + newLineIndex: 1, | |
| 433 | + type: "new", | |
| 434 | + }, | |
| 435 | + { | |
| 436 | + char: "e", | |
| 437 | + newCharIndexInLine: 6, | |
| 438 | + newIndex: 15, | |
| 439 | + newLineIndex: 1, | |
| 440 | + oldCharIndexInLine: 3, | |
| 441 | + oldIndex: 12, | |
| 442 | + oldLineIndex: 1, | |
| 443 | + type: "same", | |
| 444 | + }, | |
| 445 | + { | |
| 446 | + char: "d", | |
| 447 | + newCharIndexInLine: 7, | |
| 448 | + newIndex: 16, | |
| 449 | + newLineIndex: 1, | |
| 450 | + type: "new", | |
| 451 | + }, | |
| 452 | + { | |
| 453 | + char: " ", | |
| 454 | + newCharIndexInLine: 8, | |
| 455 | + newIndex: 17, | |
| 456 | + newLineIndex: 1, | |
| 457 | + oldCharIndexInLine: 4, | |
| 458 | + oldIndex: 13, | |
| 459 | + oldLineIndex: 1, | |
| 460 | + type: "same", | |
| 461 | + }, | |
| 462 | + { | |
| 463 | + type: "old", | |
| 464 | + char: "two", | |
| 465 | + oldCharIndexInLine: 5, | |
| 466 | + oldIndex: 14, | |
| 467 | + oldLineIndex: 1, | |
| 468 | + }, | |
| 469 | + { | |
| 470 | + type: "new", | |
| 471 | + char: "line", | |
| 472 | + newCharIndexInLine: 9, | |
| 473 | + newIndex: 18, | |
| 474 | + newLineIndex: 1, | |
| 475 | + }, | |
| 476 | + { | |
| 477 | + type: "same", | |
| 478 | + char: "\n", | |
| 479 | + oldIndex: 17, | |
| 480 | + oldCharIndexInLine: 8, | |
| 481 | + oldLineIndex: 1, | |
| 482 | + newIndex: 22, | |
| 483 | + newCharIndexInLine: 13, | |
| 484 | + newLineIndex: 1, | |
| 485 | + }, | |
| 486 | + { | |
| 487 | + type: "same", | |
| 488 | + char: "Line three", | |
| 489 | + oldIndex: 18, | |
| 490 | + newIndex: 23, | |
| 491 | + oldCharIndexInLine: 0, | |
| 492 | + newCharIndexInLine: 0, | |
| 493 | + oldLineIndex: 2, | |
| 494 | + newLineIndex: 2, | |
| 495 | + }, | |
| 496 | + ]); | |
| 497 | + }); | |
| 498 | + | |
| 499 | + test("should track line indices when adding new lines", () => { | |
| 500 | + const oldContent = ["First line", "Last line"].join("\n"); | |
| 501 | + | |
| 502 | + const newContent = [ | |
| 503 | + "First line", | |
| 504 | + "Middle line", | |
| 505 | + "Another middle", | |
| 506 | + "Last line", | |
| 507 | + ].join("\n"); | |
| 508 | + | |
| 509 | + const diffChars = myersCharDiff(oldContent, newContent); | |
| 510 | + expect(diffChars).toEqual([ | |
| 511 | + { | |
| 512 | + type: "same", | |
| 513 | + char: "First line", | |
| 514 | + oldIndex: 0, | |
| 515 | + newIndex: 0, | |
| 516 | + oldCharIndexInLine: 0, | |
| 517 | + newCharIndexInLine: 0, | |
| 518 | + oldLineIndex: 0, | |
| 519 | + newLineIndex: 0, | |
| 520 | + }, | |
| 521 | + { | |
| 522 | + type: "same", | |
| 523 | + char: "\n", | |
| 524 | + oldIndex: 10, | |
| 525 | + newIndex: 10, | |
| 526 | + oldCharIndexInLine: 10, | |
| 527 | + newCharIndexInLine: 10, | |
| 528 | + oldLineIndex: 0, | |
| 529 | + newLineIndex: 0, | |
| 530 | + }, | |
| 531 | + { | |
| 532 | + type: "new", | |
| 533 | + char: "Middle line", | |
| 534 | + newIndex: 11, | |
| 535 | + newCharIndexInLine: 0, | |
| 536 | + newLineIndex: 1, | |
| 537 | + }, | |
| 538 | + { | |
| 539 | + type: "new", | |
| 540 | + char: "\n", | |
| 541 | + newIndex: 22, | |
| 542 | + newCharIndexInLine: 11, | |
| 543 | + newLineIndex: 1, | |
| 544 | + }, | |
| 545 | + { | |
| 546 | + type: "new", | |
| 547 | + char: "Another middle", | |
| 548 | + newCharIndexInLine: 0, | |
| 549 | + newIndex: 23, | |
| 550 | + newLineIndex: 2, | |
| 551 | + }, | |
| 552 | + { | |
| 553 | + type: "new", | |
| 554 | + char: "\n", | |
| 555 | + newCharIndexInLine: 14, | |
| 556 | + newIndex: 37, | |
| 557 | + newLineIndex: 2, | |
| 558 | + }, | |
| 559 | + { | |
| 560 | + type: "same", | |
| 561 | + char: "Last line", | |
| 562 | + oldIndex: 11, | |
| 563 | + oldCharIndexInLine: 0, | |
| 564 | + oldLineIndex: 1, | |
| 565 | + newIndex: 38, | |
| 566 | + newCharIndexInLine: 0, | |
| 567 | + newLineIndex: 3, | |
| 568 | + }, | |
| 569 | + ]); | |
| 570 | + }); | |
| 571 | + | |
| 572 | + test("should track line indices when removing lines", () => { | |
| 573 | + const oldContent = [ | |
| 574 | + "Start", | |
| 575 | + "Line to remove", | |
| 576 | + "Another to remove", | |
| 577 | + "End", | |
| 578 | + ].join("\n"); | |
| 579 | + | |
| 580 | + const newContent = ["Start", "End"].join("\n"); | |
| 581 | + | |
| 582 | + const diffChars = myersCharDiff(oldContent, newContent); | |
| 583 | + expect(diffChars).toEqual([ | |
| 584 | + { | |
| 585 | + type: "same", | |
| 586 | + char: "Start", | |
| 587 | + oldIndex: 0, | |
| 588 | + newIndex: 0, | |
| 589 | + oldCharIndexInLine: 0, | |
| 590 | + newCharIndexInLine: 0, | |
| 591 | + oldLineIndex: 0, | |
| 592 | + newLineIndex: 0, | |
| 593 | + }, | |
| 594 | + { | |
| 595 | + type: "same", | |
| 596 | + char: "\n", | |
| 597 | + oldIndex: 5, | |
| 598 | + newIndex: 5, | |
| 599 | + oldCharIndexInLine: 5, | |
| 600 | + newCharIndexInLine: 5, | |
| 601 | + oldLineIndex: 0, | |
| 602 | + newLineIndex: 0, | |
| 603 | + }, | |
| 604 | + { | |
| 605 | + type: "old", | |
| 606 | + char: "Line to remove", | |
| 607 | + oldIndex: 6, | |
| 608 | + oldCharIndexInLine: 0, | |
| 609 | + oldLineIndex: 1, | |
| 610 | + }, | |
| 611 | + { | |
| 612 | + type: "old", | |
| 613 | + char: "\n", | |
| 614 | + oldCharIndexInLine: 14, | |
| 615 | + oldIndex: 20, | |
| 616 | + oldLineIndex: 1, | |
| 617 | + }, | |
| 618 | + { | |
| 619 | + type: "old", | |
| 620 | + char: "Another to remove", | |
| 621 | + oldCharIndexInLine: 0, | |
| 622 | + oldIndex: 21, | |
| 623 | + oldLineIndex: 2, | |
| 624 | + }, | |
| 625 | + { | |
| 626 | + type: "old", | |
| 627 | + char: "\n", | |
| 628 | + oldCharIndexInLine: 17, | |
| 629 | + oldIndex: 38, | |
| 630 | + oldLineIndex: 2, | |
| 631 | + }, | |
| 632 | + { | |
| 633 | + type: "same", | |
| 634 | + char: "End", | |
| 635 | + oldIndex: 39, | |
| 636 | + newIndex: 6, | |
| 637 | + oldCharIndexInLine: 0, | |
| 638 | + newCharIndexInLine: 0, | |
| 639 | + oldLineIndex: 3, | |
| 640 | + newLineIndex: 1, | |
| 641 | + }, | |
| 642 | + ]); | |
| 643 | + }); | |
| 644 | +}); |
modified core/diff/streamDiff.ts +2 −2
| @@ -1,4 +1,4 @@ | ||
| 1 | -import { DiffLine, DiffLineType } from "../index.js"; | |
| 1 | +import { DiffLine, DiffType } from "../index.js"; | |
| 2 | 2 | |
| 3 | 3 | import { LineStream, matchLine } from "./util.js"; |
| 4 | 4 | |
| @@ -33,7 +33,7 @@ | ||
| 33 | 33 | seenIndentationMistake = true; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - let type: DiffLineType; | |
| 36 | + let type: DiffType; | |
| 37 | 37 | |
| 38 | 38 | const isNewLine = matchIndex === -1; |
| 39 | 39 |
modified core/diff/streamDiff.vitest.ts +2 −2
| @@ -6,11 +6,11 @@ | ||
| 6 | 6 | // @ts-ignore no typings available |
| 7 | 7 | import { changed, diff as myersDiff } from "myers-diff"; |
| 8 | 8 | import { streamDiff } from "../diff/streamDiff.js"; |
| 9 | -import { DiffLine, DiffLineType } from "../index.js"; | |
| 9 | +import { DiffLine, DiffType } from "../index.js"; | |
| 10 | 10 | import { generateLines } from "./util.js"; |
| 11 | 11 | |
| 12 | 12 | // "modification" is an extra type used to represent an "old" + "new" diff line |
| 13 | -type MyersDiffTypes = Extract<DiffLineType, "new" | "old"> | "modification"; | |
| 13 | +type MyersDiffTypes = Extract<DiffType, "new" | "old"> | "modification"; | |
| 14 | 14 | |
| 15 | 15 | const UNIFIED_DIFF_SYMBOLS = { |
| 16 | 16 | same: "", |
modified core/index.d.ts +16 −3
| @@ -666,13 +666,26 @@ | ||
| 666 | 666 | |
| 667 | 667 | // IDE |
| 668 | 668 | |
| 669 | -export type DiffLineType = "new" | "old" | "same"; | |
| 669 | +export type DiffType = "new" | "old" | "same"; | |
| 670 | 670 | |
| 671 | -export interface DiffLine { | |
| 672 | - type: DiffLineType; | |
| 671 | +export interface DiffObject { | |
| 672 | + type: DiffType; | |
| 673 | +} | |
| 674 | + | |
| 675 | +export interface DiffLine extends DiffObject { | |
| 673 | 676 | line: string; |
| 674 | 677 | } |
| 675 | 678 | |
| 679 | +interface DiffChar extends DiffObject { | |
| 680 | + char: string; | |
| 681 | + oldIndex?: number; // Character index assuming a flattened line string. | |
| 682 | + newIndex?: number; | |
| 683 | + oldCharIndexInLine?: number; // Character index assuming new lines reset the character index to 0. | |
| 684 | + newCharIndexInLine?: number; | |
| 685 | + oldLineIndex?: number; | |
| 686 | + newLineIndex?: number; | |
| 687 | +} | |
| 688 | + | |
| 676 | 689 | export interface Problem { |
| 677 | 690 | filepath: string; |
| 678 | 691 | range: Range; |
modified core/nextEdit/constants.ts +1 −1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | export const IS_NEXT_EDIT_ACTIVE = false; |
| 2 | -export const NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN = 5; | |
| 2 | +export const NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN = 0; | |
| 3 | 3 | export const NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN = 5; |
| 4 | 4 | export const USER_CURSOR_IS_HERE_TOKEN = "<|user_cursor_is_here|>"; |
| 5 | 5 | export const EDITABLE_REGION_START_TOKEN = "<|editable_region_start|>"; |
modified extensions/vscode/src/activation/NextEditWindowManager.ts +88 −16
| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | // @ts-ignore |
| 4 | 4 | import * as vscode from "vscode"; |
| 5 | 5 | |
| 6 | -import { DiffLine } from "core"; | |
| 6 | +import { DiffChar, DiffLine } from "core"; | |
| 7 | 7 | import { CodeRenderer } from "core/codeRenderer/CodeRenderer"; |
| 8 | +import { myersCharDiff } from "core/diff/myers"; | |
| 8 | 9 | import { |
| 9 | 10 | NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, |
| 10 | 11 | NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, |
| @@ -295,14 +296,19 @@ | ||
| 295 | 296 | ); |
| 296 | 297 | |
| 297 | 298 | // Create and apply decoration with the text. |
| 298 | - await this.renderTooltip( | |
| 299 | + await this.renderWindow( | |
| 299 | 300 | editor, |
| 300 | 301 | currCursorPos, |
| 301 | 302 | oldEditRangeSlice, |
| 302 | 303 | newEditRangeSlice, |
| 303 | 304 | editableRegionStartLine, |
| 305 | + diffLines, | |
| 304 | 306 | ); |
| 305 | 307 | |
| 308 | + const diffChars = myersCharDiff(oldEditRangeSlice, newEditRangeSlice); | |
| 309 | + | |
| 310 | + this.renderDeletes(editor, editableRegionStartLine, diffChars); | |
| 311 | + | |
| 306 | 312 | // Reserve tab and esc to either accept or reject the displayed next edit contents. |
| 307 | 313 | await NextEditWindowManager.reserveTabAndEsc(); |
| 308 | 314 | } |
| @@ -531,6 +537,7 @@ | ||
| 531 | 537 | private async createCodeRender( |
| 532 | 538 | text: string, |
| 533 | 539 | currLineOffsetFromTop: number, |
| 540 | + newDiffLines: DiffLine[], | |
| 534 | 541 | ): Promise< |
| 535 | 542 | | { uri: vscode.Uri; dimensions: { width: number; height: number } } |
| 536 | 543 | | undefined |
| @@ -546,14 +553,15 @@ | ||
| 546 | 553 | const uri = await this.codeRenderer.getDataUri( |
| 547 | 554 | text, |
| 548 | 555 | "typescript", |
| 549 | - this.fontSize, | |
| 550 | - this.fontFamily, | |
| 551 | - dimensions, | |
| 552 | - SVG_CONFIG.lineHeight, | |
| 553 | 556 | { |
| 554 | 557 | imageType: "svg", |
| 558 | + fontSize: this.fontSize, | |
| 559 | + fontFamily: this.fontFamily, | |
| 560 | + dimensions: dimensions, | |
| 561 | + lineHeight: SVG_CONFIG.lineHeight, | |
| 555 | 562 | }, |
| 556 | 563 | currLineOffsetFromTop, |
| 564 | + newDiffLines, | |
| 557 | 565 | ); |
| 558 | 566 | |
| 559 | 567 | return { |
| @@ -576,11 +584,13 @@ | ||
| 576 | 584 | predictedCode: string, |
| 577 | 585 | position: vscode.Position, |
| 578 | 586 | editableRegionStartLine: number, |
| 587 | + newDiffLines: DiffLine[], | |
| 579 | 588 | ): Promise<vscode.TextEditorDecorationType | undefined> { |
| 580 | 589 | const currLineOffsetFromTop = position.line - editableRegionStartLine; |
| 581 | 590 | const uriAndDimensions = await this.createCodeRender( |
| 582 | 591 | predictedCode, |
| 583 | 592 | currLineOffsetFromTop, |
| 593 | + newDiffLines, | |
| 584 | 594 | ); |
| 585 | 595 | if (!uriAndDimensions) { |
| 586 | 596 | return undefined; |
| @@ -598,12 +608,12 @@ | ||
| 598 | 608 | SVG_CONFIG.getTipWidth(originalCode) - |
| 599 | 609 | SVG_CONFIG.getTipWidth(originalCode.split("\n")[currLineOffsetFromTop]); |
| 600 | 610 | |
| 601 | - console.log(marginLeft); | |
| 602 | - console.log(SVG_CONFIG.getTipWidth(originalCode)); | |
| 603 | - console.log( | |
| 604 | - SVG_CONFIG.getTipWidth(originalCode.split("\n")[currLineOffsetFromTop]), | |
| 605 | - ); | |
| 606 | - console.log(originalCode.split("\n")[currLineOffsetFromTop]); | |
| 611 | + // console.log(marginLeft); | |
| 612 | + // console.log(SVG_CONFIG.getTipWidth(originalCode)); | |
| 613 | + // console.log( | |
| 614 | + // SVG_CONFIG.getTipWidth(originalCode.split("\n")[currLineOffsetFromTop]), | |
| 615 | + // ); | |
| 616 | + // console.log(originalCode.split("\n")[currLineOffsetFromTop]); | |
| 607 | 617 | return vscode.window.createTextEditorDecorationType({ |
| 608 | 618 | before: { |
| 609 | 619 | contentIconPath: uri, |
| @@ -703,16 +713,16 @@ | ||
| 703 | 713 | } |
| 704 | 714 | |
| 705 | 715 | /** |
| 706 | - * Render a tooltip with the given text at the specified position. | |
| 716 | + * Render a window with the given text at the specified position. | |
| 707 | 717 | */ |
| 708 | - private async renderTooltip( | |
| 718 | + private async renderWindow( | |
| 709 | 719 | editor: vscode.TextEditor, |
| 710 | 720 | position: vscode.Position, |
| 711 | 721 | originalCode: string, |
| 712 | 722 | predictedCode: string, |
| 713 | 723 | editableRegionStartLine: number, |
| 724 | + newDiffLines: DiffLine[], | |
| 714 | 725 | ) { |
| 715 | - console.log("renderTooltip"); | |
| 716 | 726 | // Capture document version to detect changes. |
| 717 | 727 | const docVersion = editor.document.version; |
| 718 | 728 | |
| @@ -722,6 +732,7 @@ | ||
| 722 | 732 | predictedCode, |
| 723 | 733 | position, |
| 724 | 734 | editableRegionStartLine, |
| 735 | + newDiffLines, | |
| 725 | 736 | ); |
| 726 | 737 | if (!decoration) { |
| 727 | 738 | console.error("Failed to create decoration for text:", predictedCode); |
| @@ -737,7 +748,8 @@ | ||
| 737 | 748 | |
| 738 | 749 | // Store the decoration and editor. |
| 739 | 750 | await this.hideAllNextEditWindows(); |
| 740 | - this.currentDecoration = decoration; | |
| 751 | + this.currentDecoration = decoration; // TODO: This might be redundant. | |
| 752 | + this.disposables.push(decoration); | |
| 741 | 753 | this.activeEditor = editor; |
| 742 | 754 | |
| 743 | 755 | // Calculate how far off to the right of the cursor the decoration should be. |
| @@ -773,6 +785,66 @@ | ||
| 773 | 785 | this.mostRecentCompletionId, |
| 774 | 786 | ); |
| 775 | 787 | } |
| 788 | + | |
| 789 | + private renderDeletes( | |
| 790 | + editor: vscode.TextEditor, | |
| 791 | + editableRegionStartLine: number, | |
| 792 | + // oldEditRangeSlice: string, | |
| 793 | + // newEditRangeSlice: string, | |
| 794 | + oldDiffChars: DiffChar[], | |
| 795 | + ) { | |
| 796 | + const charsToDelete: vscode.DecorationOptions[] = []; | |
| 797 | + | |
| 798 | + // const diffChars = myersCharDiff(oldEditRangeSlice, newEditRangeSlice); | |
| 799 | + | |
| 800 | + oldDiffChars.forEach((diff) => { | |
| 801 | + // TODO: This check if technically redundant. | |
| 802 | + if (diff.type === "old") { | |
| 803 | + charsToDelete.push({ | |
| 804 | + range: new vscode.Range( | |
| 805 | + new vscode.Position( | |
| 806 | + editableRegionStartLine + diff.oldLineIndex!, | |
| 807 | + diff.oldCharIndexInLine!, | |
| 808 | + ), | |
| 809 | + new vscode.Position( | |
| 810 | + editableRegionStartLine + diff.oldLineIndex!, | |
| 811 | + diff.oldCharIndexInLine! + diff.char.length, | |
| 812 | + ), | |
| 813 | + ), | |
| 814 | + }); | |
| 815 | + } | |
| 816 | + }); | |
| 817 | + | |
| 818 | + const deleteDecorationType = vscode.window.createTextEditorDecorationType({ | |
| 819 | + backgroundColor: "rgba(255, 0, 0, 0.5)", | |
| 820 | + textDecoration: "line-through", | |
| 821 | + }); | |
| 822 | + | |
| 823 | + editor.setDecorations(deleteDecorationType, charsToDelete); | |
| 824 | + this.disposables.push(deleteDecorationType); | |
| 825 | + } | |
| 826 | + | |
| 827 | + async getExactCharacterWidth(): Promise<number> { | |
| 828 | + // For VS Code extensions, you can sometimes access the editor's text metrics | |
| 829 | + const activeEditor = vscode.window.activeTextEditor; | |
| 830 | + if (activeEditor) { | |
| 831 | + // VS Code has internal methods to measure text, but they're not all exposed | |
| 832 | + // in the public API. You might need to use reflection or known properties. | |
| 833 | + | |
| 834 | + // Example accessing through reflection (this is pseudocode) | |
| 835 | + const editorInstance = activeEditor as any; | |
| 836 | + if (editorInstance._modelData && editorInstance._modelData.viewModel) { | |
| 837 | + const viewModel = editorInstance._modelData.viewModel; | |
| 838 | + return ( | |
| 839 | + viewModel.getLineWidth(0) / | |
| 840 | + activeEditor.document.lineAt(0).text.length | |
| 841 | + ); | |
| 842 | + } | |
| 843 | + } | |
| 844 | + | |
| 845 | + // If all else fails, return a reasonable default | |
| 846 | + return SVG_CONFIG.fontSize * 0.6; | |
| 847 | + } | |
| 776 | 848 | } |
| 777 | 849 | |
| 778 | 850 | export default async function setupNextEditWindowManager( |