-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
137 lines (124 loc) · 4.99 KB
/
index.ts
File metadata and controls
137 lines (124 loc) · 4.99 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
import * as path from 'path';
import * as vscode from 'vscode';
import { getCountForGroup, getResults, getResultsForGroup, navigateToHitResult } from './DocumentHelper';
import { HitResult } from './types';
import updater from './updater';
import { getGroups } from './utils';
export let coreConfig: vscode.WorkspaceConfiguration;
const disposables: vscode.Disposable[] = [];
let extensionContext: vscode.ExtensionContext | undefined = undefined;
// Initializes the ProVision Core
export const initialize = (context: vscode.ExtensionContext) => {
extensionContext = context;
// Setup all the configruation settings
handleConfigUpdate();
// Update ProVision if needed
if (updater.updateSettings()) {
handleConfigUpdate();
}
// Configuration change listener
vscode.workspace.onDidChangeConfiguration(() => {
handleConfigUpdate();
});
// Rregister all the commands
vscode.commands.getCommands().then(commands => {
if (!commands.includes('ProVision.listGroup'))
disposables.push(vscode.commands.registerCommand('ProVision.listGroup', handleListGroup));
if (!commands.includes('ProVision.help'))
disposables.push(vscode.commands.registerCommand('ProVision.help', handleHelp));
if (!commands.includes('ProVision.listAll'))
disposables.push(vscode.commands.registerCommand('ProVision.listAll', handleListAll));
});
};
// Destroys everything the core has created
export const destroy = () => {
for (const disposable of disposables) {
disposable.dispose();
}
};
const handleConfigUpdate = () => {
coreConfig = vscode.workspace.getConfiguration('ProVision');
};
// Handle the listing of groups
const handleListGroup = (...args: any[]) => {
let groupToGet = args[0];
const range: vscode.Range | undefined = args[1];
if (!groupToGet) {
const listItems: vscode.QuickPickItem[] = [];
const groups = getGroups();
for (const group of groups) {
const count = getCountForGroup(group, vscode.window.activeTextEditor?.document as vscode.TextDocument, range);
listItems.push({
label: `${group.charAt(0).toUpperCase()}${group.slice(1).toLowerCase()} (${count} ${count === 1 ? 'result' : 'results'})`
});
}
// Show the quick picker
vscode.window.showQuickPick(listItems, {
canPickMany: false,
placeHolder: 'Select a group'
}).then(option => {
if (!option) return;
// Get the correct hit which was chosen from the list and navigate towards it
const chosenGroup = groups[listItems.indexOf(option)];
if (!chosenGroup) {
vscode.window.showErrorMessage('Could not find hit result, please try again');
return;
}
// Get the items which should be listed
const hits = getResultsForGroup(chosenGroup, vscode.window.activeTextEditor?.document as vscode.TextDocument, range);
handleListShow(hits);
});
} else {
// Get the items which should be listed
const hits = getResultsForGroup(groupToGet, vscode.window.activeTextEditor?.document as vscode.TextDocument, range);
handleListShow(hits);
}
};
const handleListAll = (...args: any[]) => {
const range: vscode.Range | undefined = args[0];
// Get the items which should be listed
const hits = getResults(vscode.window.activeTextEditor?.document as vscode.TextDocument, range);
handleListShow(hits);
};
const handleListShow = (hits: HitResult[]) => {
// If there are no hits, show an information message
if (hits.length === 0) {
vscode.window.showInformationMessage('No notes found. Good job!');
return;
}
// Move when single result is given and list.moveOnSingleResult is true
if (hits.length === 1 && (coreConfig.get<boolean>('list.moveOnSingleResult') ?? true)) {
navigateToHitResult(hits[0]);
return;
}
const listItems: vscode.QuickPickItem[] = [];
for (const hit of hits) {
listItems.push({
label: `${hit.keyword} (Line: ${hit.lineNumber})`,
description: hit.note
});
}
// Show the quick picker
vscode.window.showQuickPick(listItems, {
canPickMany: false,
placeHolder: 'Select a note'
}).then(option => {
if (!option) return;
// Get the correct hit which was chosen from the list and navigate towards it
const chosenHit = hits[listItems.indexOf(option)];
if (!chosenHit) {
vscode.window.showErrorMessage('Could not find hit result, please try again');
return;
}
navigateToHitResult(chosenHit);
});
};
// Show the help readme for users which need more help
const handleHelp = () => {
if (!extensionContext) return;
vscode.commands.executeCommand('markdown.showPreview', vscode.Uri.file(path.join(extensionContext.extensionPath, "MANUAL.md")));
};
export default {
initialize,
destroy
};