Skip to content

Commit

Permalink
Merge pull request #277 from jdanbrown/add-toggle-all-folds-for-chang…
Browse files Browse the repository at this point in the history
…e-views

Add command toggle-all-folds-for-change-views
  • Loading branch information
kahole authored Oct 8, 2023
2 parents 5c7676b + 2230d0d commit 284dd61
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@
{
"command": "magit.toggle-fold",
"title": "Magit Toggle Fold"
},
{
"command": "magit.toggle-all-folds-in-change-section-at-point",
"title": "Magit Toggle All Folds in Change Section at Point"
},
{
"command": "magit.toggle-all-folds-for-change-views",
"title": "Magit Toggle All Folds for Change Views"
}
],
"menus": {
Expand Down Expand Up @@ -367,6 +375,14 @@
{
"command": "magit.toggle-fold",
"when": "editorLangId == magit"
},
{
"command": "magit.toggle-all-folds-in-change-section-at-point",
"when": "editorLangId == magit"
},
{
"command": "magit.toggle-all-folds-for-change-views",
"when": "editorLangId == magit"
}
]
},
Expand Down
12 changes: 12 additions & 0 deletions src/commands/macros.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { commands, TextEditor, Range, window, Selection, TextEditorRevealType, Position } from 'vscode';
import { MagitRepository } from '../models/magitRepository';
import { ChangeSectionView } from '../views/changes/changesSectionView';
import { ChangeView } from '../views/changes/changeView';
import { DocumentView } from '../views/general/documentView';
import { View } from '../views/general/view';

Expand Down Expand Up @@ -37,6 +38,17 @@ export async function toggleAllFoldsInChangeSection(repo: MagitRepository, view:
}
}

export async function toggleAllFoldsForChangeViews(repo: MagitRepository, view: DocumentView) {
let changeViews = Array.from(view.walkAllSubViews()).filter(x => x instanceof ChangeView);
if (changeViews.length === 0) {
return;
}
const newFoldState = !changeViews[0].folded;
for (let changeView of changeViews) {
changeView.folded = newFoldState;
}
}

const subViewDepthSearchFlatten = (view: View, depth: number = 0): View[] => {
if (view.folded || depth >= 3) {
return [];
Expand Down
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { magitVisitAtPoint } from './commands/visitAtPointCommands';
import { MagitRepository } from './models/magitRepository';
import { magitCommit, setCodePath } from './commands/commitCommands';
import { magitStage, magitStageAll, magitUnstageAll, magitUnstage, stageFile, unstageFile } from './commands/stagingCommands';
import { saveClose, clearSaveClose, quitMagitView, toggleAllFoldsInChangeSection, moveToPreviousEntity, moveToNextEntity } from './commands/macros';
import { saveClose, clearSaveClose, quitMagitView, toggleAllFoldsForChangeViews, toggleAllFoldsInChangeSection, moveToPreviousEntity, moveToNextEntity } from './commands/macros';
import HighlightProvider from './providers/highlightProvider';
import SemanticTokensProvider from './providers/semanticTokensProvider';
import { CommandPrimer } from './commands/commandPrimer';
Expand Down Expand Up @@ -179,6 +179,7 @@ export function activate(context: ExtensionContext) {
context.subscriptions.push(commands.registerTextEditorCommand('magit.move-next-entity', CommandPrimer.primeRepoAndView(moveToNextEntity, false)));
context.subscriptions.push(commands.registerTextEditorCommand('magit.move-previous-entity', CommandPrimer.primeRepoAndView(moveToPreviousEntity, false)));
context.subscriptions.push(commands.registerTextEditorCommand('magit.toggle-all-folds-in-change-section-at-point', CommandPrimer.primeRepoAndView(toggleAllFoldsInChangeSection, true)));
context.subscriptions.push(commands.registerTextEditorCommand('magit.toggle-all-folds-for-change-views', CommandPrimer.primeRepoAndView(toggleAllFoldsForChangeViews, true)));

context.subscriptions.push(commands.registerTextEditorCommand('magit.save-and-close-editor', saveClose));
context.subscriptions.push(commands.registerTextEditorCommand('magit.clear-and-abort-editor', clearSaveClose));
Expand Down
9 changes: 8 additions & 1 deletion src/views/general/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ export abstract class View {
addSubview(...views: View[]) {
this.subViews.push(...views);
}
}

*walkAllSubViews(): Generator<View> {
for (let subView of this.subViews) {
yield subView;
yield* subView.walkAllSubViews();
}
}
}

0 comments on commit 284dd61

Please sign in to comment.