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

This commit is contained in:
Viktor Suprun 2022-02-08 17:15:04 +11:00 committed by GitHub
parent 60f203385e
commit 4d939845d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 9 deletions

View File

@ -5,6 +5,8 @@
package user package user
const ( const (
// SettingsKeyHiddenCommentTypes is the settings key for hidden comment types // SettingsKeyHiddenCommentTypes is the setting key for hidden comment types
SettingsKeyHiddenCommentTypes = "issue.hidden_comment_types" SettingsKeyHiddenCommentTypes = "issue.hidden_comment_types"
// SettingsKeyDiffWhitespaceBehavior is the setting key for whitespace behavior of diff
SettingsKeyDiffWhitespaceBehavior = "diff.whitespace_behaviour"
) )

View File

@ -63,11 +63,29 @@ func SetDiffViewStyle(ctx *context.Context) {
// SetWhitespaceBehavior set whitespace behavior as render variable // SetWhitespaceBehavior set whitespace behavior as render variable
func SetWhitespaceBehavior(ctx *context.Context) { func SetWhitespaceBehavior(ctx *context.Context) {
const defaultWhitespaceBehavior = "show-all"
whitespaceBehavior := ctx.FormString("whitespace") whitespaceBehavior := ctx.FormString("whitespace")
switch whitespaceBehavior { switch whitespaceBehavior {
case "ignore-all", "ignore-eol", "ignore-change": case "", "ignore-all", "ignore-eol", "ignore-change":
ctx.Data["WhitespaceBehavior"] = whitespaceBehavior break
default: 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)
}
} // else: we can ignore the error safely
}
// these behaviors are for gitdiff.GetWhitespaceFlag
if whitespaceBehavior == "" {
ctx.Data["WhitespaceBehavior"] = defaultWhitespaceBehavior
} else {
ctx.Data["WhitespaceBehavior"] = whitespaceBehavior
} }
} }

View File

@ -1532,13 +1532,17 @@ func CommentMustAsDiff(c *models.Comment) *Diff {
} }
// GetWhitespaceFlag returns git diff flag for treating whitespaces // GetWhitespaceFlag returns git diff flag for treating whitespaces
func GetWhitespaceFlag(whiteSpaceBehavior string) string { func GetWhitespaceFlag(whitespaceBehavior string) string {
whitespaceFlags := map[string]string{ whitespaceFlags := map[string]string{
"ignore-all": "-w", "ignore-all": "-w",
"ignore-change": "-b", "ignore-change": "-b",
"ignore-eol": "--ignore-space-at-eol", "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 ""
} }

View File

@ -2,8 +2,8 @@
{{.i18n.Tr "repo.diff.whitespace_button"}} {{.i18n.Tr "repo.diff.whitespace_button"}}
{{svg "octicon-triangle-down" 14 "dropdown icon"}} {{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="menu"> <div class="menu">
<a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace="> <a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=show-all">
<i class="circle {{ if eq .WhitespaceBehavior "" }}dot{{else}}outline{{end}} icon"></i> <i class="circle {{ if eq .WhitespaceBehavior "show-all" }}dot{{else}}outline{{end}} icon"></i>
{{.i18n.Tr "repo.diff.whitespace_show_everything"}} {{.i18n.Tr "repo.diff.whitespace_show_everything"}}
</a> </a>
<a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=ignore-all"> <a class="item" href="?style={{if .IsSplitStyle}}split{{else}}unified{{end}}&whitespace=ignore-all">