Skip to content

Commit

Permalink
Merge pull request #2520 from BetterThanTomorrow/2519-inspector-item-…
Browse files Browse the repository at this point in the history
…commands

Make inspector commands public and act on the selected item
  • Loading branch information
PEZ authored Apr 19, 2024
2 parents 309f55a + b0f3f93 commit a1199fc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes to Calva.

## [Unreleased]

- [Make the inspector item context commands work on the selected item when not invoked via the buttons](https://github.com/BetterThanTomorrow/calva/issues/2519)

## [2.0.443] - 2024-04-18

- Fix: [Replace Current Form seems to stop working from v2.0.428 onwards](https://github.com/BetterThanTomorrow/calva/issues/2512)
Expand Down
13 changes: 9 additions & 4 deletions docs/site/inspector.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ The view itself has two buttons, one for clearing all results and one for pastin

## Commands

There are four commands for the inspector:
These are the commands available for the inspector:

* **Calva: Reveal Inspector**. Reveals/shows the inspector. Default keybinding is `ctrl+alt+o i`
* **Calva: Reveal Inspector**. Reveals/shows the inspector without focusing it. Default keybinding is `ctrl+alt+o i`
* This command, when used from keyboard shortcuts or [Joyride](https://github.com/BetterThanTomorrow/joyride), has the command ID of `calva.revealInspector`, and takes an object argument: `{ focus: boolean, select: boolean, expand: boolean | number }`. `focus` lets you focus the inspector view (or not), `select` and `expand` lets you select or expand the current item which, like with several of the inspector commands, will be the already selected item, or the topmost item if none is selected. It is passed to the TreeView backing the inspector, see [the VS Code API docs regarding revealing tree view items](https://code.visualstudio.com/api/references/vscode-api#TreeView.reveal) for details.
* **Calva: Add Selection or Current Form to Inspector**. Adds the current form (without evaluating it) to the inspector. If there is text selected, the command will add that text instead. (Only the first selection, if there are multiple). There is no default keybinding for this command.
* **Calva: Clear Inspector**. You can use the command or the button for this in the view. No default keybinding.
* This command has the ID of `calva.addToInspector`. The command takes an optional argument, a string to be added. This is meant for Joyride scripts that want to add things to the inspector.
* **Calva: Clear All Inspector Items**. You can use the command or the button for this in the view. No default keybinding.
* **Calva: Paste as Inspector Item**. Same as the view button. No default keybinding.
* **Calva: Inspect Item**. Makes the selected item inspectable, expands it one level, selects it, and focuses the inspector. Works on the topmost item if none is selected. No default keybinding.
* **Calva: Copy Inspector Item**. Copies the text of the selected inspector item, or the topmost item if none are selected. No default keybinding.
* **Calva: Clear Inspector Item**. Clears the selected inspector item, or the topmost item if none are selected. No default keybinding.

The command for adding the current form to the inspector has a command ID of `calva.addToInspector`. The command takes an optional argument, a string to be added. This is meant for [Joyride](https://github.com/BetterThanTomorrow/joyride) scripts that want to add things to the inspector.
The VS Code Shortcuts editor is a good tool for looking up command IDs and, duh, register custom shortcuts for these commands.

## Configuration

Expand Down
15 changes: 6 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2117,29 +2117,26 @@
{
"category": "Calva",
"command": "calva.clearInspector",
"title": "Clear All",
"title": "Clear All Inspector Items",
"icon": "$(close-all)"
},
{
"category": "Calva",
"command": "calva.clearInspectorItem",
"title": "Clear",
"icon": "$(close)",
"enablement": "view == calva.inspector && viewItem =~ /inspectable|raw/"
"title": "Clear Inspector Item",
"icon": "$(close)"
},
{
"category": "Calva",
"command": "calva.copyInspectorItem",
"title": "Copy",
"icon": "$(copy)",
"enablement": "view == calva.inspector"
"title": "Copy Inspector Item",
"icon": "$(copy)"
},
{
"category": "Calva",
"command": "calva.inspectItem",
"title": "Inspect Item",
"icon": "$(inspect)",
"enablement": "view == calva.inspector && viewItem == raw"
"icon": "$(inspect)"
},
{
"category": "Calva",
Expand Down
31 changes: 20 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,24 +325,33 @@ async function activate(context: vscode.ExtensionContext) {
resolve(true);
});
},
clearInspector: () => {
inspectorDataProvider.clearInspector.bind(inspectorDataProvider)();
},
clearInspectorItem: (arg) => {
inspectorDataProvider.clearInspector.bind(inspectorDataProvider)(arg);
},
copyInspectorItem: inspector.copyItemValue,
pasteAsInspectorItem: () => {
inspector.pasteFromClipboard.bind(inspectorDataProvider)();
},
addToInspector: (arg) => {
addToInspector: (arg: any) => {
inspector.addToInspector.bind(inspectorDataProvider)(arg);
},
clearInspector: () => {
inspectorDataProvider.clearInspector.bind(inspectorDataProvider)();
},
clearInspectorItem: (item) => {
const selectedItem =
item || inspectorTreeView.selection[0] || inspectorDataProvider.getTopMostItem();
inspectorDataProvider.clearInspector.bind(inspectorDataProvider)(selectedItem);
},
copyInspectorItem: (item) => {
const selectedItem =
item || inspectorTreeView.selection[0] || inspectorDataProvider.getTopMostItem();
return inspector.copyItemValue(selectedItem);
},
inspectItem: (item) => {
inspector.createTreeStructure.bind(inspectorDataProvider)(item);
const selectedItem =
item || inspectorTreeView.selection[0] || inspectorDataProvider.getTopMostItem();
inspector.createTreeStructure.bind(inspectorDataProvider)(selectedItem);
},
revealInspector: () => {
void inspectorTreeView.reveal(undefined, { focus: false, select: false });
revealInspector: ({ select = true, focus = false, expand = false } = {}) => {
const selectedItem = inspectorTreeView.selection[0] || inspectorDataProvider.getTopMostItem();
return inspectorTreeView.reveal(selectedItem, { select, focus, expand });
},
};

Expand Down
4 changes: 4 additions & 0 deletions src/providers/inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class InspectorDataProvider implements vscode.TreeDataProvider<InspectorI
return element ? element.parent : null;
}

public getTopMostItem(): InspectorItem | undefined {
return this.treeData[0];
}

getChildren(element?: InspectorItem): vscode.ProviderResult<InspectorItem[]> {
if (element) {
const children = Array.isArray(element.children)
Expand Down

0 comments on commit a1199fc

Please sign in to comment.