Skip to content

Commit 2961c99

Browse files
committed
Add user config to use hunk mode by default when entering staging view
1 parent a6001dc commit 2961c99

File tree

6 files changed

+23
-7
lines changed

6 files changed

+23
-7
lines changed

docs/Config.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ gui:
116116
# paragraphs of markdown text.
117117
wrapLinesInStagingView: true
118118

119+
# If true, hunk selection mode will be enabled by default when entering the staging view.
120+
useHunkModeInStagingView: false
121+
119122
# One of 'auto' (default) | 'en' | 'zh-CN' | 'zh-TW' | 'pl' | 'nl' | 'ja' | 'ko' | 'ru'
120123
language: auto
121124

pkg/config/user_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ type GuiConfig struct {
107107
// makes it much easier to work with diffs that have long lines, e.g.
108108
// paragraphs of markdown text.
109109
WrapLinesInStagingView bool `yaml:"wrapLinesInStagingView"`
110+
// If true, hunk selection mode will be enabled by default when entering the staging view.
111+
UseHunkModeInStagingView bool `yaml:"useHunkModeInStagingView"`
110112
// One of 'auto' (default) | 'en' | 'zh-CN' | 'zh-TW' | 'pl' | 'nl' | 'ja' | 'ko' | 'ru'
111113
Language string `yaml:"language" jsonschema:"enum=auto,enum=en,enum=zh-TW,enum=zh-CN,enum=pl,enum=nl,enum=ja,enum=ko,enum=ru"`
112114
// Format used when displaying time e.g. commit time.
@@ -745,6 +747,7 @@ func GetDefaultConfig() *UserConfig {
745747
MainPanelSplitMode: "flexible",
746748
EnlargedSideViewLocation: "left",
747749
WrapLinesInStagingView: true,
750+
UseHunkModeInStagingView: false,
748751
Language: "auto",
749752
TimeFormat: "02 Jan 06",
750753
ShortTimeFormat: time.Kitchen,

pkg/gui/controllers/helpers/patch_building_helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (self *PatchBuildingHelper) RefreshPatchBuildingPanel(opts types.OnFocusOpt
8484

8585
oldState := context.GetState()
8686

87-
state := patch_exploring.NewState(diff, selectedLineIdx, context.GetView(), oldState)
87+
state := patch_exploring.NewState(diff, selectedLineIdx, context.GetView(), oldState, self.c.UserConfig().Gui.UseHunkModeInStagingView)
8888
context.SetState(state)
8989
if state == nil {
9090
self.Escape()

pkg/gui/controllers/helpers/staging_helper.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ func (self *StagingHelper) RefreshStagingPanel(focusOpts types.OnFocusOpts) {
6262
mainContext.GetMutex().Lock()
6363
secondaryContext.GetMutex().Lock()
6464

65+
hunkMode := self.c.UserConfig().Gui.UseHunkModeInStagingView
6566
mainContext.SetState(
66-
patch_exploring.NewState(mainDiff, mainSelectedLineIdx, mainContext.GetView(), mainContext.GetState()),
67+
patch_exploring.NewState(mainDiff, mainSelectedLineIdx, mainContext.GetView(), mainContext.GetState(), hunkMode),
6768
)
6869

6970
secondaryContext.SetState(
70-
patch_exploring.NewState(secondaryDiff, secondarySelectedLineIdx, secondaryContext.GetView(), secondaryContext.GetState()),
71+
patch_exploring.NewState(secondaryDiff, secondarySelectedLineIdx, secondaryContext.GetView(), secondaryContext.GetState(), hunkMode),
7172
)
7273

7374
mainState := mainContext.GetState()

pkg/gui/patch_exploring/state.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const (
3939
HUNK
4040
)
4141

42-
func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *State) *State {
42+
func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *State, useHunkModeByDefault bool) *State {
4343
if oldState != nil && diff == oldState.diff && selectedLineIdx == -1 {
4444
// if we're here then we can return the old state. If selectedLineIdx was not -1
4545
// then that would mean we were trying to click and potentially drag a range, which
@@ -61,6 +61,10 @@ func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *Stat
6161
}
6262

6363
selectMode := LINE
64+
if useHunkModeByDefault {
65+
selectMode = HUNK
66+
}
67+
6468
// if we have clicked from the outside to focus the main view we'll pass in a non-negative line index so that we can instantly select that line
6569
if selectedLineIdx >= 0 {
6670
// Clamp to the number of wrapped view lines; index might be out of
@@ -70,9 +74,9 @@ func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *Stat
7074
selectMode = RANGE
7175
rangeStartLineIdx = selectedLineIdx
7276
} else if oldState != nil {
73-
// if we previously had a selectMode of RANGE, we want that to now be line again
74-
if oldState.selectMode == HUNK {
75-
selectMode = HUNK
77+
// if we previously had a selectMode of RANGE, we want that to now be line again (or hunk, if that's the default)
78+
if oldState.selectMode != RANGE {
79+
selectMode = oldState.selectMode
7680
}
7781
selectedLineIdx = viewLineIndices[patch.GetNextChangeIdx(oldState.patchLineIndices[oldState.selectedLineIdx])]
7882
} else {

schema/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@
530530
"description": "If true, wrap lines in the staging view to the width of the view. This\nmakes it much easier to work with diffs that have long lines, e.g.\nparagraphs of markdown text.",
531531
"default": true
532532
},
533+
"useHunkModeInStagingView": {
534+
"type": "boolean",
535+
"description": "If true, hunk selection mode will be enabled by default when entering the staging view.",
536+
"default": false
537+
},
533538
"language": {
534539
"type": "string",
535540
"enum": [

0 commit comments

Comments
 (0)