forked from gitkraken/vscode-gitlens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
209 lines (173 loc) · 6.22 KB
/
utils.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import type { ColorTheme, TextDocument, TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
import { version as codeVersion, ColorThemeKind, env, ViewColumn, window, workspace } from 'vscode';
import { ImageMimetypes, Schemes } from '../constants';
import { isGitUri } from '../git/gitUri';
import { executeCoreCommand } from './command';
import { configuration } from './configuration';
import { Logger } from './logger';
import { extname } from './path';
import { satisfies } from './version';
export function findTextDocument(uri: Uri): TextDocument | undefined {
const normalizedUri = uri.toString();
return workspace.textDocuments.find(d => d.uri.toString() === normalizedUri);
}
export function findEditor(uri: Uri): TextEditor | undefined {
const active = window.activeTextEditor;
const normalizedUri = uri.toString();
for (const e of [...(active != null ? [active] : []), ...window.visibleTextEditors]) {
// Don't include diff editors
if (e.document.uri.toString() === normalizedUri && e?.viewColumn != null) {
return e;
}
}
return undefined;
}
export async function findOrOpenEditor(
uri: Uri,
options?: TextDocumentShowOptions & { throwOnError?: boolean },
): Promise<TextEditor | undefined> {
const e = findEditor(uri);
if (e != null) {
if (!options?.preserveFocus) {
await window.showTextDocument(e.document, { ...options, viewColumn: e.viewColumn });
}
return e;
}
return openEditor(uri, { viewColumn: window.activeTextEditor?.viewColumn, ...options });
}
export function findOrOpenEditors(uris: Uri[]): void {
const normalizedUris = new Map(uris.map(uri => [uri.toString(), uri]));
for (const e of window.visibleTextEditors) {
// Don't include diff editors
if (e?.viewColumn != null) {
normalizedUris.delete(e.document.uri.toString());
}
}
for (const uri of normalizedUris.values()) {
void executeCoreCommand('vscode.open', uri, { background: true, preview: false });
}
}
export function getEditorIfActive(document: TextDocument): TextEditor | undefined {
const editor = window.activeTextEditor;
return editor != null && editor.document === document ? editor : undefined;
}
export function getQuickPickIgnoreFocusOut() {
return !configuration.get('advanced.quickPick.closeOnFocusOut');
}
export function hasVisibleTextEditor(uri?: Uri): boolean {
if (window.visibleTextEditors.length === 0) return false;
if (uri == null) return window.visibleTextEditors.some(e => isTextEditor(e));
const url = uri.toString();
return window.visibleTextEditors.some(e => e.document.uri.toString() === url && isTextEditor(e));
}
export function isActiveDocument(document: TextDocument): boolean {
const editor = window.activeTextEditor;
return editor != null && editor.document === document;
}
export function isDarkTheme(theme: ColorTheme): boolean {
return theme.kind === ColorThemeKind.Dark || theme.kind === ColorThemeKind.HighContrast;
}
export function isLightTheme(theme: ColorTheme): boolean {
return theme.kind === ColorThemeKind.Light || theme.kind === ColorThemeKind.HighContrastLight;
}
export function isVirtualUri(uri: Uri): boolean {
return uri.scheme === Schemes.Virtual || uri.scheme === Schemes.GitHub;
}
export function isVisibleDocument(document: TextDocument): boolean {
if (window.visibleTextEditors.length === 0) return false;
return window.visibleTextEditors.some(e => e.document === document);
}
export function isTextEditor(editor: TextEditor): boolean {
const scheme = editor.document.uri.scheme;
return scheme !== Schemes.DebugConsole && scheme !== Schemes.Output && scheme !== Schemes.Terminal;
}
export async function openEditor(
uri: Uri,
options: TextDocumentShowOptions & { rethrow?: boolean } = {},
): Promise<TextEditor | undefined> {
const { rethrow, ...opts } = options;
try {
if (isGitUri(uri)) {
uri = uri.documentUri();
}
if (uri.scheme === Schemes.GitLens && ImageMimetypes[extname(uri.fsPath)]) {
await executeCoreCommand('vscode.open', uri);
return undefined;
}
const document = await workspace.openTextDocument(uri);
return window.showTextDocument(document, {
preserveFocus: false,
preview: true,
viewColumn: ViewColumn.Active,
...opts,
});
} catch (ex) {
const msg: string = ex?.toString() ?? '';
if (msg.includes('File seems to be binary and cannot be opened as text')) {
await executeCoreCommand('vscode.open', uri);
return undefined;
}
if (rethrow) throw ex;
Logger.error(ex, 'openEditor');
return undefined;
}
}
export async function openWalkthrough(
extensionId: string,
walkthroughId: string,
stepId?: string,
openToSide: boolean = true,
): Promise<void> {
// Only open to side if there is an active tab
if (openToSide && window.tabGroups.activeTabGroup.activeTab == null) {
openToSide = false;
}
// Takes the following params: walkthroughID: string | { category: string, step: string } | undefined, toSide: boolean | undefined
void (await executeCoreCommand(
'workbench.action.openWalkthrough',
{
category: `${extensionId}#${walkthroughId}`,
step: stepId ? `${extensionId}#${walkthroughId}#${stepId}` : undefined,
},
openToSide,
));
}
export type OpenWorkspaceLocation = 'currentWindow' | 'newWindow' | 'addToWorkspace';
export function openWorkspace(
uri: Uri,
options: { location?: OpenWorkspaceLocation; name?: string } = { location: 'currentWindow' },
): void {
if (options?.location === 'addToWorkspace') {
const count = workspace.workspaceFolders?.length ?? 0;
return void workspace.updateWorkspaceFolders(count, 0, { uri: uri, name: options?.name });
}
return void executeCoreCommand('vscode.openFolder', uri, {
forceNewWindow: options?.location === 'newWindow',
});
}
export function getEditorCommand() {
let editor;
switch (env.appName) {
case 'Visual Studio Code - Insiders':
editor = 'code-insiders --wait --reuse-window';
break;
case 'Visual Studio Code - Exploration':
editor = 'code-exploration --wait --reuse-window';
break;
case 'VSCodium':
editor = 'codium --wait --reuse-window';
break;
default:
editor = 'code --wait --reuse-window';
break;
}
return editor;
}
export function supportedInVSCodeVersion(feature: 'input-prompt-links') {
switch (feature) {
case 'input-prompt-links':
return satisfies(codeVersion, '>= 1.76');
default:
return false;
}
}