Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompt/node/editGeneration.ts
13399 views
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 type * as vscode from 'vscode';
7
import { Range, TextEdit } from '../../../vscodeTypes';
8
9
export type Lines = readonly string[];
10
11
export type LineRange = { readonly firstLineIndex: number; readonly endLineIndex: number };
12
13
export class LinesEdit {
14
constructor(public readonly firstLineIndex: number, readonly endLineIndex: number, public readonly lines: Lines, public readonly prefix = '', public readonly suffix = '\n') {
15
}
16
toTextEdit(): TextEdit {
17
const text = this.lines.length > 0 ? (this.prefix + this.lines.join('\n') + this.suffix) : '';
18
return TextEdit.replace(new Range(this.firstLineIndex, 0, this.endLineIndex, 0), text);
19
}
20
apply(lines: Lines): Lines {
21
const before = lines.slice(0, this.firstLineIndex);
22
const after = lines.slice(this.endLineIndex);
23
return before.concat(this.lines, after);
24
}
25
static insert(line: number, lines: Lines) {
26
return new LinesEdit(line, line, lines);
27
}
28
static replace(firstLineIndex: number, endLineIndex: number, lines: Lines, isLastLine = false) {
29
if (isLastLine) {
30
return new LinesEdit(firstLineIndex, endLineIndex, lines, '', '');
31
}
32
return new LinesEdit(firstLineIndex, endLineIndex, lines);
33
}
34
}
35
36
export namespace Lines {
37
export function fromString(code: string): Lines {
38
if (code.length === 0) {
39
return [];
40
}
41
return code.split(/\r\n|\r|\n/g);
42
}
43
export function fromDocument(doc: vscode.TextDocument): Lines {
44
if (doc.lineCount === 0) {
45
return [];
46
}
47
const result: string[] = [];
48
for (let i = 0; i < doc.lineCount; i++) {
49
result.push(doc.lineAt(i).text);
50
}
51
return result;
52
}
53
}
54
55
export function isLines(lines: any): lines is Lines {
56
return Array.isArray(lines) && typeof lines[0] === 'string';
57
}
58
59
export const enum EditStrategy {
60
/**
61
* In case we have no hints (no code markers and no diffing heuristics)
62
* about the edits to generate, we can use a default strategy.
63
*/
64
FallbackToInsertAboveRange = 1,
65
/**
66
* In case we have no hints (no code markers and no diffing heuristics)
67
* about the edits to generate, we can use a default strategy.
68
*/
69
FallbackToReplaceRange = 2,
70
/**
71
* In case we have no hints (no code markers and no diffing heuristics)
72
* about the edits to generate, we can use a default strategy.
73
*/
74
FallbackToInsertBelowRange = 3,
75
/**
76
* Code Generation: always insert at the cursor location.
77
*/
78
ForceInsertion = 4
79
}
80
81
export function trimLeadingWhitespace(str: string): string {
82
return str.replace(/^\s+/g, '');
83
}
84
85
86