|
| 1 | +# Issue #31: Focus Command - Open and Show Code Organizer View |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +Users need a quick way to open and focus the Code Organizer view when it's closed or hidden. Currently: |
| 6 | +- Users must manually find and click the activity bar icon |
| 7 | +- No keyboard shortcut exists |
| 8 | +- No command in Command Palette to quickly access the view |
| 9 | + |
| 10 | +**Goal:** Create a "Show Code Organizer" command that: |
| 11 | +1. Opens the Code Organizer activity bar tab if closed |
| 12 | +2. Focuses the "Sections" view |
| 13 | +3. Makes the view immediately visible and accessible |
| 14 | +4. Works via Command Palette or keyboard shortcut |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## API Research Summary |
| 19 | + |
| 20 | +### Auto-Generated `.focus` Commands |
| 21 | + |
| 22 | +VS Code automatically creates commands for every registered TreeView: |
| 23 | +- **Pattern:** `<viewId>.focus` |
| 24 | +- **Our view:** `codeOrganizerOutlineActivity.focus` |
| 25 | +- **Behavior:** Opens container (if closed) + focuses the specific view |
| 26 | +- **No package.json definition needed** - it's auto-generated! |
| 27 | + |
| 28 | +### Related Commands |
| 29 | + |
| 30 | +VS Code also generates: |
| 31 | +- `workbench.view.extension.codeOrganizer` - Opens the container (but doesn't focus specific view) |
| 32 | +- `workbench.actions.treeView.codeOrganizerOutlineActivity.refresh` - Refresh tree |
| 33 | +- `workbench.actions.treeView.codeOrganizerOutlineActivity.collapseAll` - Collapse all |
| 34 | + |
| 35 | +**See:** [.context/vs-code-api/view-focus-commands.md](.context/vs-code-api/view-focus-commands.md) for complete documentation |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## Implementation Plan |
| 40 | + |
| 41 | +### Approach: Wrapper Command |
| 42 | + |
| 43 | +Create a user-friendly wrapper command that calls the auto-generated `.focus` command: |
| 44 | + |
| 45 | +```typescript |
| 46 | +// In extension.ts |
| 47 | +context.subscriptions.push( |
| 48 | + vscode.commands.registerCommand('codeOrganizer.showView', async () => { |
| 49 | + // Use auto-generated .focus command |
| 50 | + await vscode.commands.executeCommand('codeOrganizerOutlineActivity.focus'); |
| 51 | + }) |
| 52 | +); |
| 53 | +``` |
| 54 | + |
| 55 | +### Benefits |
| 56 | +- ✅ Simple and reliable (uses VS Code's built-in behavior) |
| 57 | +- ✅ User-friendly command name ("Show Code Organizer") |
| 58 | +- ✅ Can add keyboard shortcut |
| 59 | +- ✅ Appears in Command Palette with proper category |
| 60 | +- ✅ No complex logic needed |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## Implementation Steps |
| 65 | + |
| 66 | +### Step 1: Register Command in extension.ts |
| 67 | + |
| 68 | +**Location:** `src/extension.ts` (in `activate` function) |
| 69 | + |
| 70 | +**Code to add:** |
| 71 | +```typescript |
| 72 | +// Register "Show Code Organizer" command |
| 73 | +context.subscriptions.push( |
| 74 | + vscode.commands.registerCommand('codeOrganizer.showView', async () => { |
| 75 | + // Use the auto-generated .focus command for our view |
| 76 | + await vscode.commands.executeCommand('codeOrganizerOutlineActivity.focus'); |
| 77 | + }) |
| 78 | +); |
| 79 | +``` |
| 80 | + |
| 81 | +**Placement:** After TreeView registration, before event listeners |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +### Step 2: Add Command to package.json |
| 86 | + |
| 87 | +**Location:** `package.json` → `contributes.commands` |
| 88 | + |
| 89 | +**Code to add:** |
| 90 | +```json |
| 91 | +{ |
| 92 | + "command": "codeOrganizer.showView", |
| 93 | + "title": "Show Code Organizer", |
| 94 | + "category": "Code Organizer" |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +**Result in Command Palette:** |
| 99 | +``` |
| 100 | +Code Organizer: Show Code Organizer |
| 101 | +``` |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +### Step 3: Add Keyboard Shortcut (Optional) |
| 106 | + |
| 107 | +**Location:** `package.json` → `contributes.keybindings` |
| 108 | + |
| 109 | +**Code to add:** |
| 110 | +```json |
| 111 | +{ |
| 112 | + "command": "codeOrganizer.showView", |
| 113 | + "key": "ctrl+shift+o", |
| 114 | + "mac": "cmd+shift+o", |
| 115 | + "when": "editorTextFocus" |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +**Note:** `Ctrl+Shift+O` is commonly used for "Go to Symbol" - might conflict. Consider alternative: |
| 120 | +- `Ctrl+Alt+O` (Windows/Linux) |
| 121 | +- `Cmd+Alt+O` (Mac) |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## Testing Checklist |
| 126 | + |
| 127 | +- [ ] Command appears in Command Palette as "Code Organizer: Show Code Organizer" |
| 128 | +- [ ] Executing command opens Code Organizer activity bar tab (if closed) |
| 129 | +- [ ] Executing command focuses "Sections" view |
| 130 | +- [ ] Works when view is already open (no errors) |
| 131 | +- [ ] Keyboard shortcut triggers command (if implemented) |
| 132 | +- [ ] Command works from any file type (respects `when` clause if added) |
| 133 | +- [ ] No console errors in Developer Tools |
| 134 | + |
| 135 | +### Test Scenarios |
| 136 | + |
| 137 | +1. **View Closed → Open:** |
| 138 | + - Close Code Organizer activity bar tab |
| 139 | + - Execute `Code Organizer: Show Code Organizer` |
| 140 | + - ✅ View should open and be visible |
| 141 | + |
| 142 | +2. **View Open → Focus:** |
| 143 | + - Code Organizer already open |
| 144 | + - Switch to another activity bar tab (Explorer) |
| 145 | + - Execute command |
| 146 | + - ✅ Should switch back to Code Organizer |
| 147 | + |
| 148 | +3. **Keyboard Shortcut:** |
| 149 | + - Press configured shortcut |
| 150 | + - ✅ Should behave same as Command Palette |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## Alternative Approaches (Not Recommended) |
| 155 | + |
| 156 | +### ❌ Alternative 1: Use Container Command Only |
| 157 | +```typescript |
| 158 | +await vscode.commands.executeCommand('workbench.view.extension.codeOrganizer'); |
| 159 | +``` |
| 160 | +**Problem:** Opens container but doesn't focus the specific "Sections" view |
| 161 | + |
| 162 | +### ❌ Alternative 2: Use TreeView.reveal() |
| 163 | +```typescript |
| 164 | +const sections = treeDataProvider.getSections(); |
| 165 | +if (sections.length > 0) { |
| 166 | + const firstItem = treeDataProvider.getOrCreateTreeItem(sections[0]); |
| 167 | + await treeView.reveal(firstItem, { focus: true }); |
| 168 | +} |
| 169 | +``` |
| 170 | +**Problem:** Requires tree to have items (won't work on empty tree) |
| 171 | + |
| 172 | +### ✅ Recommended: Use Auto-Generated `.focus` Command |
| 173 | +**Why:** Simple, reliable, works in all scenarios |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +## Current Commands Overview |
| 178 | + |
| 179 | +After implementation, Code Organizer will have: |
| 180 | + |
| 181 | +| Command | Purpose | User-Facing | |
| 182 | +|---------|---------|-------------| |
| 183 | +| `codeOrganizer.activate` | Initialize extension | Yes (Command Palette) | |
| 184 | +| `codeOrganizer.showView` | **NEW:** Open/focus view | Yes (Command Palette + Shortcut) | |
| 185 | +| `codeOrganizer.goToSection` | Jump to section (programmatic) | No (internal only) | |
| 186 | +| `codeOrganizerOutlineActivity.focus` | Focus view (auto-generated) | Yes (but generic name) | |
| 187 | + |
| 188 | +**User-friendly workflow:** |
| 189 | +- Users see: "Code Organizer: Show Code Organizer" in Command Palette |
| 190 | +- Behind the scenes: Executes auto-generated `.focus` command |
| 191 | + |
| 192 | +--- |
| 193 | + |
| 194 | +## Difficulty Assessment |
| 195 | + |
| 196 | +**Difficulty:** 🟢 **Easy** |
| 197 | + |
| 198 | +**Reasoning:** |
| 199 | +- VS Code provides auto-generated `.focus` command |
| 200 | +- Implementation is 3 lines of code (wrapper command) |
| 201 | +- No complex logic or state management needed |
| 202 | +- Well-documented API behavior |
| 203 | +- Low risk of bugs or edge cases |
| 204 | + |
| 205 | +**Estimated time:** 10 minutes (coding + testing) |
| 206 | + |
| 207 | +--- |
| 208 | + |
| 209 | +## Implementation Commit Checklist |
| 210 | + |
| 211 | +- [ ] Add `codeOrganizer.showView` command registration in `src/extension.ts` |
| 212 | +- [ ] Add command definition to `package.json` → `contributes.commands` |
| 213 | +- [ ] (Optional) Add keyboard shortcut to `package.json` → `contributes.keybindings` |
| 214 | +- [ ] Compile and test in Extension Development Host |
| 215 | +- [ ] Verify command appears in Command Palette |
| 216 | +- [ ] Test with view closed and open |
| 217 | +- [ ] Commit with message: "Add 'Show Code Organizer' focus command #31" |
| 218 | + |
| 219 | +--- |
| 220 | + |
| 221 | +## Related Documentation |
| 222 | + |
| 223 | +- [.context/vs-code-api/view-focus-commands.md](.context/vs-code-api/view-focus-commands.md) - Complete API documentation |
| 224 | +- [.context/features/issue-22.md](.context/features/issue-22.md) - View container strategy |
| 225 | +- [.context/features/issue-29.md](.context/features/issue-29.md) - Editor → Outline sync implementation |
| 226 | + |
| 227 | +--- |
| 228 | + |
| 229 | +## Notes |
| 230 | + |
| 231 | +- This complements the existing `codeOrganizer.activate` command (which was a backup for activation issues) |
| 232 | +- The auto-generated `.focus` command is more reliable than manually managing view visibility |
| 233 | +- Users can still click the activity bar icon - this just adds a faster alternative |
| 234 | +- Consider adding to extension README.md as a "Quick Start" tip |
0 commit comments