-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathuiUtil_vscode.ts
More file actions
130 lines (110 loc) · 4.2 KB
/
uiUtil_vscode.ts
File metadata and controls
130 lines (110 loc) · 4.2 KB
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
import * as vscode from 'vscode';
import ExtensionContextHolder from './extensionContext';
import { UiUtil } from './uiUtil';
import { logger } from './logger';
export class UiUtilVscode implements UiUtil {
public async languageId(uri: string): Promise<string> {
const document = await vscode.workspace.openTextDocument(uri);
return document.languageId;
}
public workspaceFoldersFirstPath(): string | undefined {
return vscode.workspace.workspaceFolders?.[0].uri.fsPath;
}
public getConfiguration(key1: string, key2: string): string | undefined {
return vscode.workspace.getConfiguration(key1).get(key2);
}
public async updateConfiguration(key1: string, key2: string, value: string): Promise<void> {
try {
await vscode.workspace.getConfiguration(key1).update(key2, value, vscode.ConfigurationTarget.Global);
await vscode.workspace.getConfiguration(key1).update(key2, value, vscode.ConfigurationTarget.Workspace);
} catch(error) {
return;
}
}
public async secretStorageGet(key: string): Promise<string | undefined> {
try {
const secretStorage: vscode.SecretStorage = ExtensionContextHolder.context!.secrets;
let openaiApiKey = await secretStorage.get(key);
return openaiApiKey;
} catch (error) {
logger.channel()?.error(`secretStorageGet error: ${error}`);
return undefined;
}
}
public async writeFile(uri: string, content: string): Promise<void> {
await vscode.workspace.fs.writeFile(vscode.Uri.file(uri), Buffer.from(content));
}
public async showInputBox(option: object): Promise<string | undefined> {
return vscode.window.showInputBox(option);
}
public async storeSecret(key: string, value: string): Promise<void> {
const secretStorage: vscode.SecretStorage = ExtensionContextHolder.context!.secrets;
await secretStorage.store(key, value);
}
public extensionPath(): string {
return ExtensionContextHolder.context!.extensionUri.fsPath;
}
public runTerminal(terminalName: string, command: string): void {
const terminals = vscode.window.terminals;
for (const terminal of terminals) {
if (terminal.name === terminalName) {
terminal.dispose();
}
}
const terminal = vscode.window.createTerminal(terminalName);
terminal.sendText(command);
terminal.show();
}
// current active file path
public activeFilePath(): string | undefined {
const validVisibleTextEditors = vscode.window.visibleTextEditors.filter(editor => editor.viewColumn !== undefined);
if (validVisibleTextEditors.length > 1) {
vscode.window.showErrorMessage(`There are more then one visible text editors. Please close all but one and try again.`);
return undefined;
}
const editor = validVisibleTextEditors[0];
if (!editor) {
return undefined;
}
return editor.document.fileName;
}
// current selected range, return undefined if no selection
public selectRange(): [number, number] | undefined {
const validVisibleTextEditors = vscode.window.visibleTextEditors.filter(editor => editor.viewColumn !== undefined);
if (validVisibleTextEditors.length > 1) {
vscode.window.showErrorMessage(`There are more then one visible text editors. Please close all but one and try again.`);
return undefined;
}
const editor = validVisibleTextEditors[0];
if (!editor) {
return undefined;
}
if (editor.selection.isEmpty) {
return undefined;
}
return [editor.selection.start.character, editor.selection.end.character];
}
// current selected text
public selectText(): string | undefined {
const validVisibleTextEditors = vscode.window.visibleTextEditors.filter(editor => editor.viewColumn !== undefined);
if (validVisibleTextEditors.length > 1) {
vscode.window.showErrorMessage(`There are more then one visible text editors. Please close all but one and try again.`);
return undefined;
}
const editor = validVisibleTextEditors[0];
if (!editor) {
return undefined;
}
if (editor.selection.isEmpty) {
return undefined;
}
return editor.document.getText(editor.selection);
}
public showErrorMessage(message: string): void {
vscode.window.showErrorMessage(message);
}
public async getLSPBrigePort(): Promise<number | undefined> {
const port = await vscode.commands.executeCommand('LangBrige.getAddress') as number | undefined;;
return port;
}
}