|
| 1 | +// Handle the context commands. This are commands that are |
| 2 | +// launched from right-click menus. |
| 3 | + |
| 4 | +// Standard imports |
| 5 | +const vscode = require("vscode"); |
| 6 | + |
| 7 | +// Local imports. |
| 8 | +const apioLog = require("./apio-log.js"); |
| 9 | +const actions = require("./actions.js"); |
| 10 | + |
| 11 | +// A callback for handling commands invocations from vscode context |
| 12 | +// right click menu. |
| 13 | +async function contextCmdHandler(taskTitle, taskCmds, contextUri) { |
| 14 | + let targetUri; |
| 15 | + |
| 16 | + apioLog.msg("fileContextHandler() invoked"); |
| 17 | + apioLog.msg(`contextUri=${contextUri}`); |
| 18 | + |
| 19 | + // Determine the target URI. |
| 20 | + if (contextUri instanceof vscode.Uri) { |
| 21 | + // We got the uri from the explorer. |
| 22 | + targetUri = contextUri; |
| 23 | + } else { |
| 24 | + // Otherwise, the invocation from the editor editor. |
| 25 | + const activeEditor = vscode.window.activeTextEditor; |
| 26 | + if (activeEditor) { |
| 27 | + targetUri = activeEditor.document.uri; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // Error if we didn't figure out the. |
| 32 | + if (!targetUri) { |
| 33 | + vscode.window.showWarningMessage("No file selected or no active editor."); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // Convert the uri to a path relative to the workspace. |
| 38 | + const contextPath = vscode.workspace.asRelativePath(targetUri); |
| 39 | + |
| 40 | + // Expand the {context-path} placeholder in the task title and |
| 41 | + // in the commands. |
| 42 | + taskTitle = taskTitle.replace("{context-path}", contextPath); |
| 43 | + taskCmds = taskCmds.map((cmd) => cmd.replace("{context-path}", contextPath)); |
| 44 | + |
| 45 | + // Launch the commands as a task. |
| 46 | + launcher = actions.getActionLauncher( |
| 47 | + (taskTitle = taskTitle), |
| 48 | + (taskCmds = taskCmds), |
| 49 | + (taskCompletionMsgs = null), |
| 50 | + (urlToOpen = null), |
| 51 | + (cmdIdToInvoke = null), |
| 52 | + ); |
| 53 | + |
| 54 | + await launcher(); |
| 55 | +} |
| 56 | + |
| 57 | +// Register the handlers for the file context operations |
| 58 | +function registerFileContextHandlers(context, preCmds) { |
| 59 | + // Per context command cmdId, taskTitle, and taskCmds. |
| 60 | + const contextCmds = [ |
| 61 | + [ |
| 62 | + "apio.context.sim", |
| 63 | + "CONTEXT / SIM", |
| 64 | + [...preCmds, `{apio-bin} sim "{context-path}" {env-flag}`], |
| 65 | + ], |
| 66 | + [ |
| 67 | + "apio.context.test", |
| 68 | + "CONTEXT / TEST", |
| 69 | + [...preCmds, `{apio-bin} test "{context-path}" {env-flag}`], |
| 70 | + ], |
| 71 | + [ |
| 72 | + "apio.context.format", |
| 73 | + "CONTEXT / FORMAT", |
| 74 | + [...preCmds, `{apio-bin} format "{context-path}" {env-flag}`], |
| 75 | + ], |
| 76 | + ]; |
| 77 | + |
| 78 | + for (const [cmdId, taskTitle, taskCmds] of contextCmds) { |
| 79 | + context.subscriptions.push( |
| 80 | + vscode.commands.registerCommand(cmdId, (contextUri) => |
| 81 | + contextCmdHandler(taskTitle, taskCmds, contextUri), |
| 82 | + ), |
| 83 | + ); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Export for require() |
| 88 | +module.exports = { |
| 89 | + registerFileContextHandlers, |
| 90 | +}; |
0 commit comments