Skip to content

Commit 8f1cbc9

Browse files
committed
Seperate out some logic from runAction as unique APIs
Signed-off-by: worksofliam <[email protected]>
1 parent be85215 commit 8f1cbc9

File tree

1 file changed

+55
-51
lines changed

1 file changed

+55
-51
lines changed

src/ui/actions.ts

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ type CommandObject = {
2121
library?: string
2222
}
2323

24-
type ActionTarget = {
24+
export type ActionTarget = {
2525
uri: vscode.Uri
2626
extension: string
2727
fragment: string
2828
protected: boolean
29-
workspaceFolder: vscode.WorkspaceFolder
29+
workspaceFolder?: vscode.WorkspaceFolder
3030
executionOK: boolean,
3131
hasRun: boolean,
3232
processed: boolean,
@@ -42,6 +42,20 @@ export function registerActionTools(context: vscode.ExtensionContext) {
4242
);
4343
}
4444

45+
export function uriToActionTarget(uri: vscode.Uri, workspaceFolder?: WorkspaceFolder): ActionTarget {
46+
return {
47+
uri,
48+
extension: uri.path.substring(uri.path.lastIndexOf(`.`) + 1).toUpperCase(),
49+
fragment: uri.fragment.toUpperCase(),
50+
protected: parseFSOptions(uri).readonly || false,
51+
workspaceFolder: workspaceFolder || vscode.workspace.getWorkspaceFolder(uri),
52+
executionOK: false,
53+
hasRun: false,
54+
processed: false,
55+
output: []
56+
};
57+
}
58+
4559
export async function runAction(instance: Instance, uris: vscode.Uri | vscode.Uri[], customAction?: Action, method?: DeploymentMethod, browserItems?: BrowserItem[], workspaceFolder?: WorkspaceFolder): Promise<boolean> {
4660
uris = Array.isArray(uris) ? uris : [uris];
4761
//Global scheme: all URIs share the same
@@ -56,17 +70,7 @@ export async function runAction(instance: Instance, uris: vscode.Uri | vscode.Ur
5670
const config = connection.getConfig();
5771
const content = connection.getContent();
5872

59-
const targets = uris.map(uri => ({
60-
uri,
61-
extension: uri.path.substring(uri.path.lastIndexOf(`.`) + 1).toUpperCase(),
62-
fragment: uri.fragment.toUpperCase(),
63-
protected: parseFSOptions(uri).readonly || config?.readOnlyMode,
64-
workspaceFolder: workspaceFolder || vscode.workspace.getWorkspaceFolder(uri),
65-
executionOK: false,
66-
hasRun: false,
67-
processed: false,
68-
output: []
69-
}) as ActionTarget);
73+
const targets = uris.map(uri => uriToActionTarget(uri, workspaceFolder));
7074

7175
workspaceFolder = targets[0].workspaceFolder;
7276
if (!targets.every(target => target.workspaceFolder === workspaceFolder)) {
@@ -76,34 +80,10 @@ export async function runAction(instance: Instance, uris: vscode.Uri | vscode.Ur
7680

7781
let remoteCwd = config?.homeDirectory || `.`;
7882

79-
let availableActions: { label: string; action: Action; }[] = [];
83+
let availableActions: AvailableAction[] = [];
8084
if (!customAction) {
8185
// First we grab a copy the predefined Actions in the VS Code settings
82-
const allActions = [...IBMi.connectionManager.get<Action[]>(`actions`) || []];
83-
84-
// Then, if we're being called from a local file
85-
// we fetch the Actions defined from the workspace.
86-
if (targets[0].workspaceFolder && scheme === `file`) {
87-
const localActions = await getLocalActions(targets[0].workspaceFolder);
88-
allActions.push(...localActions);
89-
}
90-
91-
// We make sure all extensions are uppercase
92-
allActions.forEach(action => {
93-
if (action.extensions) {
94-
action.extensions = action.extensions.map(ext => ext.toUpperCase());
95-
};
96-
});
97-
98-
// Then we get all the available Actions for the current context
99-
availableActions = allActions.filter(action => action.type === scheme)
100-
.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`))
101-
.filter(action => action.runOnProtected || !targets.some(t => t.protected))
102-
.sort((a, b) => (actionUsed.get(b.name) || 0) - (actionUsed.get(a.name) || 0))
103-
.map(action => ({
104-
label: action.name,
105-
action
106-
}));
86+
availableActions = await getAllAvailableActions(targets, scheme);
10787
}
10888

10989
if (customAction || availableActions.length) {
@@ -416,7 +396,7 @@ export async function runAction(instance: Instance, uris: vscode.Uri | vscode.Ur
416396
else if (evfeventInfo.object && evfeventInfo.library) {
417397
if (chosenAction.command.includes(`*EVENTF`)) {
418398
writeEmitter.fire(`Fetching errors for ${evfeventInfo.library}/${evfeventInfo.object}.` + CompileTools.NEWLINE);
419-
refreshDiagnosticsFromServer(instance, evfeventInfo);
399+
await refreshDiagnosticsFromServer(instance, evfeventInfo);
420400
problemsFetched = true;
421401
} else if (chosenAction.command.trimStart().toUpperCase().startsWith(`CRT`)) {
422402
writeEmitter.fire(`*EVENTF not found in command string. Not fetching errors for ${evfeventInfo.library}/${evfeventInfo.object}.` + CompileTools.NEWLINE);
@@ -501,7 +481,7 @@ export async function runAction(instance: Instance, uris: vscode.Uri | vscode.Ur
501481

502482
// Process locally downloaded evfevent files:
503483
if (useLocalEvfevent) {
504-
refreshDiagnosticsFromLocal(instance, evfeventInfo);
484+
await refreshDiagnosticsFromLocal(instance, evfeventInfo);
505485
problemsFetched = true;
506486
}
507487
})
@@ -561,17 +541,11 @@ export async function runAction(instance: Instance, uris: vscode.Uri | vscode.Ur
561541
const now = new Date();
562542
const resultsPanel = new CustomUI();
563543
if (targets.length === 1) {
564-
resultsPanel.addParagraph(`<pre>${targets[0].output.join("")}</pre>`)
565-
.setOptions({ fullPage: true ,
566-
css: /* css */ `
567-
pre{
568-
background-color: transparent;
569-
}
570-
`
571-
});
544+
resultsPanel.addParagraph(`<pre><code>${targets[0].output.join("")}</code></pre>`)
545+
.setOptions({ fullPage: true });
572546
}
573547
else {
574-
resultsPanel.addBrowser("results", targets.filter(target => target.processed).map(target => ({ label: `${getTargetResultIcon(target)} ${path.basename(target.uri.path)}`, value: `<pre>${target.output.join("")}</pre>` } as TreeListItem)))
548+
resultsPanel.addBrowser("results", targets.filter(target => target.processed).map(target => ({ label: `${getTargetResultIcon(target)} ${path.basename(target.uri.path)}`, value: `<pre><code>${target.output.join("")}</code></pre>` } as TreeListItem)))
575549
.setOptions({
576550
fullPage: true,
577551
css: /* css */ `
@@ -583,7 +557,6 @@ export async function runAction(instance: Instance, uris: vscode.Uri | vscode.Ur
583557
584558
pre {
585559
margin: 1em;
586-
background-color: transparent;
587560
}
588561
`
589562
});
@@ -604,6 +577,37 @@ export async function runAction(instance: Instance, uris: vscode.Uri | vscode.Ur
604577
}
605578
}
606579

580+
export type AvailableAction = { label: string; action: Action; }
581+
582+
export async function getAllAvailableActions(targets: ActionTarget[], scheme: string) {
583+
const allActions = [...IBMi.connectionManager.get<Action[]>(`actions`) || []];
584+
585+
// Then, if we're being called from a local file
586+
// we fetch the Actions defined from the workspace.
587+
if (targets[0].workspaceFolder && scheme === `file`) {
588+
const localActions = await getLocalActions(targets[0].workspaceFolder);
589+
allActions.push(...localActions);
590+
}
591+
592+
// We make sure all extensions are uppercase
593+
allActions.forEach(action => {
594+
if (action.extensions) {
595+
action.extensions = action.extensions.map(ext => ext.toUpperCase());
596+
};
597+
});
598+
599+
// Then we get all the available Actions for the current context
600+
const availableActions: AvailableAction[] = allActions.filter(action => action.type === scheme)
601+
.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`))
602+
.filter(action => action.runOnProtected || !targets.some(t => t.protected))
603+
.sort((a, b) => (actionUsed.get(b.name) || 0) - (actionUsed.get(a.name) || 0))
604+
.map(action => ({
605+
label: action.name,
606+
action
607+
}));
608+
609+
return availableActions;
610+
}
607611

608612
function getObjectFromCommand(baseCommand?: string): CommandObject | undefined {
609613
if (baseCommand) {

0 commit comments

Comments
 (0)