Skip to content

Commit

Permalink
chore(audoedit): simplify diff utils and renderer data structures (#6172
Browse files Browse the repository at this point in the history
)

- No functional changes.
- Simplifies data structures used to calculate the diff between the
current document content and predicted edits.
- Simplifies data structures used to render autoedit decorations. 
- Removes redundant data structures and transformations happening
between the diff calculation and the renderer call.
- Adds granular `changes: LineChange` to `ModifiedLineInfo`,
representing individual insertions and deletions in modified lines. This
is helpful for troubleshooting purposes, and I plan to use it for the
experimental inline renderer implementation that we discussed recently
in Slack.
- Updates the default autoedits renderer to use all the above changes.
  • Loading branch information
valerybugakov authored Nov 22, 2024
1 parent bdf587d commit ffb6bcb
Show file tree
Hide file tree
Showing 9 changed files with 840 additions and 533 deletions.
75 changes: 44 additions & 31 deletions vscode/src/autoedits/renderer/decorators/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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.
Expand All @@ -8,63 +7,77 @@ import type { ModifiedRange } from '../diff-utils'
* that visualize proposed text changes in the editor.
*
* Lifecycle:
* - Single instance should be created per decoration session and disposed of when the decorations
* - A 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(...);
* const decorator = createAutoEditsDecorator(...);
* try {
* decorator.setDecorations(decorationInfo);
* ...
* } finally {
* decorator.clearDecorations();
* decorator.dispose();
* }
* ```
*/
export interface AutoeditsDecorator extends vscode.Disposable {
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
* @param decorationInfo Contains the line-by-line information about text changes
* and how they should be decorated in the editor.
*/
setDecorations(decorationInformation: DecorationInformation): void
setDecorations(decorationInfo: DecorationInfo): void
}

/**
* Represents the different types of line decorations that can be applied.
* Represents a line of text with its change type and content.
*/
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 type DecorationLineInfo = AddedLineInfo | RemovedLineInfo | ModifiedLineInfo | UnchangedLineInfo

export interface AddedLineInfo {
type: 'added'
text: string
/** `lineNumber` in the modified text */
lineNumber: number
}

export interface RemovedLineInfo {
type: 'removed'
text: string
/** `lineNumber` in the original text */
lineNumber: number
}

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.
export interface ModifiedLineInfo {
type: 'modified'
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[]
changes: LineChange[]
/** `lineNumber` in the modified text */
lineNumber: number
}

export interface UnchangedLineInfo {
type: 'unchanged'
text: string
/** `lineNumber` in the modified text */
lineNumber: number
}

export type LineChange = {
type: 'insert' | 'delete'
/** `range` in the modified text relative to the document start */
range: vscode.Range
text: string
}

export interface DecorationInformation {
lines: DecorationLineInformation[]
oldLines: string[]
newLines: string[]
export interface DecorationInfo {
modifiedLines: ModifiedLineInfo[]
removedLines: RemovedLineInfo[]
addedLines: AddedLineInfo[]
unchangedLines: UnchangedLineInfo[]
}
51 changes: 0 additions & 51 deletions vscode/src/autoedits/renderer/decorators/common.ts

This file was deleted.

Loading

0 comments on commit ffb6bcb

Please sign in to comment.