forked from microsoft/vscode-cpptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
140 lines (122 loc) · 5.34 KB
/
Copy pathutils.ts
File metadata and controls
140 lines (122 loc) · 5.34 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
131
132
133
134
135
136
137
138
139
140
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import * as os from 'os';
import * as vscode from 'vscode';
import { Range } from 'vscode-languageclient';
import { SessionState } from '../sessionState';
import { Location, TextEdit } from './commonTypes';
import { CppSettings } from './settings';
export function makeLspRange(vscRange: vscode.Range): Range {
return {
start: { line: vscRange.start.line, character: vscRange.start.character },
end: { line: vscRange.end.line, character: vscRange.end.character }
};
}
export function makeVscodeRange(cpptoolsRange: Range): vscode.Range {
return new vscode.Range(cpptoolsRange.start.line, cpptoolsRange.start.character, cpptoolsRange.end.line, cpptoolsRange.end.character);
}
export function makeVscodeLocation(cpptoolsLocation: Location): vscode.Location {
return new vscode.Location(vscode.Uri.parse(cpptoolsLocation.uri), makeVscodeRange(cpptoolsLocation.range));
}
export function makeVscodeTextEdits(cpptoolsTextEdits: TextEdit[]): vscode.TextEdit[] {
return cpptoolsTextEdits.map(textEdit => new vscode.TextEdit(makeVscodeRange(textEdit.range), textEdit.newText));
}
export function rangeEquals(range1: vscode.Range | Range, range2: vscode.Range | Range): boolean {
return range1.start.line === range2.start.line && range1.start.character === range2.start.character &&
range1.end.line === range2.end.line && range1.end.character === range2.end.character;
}
// Check this before attempting to switch a document from C to C++.
export function shouldChangeFromCToCpp(document: vscode.TextDocument): boolean {
if (document.fileName.endsWith(".C") || document.fileName.endsWith(".H")) {
const cppSettings: CppSettings = new CppSettings();
if (cppSettings.autoAddFileAssociations) {
return !docsChangedFromCppToC.has(document.fileName);
}
// We could potentially add a new setting to enable switching to cpp even when files.associations isn't changed.
}
return false;
}
// Call this before changing from C++ to C.
export function handleChangedFromCppToC(document: vscode.TextDocument): void {
if (shouldChangeFromCToCpp(document)) {
docsChangedFromCppToC.add(document.fileName);
}
}
export function showInstallCompilerWalkthrough(): void {
// Because we need to conditionally enable/disable steps to alter their contents,
// we need to determine which step is actually visible. If the steps change, this
// logic will need to change to reflect them.
enum Step {
Activation = 'awaiting.activation',
NoCompilers = 'no.compilers.found',
Verify = 'verify.compiler'
}
const step = (() => {
if (!SessionState.scanForCompilersDone.get()) {
return Step.Activation;
} else if (!SessionState.trustedCompilerFound.get()) {
return Step.NoCompilers;
} else {
return Step.Verify;
}
})();
const platform = (() => {
switch (os.platform()) {
case 'win32': return 'windows';
case 'darwin': return 'mac';
default: return 'linux';
}
})();
const version = (platform === 'windows') ? SessionState.windowsVersion.get() : '';
const index = `ms-vscode.cpptools#${step}.${platform}${version}`;
void vscode.commands.executeCommand(
'workbench.action.openWalkthrough',
{ category: 'ms-vscode.cpptools#cppWelcome', step: index },
false)
// Run it twice for now because of VS Code bug #187958
.then(() => vscode.commands.executeCommand(
"workbench.action.openWalkthrough",
{ category: 'ms-vscode.cpptools#cppWelcome', step: index },
false)
);
return;
}
const docsChangedFromCppToC: Set<string> = new Set<string>();
export async function withCancellation<T>(promise: Promise<T>, token: vscode.CancellationToken): Promise<T> {
return new Promise<T>((resolve, reject) => {
const disposable = token.onCancellationRequested(() => reject(new vscode.CancellationError()));
promise.then((value) => {
disposable.dispose();
resolve(value);
}, (reason) => {
disposable.dispose();
reject(reason);
});
});
}
export async function checkDuration<T>(fn: () => Promise<T>): Promise<{ result: T; duration: number }> {
const start = performance.now();
const result = await fn();
return { result, duration: performance.now() - start };
}
/**
* A class that delays the execution of a callback until a specified timeout period has elapsed.
*/
export class Deferral {
private timer?: NodeJS.Timeout;
constructor(callback: () => void, timeout: number) {
this.timer = setTimeout(() => {
this.timer = undefined;
callback();
}, timeout);
}
public cancel() {
if (this.timer) {
clearTimeout(this.timer);
this.timer = undefined;
}
}
}