-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevents.ts
More file actions
60 lines (56 loc) · 2.32 KB
/
events.ts
File metadata and controls
60 lines (56 loc) · 2.32 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
import * as vscode from 'vscode';
import { extension } from '../state';
import { updateStateMachine } from './state-machine';
import { Selection } from '../types/context';
import { SELECTION_DEBOUNCE_MS } from '../utils/constants';
let selectionTimeout: NodeJS.Timeout | null = null;
let currentSelection: Selection = { startLine: 0, startColumn: 0, endLine: 0, endColumn: 0 };
/**
* Initializes file system event listeners
* @param context The extension context
*/
export function registerEvents(context: vscode.ExtensionContext) {
// listen for active text editor changes
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(async editor => {
if (!editor || editor.document.languageId !== "java") return;
await onActiveFileChange(editor);
}),
vscode.workspace.onDidSaveTextDocument(async document => {
if (document.uri.scheme !== 'file' || document.languageId !== "java") return;
await updateStateMachine(document)
}),
vscode.window.onDidChangeTextEditorSelection(event => {
if (event.textEditor.document.uri.scheme !== 'file' || event.textEditor.document.languageId !== "java") return;
if (event.selections.length === 0) return;
onSelectionChange(event);
})
);
}
/**
* Handles active file change events
* @param editor The active text editor
*/
export async function onActiveFileChange(editor: vscode.TextEditor) {
extension.file = editor.document.uri.fsPath;
extension.webview?.sendMessage({ type: "file", file: extension.file });
await updateStateMachine(editor.document);
}
/**
* Handles selection change events
* @param event The selection change event
*/
export async function onSelectionChange(event: vscode.TextEditorSelectionChangeEvent) {
// update current selection
const selectionStart = event.selections[0].start;
const selectionEnd = event.selections[0].end;
currentSelection = {
startLine: selectionStart.line,
startColumn: selectionStart.character,
endLine: selectionEnd.line,
endColumn: selectionEnd.character
};
// debounce selection changes
if (selectionTimeout) clearTimeout(selectionTimeout);
selectionTimeout = setTimeout(() => extension.selection = currentSelection, SELECTION_DEBOUNCE_MS);
}