Skip to content

Commit b791db6

Browse files
authored
Properly dispose of all event handlers (intersystems-community#967)
1 parent c9f569a commit b791db6

File tree

1 file changed

+132
-137
lines changed

1 file changed

+132
-137
lines changed

src/extension.ts

Lines changed: 132 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import vscode = require("vscode");
44
import * as semver from "semver";
55

66
import { AtelierJob } from "./api/atelier";
7-
const { workspace, window } = vscode;
87
export const OBJECTSCRIPT_FILE_SCHEMA = "objectscript";
98
export const OBJECTSCRIPTXML_FILE_SCHEMA = "objectscriptxml";
109
export const FILESYSTEM_SCHEMA = "isfs";
@@ -549,136 +548,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
549548
checkConnection(true, uri).finally();
550549
}
551550

552-
vscode.workspace.onDidChangeWorkspaceFolders(async ({ added, removed }) => {
553-
const folders = vscode.workspace.workspaceFolders;
554-
555-
// Make sure we have a resolved connection spec for the targets of all added folders
556-
const toCheck = new Map<string, vscode.Uri>();
557-
added.map((workspaceFolder) => {
558-
const uri = workspaceFolder.uri;
559-
const { configName } = connectionTarget(uri);
560-
toCheck.set(configName, uri);
561-
});
562-
for await (const oneToCheck of toCheck) {
563-
const configName = oneToCheck[0];
564-
const uri = oneToCheck[1];
565-
const serverName = uri.scheme === "file" ? config("conn", configName).server : configName;
566-
await resolveConnectionSpec(serverName);
567-
}
568-
569-
// If it was just the addition of the first folder, and this is one of the isfs types, hide the ObjectScript Explorer for this workspace
570-
if (
571-
folders?.length === 1 &&
572-
added?.length === 1 &&
573-
removed?.length === 0 &&
574-
filesystemSchemas.includes(added[0].uri.scheme)
575-
) {
576-
vscode.workspace
577-
.getConfiguration("objectscript")
578-
.update("showExplorer", false, vscode.ConfigurationTarget.Workspace);
579-
}
580-
});
581-
582-
vscode.workspace.onDidChangeConfiguration(async ({ affectsConfiguration }) => {
583-
if (affectsConfiguration("objectscript.conn") || affectsConfiguration("intersystems.servers")) {
584-
if (affectsConfiguration("intersystems.servers")) {
585-
// Gather the server names previously resolved
586-
const resolvedServers: string[] = [];
587-
resolvedConnSpecs.forEach((v, k) => resolvedServers.push(k));
588-
// Clear the cache
589-
resolvedConnSpecs.clear();
590-
// Resolve them again, sequentially in case user needs to be prompted for credentials
591-
for await (const serverName of resolvedServers) {
592-
await resolveConnectionSpec(serverName);
593-
}
594-
}
595-
// Check connections sequentially for each workspace folder
596-
let refreshFilesExplorer = false;
597-
for await (const folder of vscode.workspace.workspaceFolders) {
598-
if (schemas.includes(folder.uri.scheme)) {
599-
refreshFilesExplorer = true;
600-
}
601-
try {
602-
await checkConnection(true, folder.uri);
603-
} catch (_) {
604-
continue;
605-
}
606-
}
607-
explorerProvider.refresh();
608-
projectsExplorerProvider.refresh();
609-
if (refreshFilesExplorer) {
610-
// This unavoidably switches to the File Explorer view, so only do it if isfs folders were found
611-
vscode.commands.executeCommand("workbench.files.action.refreshFilesExplorer");
612-
}
613-
}
614-
});
615-
vscode.window.onDidCloseTerminal((t) => {
616-
const terminalIndex = terminals.findIndex((terminal) => terminal.name == t.name);
617-
if (terminalIndex > -1) {
618-
terminals.splice(terminalIndex, 1);
619-
}
620-
});
621-
622-
workspace.onDidSaveTextDocument((file) => {
623-
if (schemas.includes(file.uri.scheme) || languages.includes(file.languageId)) {
624-
if (documentBeingProcessed !== file) {
625-
return importAndCompile(false, file, config("compileOnSave"));
626-
}
627-
} else if (file.uri.scheme === "file") {
628-
if (isImportableLocalFile(file)) {
629-
// This local file is part of a CSP application
630-
// or matches our export settings, so import it on save
631-
return importFileOrFolder(file.uri, true);
632-
}
633-
}
634-
});
635-
636-
vscode.window.onDidChangeActiveTextEditor(async (textEditor: vscode.TextEditor) => {
637-
await checkConnection(false, textEditor?.document.uri);
638-
posPanel.text = "";
639-
if (textEditor?.document.fileName.endsWith(".xml") && config("autoPreviewXML")) {
640-
return xml2doc(context, textEditor);
641-
}
642-
});
643-
vscode.window.onDidChangeTextEditorSelection((event: vscode.TextEditorSelectionChangeEvent) => {
644-
posPanel.text = "";
645-
const document = event.textEditor.document;
646-
if (!["objectscript", "objectscript-int"].includes(document.languageId)) {
647-
return;
648-
}
649-
if (event.selections.length > 1 || !event.selections[0].isEmpty) {
650-
return;
651-
}
652-
653-
const file = currentFile(document);
654-
const nameMatch = file.name.match(/(.*)\.(int|mac)$/i);
655-
if (!nameMatch) {
656-
return;
657-
}
658-
const [, routine] = nameMatch;
659-
let label = "";
660-
let pos = 0;
661-
vscode.commands
662-
.executeCommand<vscode.DocumentSymbol[]>("vscode.executeDocumentSymbolProvider", document.uri)
663-
.then((symbols) => {
664-
if (symbols != undefined) {
665-
const cursor = event.selections[0].active;
666-
if (symbols.length == 0 || cursor.isBefore(symbols[0].range.start)) {
667-
pos = cursor.line - 1;
668-
} else {
669-
for (const symbol of symbols) {
670-
if (symbol.range.contains(cursor)) {
671-
label = symbol.name;
672-
pos = cursor.line - symbol.range.start.line;
673-
break;
674-
}
675-
}
676-
}
677-
posPanel.text = `${label}${pos > 0 ? "+" + pos : ""}^${routine}`;
678-
}
679-
});
680-
});
681-
682551
const documentSelector = (...list) =>
683552
["file", ...schemas].reduce((acc, scheme) => acc.concat(list.map((language) => ({ scheme, language }))), []);
684553

@@ -726,10 +595,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
726595
diagnosticProvider.updateDiagnostics(vscode.window.activeTextEditor.document);
727596
}
728597
noLSsubscriptions.push(
729-
workspace.onDidChangeTextDocument((event) => {
598+
vscode.workspace.onDidChangeTextDocument((event) => {
730599
diagnosticProvider.updateDiagnostics(event.document);
731600
}),
732-
window.onDidChangeActiveTextEditor(async (editor) => {
601+
vscode.window.onDidChangeActiveTextEditor(async (editor) => {
733602
if (editor) {
734603
diagnosticProvider.updateDiagnostics(editor.document);
735604
}
@@ -799,7 +668,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
799668
});
800669
}
801670
}),
802-
workspace.onDidChangeTextDocument((event) => {
671+
vscode.workspace.onDidChangeTextDocument((event) => {
803672
if (
804673
event.contentChanges.length !== 0 &&
805674
event.document.uri.scheme === FILESYSTEM_SCHEMA &&
@@ -811,8 +680,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
811680
checkChangedOnServer(currentFile(event.document));
812681
}
813682
}),
814-
window.onDidChangeActiveTextEditor(async (editor) => {
815-
if (workspace.workspaceFolders && workspace.workspaceFolders.length > 1) {
683+
vscode.window.onDidChangeActiveTextEditor(async (editor) => {
684+
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 1) {
816685
const workspaceFolder = currentWorkspaceFolder();
817686
if (workspaceFolder && workspaceFolder !== workspaceState.get<string>("workspaceFolder")) {
818687
workspaceState.update("workspaceFolder", workspaceFolder);
@@ -939,7 +808,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
939808
}
940809
),
941810
vscode.commands.registerCommand("vscode-objectscript.previewXml", () => {
942-
xml2doc(context, window.activeTextEditor);
811+
xml2doc(context, vscode.window.activeTextEditor);
943812
}),
944813
vscode.commands.registerCommand("vscode-objectscript.addServerNamespaceToWorkspace", () => {
945814
addServerNamespaceToWorkspace();
@@ -1082,6 +951,132 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
1082951
vscode.commands.registerCommand("vscode-objectscript.explorer.project.addWorkspaceFolderForProject", (node) =>
1083952
addWorkspaceFolderForProject(node)
1084953
),
954+
vscode.workspace.onDidChangeWorkspaceFolders(async ({ added, removed }) => {
955+
const folders = vscode.workspace.workspaceFolders;
956+
957+
// Make sure we have a resolved connection spec for the targets of all added folders
958+
const toCheck = new Map<string, vscode.Uri>();
959+
added.map((workspaceFolder) => {
960+
const uri = workspaceFolder.uri;
961+
const { configName } = connectionTarget(uri);
962+
toCheck.set(configName, uri);
963+
});
964+
for await (const oneToCheck of toCheck) {
965+
const configName = oneToCheck[0];
966+
const uri = oneToCheck[1];
967+
const serverName = uri.scheme === "file" ? config("conn", configName).server : configName;
968+
await resolveConnectionSpec(serverName);
969+
}
970+
971+
// If it was just the addition of the first folder, and this is one of the isfs types, hide the ObjectScript Explorer for this workspace
972+
if (
973+
folders?.length === 1 &&
974+
added?.length === 1 &&
975+
removed?.length === 0 &&
976+
filesystemSchemas.includes(added[0].uri.scheme)
977+
) {
978+
vscode.workspace
979+
.getConfiguration("objectscript")
980+
.update("showExplorer", false, vscode.ConfigurationTarget.Workspace);
981+
}
982+
}),
983+
vscode.workspace.onDidChangeConfiguration(async ({ affectsConfiguration }) => {
984+
if (affectsConfiguration("objectscript.conn") || affectsConfiguration("intersystems.servers")) {
985+
if (affectsConfiguration("intersystems.servers")) {
986+
// Gather the server names previously resolved
987+
const resolvedServers: string[] = [];
988+
resolvedConnSpecs.forEach((v, k) => resolvedServers.push(k));
989+
// Clear the cache
990+
resolvedConnSpecs.clear();
991+
// Resolve them again, sequentially in case user needs to be prompted for credentials
992+
for await (const serverName of resolvedServers) {
993+
await resolveConnectionSpec(serverName);
994+
}
995+
}
996+
// Check connections sequentially for each workspace folder
997+
let refreshFilesExplorer = false;
998+
for await (const folder of vscode.workspace.workspaceFolders) {
999+
if (schemas.includes(folder.uri.scheme)) {
1000+
refreshFilesExplorer = true;
1001+
}
1002+
try {
1003+
await checkConnection(true, folder.uri);
1004+
} catch (_) {
1005+
continue;
1006+
}
1007+
}
1008+
explorerProvider.refresh();
1009+
projectsExplorerProvider.refresh();
1010+
if (refreshFilesExplorer) {
1011+
// This unavoidably switches to the File Explorer view, so only do it if isfs folders were found
1012+
vscode.commands.executeCommand("workbench.files.action.refreshFilesExplorer");
1013+
}
1014+
}
1015+
}),
1016+
vscode.window.onDidCloseTerminal((t) => {
1017+
const terminalIndex = terminals.findIndex((terminal) => terminal.name == t.name);
1018+
if (terminalIndex > -1) {
1019+
terminals.splice(terminalIndex, 1);
1020+
}
1021+
}),
1022+
vscode.workspace.onDidSaveTextDocument((file) => {
1023+
if (schemas.includes(file.uri.scheme) || languages.includes(file.languageId)) {
1024+
if (documentBeingProcessed !== file) {
1025+
return importAndCompile(false, file, config("compileOnSave"));
1026+
}
1027+
} else if (file.uri.scheme === "file") {
1028+
if (isImportableLocalFile(file)) {
1029+
// This local file is part of a CSP application
1030+
// or matches our export settings, so import it on save
1031+
return importFileOrFolder(file.uri, true);
1032+
}
1033+
}
1034+
}),
1035+
vscode.window.onDidChangeActiveTextEditor(async (textEditor: vscode.TextEditor) => {
1036+
await checkConnection(false, textEditor?.document.uri);
1037+
posPanel.text = "";
1038+
if (textEditor?.document.fileName.endsWith(".xml") && config("autoPreviewXML")) {
1039+
return xml2doc(context, textEditor);
1040+
}
1041+
}),
1042+
vscode.window.onDidChangeTextEditorSelection((event: vscode.TextEditorSelectionChangeEvent) => {
1043+
posPanel.text = "";
1044+
const document = event.textEditor.document;
1045+
if (!["objectscript", "objectscript-int"].includes(document.languageId)) {
1046+
return;
1047+
}
1048+
if (event.selections.length > 1 || !event.selections[0].isEmpty) {
1049+
return;
1050+
}
1051+
1052+
const file = currentFile(document);
1053+
const nameMatch = file.name.match(/(.*)\.(int|mac)$/i);
1054+
if (!nameMatch) {
1055+
return;
1056+
}
1057+
const [, routine] = nameMatch;
1058+
let label = "";
1059+
let pos = 0;
1060+
vscode.commands
1061+
.executeCommand<vscode.DocumentSymbol[]>("vscode.executeDocumentSymbolProvider", document.uri)
1062+
.then((symbols) => {
1063+
if (symbols != undefined) {
1064+
const cursor = event.selections[0].active;
1065+
if (symbols.length == 0 || cursor.isBefore(symbols[0].range.start)) {
1066+
pos = cursor.line - 1;
1067+
} else {
1068+
for (const symbol of symbols) {
1069+
if (symbol.range.contains(cursor)) {
1070+
label = symbol.name;
1071+
pos = cursor.line - symbol.range.start.line;
1072+
break;
1073+
}
1074+
}
1075+
}
1076+
posPanel.text = `${label}${pos > 0 ? "+" + pos : ""}^${routine}`;
1077+
}
1078+
});
1079+
}),
10851080

10861081
/* Anything we use from the VS Code proposed API */
10871082
...proposed

0 commit comments

Comments
 (0)