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

Add ui.EDITOR_EOL, default to saving files with LF again #28119

Closed
wants to merge 10 commits into from

Conversation

silverwind
Copy link
Member

@silverwind silverwind commented Nov 19, 2023

Fixes: #28097
Replaces: #28101

Assuming we consider it a bug that people on Windows get CRLF endings without a editorconfig, we should backport this to 1.21.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Nov 19, 2023
@pull-request-size pull-request-size bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Nov 19, 2023
@silverwind silverwind added the backport/v1.21 This PR should be backported to Gitea 1.21 label Nov 19, 2023
@silverwind silverwind added the type/enhancement An improvement of existing functionality label Nov 19, 2023
@@ -34,6 +34,7 @@ var UI = struct {
SearchRepoDescription bool
OnlyShowRelevantRepos bool
ExploreDefaultSort string `ini:"EXPLORE_PAGING_DEFAULT_SORT"`
EditorEol string `ini:"EDITOR_EOL"`
Copy link
Member Author

@silverwind silverwind Nov 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this ini tag here is unnecessary because thiscase conversation is done implicitely, but I do like this expliciteness so that the name stays greppable.

By the way, where does this case conversation happen? It's not from the ini package, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the correct Go name would even be EditorEOL

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Nov 19, 2023
@wxiaoguang
Copy link
Contributor

I do not think this is right.

See the discussion #27141 (comment)

Even if you set model.setEOL(monaco.editor.EndOfLineSequence.LF);, you still get \r\n when submitting the form

image

I believe the best choice is to revert #27141 at the moment, make 1.21 users happy. At least, "\n" is more widely used but "\r\n" causes problems in POSIX.

@silverwind
Copy link
Member Author

If that's because of textarea behaviour, we can fix this by setting the textarea value.

@pull-request-size pull-request-size bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Nov 21, 2023
@silverwind
Copy link
Member Author

silverwind commented Nov 21, 2023

Added the previous server-side eol convertion back, but it now respects the new setting of LF vs. CRLF too: e58dcc0

@silverwind
Copy link
Member Author

I guess the only thing missing here is to respect editorconfig on server-side as well, but for all intends, this will restore the previous replacement to LF that was done server-side.

@pull-request-size pull-request-size bot removed the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 21, 2023
@silverwind
Copy link
Member Author

silverwind commented Nov 21, 2023

I guess the only thing missing here is to respect editorconfig on server-side as well, but for all intends, this will restore the previous replacement to LF that was done server-side.

Done as well in 2dfcfe0, now the server also respects the editorconfig for this replacement. I guess it's still good to retain the client-side stuff so that copy out of monaco copies with the correct line endings.

@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 21, 2023
@silverwind silverwind changed the title Add ui.EDITOR_EOL setting and use ist instead of monaco's detection Add ui.EDITOR_EOL setting, default to saving files with LF again Nov 21, 2023
@silverwind silverwind changed the title Add ui.EDITOR_EOL setting, default to saving files with LF again Add ui.EDITOR_EOL, default to saving files with LF again Nov 21, 2023
@wxiaoguang
Copy link
Contributor

  • If there is no "editorconfig", it should use the original EOL. eg: if the file was stored in "\n", the newly saved file should also use "\n"; if the file was stored in "\r\n", the newly saved file should also use "\r\n"
  • The config option "EDITOR_EOL" is not necessary (not used in most cases, according to the behavior above), the newly created file could just use "\n", which is widely used and doesn't cause problem in modern days.
  • I guess the JS trick for data-initial-value="{{JsonUtils.EncodeToString .FileContent}}" is a no-op now, it should work well by using <textarea>{{.FileContent}}</textarea> ?

@@ -34,6 +34,7 @@ var UI = struct {
SearchRepoDescription bool
OnlyShowRelevantRepos bool
ExploreDefaultSort string `ini:"EXPLORE_PAGING_DEFAULT_SORT"`
EditorEol string `ini:"EDITOR_EOL"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the correct Go name would even be EditorEOL

@@ -149,6 +149,9 @@ func NewFuncMap() template.FuncMap {
"MermaidMaxSourceCharacters": func() int {
return setting.MermaidMaxSourceCharacters
},
"EditorEol": func() string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

@@ -149,6 +149,9 @@ func NewFuncMap() template.FuncMap {
"MermaidMaxSourceCharacters": func() int {
return setting.MermaidMaxSourceCharacters
},
"EditorEol": func() string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above


// convert all newlines in string to \n
func ToLF(str string) string {
return strings.ReplaceAll(str, "\r", "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return strings.ReplaceAll(str, "\r", "")
return strings.ReplaceAll(str, "\r\n", "\n")


// convert all newlines in string to \r\n
func ToCRLF(str string) string {
return strings.ReplaceAll(ToLF(str), "\n", "\r\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return strings.ReplaceAll(ToLF(str), "\n", "\r\n")
return strings.ReplaceAll(str, "(?:[^\r])\n", "\r\n")

@@ -45,3 +45,11 @@ func TestToSnakeCase(t *testing.T) {
assert.Equal(t, expected, ToSnakeCase(input))
}
}

func TestToLF(t *testing.T) {
assert.Equal(t, "\na\nbc\n\n", ToLF("\r\na\r\nb\rc\n\n"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.Equal(t, "\na\nbc\n\n", ToLF("\r\na\r\nb\rc\n\n"))
assert.Equal(t, "\r\na\nbc\n\n", ToLF("\r\r\na\r\nb\rc\n\n"))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or how do we want to handle this case?
I can live both with the current approach of removing it completely, and my proposed approach of only converting \r\n -> \n

@wxiaoguang
Copy link
Contributor

  • If there is no "editorconfig", it should use the original EOL. eg: if the file was stored in "\n", the newly saved file should also use "\n"; if the file was stored in "\r\n", the newly saved file should also use "\r\n"

The same problem as BOM: #28935 (comment)

  • if the file was stored in "\n", the newly saved file should also use "\n"; if the file was stored in "\r\n", the newly saved file should also use "\r\n"
  • (and more) if the file was stored with some BOM, the newly saved file should also use that BOM

@silverwind
Copy link
Member Author

silverwind commented Jan 27, 2024

if the file was stored in "\n", the newly saved file should also use "\n"; if the file was stored in "\r\n", the newly saved file should also use "\r\n"

Yes, like with BOM we should just round-trip the file unmodified. The only problem is new files and files with equal amount of CRLF and LF, for those we need to bias towards one format, which could come from editorconfig with fallback to global server config.

I think the approach here is not correct and we should actually avoid backend convertion completely. So I think it's better to make a new PR, maybe I will have a look later, but feel free to raise something @wxiaoguang.

@silverwind silverwind closed this Jan 27, 2024
@wxiaoguang
Copy link
Contributor

I think the approach here is not correct and we should actually avoid backend convertion completely.

IMO at the moment, backend convertion can't be avoided. Because HTML textarea always passes "\r\n" to backend, it is the standard.

@silverwind
Copy link
Member Author

silverwind commented Jan 29, 2024

I think the approach here is not correct and we should actually avoid backend convertion completely.

IMO at the moment, backend convertion can't be avoided. Because HTML textarea always passes "\r\n" to backend, it is the standard.

Yeah, but we can at least avoid altering the content until the point where it reaches the editor. Maybe it's ideal to use a data- attribute to ensure the browser does no shenenigans with the textarea value even when reading it.

@silverwind silverwind deleted the editoreol branch January 29, 2024 17:19
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 4, 2024
@lunny lunny removed the backport/v1.21 This PR should be backported to Gitea 1.21 label Mar 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 1 This PR needs approval from one additional maintainer to be merged. modifies/docs size/M Denotes a PR that changes 30-99 lines, ignoring generated files. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1.21 broke Linux EOL in code editor
6 participants