1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
import * as vscode from "vscode";
import * as path from "path";
import * as fs from "fs";
export function translate(range: vscode.Range, lines: number): vscode.Range {
return new vscode.Range(
range.start.line + lines,
range.start.character,
range.end.line + lines,
range.end.character
);
}
export function getNonce() {
let text = "";
const possible =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < 32; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
export function getTestFile(
filename: string,
createFile: boolean = false
): string {
let basename = path.basename(filename).split(".")[0];
switch (path.extname(filename)) {
case ".py":
basename += "_test";
break;
case ".js":
case ".jsx":
case ".ts":
case ".tsx":
basename += ".test";
break;
default:
basename += "_test";
}
const directory = path.join(path.dirname(filename), "tests");
const testFilename = path.join(directory, basename + path.extname(filename));
// Optionally, create the file if it doesn't exist
if (createFile && !fs.existsSync(testFilename)) {
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
fs.writeFileSync(testFilename, "");
}
return testFilename;
}
export function getExtensionUri(): vscode.Uri {
return vscode.extensions.getExtension("Continue.continue")!.extensionUri;
}
export function getViewColumnOfFile(
filepath: string
): vscode.ViewColumn | undefined {
for (let tabGroup of vscode.window.tabGroups.all) {
for (let tab of tabGroup.tabs) {
if (
(tab?.input as any)?.uri &&
(tab.input as any).uri.fsPath === filepath
) {
return tabGroup.viewColumn;
}
}
}
return undefined;
}
export function getRightViewColumn(): vscode.ViewColumn {
// When you want to place in the rightmost panel if there is already more than one, otherwise use Beside
let column = vscode.ViewColumn.Beside;
let columnOrdering = [
vscode.ViewColumn.One,
vscode.ViewColumn.Beside,
vscode.ViewColumn.Two,
vscode.ViewColumn.Three,
vscode.ViewColumn.Four,
vscode.ViewColumn.Five,
vscode.ViewColumn.Six,
vscode.ViewColumn.Seven,
vscode.ViewColumn.Eight,
vscode.ViewColumn.Nine,
];
for (let tabGroup of vscode.window.tabGroups.all) {
if (
columnOrdering.indexOf(tabGroup.viewColumn) >
columnOrdering.indexOf(column)
) {
column = tabGroup.viewColumn;
}
}
return column;
}
export async function readFileAtRange(
range: vscode.Range,
filepath: string
): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(filepath, (err, data) => {
if (err) {
reject(err);
} else {
let lines = data.toString().split("\n");
if (range.isSingleLine) {
resolve(
lines[range.start.line].slice(
range.start.character,
range.end.character
)
);
} else {
let firstLine = lines[range.start.line].slice(range.start.character);
let lastLine = lines[range.end.line].slice(0, range.end.character);
let middleLines = lines.slice(range.start.line + 1, range.end.line);
resolve([firstLine, ...middleLines, lastLine].join("\n"));
}
}
});
});
}
let showTextDocumentInProcess = false;
export function openEditorAndRevealRange(
editorFilename: string,
range?: vscode.Range,
viewColumn?: vscode.ViewColumn
): Promise<vscode.TextEditor> {
return new Promise((resolve, _) => {
vscode.workspace.openTextDocument(editorFilename).then(async (doc) => {
try {
// An error is thrown mysteriously if you open two documents in parallel, hence this
while (showTextDocumentInProcess) {
await new Promise((resolve) => {
setInterval(() => {
resolve(null);
}, 200);
})
}
showTextDocumentInProcess = true;
vscode.window
.showTextDocument(
doc,
getViewColumnOfFile(editorFilename) || viewColumn
)
.then((editor) => {
if (range) {
editor.revealRange(range);
}
resolve(editor);
showTextDocumentInProcess = false;
})
} catch (err) {
console.log(err);
}
});
});
}
|