summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/toggle_whitespace_action.go
blob: 5e9c1bf1bcb925fb93bd7f5a5853a768cc493a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package controllers

import (
	"github.com/jesseduffield/lazygit/pkg/gui/context"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
	"github.com/samber/lo"
)

type ToggleWhitespaceAction struct {
	c *ControllerCommon
}

func (self *ToggleWhitespaceAction) Call() error {
	contextsThatDontSupportIgnoringWhitespace := []types.ContextKey{
		context.STAGING_MAIN_CONTEXT_KEY,
		context.STAGING_SECONDARY_CONTEXT_KEY,
		context.PATCH_BUILDING_MAIN_CONTEXT_KEY,
	}

	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.GetAppState().IgnoreWhitespaceInDiffView = !self.c.GetAppState().IgnoreWhitespaceInDiffView
	if err := self.c.SaveAppState(); err != nil {
		self.c.Log.Errorf("error when saving app state: %v", err)
	}

	return self.c.CurrentSideContext().HandleFocus(types.OnFocusOpts{})
}