1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-15 11:56:37 +02:00

Merge pull request #2641 from stefanhaller/visualize-ignore-whitespace-config

This commit is contained in:
Jesse Duffield 2023-05-20 13:01:22 +10:00 committed by GitHub
commit 2304ffc804
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 100 additions and 55 deletions

View File

@ -59,8 +59,19 @@ func (self *StashCommands) Sha(index int) (string, error) {
return strings.Trim(sha, "\r\n"), err return strings.Trim(sha, "\r\n"), err
} }
func (self *StashCommands) ShowStashEntryCmdObj(index int) oscommands.ICmdObj { func (self *StashCommands) ShowStashEntryCmdObj(index int, ignoreWhitespace bool) oscommands.ICmdObj {
cmdStr := fmt.Sprintf("git stash show -p --stat --color=%s --unified=%d stash@{%d}", self.UserConfig.Git.Paging.ColorArg, self.UserConfig.Git.DiffContextSize, index) ignoreWhitespaceFlag := ""
if ignoreWhitespace {
ignoreWhitespaceFlag = " --ignore-all-space"
}
cmdStr := fmt.Sprintf(
"git stash show -p --stat --color=%s --unified=%d%s stash@{%d}",
self.UserConfig.Git.Paging.ColorArg,
self.UserConfig.Git.DiffContextSize,
ignoreWhitespaceFlag,
index,
)
return self.cmd.New(cmdStr).DontLog() return self.cmd.New(cmdStr).DontLog()
} }

View File

@ -99,24 +99,34 @@ func TestStashSha(t *testing.T) {
func TestStashStashEntryCmdObj(t *testing.T) { func TestStashStashEntryCmdObj(t *testing.T) {
type scenario struct { type scenario struct {
testName string testName string
index int index int
contextSize int contextSize int
expected string ignoreWhitespace bool
expected string
} }
scenarios := []scenario{ scenarios := []scenario{
{ {
testName: "Default case", testName: "Default case",
index: 5, index: 5,
contextSize: 3, contextSize: 3,
expected: "git stash show -p --stat --color=always --unified=3 stash@{5}", ignoreWhitespace: false,
expected: "git stash show -p --stat --color=always --unified=3 stash@{5}",
}, },
{ {
testName: "Show diff with custom context size", testName: "Show diff with custom context size",
index: 5, index: 5,
contextSize: 77, contextSize: 77,
expected: "git stash show -p --stat --color=always --unified=77 stash@{5}", ignoreWhitespace: false,
expected: "git stash show -p --stat --color=always --unified=77 stash@{5}",
},
{
testName: "Default case",
index: 5,
contextSize: 3,
ignoreWhitespace: true,
expected: "git stash show -p --stat --color=always --unified=3 --ignore-all-space stash@{5}",
}, },
} }
@ -127,7 +137,7 @@ func TestStashStashEntryCmdObj(t *testing.T) {
userConfig.Git.DiffContextSize = s.contextSize userConfig.Git.DiffContextSize = s.contextSize
instance := buildStashCommands(commonDeps{userConfig: userConfig}) instance := buildStashCommands(commonDeps{userConfig: userConfig})
cmdStr := instance.ShowStashEntryCmdObj(s.index).ToString() cmdStr := instance.ShowStashEntryCmdObj(s.index, s.ignoreWhitespace).ToString()
assert.Equal(t, s.expected, cmdStr) assert.Equal(t, s.expected, cmdStr)
}) })
} }

View File

@ -126,8 +126,9 @@ func (self *CommitFilesController) GetOnRenderToMain() func() error {
return self.c.RenderToMainViews(types.RefreshMainOpts{ return self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: pair, Pair: pair,
Main: &types.ViewUpdateOpts{ Main: &types.ViewUpdateOpts{
Title: self.c.Tr.Patch, Title: self.c.Tr.Patch,
Task: task, SubTitle: self.c.Helpers().Diff.IgnoringWhitespaceSubTitle(),
Task: task,
}, },
Secondary: secondaryPatchPanelUpdateOpts(self.c), Secondary: secondaryPatchPanelUpdateOpts(self.c),
}) })

View File

@ -174,8 +174,9 @@ func (self *FilesController) GetOnRenderToMain() func() error {
return self.c.RenderToMainViews(types.RefreshMainOpts{ return self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: self.c.MainViewPairs().Normal, Pair: self.c.MainViewPairs().Normal,
Main: &types.ViewUpdateOpts{ Main: &types.ViewUpdateOpts{
Title: self.c.Tr.DiffTitle, Title: self.c.Tr.DiffTitle,
Task: types.NewRenderStringTask(self.c.Tr.NoChangedFiles), SubTitle: self.c.Helpers().Diff.IgnoringWhitespaceSubTitle(),
Task: types.NewRenderStringTask(self.c.Tr.NoChangedFiles),
}, },
}) })
} }
@ -209,8 +210,9 @@ func (self *FilesController) GetOnRenderToMain() func() error {
refreshOpts := types.RefreshMainOpts{ refreshOpts := types.RefreshMainOpts{
Pair: pair, Pair: pair,
Main: &types.ViewUpdateOpts{ Main: &types.ViewUpdateOpts{
Task: types.NewRunPtyTask(cmdObj.GetCmd()), Task: types.NewRunPtyTask(cmdObj.GetCmd()),
Title: title, SubTitle: self.c.Helpers().Diff.IgnoringWhitespaceSubTitle(),
Title: title,
}, },
} }
@ -223,8 +225,9 @@ func (self *FilesController) GetOnRenderToMain() func() error {
} }
refreshOpts.Secondary = &types.ViewUpdateOpts{ refreshOpts.Secondary = &types.ViewUpdateOpts{
Title: title, Title: title,
Task: types.NewRunPtyTask(cmdObj.GetCmd()), SubTitle: self.c.Helpers().Diff.IgnoringWhitespaceSubTitle(),
Task: types.NewRunPtyTask(cmdObj.GetCmd()),
} }
} }

View File

@ -59,8 +59,9 @@ func (self *DiffHelper) RenderDiff() error {
return self.c.RenderToMainViews(types.RefreshMainOpts{ return self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: self.c.MainViewPairs().Normal, Pair: self.c.MainViewPairs().Normal,
Main: &types.ViewUpdateOpts{ Main: &types.ViewUpdateOpts{
Title: "Diff", Title: "Diff",
Task: task, SubTitle: self.IgnoringWhitespaceSubTitle(),
Task: task,
}, },
}) })
} }
@ -112,3 +113,11 @@ func (self *DiffHelper) WithDiffModeCheck(f func() error) error {
return f() return f()
} }
func (self *DiffHelper) IgnoringWhitespaceSubTitle() string {
if self.c.State().GetIgnoreWhitespaceInDiffView() {
return self.c.Tr.IgnoreWhitespaceDiffViewSubTitle
}
return ""
}

View File

@ -73,7 +73,9 @@ func (self *PatchBuildingHelper) RefreshPatchBuildingPanel(opts types.OnFocusOpt
ref := self.c.Contexts().CommitFiles.CommitFileTreeViewModel.GetRef() ref := self.c.Contexts().CommitFiles.CommitFileTreeViewModel.GetRef()
to := ref.RefName() to := ref.RefName()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName()) from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
diff, err := self.c.Git().WorkingTree.ShowFileDiff(from, to, reverse, path, true, self.c.State().GetIgnoreWhitespaceInDiffView()) // Passing false for ignoreWhitespace because the patch building panel
// doesn't work when whitespace is ignored
diff, err := self.c.Git().WorkingTree.ShowFileDiff(from, to, reverse, path, true, false)
if err != nil { if err != nil {
return err return err
} }

View File

@ -176,8 +176,9 @@ func (self *LocalCommitsController) GetOnRenderToMain() func() error {
return self.c.RenderToMainViews(types.RefreshMainOpts{ return self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: self.c.MainViewPairs().Normal, Pair: self.c.MainViewPairs().Normal,
Main: &types.ViewUpdateOpts{ Main: &types.ViewUpdateOpts{
Title: "Patch", Title: "Patch",
Task: task, SubTitle: self.c.Helpers().Diff.IgnoringWhitespaceSubTitle(),
Task: task,
}, },
Secondary: secondaryPatchPanelUpdateOpts(self.c), Secondary: secondaryPatchPanelUpdateOpts(self.c),
}) })

View File

@ -63,14 +63,20 @@ func (self *StashController) GetOnRenderToMain() func() error {
if stashEntry == nil { if stashEntry == nil {
task = types.NewRenderStringTask(self.c.Tr.NoStashEntries) task = types.NewRenderStringTask(self.c.Tr.NoStashEntries)
} else { } else {
task = types.NewRunPtyTask(self.c.Git().Stash.ShowStashEntryCmdObj(stashEntry.Index).GetCmd()) task = types.NewRunPtyTask(
self.c.Git().Stash.ShowStashEntryCmdObj(
stashEntry.Index,
self.c.State().GetIgnoreWhitespaceInDiffView(),
).GetCmd(),
)
} }
return self.c.RenderToMainViews(types.RefreshMainOpts{ return self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: self.c.MainViewPairs().Normal, Pair: self.c.MainViewPairs().Normal,
Main: &types.ViewUpdateOpts{ Main: &types.ViewUpdateOpts{
Title: "Stash", Title: "Stash",
Task: task, SubTitle: self.c.Helpers().Diff.IgnoringWhitespaceSubTitle(),
Task: task,
}, },
}) })
}) })

View File

@ -46,8 +46,9 @@ func (self *SubCommitsController) GetOnRenderToMain() func() error {
return self.c.RenderToMainViews(types.RefreshMainOpts{ return self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: self.c.MainViewPairs().Normal, Pair: self.c.MainViewPairs().Normal,
Main: &types.ViewUpdateOpts{ Main: &types.ViewUpdateOpts{
Title: "Commit", Title: "Commit",
Task: task, SubTitle: self.c.Helpers().Diff.IgnoringWhitespaceSubTitle(),
Task: task,
}, },
}) })
}) })

View File

@ -1,7 +1,9 @@
package controllers package controllers
import ( import (
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/samber/lo"
) )
type ToggleWhitespaceAction struct { type ToggleWhitespaceAction struct {
@ -9,13 +11,19 @@ type ToggleWhitespaceAction struct {
} }
func (self *ToggleWhitespaceAction) Call() error { func (self *ToggleWhitespaceAction) Call() error {
self.c.State().SetIgnoreWhitespaceInDiffView(!self.c.State().GetIgnoreWhitespaceInDiffView()) contextsThatDontSupportIgnoringWhitespace := []types.ContextKey{
context.STAGING_MAIN_CONTEXT_KEY,
toastMessage := self.c.Tr.ShowingWhitespaceInDiffView context.STAGING_SECONDARY_CONTEXT_KEY,
if self.c.State().GetIgnoreWhitespaceInDiffView() { context.PATCH_BUILDING_MAIN_CONTEXT_KEY,
toastMessage = self.c.Tr.IgnoringWhitespaceInDiffView
} }
self.c.Toast(toastMessage)
if lo.Contains(contextsThatDontSupportIgnoringWhitespace, self.c.CurrentContext().GetKey()) {
// Ignoring whitespace is not supported in these views. Let the user
// know that it's not going to work in case they try to turn it on.
return self.c.ErrorMsg(self.c.Tr.IgnoreWhitespaceNotSupportedHere)
}
self.c.State().SetIgnoreWhitespaceInDiffView(!self.c.State().GetIgnoreWhitespaceInDiffView())
return self.c.CurrentSideContext().HandleFocus(types.OnFocusOpts{}) return self.c.CurrentSideContext().HandleFocus(types.OnFocusOpts{})
} }

View File

@ -66,6 +66,8 @@ func (gui *Gui) RefreshMainView(opts *types.ViewUpdateOpts, context types.Contex
view.Title = opts.Title view.Title = opts.Title
} }
view.Subtitle = opts.SubTitle
if err := gui.runTaskForView(view, opts.Task); err != nil { if err := gui.runTaskForView(view, opts.Task); err != nil {
gui.c.Log.Error(err) gui.c.Log.Error(err)
return nil return nil

View File

@ -21,7 +21,8 @@ type MainViewPairs struct {
} }
type ViewUpdateOpts struct { type ViewUpdateOpts struct {
Title string Title string
SubTitle string
Task UpdateTask Task UpdateTask
} }

View File

@ -437,8 +437,6 @@ func chineseTranslationSet() TranslationSet {
RandomTip: "随机小提示", RandomTip: "随机小提示",
SelectParentCommitForMerge: "选择父提交进行合并", SelectParentCommitForMerge: "选择父提交进行合并",
ToggleWhitespaceInDiffView: "切换是否在差异视图中显示空白字符差异", ToggleWhitespaceInDiffView: "切换是否在差异视图中显示空白字符差异",
IgnoringWhitespaceInDiffView: "将会在差异视图中忽略空格字符差异",
ShowingWhitespaceInDiffView: "将会在差异视图中显示空白字符差异",
IncreaseContextInDiffView: "扩大差异视图中显示的上下文范围", IncreaseContextInDiffView: "扩大差异视图中显示的上下文范围",
DecreaseContextInDiffView: "缩小差异视图中显示的上下文范围", DecreaseContextInDiffView: "缩小差异视图中显示的上下文范围",
CreatePullRequest: "创建抓取请求", CreatePullRequest: "创建抓取请求",

View File

@ -481,8 +481,8 @@ type TranslationSet struct {
RandomTip string RandomTip string
SelectParentCommitForMerge string SelectParentCommitForMerge string
ToggleWhitespaceInDiffView string ToggleWhitespaceInDiffView string
IgnoringWhitespaceInDiffView string IgnoreWhitespaceDiffViewSubTitle string
ShowingWhitespaceInDiffView string IgnoreWhitespaceNotSupportedHere string
IncreaseContextInDiffView string IncreaseContextInDiffView string
DecreaseContextInDiffView string DecreaseContextInDiffView string
CreatePullRequestOptions string CreatePullRequestOptions string
@ -1152,8 +1152,8 @@ func EnglishTranslationSet() TranslationSet {
RandomTip: "Random Tip", RandomTip: "Random Tip",
SelectParentCommitForMerge: "Select parent commit for merge", SelectParentCommitForMerge: "Select parent commit for merge",
ToggleWhitespaceInDiffView: "Toggle whether or not whitespace changes are shown in the diff view", ToggleWhitespaceInDiffView: "Toggle whether or not whitespace changes are shown in the diff view",
IgnoringWhitespaceInDiffView: "Whitespace will be ignored in the diff view", IgnoreWhitespaceDiffViewSubTitle: "(ignoring whitespace)",
ShowingWhitespaceInDiffView: "Whitespace will be shown in the diff view", IgnoreWhitespaceNotSupportedHere: "Ignoring whitespace is not supported in this view",
IncreaseContextInDiffView: "Increase the size of the context shown around changes in the diff view", IncreaseContextInDiffView: "Increase the size of the context shown around changes in the diff view",
DecreaseContextInDiffView: "Decrease the size of the context shown around changes in the diff view", DecreaseContextInDiffView: "Decrease the size of the context shown around changes in the diff view",
CreatePullRequest: "Create pull request", CreatePullRequest: "Create pull request",

View File

@ -452,9 +452,7 @@ func japaneseTranslationSet() TranslationSet {
CommandLogHeader: "コマンドログの表示/非表示は '%s' で切り替えられます。\n", CommandLogHeader: "コマンドログの表示/非表示は '%s' で切り替えられます。\n",
RandomTip: "ランダムTips", RandomTip: "ランダムTips",
// SelectParentCommitForMerge: "Select parent commit for merge", // SelectParentCommitForMerge: "Select parent commit for merge",
ToggleWhitespaceInDiffView: "空白文字の差分の表示有無を切り替え", ToggleWhitespaceInDiffView: "空白文字の差分の表示有無を切り替え",
IgnoringWhitespaceInDiffView: "空白文字の変更は差分画面に表示されません",
ShowingWhitespaceInDiffView: "空白文字の変更は差分画面に表示されます",
// IncreaseContextInDiffView: "Increase the size of the context shown around changes in the diff view", // IncreaseContextInDiffView: "Increase the size of the context shown around changes in the diff view",
// DecreaseContextInDiffView: "Decrease the size of the context shown around changes in the diff view", // DecreaseContextInDiffView: "Decrease the size of the context shown around changes in the diff view",
CreatePullRequest: "pull requestを作成", CreatePullRequest: "pull requestを作成",

View File

@ -452,8 +452,6 @@ func koreanTranslationSet() TranslationSet {
RandomTip: "랜덤 Tip", RandomTip: "랜덤 Tip",
SelectParentCommitForMerge: "병합을 위한 상위 커밋 선택", SelectParentCommitForMerge: "병합을 위한 상위 커밋 선택",
ToggleWhitespaceInDiffView: "공백문자를 Diff 뷰에서 표시 여부 전환", ToggleWhitespaceInDiffView: "공백문자를 Diff 뷰에서 표시 여부 전환",
IgnoringWhitespaceInDiffView: "공백문자를 Diff 뷰에서 무시",
ShowingWhitespaceInDiffView: "공백문자를 Diff 뷰에서 표시",
IncreaseContextInDiffView: "diff 보기의 변경 사항 주위에 표시되는 컨텍스트의 크기를 늘리기", IncreaseContextInDiffView: "diff 보기의 변경 사항 주위에 표시되는 컨텍스트의 크기를 늘리기",
DecreaseContextInDiffView: "diff 보기의 변경 사항 주위에 표시되는 컨텍스트 크기 줄이기", DecreaseContextInDiffView: "diff 보기의 변경 사항 주위에 표시되는 컨텍스트 크기 줄이기",
CreatePullRequest: "풀 리퀘스트 생성", CreatePullRequest: "풀 리퀘스트 생성",

View File

@ -35,8 +35,6 @@ var IgnoreWhitespace = NewIntegrationTest(NewIntegrationTestArgs{
IsFocused(). IsFocused().
Press(keys.Universal.ToggleWhitespaceInDiffView) Press(keys.Universal.ToggleWhitespaceInDiffView)
t.ExpectToast(Equals("Whitespace will be ignored in the diff view"))
// lines with only whitespace changes are ignored (first and third lines) // lines with only whitespace changes are ignored (first and third lines)
t.Views().Main().ContainsLines( t.Views().Main().ContainsLines(
Contains(` first-line`), Contains(` first-line`),
@ -50,8 +48,6 @@ var IgnoreWhitespace = NewIntegrationTest(NewIntegrationTestArgs{
IsFocused(). IsFocused().
Press(keys.Universal.ToggleWhitespaceInDiffView) Press(keys.Universal.ToggleWhitespaceInDiffView)
t.ExpectToast(Equals("Whitespace will be shown in the diff view"))
t.Views().Main().ContainsLines( t.Views().Main().ContainsLines(
Contains(`-first-line`), Contains(`-first-line`),
Contains(`-old-second-line`), Contains(`-old-second-line`),