Skip to content

Commit 3004d99

Browse files
committed
nes: refactor: keep active document props in a single object
1 parent d8bff7c commit 3004d99

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { Position } from '../../../util/vs/editor/common/core/position';
7+
import { Lazy } from '../../../util/vs/base/common/lazy';
8+
import { StringText } from '../../../util/vs/editor/common/core/text/abstractText';
9+
10+
export class CurrentDocument {
11+
12+
public readonly lines: Lazy<string[]>;
13+
public readonly cursorOffset: number;
14+
15+
constructor(
16+
public readonly content: StringText,
17+
public readonly cursorPosition: Position,
18+
) {
19+
this.lines = new Lazy(() => content.getLines());
20+
this.cursorOffset = content.getTransformer().getOffset(cursorPosition);
21+
}
22+
}

src/extension/xtab/node/xtabProvider.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import { Position } from '../../../util/vs/editor/common/core/position';
4343
import { Range } from '../../../util/vs/editor/common/core/range';
4444
import { LineRange } from '../../../util/vs/editor/common/core/ranges/lineRange';
4545
import { OffsetRange } from '../../../util/vs/editor/common/core/ranges/offsetRange';
46-
import { StringText } from '../../../util/vs/editor/common/core/text/abstractText';
4746
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
4847
import { Position as VscodePosition } from '../../../vscodeTypes';
4948
import { Delayer, DelaySession } from '../../inlineEdits/common/delayer';
@@ -53,6 +52,7 @@ import { IgnoreImportChangesAspect } from '../../inlineEdits/node/importFilterin
5352
import { createTaggedCurrentFileContentUsingPagedClipping, getUserPrompt, N_LINES_ABOVE, N_LINES_AS_CONTEXT, N_LINES_BELOW, nes41Miniv3SystemPrompt, PromptPieces, PromptTags, simplifiedPrompt, systemPromptTemplate, unifiedModelSystemPrompt, xtab275SystemPrompt } from '../common/promptCrafting';
5453
import { XtabEndpoint } from './xtabEndpoint';
5554
import { linesWithBackticksRemoved, toLines } from './xtabUtils';
55+
import { CurrentDocument } from './xtabCurrentDocument';
5656

5757
namespace ResponseTags {
5858
export const NO_CHANGE = {
@@ -86,6 +86,8 @@ export class XtabProvider implements IStatelessNextEditProvider {
8686
public readonly dependsOnSelection = true;
8787
public readonly showNextEditPreference = ShowNextEditPreference.Always;
8888

89+
private static computeTokens = (s: string) => Math.floor(s.length / 4);
90+
8991
private readonly tracer: ITracer;
9092
private readonly delayer: Delayer;
9193

@@ -226,47 +228,40 @@ export class XtabProvider implements IStatelessNextEditProvider {
226228
logContext.setEndpointInfo(typeof endpoint.urlOrRequestMetadata === 'string' ? endpoint.urlOrRequestMetadata : JSON.stringify(endpoint.urlOrRequestMetadata.type), endpoint.model);
227229
telemetryBuilder.setModelName(endpoint.model);
228230

229-
const computeTokens = (s: string) => Math.floor(s.length / 4);
230-
231231
const cursorPosition = new Position(selection.endLineNumber, selection.endColumn);
232232

233-
const cursorOffset = activeDocument.documentAfterEdits.getTransformer().getOffset(cursorPosition);
234-
235-
const currentFileContent = activeDocument.documentAfterEdits;
236-
const currentFileContentLines = currentFileContent.getLines();
233+
const currentDocument = new CurrentDocument(activeDocument.documentAfterEdits, cursorPosition);
237234

238235
const cursorLineIdx = cursorPosition.lineNumber - 1 /* to convert to 0-based */;
239236

240-
const cursorLine = currentFileContentLines[cursorLineIdx];
237+
const cursorLine = currentDocument.lines.value[cursorLineIdx];
241238
const isCursorAtEndOfLine = cursorPosition.column === cursorLine.trimEnd().length;
242239
if (isCursorAtEndOfLine) {
243240
delaySession.setExtraDebounce(this.configService.getExperimentBasedConfig(ConfigKey.Internal.InlineEditsExtraDebounceEndOfLine, this.expService));
244241
}
245242
telemetryBuilder.setIsCursorAtLineEnd(isCursorAtEndOfLine);
246243

247-
const areaAroundEditWindowLinesRange = this.computeAreaAroundEditWindowLinesRange(currentFileContentLines, cursorLineIdx);
244+
const areaAroundEditWindowLinesRange = this.computeAreaAroundEditWindowLinesRange(currentDocument.lines.value, cursorLineIdx);
248245

249-
const editWindowLinesRange = this.computeEditWindowLinesRange(currentFileContentLines, cursorLineIdx, request, retryState);
246+
const editWindowLinesRange = this.computeEditWindowLinesRange(currentDocument.lines.value, cursorLineIdx, request, retryState);
250247

251248
const cursorOriginalLinesOffset = Math.max(0, cursorLineIdx - editWindowLinesRange.start);
252249
const editWindowLastLineLength = activeDocument.documentAfterEdits.getTransformer().getLineLength(editWindowLinesRange.endExclusive);
253250
const editWindow = activeDocument.documentAfterEdits.getTransformer().getOffsetRange(new Range(editWindowLinesRange.start + 1, 1, editWindowLinesRange.endExclusive, editWindowLastLineLength + 1));
254251

255-
const editWindowLines = currentFileContentLines.slice(editWindowLinesRange.start, editWindowLinesRange.endExclusive);
252+
const editWindowLines = currentDocument.lines.value.slice(editWindowLinesRange.start, editWindowLinesRange.endExclusive);
256253

257254
// Expected: editWindow.substring(activeDocument.documentAfterEdits.value) === editWindowLines.join('\n')
258255

259256
const doesIncludeCursorTag = editWindowLines.some(line => line.includes(PromptTags.CURSOR));
260257
const shouldRemoveCursorTagFromResponse = !doesIncludeCursorTag; // we'd like to remove the tag only if the original edit-window didn't include the tag
261258

262259
const taggedCurrentFileContentResult = this.constructTaggedFile(
263-
currentFileContent,
264-
currentFileContentLines,
265-
cursorOffset,
260+
currentDocument,
266261
editWindowLinesRange,
267262
areaAroundEditWindowLinesRange,
268263
promptOptions,
269-
computeTokens,
264+
XtabProvider.computeTokens,
270265
{ includeLineNumbers: false }
271266
);
272267

@@ -304,7 +299,7 @@ export class XtabProvider implements IStatelessNextEditProvider {
304299
taggedCurrentFileContent,
305300
areaAroundCodeToEdit,
306301
langCtx,
307-
computeTokens,
302+
XtabProvider.computeTokens,
308303
promptOptions
309304
);
310305

@@ -363,9 +358,7 @@ export class XtabProvider implements IStatelessNextEditProvider {
363358
}
364359

365360
private constructTaggedFile(
366-
currentFileContent: StringText,
367-
currentFileContentLines: string[],
368-
cursorOffset: number,
361+
currentDocument: CurrentDocument,
369362
editWindowLinesRange: OffsetRange,
370363
areaAroundEditWindowLinesRange: OffsetRange,
371364
promptOptions: ModelConfig,
@@ -375,8 +368,8 @@ export class XtabProvider implements IStatelessNextEditProvider {
375368
}
376369
) {
377370
const contentWithCursorAsLinesOriginal = (() => {
378-
const addCursorTagEdit = StringEdit.single(StringReplacement.insert(cursorOffset, PromptTags.CURSOR));
379-
const contentWithCursor = addCursorTagEdit.applyOnText(currentFileContent);
371+
const addCursorTagEdit = StringEdit.single(StringReplacement.insert(currentDocument.cursorOffset, PromptTags.CURSOR));
372+
const contentWithCursor = addCursorTagEdit.applyOnText(currentDocument.content);
380373
return contentWithCursor.getLines();
381374
})();
382375

@@ -398,9 +391,9 @@ export class XtabProvider implements IStatelessNextEditProvider {
398391
PromptTags.AREA_AROUND.end
399392
].join('\n');
400393

401-
currentFileContentLines = opts.includeLineNumbers
402-
? addLineNumbers(currentFileContentLines)
403-
: currentFileContentLines;
394+
const currentFileContentLines = opts.includeLineNumbers
395+
? addLineNumbers(currentDocument.lines.value)
396+
: currentDocument.lines.value;
404397

405398
let areaAroundCodeToEditForCurrentFile: string;
406399
if (promptOptions.currentFile.includeTags) {

0 commit comments

Comments
 (0)