This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathutils.ts
More file actions
118 lines (106 loc) · 3.37 KB
/
Copy pathutils.ts
File metadata and controls
118 lines (106 loc) · 3.37 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
import * as path from 'path';
import {
Point,
TextBuffer,
TextEditor,
Range,
BufferScanResult,
} from 'atom';
import {
CancellationToken,
CancellationTokenSource,
} from 'vscode-jsonrpc';
export type ReportBusyWhile = <T>(
title: string,
f: () => Promise<T>,
) => Promise<T>;
/**
* Obtain the range of the word at the given editor position.
* Uses the non-word characters from the position's grammar scope.
*/
export function getWordAtPosition(editor: TextEditor, position: Point): Range {
const nonWordCharacters = escapeRegExp(editor.getNonWordCharacters(position));
const range = _getRegexpRangeAtPosition(
editor.getBuffer(),
position,
new RegExp(`^[\t ]*$|[^\\s${nonWordCharacters}]+`, 'g'),
);
if (range == null) {
return new Range(position, position);
}
return range;
}
export function escapeRegExp(string: string): string {
// From atom/underscore-plus.
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
}
function _getRegexpRangeAtPosition(buffer: TextBuffer, position: Point, wordRegex: RegExp): Range | null {
const { row, column } = position;
const rowRange = buffer.rangeForRow(row, false);
let matchData: BufferScanResult | undefined | null;
// Extract the expression from the row text.
buffer.scanInRange(wordRegex, rowRange, (data) => {
const { range } = data;
if (
position.isGreaterThanOrEqual(range.start) &&
// Range endpoints are exclusive.
position.isLessThan(range.end)
) {
matchData = data;
data.stop();
return;
}
// Stop the scan if the scanner has passed our position.
if (range.end.column > column) {
data.stop();
}
});
return matchData == null ? null : matchData.range;
}
/**
* For the given connection and cancellationTokens map, cancel the existing
* CancellationToken for that connection then create and store a new
* CancellationToken to be used for the current request.
*/
export function cancelAndRefreshCancellationToken<T extends object>(
key: T,
cancellationTokens: WeakMap<T, CancellationTokenSource>): CancellationToken {
let cancellationToken = cancellationTokens.get(key);
if (cancellationToken !== undefined && !cancellationToken.token.isCancellationRequested) {
cancellationToken.cancel();
}
cancellationToken = new CancellationTokenSource();
cancellationTokens.set(key, cancellationToken);
return cancellationToken.token;
}
export async function doWithCancellationToken<T1 extends object, T2>(
key: T1,
cancellationTokens: WeakMap<T1, CancellationTokenSource>,
work: (token: CancellationToken) => Promise<T2>,
): Promise<T2> {
const token = cancelAndRefreshCancellationToken(key, cancellationTokens);
const result: T2 = await work(token);
cancellationTokens.delete(key);
return result;
}
export function assertUnreachable(_: never): never {
return _;
}
export function promiseWithTimeout<T>(ms: number, promise: Promise<T>): Promise<T> {
return new Promise((resolve, reject) => {
// create a timeout to reject promise if not resolved
const timer = setTimeout(() => {
reject(new Error(`Timeout after ${ms}ms`));
}, ms);
promise.then((res) => {
clearTimeout(timer);
resolve(res);
}).catch((err) => {
clearTimeout(timer);
reject(err);
});
});
}
export function normalizePath(p: string): string {
return !p.endsWith(path.sep) ? path.join(p, path.sep) : p;
}