Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ interface DocState {
/** Arguments for {@link NextEditProvider._rebaseAndCacheStreamedEdit}. */
interface RebaseAndCacheStreamedEditArgs {
readonly statePerDoc: CachedFunction<DocumentId, DocState>;
/** Predicate identifying documents `statePerDoc` can resolve without throwing. */
readonly isKnownDocument: (id: DocumentId) => boolean;
readonly streamedEdit: StreamedEdit;
/** Zero-based index of this edit within the stream. */
readonly ithEdit: number;
Expand All @@ -127,7 +129,14 @@ interface RebaseAndCacheStreamedEditArgs {
readonly logger: ILogger;
}

function createDocStateLookupMap(projectedDocuments: readonly ProcessedDoc[], xtabEditHistory: readonly IXtabHistoryEntry[]): CachedFunction<DocumentId, DocState> {
function createDocStateLookupMap(projectedDocuments: readonly ProcessedDoc[], xtabEditHistory: readonly IXtabHistoryEntry[]): { statePerDoc: CachedFunction<DocumentId, DocState>; isKnownDocument: (id: DocumentId) => boolean } {
const isKnownDocument = (id: DocumentId): boolean => {
if (projectedDocuments.some(d => d.nextEditDoc.id === id)) {
return true;
}
return xtabEditHistory.some(entry => entry.docId === id && entry.kind === 'edit');
};

const statePerDoc = new CachedFunction((id: DocumentId) => {
const doc = projectedDocuments.find(d => d.nextEditDoc.id === id);
if (!doc) {
Expand Down Expand Up @@ -158,7 +167,7 @@ function createDocStateLookupMap(projectedDocuments: readonly ProcessedDoc[], xt
});


return statePerDoc;
return { statePerDoc, isKnownDocument };
}

/**
Expand Down Expand Up @@ -771,7 +780,16 @@ export class NextEditProvider extends Disposable implements INextEditProvider<Ne
* `firstEdit`, stream-end handling) stays with the callers.
*/
private _rebaseAndCacheStreamedEdit(args: RebaseAndCacheStreamedEditArgs): { lineEdit: LineEdit; rebasedEdit: StringEdit; docContentsBeforeEdit: StringText; cachedEdit: CachedOrRebasedEdit | undefined; crossFileCached: boolean } | undefined {
const { statePerDoc, streamedEdit, ithEdit, activeDoc, userEditSince, modelTelemetry, source, logger } = args;
const { statePerDoc, isKnownDocument, streamedEdit, ithEdit, activeDoc, userEditSince, modelTelemetry, source, logger } = args;

// The streamed edit's target document is model-produced and may reference a document
// that was never part of this request (neither a projected document nor an edit-history
// entry). Skip such edits instead of letting `statePerDoc.get` throw a BugIndicatingError;
// both callers already treat an `undefined` result as "edit could not be rebased".
if (!isKnownDocument(streamedEdit.targetDocument)) {
logger.trace(`skipping streamed edit ${ithEdit} for unknown target document "${streamedEdit.targetDocument.baseName}"`);
return undefined;
}

const targetDocState = statePerDoc.get(streamedEdit.targetDocument);

Expand Down Expand Up @@ -922,7 +940,7 @@ export class NextEditProvider extends Disposable implements INextEditProvider<Ne
}) : undefined);


const statePerDoc = createDocStateLookupMap(projectedDocuments, xtabEditHistory);
const { statePerDoc, isKnownDocument } = createDocStateLookupMap(projectedDocuments, xtabEditHistory);

const editStream = this._statelessNextEditProvider.provideNextEdit(nextEditRequest, logger, logContext, nextEditRequest.cancellationTokenSource.token);

Expand All @@ -944,6 +962,7 @@ export class NextEditProvider extends Disposable implements INextEditProvider<Ne

const cached = this._rebaseAndCacheStreamedEdit({
statePerDoc,
isKnownDocument,
streamedEdit,
ithEdit,
activeDoc: { id: curDocId, contents: nextEditRequest.documentBeforeEdits, cursorOffset: activeDocSelection?.start },
Expand Down Expand Up @@ -1451,7 +1470,7 @@ export class NextEditProvider extends Disposable implements INextEditProvider<Ne

const xtabEditHistory = nextEditRequest.xtabEditHistory;

const statePerDoc = createDocStateLookupMap(projectedDocuments, xtabEditHistory);
const { statePerDoc, isKnownDocument } = createDocStateLookupMap(projectedDocuments, xtabEditHistory);

const logContext = req.log;
const editStream = this._statelessNextEditProvider.provideNextEdit(
Expand Down Expand Up @@ -1483,6 +1502,7 @@ export class NextEditProvider extends Disposable implements INextEditProvider<Ne

const cached = this._rebaseAndCacheStreamedEdit({
statePerDoc,
isKnownDocument,
streamedEdit,
ithEdit,
activeDoc: { id: curDocId, contents: nextEditRequest.documentBeforeEdits, cursorOffset },
Expand Down
Loading