-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentHelper.ts
More file actions
167 lines (146 loc) · 6.16 KB
/
DocumentHelper.ts
File metadata and controls
167 lines (146 loc) · 6.16 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
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
import * as vscode from 'vscode';
import { HitResult } from './types';
import { getKeyword, getKeywordNames, getKeywordsProperty } from './utils';
export const getDocumentSymbols = (document: vscode.TextDocument): Thenable<vscode.DocumentSymbol[] | undefined> => {
return new Promise((resolve) => {
(async () => {
const symbols: vscode.DocumentSymbol[] | undefined = await vscode.commands.executeCommand<vscode.DocumentSymbol[]>(
'vscode.executeDocumentSymbolProvider',
document.uri
);
if (!symbols) {
resolve(undefined);
return;
}
const result: vscode.DocumentSymbol[] = [];
for (const symbol of symbols) {
result?.push(...getDocumentSymbolChildren(symbol));
}
resolve(result);
})();
});
};
const getDocumentSymbolChildren = (symbol: vscode.DocumentSymbol): vscode.DocumentSymbol[] => {
let result: vscode.DocumentSymbol [] = [];
result.push(symbol);
for (const child of symbol.children) {
result.push(...getDocumentSymbolChildren(child));
}
return result;
};
export const navigateToHitResult = (hit: HitResult, editor?: vscode.TextEditor) => {
const editorToUse = editor ?? vscode.window.activeTextEditor;
if (!editorToUse) return;
editorToUse.selection = new vscode.Selection(hit.range.start, hit.range.start);
editorToUse.revealRange(hit.range, vscode.TextEditorRevealType.InCenter);
};
export const getResultsForKeyword = (keyword: string, document?: vscode.TextDocument, range?: vscode.Range): HitResult[] => {
// Check if there is actualy text to check
if (!document || !document?.getText?.(range)?.length) return [];
const keywordProps = getKeyword(keyword);
let result: HitResult[] = [];
const regex = new RegExp(
`\\b(${keyword}${keywordProps?.mustIncludeColon ?? true ? ':' : ''})`,
`${keywordProps?.caseSensitive ?? true ? '' : 'i'}gm`
);
// Loop over all the regex matches and create HitResults
let match: RegExpExecArray | null;
while (match = regex.exec(document.getText(range))) {
const pos = document.positionAt(match.index + (range?.start ? document.offsetAt(range?.start) : 0));
const hitRange = new vscode.Range(pos, document.positionAt(match.index + (range?.start ? document.offsetAt(range?.start) : 0) + match[0].length - ((keywordProps?.mustIncludeColon ?? true ? 1 : 0))));
result.push({
keyword,
comment: document.lineAt(pos).text.slice(pos.character).trim(),
note: document.lineAt(pos).text.slice(pos.character + keyword.length + ((keywordProps?.mustIncludeColon ?? true) ? 1 : 0)).trim(),
position: pos,
range: hitRange,
lineNumber: pos.line + 1,
});
}
// Sort the results based on line number
result.sort((last, curr) => {
if (last.lineNumber < curr.lineNumber) return -1;
if (last.lineNumber > curr.lineNumber) return 1;
return 0;
});
return result;
};
export const getResultsForGroup = (group: string, document?: vscode.TextDocument, range?: vscode.Range): HitResult[] => {
// Check if there is actualy text to check
if (!document || !document?.getText?.(range)?.length) return [];
// Get HitResults for all the keywords inside a group
let result: HitResult[] = [];
const keywords = getKeywordsProperty();
for (const keyword in keywords) {
const keywordProps = keywords[keyword];
if (!keywordProps?.group || keywordProps?.group !== group) continue;
result.push(...getResultsForKeyword(keyword, document, range));
}
// Sort the results based on line number
result.sort((last, curr) => {
if (last.lineNumber < curr.lineNumber) return -1;
if (last.lineNumber > curr.lineNumber) return 1;
return 0;
});
return result;
};
export const getResults = (document?: vscode.TextDocument, range?: vscode.Range): HitResult[] => {
// Check if there is actualy text to check
if (!document || !document?.getText?.(range)?.length) return [];
// Get HitResults for all the keywords inside a group
let result: HitResult[] = [];
const keywords = getKeywordsProperty();
for (const keyword in keywords) {
result.push(...getResultsForKeyword(keyword, document, range));
}
// Sort the results based on line number
result.sort((last, curr) => {
if (last.lineNumber < curr.lineNumber) return -1;
if (last.lineNumber > curr.lineNumber) return 1;
return 0;
});
return result;
};
export const getCountForKeyword = (keyword: string, document?: vscode.TextDocument, range?: vscode.Range): number => {
// Check if there is actualy text to check
if (!document || !document?.getText?.(range)?.length) return 0;
const keywordProps = getKeyword(keyword);
const regex = new RegExp(
`\\b(${keyword}${keywordProps?.mustIncludeColon ?? true ? ':' : ''})`,
`${keywordProps?.caseSensitive ?? true ? '' : 'i'}gm`
);
let count = 0;
while (regex.exec(document.getText(range))) {
count++;
}
return count;
};
export const getCountForGroup = (group: string, document?: vscode.TextDocument, range?: vscode.Range): number => {
// Check if there is actualy text to check
if (!document || !document?.getText?.(range)?.length) return 0;
let count = 0;
for (const keyword of getKeywordNames()) {
const keywordProps = getKeyword(keyword);
if (!keywordProps?.group || keywordProps?.group !== group) continue;
count += getCountForKeyword(keyword, document, range);
}
return count;
};
export const onDocumentChangeListener = (context: vscode.ExtensionContext, callback: () => void) => {
vscode.window.onDidChangeActiveTextEditor(() => {
callback();
}, null, context.subscriptions);
vscode.workspace.onDidChangeTextDocument(() => {
callback();
}, null, context.subscriptions);
};
export default {
getDocumentSymbols,
navigateToHitResult,
getResultsForKeyword,
getResultsForGroup,
getResults,
getCountForKeyword,
getCountForGroup,
onDocumentChangeListener
};