Refactor code render and render control chars#37078
Conversation
There was a problem hiding this comment.
Pull request overview
Refactors the file-view code rendering pipeline to explicitly render ASCII control characters (notably DEL/U+007F) instead of letting browsers display them as whitespace, and moves the related styling into a dedicated CSS module (Fixes #37057).
Changes:
- Refactors
modules/highlightrendering to escape/control-render ASCII control characters and unify escaping logic. - Updates repository file viewer to use the new
RenderFullFilesignature (no error return) and applies the updated rendering output. - Moves unicode/control-character escape styling out of
repo.cssinto a newcharescape.cssmodule and imports it globally.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| web_src/css/repo.css | Removes unicode/control-character escape styles from repo-specific stylesheet. |
| web_src/css/modules/charescape.css | Introduces a dedicated stylesheet for control/escape rendering UI. |
| web_src/css/index.css | Imports the new charescape.css module so styling applies site-wide. |
| routers/web/repo/view_file.go | Adapts file view source rendering to the updated highlight API. |
| modules/highlight/lexerdetect.go | Adds shared constants for plaintext/fallback lexer naming. |
| modules/highlight/highlight.go | Core refactor: adds control-char rendering and new escaping helpers; changes RenderFullFile signature/behavior. |
| modules/highlight/highlight_test.go | Updates tests for the new API and adds coverage for control-char escaping. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
c3f3cb5 to
3e99ae3
Compare
|
BTW how does GitHub render these? |
3e99ae3 to
c742274
Compare
You can try, but I don't have interest to keep following GitHub. |
And this won't affect you and most end users: such control chars just should not exist in the text files. So you will never actually see this. |
|
Here is a test file: https://github.com/silverwind/symlink-test/blob/master/control-chars.txt My conclusion is they don't render any of them. VSCode does render like this. I think keeping the width 1 char has benefits for alignment, would maybe do that.
|
|
Actually not sure about those VSCode renderings, text may be too small and for some reason, they render som characters with less width (probably unintentionally). |
I don't see why it's worth to do any more change
|
|
Yes, rendering is fine I guess. What happens if the user tries to copy-paste these characters? Will they copy literally? |
At the moment, yes. I have thought about using
|
What do you think? Is it still a must to change the approach? |
|
If literal characters copied before as well, then it's fine and actually desired to keep the copy content unchanged. Especially when triggered via "Copy Content" button, the content must not change, please verify. |
|
OK, since you request:
ae2a16a makes the control chars selectable and copyable
Not related and not affected. |
There was a problem hiding this comment.
-
RenderCodeByLexerno longer strips the trailing\n(strings.TrimSuffixremoved). The diff code path (highlightCodeLinesingitdiff.go) also uses this function — worth verifying the diff view with files that don't end with a newline. -
highlightCodeLinescallsRenderCodeByLexer+UnsafeSplitHighlightedLinesbut doesn't applyescapeControlChars. Is that intentional (charset escaping handles it), or a gap?
This comment was written by Claude Opus 4.6.
|
I didn't test yet but we must ensure no extra final newlines in rendered code or diff views. That has been an issue in the past but I think there should also be test coverage for it. |
I have blamed the history, I don't see anything wrong. Unless you can use a real example to show. A test is also updated due to this change, the test coverage is very good. |
|
A related PR is this Rework file highlight rendering and fix yaml copy-paste (#19967) At that time, the render related code was very dirty, you can see that there were many tricks to handle various edge cases. After my refactoring & rewriting, now we have a pretty clear render framework, the code can be just written clearly, most hacky methods have been removed. |
|
Ah, selection works fine, it was the BiDi characters that messed up my selection. |
|
Some fixes done:
|
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
silverwind
left a comment
There was a problem hiding this comment.
The only question I have is whether we should only display the picture characters while in escape mode to be consistent with other unicode chars. currently the picture-characters render in both unescaped and escaped views.
* main: Fix RPM Registry 404 when package name contains 'package' (go-gitea#37087) Improve actions notifier for `workflow_run` (go-gitea#37088) Refactor code render and render control chars (go-gitea#37078) Fix various problems (go-gitea#37077) [skip ci] Updated translations via Crowdin Support legacy run/job index-based URLs and refactor migration 326 (go-gitea#37008) Fix a bug when forking a repository in an organization (go-gitea#36950)
|
@lunny merged too soon, stop merging just because it has approvals. I was still waiting on feedback for the last question and @wxiaoguang did not review my last changes. |
|
Turns out my changes meant for this branch were pushed to the wrong remote, they are now in #37094. |
Sorry. Misunderstood. |
Follow-up to #37078. - Use Unicode Control Pictures](U+2400-U+2421) to render C0 control characters - Make it work in diff view too - Replace escape warning emoji with SVG - Align escape warning button with code lines --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>





Fix #37057