Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added auto-save whitespace behavior if it changed manually #15566

Merged
merged 9 commits into from
Feb 8, 2022
4 changes: 3 additions & 1 deletion models/user/setting_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package user

const (
// SettingsKeyHiddenCommentTypes is the settings key for hidden comment types
// SettingsKeyHiddenCommentTypes is the setting key for hidden comment types
SettingsKeyHiddenCommentTypes = "issue.hidden_comment_types"
// SettingsKeyDiffWhitespaceBehavior is the setting key for whitespace behavior of diff
SettingsKeyDiffWhitespaceBehavior = "diff.whitespace_behaviour"
)
24 changes: 21 additions & 3 deletions routers/web/repo/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,29 @@ func SetDiffViewStyle(ctx *context.Context) {

// SetWhitespaceBehavior set whitespace behavior as render variable
func SetWhitespaceBehavior(ctx *context.Context) {
const defaultWhitespaceBehavior = "show-all"
whitespaceBehavior := ctx.FormString("whitespace")
switch whitespaceBehavior {
case "ignore-all", "ignore-eol", "ignore-change":
ctx.Data["WhitespaceBehavior"] = whitespaceBehavior
case "", "ignore-all", "ignore-eol", "ignore-change":
break
default:
ctx.Data["WhitespaceBehavior"] = ""
whitespaceBehavior = defaultWhitespaceBehavior
}
if ctx.IsSigned {
userWhitespaceBehavior, err := user_model.GetUserSetting(ctx.User.ID, user_model.SettingsKeyDiffWhitespaceBehavior, defaultWhitespaceBehavior)
if err == nil {
if whitespaceBehavior == "" {
whitespaceBehavior = userWhitespaceBehavior
} else if whitespaceBehavior != userWhitespaceBehavior {
_ = user_model.SetUserSetting(ctx.User.ID, user_model.SettingsKeyDiffWhitespaceBehavior, whitespaceBehavior)
lafriks marked this conversation as resolved.
Show resolved Hide resolved
}
} // else: we can ignore the error safely
}

// these behaviors are for gitdiff.GetWhitespaceFlag
if whitespaceBehavior == "" {
ctx.Data["WhitespaceBehavior"] = defaultWhitespaceBehavior
} else {
ctx.Data["WhitespaceBehavior"] = whitespaceBehavior
}
}
10 changes: 7 additions & 3 deletions services/gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1532,13 +1532,17 @@ func CommentMustAsDiff(c *models.Comment) *Diff {
}

// GetWhitespaceFlag returns git diff flag for treating whitespaces
func GetWhitespaceFlag(whiteSpaceBehavior string) string {
func GetWhitespaceFlag(whitespaceBehavior string) string {
whitespaceFlags := map[string]string{
"ignore-all": "-w",
"ignore-change": "-b",
"ignore-eol": "--ignore-space-at-eol",
"": "",
"show-all": "",
}

return whitespaceFlags[whiteSpaceBehavior]
if flag, ok := whitespaceFlags[whitespaceBehavior]; ok {
return flag
}
log.Warn("unknown whitespace behavior: %q, default to 'show-all'", whitespaceBehavior)
return ""
}
4 changes: 2 additions & 2 deletions templates/repo/diff/whitespace_dropdown.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{{.i18n.Tr "repo.diff.whitespace_button"}}
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="menu">
<a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=">
<i class="circle {{ if eq .WhitespaceBehavior "" }}dot{{else}}outline{{end}} icon"></i>
<a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=show-all">
<i class="circle {{ if eq .WhitespaceBehavior "show-all" }}dot{{else}}outline{{end}} icon"></i>
{{.i18n.Tr "repo.diff.whitespace_show_everything"}}
</a>
<a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=ignore-all">
Expand Down