Skip to content

fix-163 Studio Context Actions command errored if no file open #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 73 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,20 @@
"when": "false"
},
{
"command": "vscode-objectscript.studio.actions",
"when": "vscode-objectscript.connectActive && resourceScheme == isfs"
"command": "vscode-objectscript.serverCommands.sourceControl",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gjsjohnmurray I don't like "sourceControl" here - maybe call it serverCommands.studioExtension? (The "studio extension" framework is more general than just source control.)

Copy link
Contributor Author

@gjsjohnmurray gjsjohnmurray Jun 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timleavitt with this PR I have implemented separate menus for the Source Control commands and the rest, to replicate how Studio behaves. This command brings up the %SourceMenu menu items, which Studio displays under a menu named "Source Control". The command vscode-objectscript.serverCommands.other brings up the items from all other menus for which context=0.

vscode-objectscript.serverCommands.contextSourceControl shows the %SourceContext menu's items, and vscode-objectscript.serverCommands.contextOther shows all the other ones for which context=1

If you'd like to see this in action you can always get the vsix artifact that our CI pipeline creates whenever I push stuff to this PR:

image

Install this in your VS Code and give it a try. I'll be glad to have more of your feedback.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gjsjohnmurray thanks - that makes more sense now. I'll try it out when I get a chance.

"when": "vscode-objectscript.connectActive && resourceScheme == isfs || vscode-objectscript.connectActive && !editorIsOpen"
},
{
"command": "vscode-objectscript.serverCommands.contextSourceControl",
"when": "false"
},
{
"command": "vscode-objectscript.serverCommands.other",
"when": "vscode-objectscript.connectActive && resourceScheme == isfs || vscode-objectscript.connectActive && !editorIsOpen"
},
{
"command": "vscode-objectscript.serverCommands.contextOther",
"when": "false"
}
],
"view/title": [
Expand Down Expand Up @@ -157,7 +169,11 @@
"when": "view == ObjectScriptExplorer && viewItem =~ /^dataNode:/"
},
{
"command": "vscode-objectscript.studio.contextActions",
"command": "vscode-objectscript.serverCommands.contextSourceControl",
"when": "view == ObjectScriptExplorer && viewItem =~ /^dataNode:/"
},
{
"command": "vscode-objectscript.serverCommands.contextOther",
"when": "view == ObjectScriptExplorer && viewItem =~ /^dataNode:/"
},
{
Expand All @@ -182,21 +198,43 @@
"editor/context": [
{
"command": "vscode-objectscript.viewOthers",
"when": "editorLangId =~ /^objectscript/ && vscode-objectscript.connectActive"
"when": "editorLangId =~ /^objectscript/ && vscode-objectscript.connectActive",
"group": "objectscript@1"
},
{
"command": "vscode-objectscript.compile",
"when": "editorLangId =~ /^objectscript/ && vscode-objectscript.connectActive"
"when": "editorLangId =~ /^objectscript/ && vscode-objectscript.connectActive",
"group": "objectscript@2"
},
{
"command": "vscode-objectscript.studio.actions",
"when": "resourceScheme == isfs && editorLangId =~ /^objectscript/ && vscode-objectscript.connectActive"
"command": "vscode-objectscript.previewXml",
"when": "editorLangId =~ /^xml/",
"group": "objectscript@3"
},
{
"command": "vscode-objectscript.previewXml",
"when": "editorLangId =~ /^xml/"
"command": "vscode-objectscript.serverCommands.contextSourceControl",
"when": "resourceScheme == isfs && editorLangId =~ /^objectscript/ && vscode-objectscript.connectActive",
"group": "objectscript@4"
},
{
"command": "vscode-objectscript.serverCommands.contextOther",
"when": "resourceScheme == isfs && editorLangId =~ /^objectscript/ && vscode-objectscript.connectActive",
"group": "objectscript@5"
}
],
"editor/title": [
{
"command": "vscode-objectscript.serverCommands.sourceControl",
"group": "navigation@1",
"when": "vscode-objectscript.connectActive && resourceScheme == isfs"
},
{
"command": "vscode-objectscript.serverCommands.other",
"group": "navigation@2",
"when": "vscode-objectscript.connectActive && resourceScheme == isfs"
}

],
"touchBar": [
{
"command": "vscode-objectscript.touchBar.compile",
Expand All @@ -212,11 +250,18 @@
"explorer/context": [
{
"command": "vscode-objectscript.compileFolder",
"when": "vscode-objectscript.connectActive"
"when": "vscode-objectscript.connectActive",
"group": "objectscript@1"
},
{
"command": "vscode-objectscript.studio.actions",
"when": "resourceScheme == isfs && resourceLangId =~ /^objectscript/ && vscode-objectscript.connectActive"
"command": "vscode-objectscript.serverCommands.contextSourceControl",
"when": "resourceScheme == isfs && resourceLangId =~ /^objectscript/ && vscode-objectscript.connectActive",
"group": "objectscript@2"
},
{
"command": "vscode-objectscript.serverCommands.contextOther",
"when": "resourceScheme == isfs && resourceLangId =~ /^objectscript/ && vscode-objectscript.connectActive",
"group": "objectscript@3"
}
]
},
Expand Down Expand Up @@ -409,13 +454,25 @@
},
{
"category": "ObjectScript",
"command": "vscode-objectscript.studio.actions",
"title": "Studio Actions..."
"command": "vscode-objectscript.serverCommands.sourceControl",
"title": "Server Source Control...",
"icon": "$(source-control)"
},
{
"category": "ObjectScript",
"command": "vscode-objectscript.serverCommands.contextSourceControl",
"title": "Server Source Control..."
},
{
"category": "ObjectScript",
"command": "vscode-objectscript.serverCommands.other",
"title": "Server Command Menu...",
"icon": "$(server-environment)"
},
{
"category": "ObjectScript",
"command": "vscode-objectscript.studio.contextActions",
"title": "Studio Context Actions..."
"command": "vscode-objectscript.serverCommands.contextOther",
"title": "Server Command Menu..."
}
],
"keybindings": [
Expand Down
39 changes: 31 additions & 8 deletions src/commands/serverActions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from "vscode";
import { config, workspaceState, checkConnection } from "../extension";
import { config, workspaceState, checkConnection, FILESYSTEM_SCHEMA } from "../extension";
import { currentWorkspaceFolder, terminalWithDocker, currentFile } from "../utils";
import { mainCommandMenu, mainSourceControlMenu } from "./studio";

export async function serverActions(): Promise<void> {
const { active, host, ns, https, port: defaultPort, username, password: defaultPassword, links } = config("conn");
Expand Down Expand Up @@ -45,34 +46,48 @@ export async function serverActions(): Promise<void> {
detail: "Use docker-compose to start session inside configured service",
});
}
const studio = [];
if (!vscode.window.activeTextEditor || vscode.window.activeTextEditor.document.uri.scheme === FILESYSTEM_SCHEMA) {
studio.push({
id: "serverSourceControlMenu",
label: "Server Source Control...",
detail: "Pick server-side source control action",
});
studio.push({
id: "serverCommandMenu",
label: "Server Command Menu...",
detail: "Pick server-side command",
});
}
return vscode.window
.showQuickPick(
[
...extraLinks,
{
id: "refreshConnection",
label: "Refresh connection",
label: "Refresh Connection",
detail: "Force attempt to connect to the server",
},
...terminal,
{
detail: "Enable/Disable current connection",
id: "toggleConnection",
label: "Toggle connection",
label: "Toggle Connection",
detail: "Enable/Disable current connection",
},
{
detail: portalUrl,
id: "openPortal",
label: "Open Management Portal",
detail: portalUrl,
},
{
detail: classRef,
id: "openClassReference",
label: "Open class reference",
label: "Open Class Reference",
detail: classRef,
},
...studio,
],
{
placeHolder: `Select action for server: ${connInfo}`,
placeHolder: `Select action for server ${connInfo}`,
}
)
.then((action) => {
Expand All @@ -99,6 +114,14 @@ export async function serverActions(): Promise<void> {
terminalWithDocker();
break;
}
case "serverSourceControlMenu": {
mainSourceControlMenu();
break;
}
case "serverCommandMenu": {
mainCommandMenu();
break;
}
default: {
vscode.env.openExternal(vscode.Uri.parse(action.detail));
}
Expand Down
76 changes: 47 additions & 29 deletions src/commands/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,41 @@ export enum OtherStudioAction {
FirstTimeDocumentSave = 7,
}

export enum StudioMenuType {
Main = "main",
Context = "context",
}

interface StudioAction extends vscode.QuickPickItem {
name: string;
id: string;
}

function getOtherStudioActionLabel(action: OtherStudioAction): string {
let label = "";
/* eslint-disable no-fallthrough */
switch (action) {
case OtherStudioAction.AttemptedEdit:
label = "Attempted Edit";
break;
case OtherStudioAction.CreatedNewDocument:
label = "Created New Document";
break;
case OtherStudioAction.DeletedDocument:
label = "Deleted Document";
break;
case OtherStudioAction.OpenedDocument:
label = "Opened Document";
break;
case OtherStudioAction.ClosedDocument:
label = "Closed Document";
break;
case OtherStudioAction.ConnectedToNewNamespace:
label = "Changed Namespace";
break;
case OtherStudioAction.FirstTimeDocumentSave:
label = "Saved Document to Server for the First Time";
break;
}
/* eslint-enable no-fallthrough */
return label;
}

Expand Down Expand Up @@ -151,7 +161,7 @@ class StudioActions {
});
});
case 3: // Run an EXE on the client.
throw new Error("Not suppoorted");
throw new Error("Not supported");
case 4: {
// Insert the text in Target in the current document at the current selection point
const editor = vscode.window.activeTextEditor;
Expand Down Expand Up @@ -212,7 +222,7 @@ class StudioActions {
};
});
default:
throw new Error("Not suppoorted");
throw new Error("Not supported");
}
}

Expand Down Expand Up @@ -265,9 +275,9 @@ class StudioActions {
);
}

private constructMenu(menu, contextOnly = false): any[] {
return menu
.filter((menuGroup) => !(contextOnly && menuGroup.type === "main"))
private prepareMenuItems(menus, sourceControl: boolean): any[] {
return menus
.filter((menu) => sourceControl == (menu.id === "%SourceMenu" || menu.id === "%SourceContext"))
.reduce(
(list, sub) =>
list.concat(
Expand All @@ -284,17 +294,10 @@ class StudioActions {
}))
),
[]
)
.sort((el1, el2) => (el1.type === "main" && el2.type !== el1.type ? -1 : 1))
.filter((item: any, index: number, self: any) => {
if (item && item.type === "main") {
return true;
}
return self.findIndex((el): boolean => el.itemId === item.itemId) === index;
});
);
}

public getMenu(menuType: string, contextOnly = false): Thenable<any> {
public getMenu(menuType: StudioMenuType, sourceControl: boolean): Thenable<any> {
let selectedText = "";
const editor = vscode.window.activeTextEditor;
if (this.uri && editor) {
Expand All @@ -308,9 +311,12 @@ class StudioActions {
return this.api
.actionQuery(query, parameters)
.then((data) => data.result.content)
.then((menu) => this.constructMenu(menu, contextOnly))
.then((menus) => this.prepareMenuItems(menus, sourceControl))
.then((menuItems) => {
return vscode.window.showQuickPick<StudioAction>(menuItems, { canPickMany: false });
return vscode.window.showQuickPick<StudioAction>(menuItems, {
canPickMany: false,
placeHolder: `Pick server-side command to perform${this.name ? " on " + this.name : ""}`,
});
})
.then((action) => this.userAction(action));
}
Expand Down Expand Up @@ -361,29 +367,41 @@ class StudioActions {
}
}

// export function contextMenu(uri: vscode.Uri): Promise<void> {
// return doMenuAction(uri, "context");
// }
export async function mainCommandMenu(uri?: vscode.Uri): Promise<any> {
return _mainMenu(false, uri);
}

export async function mainSourceControlMenu(uri?: vscode.Uri): Promise<any> {
return _mainMenu(true, uri);
}

export async function mainMenu(uri: vscode.Uri) {
uri = uri || vscode.window.activeTextEditor.document.uri;
if (!uri || uri.scheme !== FILESYSTEM_SCHEMA) {
async function _mainMenu(sourceControl: boolean, uri?: vscode.Uri): Promise<any> {
uri = uri || vscode.window.activeTextEditor?.document.uri;
if (uri && uri.scheme !== FILESYSTEM_SCHEMA) {
return;
}
const studioActions = new StudioActions(uri);
return studioActions && studioActions.getMenu("");
return studioActions && studioActions.getMenu(StudioMenuType.Main, sourceControl);
}

export async function contextCommandMenu(node: PackageNode | ClassNode | RoutineNode): Promise<any> {
return _contextMenu(false, node);
}

export async function contextSourceControlMenu(node: PackageNode | ClassNode | RoutineNode): Promise<any> {
return _contextMenu(true, node);
}

export async function contextMenu(node: PackageNode | ClassNode | RoutineNode): Promise<any> {
const nodeOrUri = node || vscode.window.activeTextEditor.document.uri;
export async function _contextMenu(sourceControl: boolean, node: PackageNode | ClassNode | RoutineNode): Promise<any> {
const nodeOrUri = node || vscode.window.activeTextEditor?.document.uri;
if (!nodeOrUri || (nodeOrUri instanceof vscode.Uri && nodeOrUri.scheme !== FILESYSTEM_SCHEMA)) {
return;
}
const studioActions = new StudioActions(nodeOrUri);
return studioActions && studioActions.getMenu("", true);
return studioActions && studioActions.getMenu(StudioMenuType.Context, sourceControl);
}

export async function fireOtherStudioAction(action: OtherStudioAction, uri?: vscode.Uri) {
export async function fireOtherStudioAction(action: OtherStudioAction, uri?: vscode.Uri): Promise<void> {
const studioActions = new StudioActions(uri);
return studioActions && studioActions.fireOtherStudioAction(action);
}
Loading