Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
40 changes: 37 additions & 3 deletions src/ui/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,8 @@ export async function getAllAvailableActions(targets: ActionTarget[], scheme: st
});

// Then we get all the available Actions for the current context
const availableActions: AvailableAction[] = allActions.filter(action => action.type === scheme)
.filter(action => !action.extensions || action.extensions.every(e => !e) || targets.every(t => action.extensions!.includes(t.extension) || action.extensions!.includes(t.fragment)) || action.extensions.includes(`GLOBAL`))
.filter(action => action.runOnProtected || !targets.some(t => t.protected))
const availableActions: AvailableAction[] = allActions
.filter(action => isActionAvailable(action, scheme, targets))
.sort((a, b) => (actionUsed.get(b.name) || 0) - (actionUsed.get(a.name) || 0))
.map(action => ({
label: action.name,
Expand All @@ -682,6 +681,41 @@ export async function getAllAvailableActions(targets: ActionTarget[], scheme: st
return availableActions;
}

export function isActionAvailable(action: Action, scheme: string, targets: ActionTarget[]): boolean {
if (action.type !== scheme) return false;

// action isn't cleared to run on protected targets and some of them are
if (!action.runOnProtected && targets.some(t => t.protected)) return false;

return targets.every((t) => targetMatchesExtensions(t, action.extensions));
Comment on lines +685 to +690

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can fuse all the checks in one statement; that will avoid intermediate returns.

Suggested change
if (action.type !== scheme) return false;
// action isn't cleared to run on protected targets and some of them are
if (!action.runOnProtected && targets.some(t => t.protected)) return false;
return targets.every((t) => targetMatchesExtensions(t, action.extensions));
return action.type === scheme &&
// action isn't cleared to run on protected targets and some of them are
(action.runOnProtected || !targets.some(t => t.protected)) &&
targets.every((t) => targetMatchesExtensions(t, action.extensions));

}

export function targetMatchesExtensions(target: ActionTarget, extensions?: string[]): boolean {
// action has no extension requirements, or is global
if (!extensions || extensions.every(e => !e) || extensions.includes("GLOBAL")) return true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Checking if extensions is truthy or empty will be less confusing than doing it with extensions.every(e => !e)

Suggested change
if (!extensions || extensions.every(e => !e) || extensions.includes("GLOBAL")) return true;
if (!extensions?.length || extensions.includes("GLOBAL")) return true;


const targetExtParts = [target.extension.toUpperCase(), target.fragment.toUpperCase()];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Filter on distinct and filter out empty string here to avoid unwanted iterations in the for loop after.

Suggested change
const targetExtParts = [target.extension.toUpperCase(), target.fragment.toUpperCase()];
const targetExtParts = [target.extension.toUpperCase(), target.fragment.toUpperCase()]
.filter(Boolean)
.filter(Tools.distinct);


for (const e of extensions) {
const ext = e.toUpperCase();
const extDotCount = ext.split('.').length - 1;

if (extDotCount === 0) {
// match on single extension (myfile.a)
if (targetExtParts.includes(ext)) return true; // match
} else {
// match on multi-part extension (myfile.a.b)
const parsed = path.parse(target.uri.path);
const targetFile = (parsed.name + parsed.ext).toUpperCase();
const targetDotCount = targetFile.split('.').length - 1;

if ((targetDotCount > extDotCount) && targetFile.endsWith(ext)) return true; // match
}
}

return false; // no matches
}

function getObjectsFromJoblog(stderr: string): CommandObject[] | undefined {
const objects: CommandObject[] = [];

Expand Down
19 changes: 12 additions & 7 deletions src/ui/views/environment/actions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { parse } from "path";
import { stringify } from "querystring";
import vscode, { l10n } from "vscode";
import { ActionTools } from "../../../api/actions";
import { parseFSOptions } from "../../../filesystems/qsys/QSysFs";
import { instance } from "../../../instantiate";
import { Action, ActionType } from "../../../typings";
import { VscodeTools } from "../../Tools";
import { EnvironmentItem } from "./environmentItem";
import { targetMatchesExtensions, uriToActionTarget } from "../../actions";

type ActionContext = {
canRun?: boolean
Expand Down Expand Up @@ -82,21 +81,27 @@ export class ActionsNode extends EnvironmentItem {
async activeEditorChanged(editor?: vscode.TextEditor) {
const uri = editor?.document.uri;
let activeEditorContext = undefined;
let actionTarget = undefined;

if (uri) {
const connection = instance.getConnection();
const workspace = vscode.workspace.getWorkspaceFolder(uri);

actionTarget = uriToActionTarget(uri, workspace, connection);

activeEditorContext = {
scheme: uri.scheme,
extension: parse(uri.path).ext.substring(1).toLocaleUpperCase(),
protected: parseFSOptions(uri).readonly || connection?.getConfig()?.readOnlyMode || connection?.getContent().isProtectedPath(uri.path),
workspace: vscode.workspace.getWorkspaceFolder(uri)
protected: actionTarget.protected,
workspace
};
}

const canRunOnEditor = (actionItem: ActionItem) => activeEditorContext !== undefined &&
actionTarget !== undefined &&
activeEditorContext.scheme === actionItem.action.type &&
activeEditorContext.workspace === actionItem.workspace &&
(actionItem.action.runOnProtected || !activeEditorContext.protected) &&
(!actionItem.action.extensions?.length || actionItem.action.extensions.includes('GLOBAL') || actionItem.action.extensions.includes(activeEditorContext.extension));
targetMatchesExtensions(actionTarget, actionItem.action.extensions);

(await this.getAllActionItems()).forEach(item => item.setContext({ canRun: canRunOnEditor(item) }));
this.refresh();
Expand Down Expand Up @@ -163,7 +168,7 @@ export class ActionItem extends EnvironmentItem {

this.iconPath = new vscode.ThemeIcon("github-action", this.context.matched ? new vscode.ThemeColor(ActionItem.matchedColor) : undefined);
this.description = this.context.matched ? l10n.t("search match") : undefined;
this.tooltip = `${ this.action.command }\nExtensions: ${this.action.extensions?.join(`, `)}`;
this.tooltip = `${this.action.command}\nExtensions: ${this.action.extensions?.join(`, `)}`;
this.resourceUri = vscode.Uri.from({
scheme: ActionItem.context,
authority: this.action.name,
Expand Down
4 changes: 2 additions & 2 deletions src/ui/views/environment/environmentView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { editAction, isActionEdited } from '../../../editors/actionEditor';
import { editConnectionProfile, isProfileEdited } from '../../../editors/connectionProfileEditor';
import { instance } from '../../../instantiate';
import { Action, ActionEnvironment, BrowserItem, ConnectionProfile, CustomVariable, FocusOptions } from '../../../typings';
import { uriToActionTarget } from '../../actions';
import { uriToActionTarget, targetMatchesExtensions } from '../../actions';
import { ActionItem, Actions, ActionsNode, ActionTypeNode } from './actions';
import { ConnectionProfiles, ProfileItem, ProfilesNode } from './connectionProfiles';
import { CustomVariableItem, CustomVariables, CustomVariablesNode } from './customVariables';
Expand Down Expand Up @@ -142,7 +142,7 @@ export function initializeEnvironmentView(context: vscode.ExtensionContext) {
}

const actionTarget = uriToActionTarget(uri);
if (action.extensions && !action.extensions.includes('GLOBAL') && !action.extensions.includes(actionTarget.extension) && !action.extensions.includes(actionTarget.fragment)) {
if (!targetMatchesExtensions(actionTarget, action.extensions)) {
vscode.window.showErrorMessage(l10n.t("This action cannot run on a file with the {0} extension.", actionTarget.extension), editActionLabel).then(edit => edit ? editAction() : '');
return;
}
Expand Down
Loading