Skip to content

Commit af4c0bd

Browse files
Merge pull request #338 from gjsjohnmurray/enh-330
Add command to link local folder to server (#330)
2 parents 889ad28 + 75541cc commit af4c0bd

File tree

5 files changed

+156
-2
lines changed

5 files changed

+156
-2
lines changed

package.json

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
"onCommand:vscode-objectscript.compileFolder",
6060
"onCommand:vscode-objectscript.importFolder",
6161
"onCommand:vscode-objectscript.addServerNamespaceToWorkspace",
62+
"onCommand:vscode-objectscript.connectFolderToServerNamespace",
63+
"onCommand:vscode-objectscript.hideExplorerForWorkspace",
64+
"onCommand:vscode-objectscript.showExplorerForWorkspace",
6265
"onLanguage:objectscript",
6366
"onLanguage:objectscript-class",
6467
"onLanguage:objectscript-macros",
@@ -74,7 +77,12 @@
7477
"viewsWelcome": [
7578
{
7679
"view": "explorer",
77-
"contents": "You can also create a new workspace to edit or view code directly on an [InterSystems server](https://intersystems-community.github.io/vscode-objectscript/serverside/).\n[Choose Server and Namespace](command:vscode-objectscript.addServerNamespaceToWorkspace)"
80+
"contents": "Begin local ObjectScript development by opening a folder or cloning a repository. Then switch to the new ObjectScript view in the Activity Bar.\nYou can also create a new workspace to [edit or view code directly on an InterSystems server](https://intersystems-community.github.io/vscode-objectscript/serverside/).\n[Choose Server and Namespace](command:vscode-objectscript.addServerNamespaceToWorkspace)"
81+
},
82+
{
83+
"view": "ObjectScriptExplorer",
84+
"contents": "Connect a workspace folder to an [InterSystems server](https://intersystems-community.github.io/vscode-objectscript/serverside/) if you want to export or import ObjectScript code.\n[Choose Server and Namespace](command:vscode-objectscript.connectFolderToServerNamespace)\nOr [hide this ObjectScript view](command:vscode-objectscript.hideExplorerForWorkspace) if it is not needed for the current workspace.",
85+
"when": "vscode-objectscript.explorerRootCount == 0"
7886
}
7987
],
8088
"menus": {
@@ -186,6 +194,14 @@
186194
{
187195
"command": "vscode-objectscript.serverCommands.contextOther",
188196
"when": "false"
197+
},
198+
{
199+
"command": "vscode-objectscript.hideExplorerForWorkspace",
200+
"when": "config.objectscript.showExplorer"
201+
},
202+
{
203+
"command": "vscode-objectscript.showExplorerForWorkspace",
204+
"when": "!config.objectscript.showExplorer"
189205
}
190206
],
191207
"view/title": [
@@ -568,6 +584,21 @@
568584
"category": "ObjectScript",
569585
"command": "vscode-objectscript.addServerNamespaceToWorkspace",
570586
"title": "Add Server Namespace to Workspace..."
587+
},
588+
{
589+
"category": "ObjectScript",
590+
"command": "vscode-objectscript.connectFolderToServerNamespace",
591+
"title": "Connect Folder to Server Namespace..."
592+
},
593+
{
594+
"category": "ObjectScript",
595+
"command": "vscode-objectscript.hideExplorerForWorkspace",
596+
"title": "Hide Explorer for Workspace"
597+
},
598+
{
599+
"category": "ObjectScript",
600+
"command": "vscode-objectscript.showExplorerForWorkspace",
601+
"title": "Show Explorer for Workspace"
571602
}
572603
],
573604
"keybindings": [

src/commands/addServerNamespaceToWorkspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function addServerNamespaceToWorkspace(): Promise<void> {
3030
.catch((reason) => {
3131
// Notify user about serverInfo failure
3232
vscode.window.showErrorMessage(
33-
reason.message || `Failed to fetch namespace list from server at ${connDisplayString}`
33+
reason.message || `Failed to fetch namespace list from server at ${connDisplayString}`
3434
);
3535
return undefined;
3636
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import * as vscode from "vscode";
2+
import { AtelierAPI } from "../api";
3+
import { panel, resolveConnectionSpec } from "../extension";
4+
5+
interface ConnSettings {
6+
server: string;
7+
ns: string;
8+
active: boolean;
9+
}
10+
11+
export async function connectFolderToServerNamespace(): Promise<void> {
12+
const serverManagerApi = await getServerManagerApi();
13+
if (!serverManagerApi) {
14+
vscode.window.showErrorMessage(
15+
"Connecting a folder to a server namespace requires the [InterSystems Server Manager extension](https://marketplace.visualstudio.com/items?itemName=intersystems-community.servermanager) to be installed and enabled."
16+
);
17+
return;
18+
}
19+
// Which folder?
20+
const allFolders = vscode.workspace.workspaceFolders;
21+
const items: vscode.QuickPickItem[] = allFolders
22+
.filter((folder) => folder.uri.scheme === "file")
23+
.map((folder) => {
24+
const config = vscode.workspace.getConfiguration("objectscript", folder);
25+
const conn: ConnSettings = config.get("conn");
26+
return {
27+
label: folder.name,
28+
description: folder.uri.fsPath,
29+
detail: !conn.server ? undefined : `Currently connected to ${conn.ns} on ${conn.server}`,
30+
};
31+
});
32+
if (!items.length) {
33+
vscode.window.showErrorMessage("No local folders in the workspace.");
34+
return;
35+
}
36+
const pick =
37+
items.length === 1 && !items[0].detail
38+
? items[0]
39+
: await vscode.window.showQuickPick(items, { placeHolder: "Choose folder" });
40+
const folder = allFolders.find((el) => el.name === pick.label);
41+
// Get user's choice of server
42+
const options: vscode.QuickPickOptions = {};
43+
const serverName: string = await serverManagerApi.pickServer(undefined, options);
44+
if (!serverName) {
45+
return;
46+
}
47+
// Get its namespace list
48+
const uri = vscode.Uri.parse(`isfs://${serverName}/?ns=%SYS`);
49+
await resolveConnectionSpec(serverName);
50+
// Prepare a displayable form of its connection spec as a hint to the user
51+
const connSpec = await serverManagerApi.getServerSpec(serverName);
52+
const connDisplayString = `${connSpec.webServer.scheme}://${connSpec.webServer.host}:${connSpec.webServer.port}/${connSpec.webServer.pathPrefix}`;
53+
// Connect and fetch namespaces
54+
const api = new AtelierAPI(uri);
55+
const allNamespaces: string[] | undefined = await api
56+
.serverInfo()
57+
.then((data) => data.result.content.namespaces)
58+
.catch((reason) => {
59+
// Notify user about serverInfo failure
60+
vscode.window.showErrorMessage(
61+
reason.message || `Failed to fetch namespace list from server at ${connDisplayString}`
62+
);
63+
return undefined;
64+
});
65+
// Clear the panel entry created by the connection
66+
panel.text = "";
67+
panel.tooltip = "";
68+
// Handle serverInfo failure
69+
if (!allNamespaces) {
70+
return;
71+
}
72+
// Handle serverInfo having returned no namespaces
73+
if (!allNamespaces.length) {
74+
vscode.window.showErrorMessage(`No namespace list returned by server at ${connDisplayString}`);
75+
return;
76+
}
77+
// Get user's choice of namespace
78+
const namespace = await vscode.window.showQuickPick(allNamespaces, {
79+
placeHolder: `Namespace on server '${serverName}' (${connDisplayString})`,
80+
});
81+
if (!namespace) {
82+
return;
83+
}
84+
// Update folder's config object
85+
const config = vscode.workspace.getConfiguration("objectscript", folder);
86+
const conn: ConnSettings = config.get("conn");
87+
conn.server = serverName;
88+
conn.ns = namespace;
89+
conn.active = true;
90+
await config.update("conn", conn);
91+
}
92+
93+
async function getServerManagerApi(): Promise<any> {
94+
const targetExtension = vscode.extensions.getExtension("intersystems-community.servermanager");
95+
if (!targetExtension) {
96+
return undefined;
97+
}
98+
if (!targetExtension.isActive) {
99+
await targetExtension.activate();
100+
}
101+
const api = targetExtension.exports;
102+
103+
if (!api) {
104+
return undefined;
105+
}
106+
return api;
107+
}

src/explorer/explorer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export class ObjectScriptExplorerProvider implements vscode.TreeDataProvider<Nod
127127
});
128128
}
129129
});
130+
await vscode.commands.executeCommand("setContext", "vscode-objectscript.explorerRootCount", rootNodes.length);
130131
return rootNodes;
131132
}
132133
}

src/extension.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
mainSourceControlMenu,
4343
} from "./commands/studio";
4444
import { addServerNamespaceToWorkspace } from "./commands/addServerNamespaceToWorkspace";
45+
import { connectFolderToServerNamespace } from "./commands/connectFolderToServerNamespace";
4546

4647
import { getLanguageConfiguration } from "./languageConfiguration";
4748

@@ -457,6 +458,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
457458
vscode.workspace.onDidChangeConfiguration(({ affectsConfiguration }) => {
458459
if (affectsConfiguration("objectscript.conn")) {
459460
checkConnection(true);
461+
explorerProvider.refresh();
460462
}
461463
});
462464
vscode.window.onDidCloseTerminal((t) => {
@@ -682,6 +684,19 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
682684
vscode.commands.registerCommand("vscode-objectscript.addServerNamespaceToWorkspace", () => {
683685
addServerNamespaceToWorkspace();
684686
}),
687+
vscode.commands.registerCommand("vscode-objectscript.connectFolderToServerNamespace", () => {
688+
connectFolderToServerNamespace();
689+
}),
690+
vscode.commands.registerCommand("vscode-objectscript.hideExplorerForWorkspace", () => {
691+
vscode.workspace
692+
.getConfiguration("objectscript")
693+
.update("showExplorer", false, vscode.ConfigurationTarget.Workspace);
694+
}),
695+
vscode.commands.registerCommand("vscode-objectscript.showExplorerForWorkspace", () => {
696+
vscode.workspace
697+
.getConfiguration("objectscript")
698+
.update("showExplorer", true, vscode.ConfigurationTarget.Workspace);
699+
}),
685700

686701
vscode.workspace.registerTextDocumentContentProvider(OBJECTSCRIPT_FILE_SCHEMA, documentContentProvider),
687702
vscode.workspace.registerTextDocumentContentProvider(OBJECTSCRIPTXML_FILE_SCHEMA, xmlContentProvider),

0 commit comments

Comments
 (0)