Skip to content

Commit

Permalink
fixed missing updates to determine the number of selected cells
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderPho committed Sep 7, 2024
1 parent 155876c commit 1a73e91
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"title": "Add Tags to Selected Cells",
"icon": "$(add)",
"when": "jupyter-cell-tags.multipleCellsSelected"
},
},
{
"command": "jupyter-cell-tags.editTagsInJSON",
"title": "Edit Cell Tags (JSON)",
Expand Down Expand Up @@ -133,7 +133,7 @@
},
"enabledApiProposals": [
"notebookCellExecutionState"
],
],
"devDependencies": {
"@types/vscode": "^1.72.0",
"@types/glob": "^7.1.3",
Expand Down
19 changes: 17 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { register as registerCellTagsView } from './cellTagsTreeDataProvider';
export function activate(context: vscode.ExtensionContext) {
registerCellTags(context);
registerCellTagsView(context);

// Update context when the active editor or selection changes
vscode.window.onDidChangeActiveNotebookEditor(updateContext);
vscode.window.onDidChangeNotebookEditorSelection(updateContext);
Expand All @@ -25,7 +25,22 @@ function updateContext() {
return;
}

const selectionCount = editor.selections.length;
const selections: readonly vscode.NotebookRange[] = editor.selections; // NotebookRange[]
const selectedRangesCount = selections.length;
var total_num_selected_cells = 0;
selections.forEach(selection => {
const range = selection as vscode.NotebookRange;
if (!range.isEmpty) {
const num_selected_cells = range.end - range.start
total_num_selected_cells += num_selected_cells;
}
});
// TODO 2024-09-05 17:47: - [ ] Got num selected cells nearly working, it will always be correct to tell if 1 vs. many cells.
// Noticed error below, there were only 3 cells in the notebook but it returned 4 cells. I think the last index should be excluded but then it would give zero for single cell selections?
// Selection num ranges count: 1
// Selected cells: Start(0), End(4)
// Selection count: 4
const selectionCount = total_num_selected_cells;
vscode.commands.executeCommand('setContext', 'jupyter-cell-tags.singleCellSelected', selectionCount === 1);
vscode.commands.executeCommand('setContext', 'jupyter-cell-tags.multipleCellsSelected', selectionCount > 1);
}
Expand Down

0 comments on commit 1a73e91

Please sign in to comment.