-
Notifications
You must be signed in to change notification settings - Fork 56
/
editor.ts
102 lines (82 loc) · 2.86 KB
/
editor.ts
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
import * as vscode from "vscode";
import * as path from "path";
import { QuickPickItem } from "vscode";
import * as fs from 'fs';
export const workspaceRoot = () => vscode.workspace.rootPath;
export const activeURI = () => vscode.window.activeTextEditor.document.uri;
export const activeFileName = () =>
vscode.window.activeTextEditor.document.fileName;
export const selectedTextStart = () =>
vscode.window.activeTextEditor.selection.start;
export const selectedTextEnd = () =>
vscode.window.activeTextEditor.selection.end;
export const activeEditor = () => vscode.window.activeTextEditor;
export const config = () => vscode.workspace.getConfiguration("glean");
export function currentEditorPath(): string {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) return;
const currentFilePath = path.dirname(activeEditor.document.fileName);
const rootMatcher = new RegExp(`^${workspaceRoot()}`);
const relativeCurrentFilePath = currentFilePath.replace(rootMatcher, "");
return relativeCurrentFilePath;
}
export function openFile(absolutePath: string): PromiseLike<string> {
return vscode.workspace
.openTextDocument(absolutePath)
.then((textDocument) => {
if (textDocument) {
vscode.window.showTextDocument(textDocument);
return absolutePath;
} else {
throw Error("Could not open document");
}
});
}
export function selectedText() {
const editor = vscode.window.activeTextEditor;
if (editor) {
const selection = editor.selection;
return editor.document.getText(selection);
} else {
return null;
}
}
export function allText() {
const editor = vscode.window.activeTextEditor;
return editor.document.getText();
}
export function showInputBox(defaultValue, placeHolder) {
return vscode.window.showInputBox({
value: defaultValue,
placeHolder,
});
}
export function showQuickPicksList(choices: QuickPickItem[], placeHolder = "") {
return vscode.window.showQuickPick<vscode.QuickPickItem>(choices, {
placeHolder,
});
}
export const convertRelativeToFullPath = (relativePath) => {
const root = workspaceRoot();
return root ? path.join(root, relativePath) : relativePath;
};
export const extractQuickPickValue = (selection) => {
if (!selection) return;
return selection.label;
};
export const toQuickPick = (label: string, description?) => ({
label,
description,
});
export const toQuickPicksList = (choices: string[]) =>
choices.map((item) => toQuickPick(item));
export const showErrorMessage = (message) =>
vscode.window.showErrorMessage(message);
export const showInformationMessage = (message, items: string[] = []) =>
vscode.window.showInformationMessage(message, ...items);
export const importMissingDependencies = (targetFile) =>
vscode.commands.executeCommand(
"_typescript.applyFixAllCodeAction",
targetFile,
{ fixId: "fixMissingImport" }
);