Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update inline completion handling based on modelVersion changes. Also apply to ghost text #239582

Merged
merged 9 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/vs/editor/common/core/offsetEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ export class OffsetEdit {
}
return postEditsOffset - accumulatedDelta;
}

equals(other: OffsetEdit): boolean {
if (this.edits.length !== other.edits.length) {
return false;
}
for (let i = 0; i < this.edits.length; i++) {
if (!this.edits[i].equals(other.edits[i])) {
return false;
}

}
return true;
}
}

export type IOffsetEdit = ISingleOffsetEdit[];
Expand Down Expand Up @@ -252,6 +265,10 @@ export class SingleOffsetEdit {
getRangeAfterApply(): OffsetRange {
return new OffsetRange(this.replaceRange.start, this.replaceRange.start + this.newText.length);
}

equals(other: SingleOffsetEdit): boolean {
return this.replaceRange.equals(other.replaceRange) && this.newText === other.newText;
}
}

/**
Expand Down Expand Up @@ -337,3 +354,70 @@ function joinEdits(edits1: OffsetEdit, edits2: OffsetEdit): OffsetEdit {

return new OffsetEdit(result).normalize();
}

export function applyEditsToRanges(sortedRanges: OffsetRange[], edits: OffsetEdit): OffsetRange[] {
sortedRanges = sortedRanges.slice();

// treat edits as deletion of the replace range and then as insertion that extends the first range
const result: OffsetRange[] = [];

let offset = 0;

for (const e of edits.edits) {
while (true) {
// ranges before the current edit
const r = sortedRanges[0];
if (!r || r.endExclusive >= e.replaceRange.start) {
break;
}
sortedRanges.shift();
result.push(r.delta(offset));
}

const intersecting: OffsetRange[] = [];
while (true) {
const r = sortedRanges[0];
if (!r || !r.intersectsOrTouches(e.replaceRange)) {
break;
}
sortedRanges.shift();
intersecting.push(r);
}

for (let i = intersecting.length - 1; i >= 0; i--) {
let r = intersecting[i];

const overlap = r.intersect(e.replaceRange)!.length;
r = r.deltaEnd(-overlap + (i === 0 ? e.newText.length : 0));

const rangeAheadOfReplaceRange = r.start - e.replaceRange.start;
if (rangeAheadOfReplaceRange > 0) {
r = r.delta(-rangeAheadOfReplaceRange);
}

if (i !== 0) {
r = r.delta(e.newText.length);
}

// We already took our offset into account.
// Because we add r back to the queue (which then adds offset again),
// we have to remove it here.
r = r.delta(-(e.newText.length - e.replaceRange.length));

sortedRanges.unshift(r);
}

offset += e.newText.length - e.replaceRange.length;
}

while (true) {
const r = sortedRanges[0];
if (!r) {
break;
}
sortedRanges.shift();
result.push(r.delta(offset));
}

return result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ export class InlineCompletionsModel extends Disposable {
return;
}
for (const inlineCompletion of inlineCompletions.inlineCompletions) {
const singleEdit = inlineCompletion.toSingleTextEdit(reader);
if (singleEdit.isEmpty) {
if (inlineCompletion.updatedEdit.read(reader) === undefined) {
this.stop();
break;
}
}
}));
Expand Down
Loading
Loading