-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(autoedit): refactor renderer code to simplify iteration on decor… (
#6163) The PR refactors the current autoedits renderer to use a common schema for which Decorations can use. Idea is to make it easy for anyone to try to implement a new decoration type without knowing the details on diff computation.
- Loading branch information
1 parent
d93cc15
commit bdf587d
Showing
13 changed files
with
740 additions
and
481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type * as vscode from 'vscode' | ||
import type { ModifiedRange } from '../diff-utils' | ||
|
||
/** | ||
* Represents a decorator that manages VS Code editor decorations for auto-edit suggestions. | ||
* | ||
* This interface defines the contract for displaying and managing decorative elements | ||
* that visualize proposed text changes in the editor. | ||
* | ||
* Lifecycle: | ||
* - Single instance should be created per decoration session and disposed of when the decorations | ||
* are no longer needed. | ||
* - Always call dispose() when the decorator is no longer needed to clean up resources. | ||
* - Dispose should always clear the decorations. | ||
* | ||
* Usage Pattern: | ||
* ```typescript | ||
* const decorator = createAutoeditsDecorator(...); | ||
* try { | ||
* decorator.setDecorations(decorationInfo); | ||
* ... | ||
* } finally { | ||
* decorator.clearDecorations(); | ||
* decorator.dispose(); | ||
* } | ||
* ``` | ||
*/ | ||
export interface AutoeditsDecorator extends vscode.Disposable { | ||
/** | ||
* Applies decorations to the editor based on the provided decoration information. | ||
* | ||
* @param decorationInformation Contains the line-by-line information about text changes | ||
* and how they should be decorated in the editor. | ||
*/ | ||
setDecorations(decorationInformation: DecorationInformation): void | ||
} | ||
|
||
/** | ||
* Represents the different types of line decorations that can be applied. | ||
*/ | ||
export enum DecorationLineType { | ||
/** Line has been modified from its original state */ | ||
Modified = 0, | ||
/** New line has been added */ | ||
Added = 1, | ||
/** Line has been removed */ | ||
Removed = 2, | ||
/** Line remains unchanged */ | ||
Unchanged = 3, | ||
} | ||
|
||
export interface DecorationLineInformation { | ||
lineType: DecorationLineType | ||
// Line number in the original text. The line number can be null if the line was added. | ||
oldLineNumber: number | null | ||
// Line number in the new predicted text. The line number can be null if the line was removed. | ||
newLineNumber: number | null | ||
// The text of the line in the original text. | ||
oldText: string | ||
// The text of the line in the new predicted text. | ||
newText: string | ||
// The ranges of text that were modified in the line. | ||
modifiedRanges: ModifiedRange[] | ||
} | ||
|
||
export interface DecorationInformation { | ||
lines: DecorationLineInformation[] | ||
oldLines: string[] | ||
newLines: string[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { type DecorationLineInformation, DecorationLineType } from './base' | ||
|
||
/** | ||
* Checks if the only changes for modified lines are additions of text | ||
*/ | ||
export function isOnlyAddingTextForModifiedLines( | ||
decorationInformation: DecorationLineInformation[] | ||
): boolean { | ||
for (const line of decorationInformation) { | ||
if (line.lineType !== DecorationLineType.Modified) { | ||
continue | ||
} | ||
if (line.modifiedRanges.some(range => range.from1 !== range.to1)) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
export function splitLineDecorationIntoLineTypes(decorationInformation: DecorationLineInformation[]): { | ||
modifiedLines: DecorationLineInformation[] | ||
removedLines: DecorationLineInformation[] | ||
addedLines: DecorationLineInformation[] | ||
unchangedLines: DecorationLineInformation[] | ||
} { | ||
const result = { | ||
modifiedLines: [] as DecorationLineInformation[], | ||
removedLines: [] as DecorationLineInformation[], | ||
addedLines: [] as DecorationLineInformation[], | ||
unchangedLines: [] as DecorationLineInformation[], | ||
} | ||
|
||
for (const line of decorationInformation) { | ||
switch (line.lineType) { | ||
case DecorationLineType.Modified: | ||
result.modifiedLines.push(line) | ||
break | ||
case DecorationLineType.Removed: | ||
result.removedLines.push(line) | ||
break | ||
case DecorationLineType.Added: | ||
result.addedLines.push(line) | ||
break | ||
case DecorationLineType.Unchanged: | ||
result.unchangedLines.push(line) | ||
break | ||
} | ||
} | ||
|
||
return result | ||
} |
2 changes: 1 addition & 1 deletion
2
vscode/src/autoedits/renderer.test.ts → ...erer/decorators/default-decorator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.