Multiline comments are not highlighted correctly #10350

Closed
opened 2025-11-02 09:05:01 -06:00 by GiteaMirror · 14 comments
Owner

Originally created by @drsybren on GitHub (Feb 27, 2023).

Description

Multi-line comments are not highlighted correctly in diff views.

Actual behaviour

Example: commit aa72e3abf9644de3c0478692d7bb357d1c7297e2 in Blender

image

Another example in Go, to show that it's not limited to C/C++: aa87c1c50d

Expected behaviour

Expected would be to see the highlighting like done when viewing the file regularly (of course with red/green background for the added/removed lines in the diff):

image

Gitea Version

1.19.0+dev-649-g2093ca517

Can you reproduce the bug on the Gitea demo site?

Yes

Log Gist

No response

Screenshots

No response

Git Version

No response

Operating System

No response

How are you running Gitea?

Self-built from Git

Database

PostgreSQL

Originally created by @drsybren on GitHub (Feb 27, 2023). ### Description Multi-line comments are not highlighted correctly in diff views. ## Actual behaviour Example: [commit aa72e3abf9644de3c0478692d7bb357d1c7297e2 in Blender](https://projects.blender.org/blender/blender/commit/aa72e3abf9644de3c0478692d7bb357d1c7297e2#diff-c53e92f8e88d8cbbf299213db5cbdb51ff328d61) ![image](https://user-images.githubusercontent.com/122987084/221576516-c1a355de-ccbe-44dd-9e56-128b9483e283.png) Another example in Go, to show that it's not limited to C/C++: https://try.gitea.io/drSybren/skyfill/commit/aa87c1c50db6dfd97ab53905097e43fce3c5568e ## Expected behaviour Expected would be to see the highlighting like done when viewing the file regularly (of course with red/green background for the added/removed lines in the diff): ![image](https://user-images.githubusercontent.com/122987084/221576786-99080adb-0a6f-48f7-9da1-8b2123f21cf4.png) ### Gitea Version 1.19.0+dev-649-g2093ca517 ### Can you reproduce the bug on the Gitea demo site? Yes ### Log Gist _No response_ ### Screenshots _No response_ ### Git Version _No response_ ### Operating System _No response_ ### How are you running Gitea? Self-built from Git ### Database PostgreSQL
GiteaMirror added the type/bug label 2025-11-02 09:05:01 -06:00
Author
Owner

@wxiaoguang commented on GitHub (Feb 27, 2023):

I guess it's caused by that Gitea renders the diff page line by line, then highlight doesn't work (the comments are processed line by line). That's why the comment highlight works for whole file rendering, but doesn't work for the diff/compare page.

Maybe related to:

@wxiaoguang commented on GitHub (Feb 27, 2023): I guess it's caused by that Gitea renders the diff page line by line, then highlight doesn't work (the comments are processed line by line). That's why the comment highlight works for whole file rendering, but doesn't work for the diff/compare page. Maybe related to: * #19684 * #21970
Author
Owner

@a1012112796 commented on GitHub (Feb 28, 2023):

looks that's because gitea hasn't checking language by file name and content like file view. but it's sound not a easy job (how to get file content ? single file name always not enough for checking language). I'd like tag this issue as kind/enhancement instead of kind/bug .

ref:
443dcc2db0/services/gitdiff/gitdiff.go (L1156-L1160)

@a1012112796 commented on GitHub (Feb 28, 2023): looks that's because gitea hasn't checking language by file name and content like file view. but it's sound not a easy job (how to get file content ? single file name always not enough for checking language). I'd like tag this issue as `kind/enhancement` instead of `kind/bug` . ref: https://github.com/go-gitea/gitea/blob/443dcc2db0676cae8b1b72c1b0b99e675c21177c/services/gitdiff/gitdiff.go#L1156-L1160
Author
Owner

@wxiaoguang commented on GitHub (Feb 28, 2023):

Not only a "language" problem, but the highlighter need all the lines together to do highlight correctly. Otherwise when the highlighter sees the second line of the comment without /*, it doesn't know it's in the comment context.

@wxiaoguang commented on GitHub (Feb 28, 2023): Not only a "language" problem, but the highlighter need all the lines together to do highlight correctly. Otherwise when the highlighter sees the second line of the comment without `/*`, it doesn't know it's in the comment context.
Author
Owner

@drsybren commented on GitHub (Mar 3, 2023):

I'd like tag this issue as kind/enhancement instead of kind/bug .

I would still argue this is a bug, as the highlighting is clearly wrong.

@drsybren commented on GitHub (Mar 3, 2023): > I'd like tag this issue as kind/enhancement instead of kind/bug . I would still argue this is a bug, as the highlighting is clearly wrong.
Author
Owner

@silverwind commented on GitHub (Mar 3, 2023):

Yes, the main reason is diff highlighting is line-by-line so all multiline stuff breaks. We should instead feed both the "before" and "after" documents separately to chroma and then use those highlighted lines to replace the lines with highlighted ones in the diff.

I'm pretty sure this issue is duplicate.

@silverwind commented on GitHub (Mar 3, 2023): Yes, the main reason is diff highlighting is line-by-line so all multiline stuff breaks. We should instead feed both the "before" and "after" documents separately to chroma and then use those highlighted lines to replace the lines with highlighted ones in the diff. I'm pretty sure this issue is duplicate.
Author
Owner

@wxiaoguang commented on GitHub (Mar 4, 2023):

This problem is quite challenging. Because at the moment many Diff Lines are provided by git commands without highlight. If we want to highlight the file first then do the diff to get Diff Lines, then the diff code should be (nearly) totally rewritten.

There could be a intermediate solution: git provides the diff lines -> gitea parses the diff lines -> git merge (join) the diff lines (update: for every hunk, but not join all chunks) as a temp file content and highlight it -> then gitea splits the highlighted content into lines as diff lines.

Correct me if I am wrong.

@wxiaoguang commented on GitHub (Mar 4, 2023): This problem is quite challenging. Because at the moment many Diff Lines are provided by git commands without highlight. If we want to highlight the file first then do the diff to get Diff Lines, then the diff code should be (nearly) totally rewritten. There could be a intermediate solution: git provides the diff lines -> gitea parses the diff lines -> git merge (join) the diff lines (update: for every hunk, but not join all chunks) as a temp file content and highlight it -> then gitea splits the highlighted content into lines as diff lines. Correct me if I am wrong.
Author
Owner

@lunny commented on GitHub (Mar 4, 2023):

Could we highlight every section?
Storing the diff as a patch file may be useful for a second time CPU reduction.

@lunny commented on GitHub (Mar 4, 2023): Could we highlight every section? Storing the diff as a patch file may be useful for a second time CPU reduction.
Author
Owner

@lunny commented on GitHub (Mar 4, 2023):

I mean the struct of DiffLineSectionInfo

@lunny commented on GitHub (Mar 4, 2023): I mean the struct of `DiffLineSectionInfo`
Author
Owner

@silverwind commented on GitHub (Mar 4, 2023):

Could we highlight every section?

If you mean the diff hunks: It may be an improvement, but will not completely fix the issue. A complete fix can only to be to highlight the whole "before" and "after" files and map back the highlighted lines into the diff lines.

Imagine a file that only consists of one big muliline string, spanning hundres of lines. git diff will give a context of +/- 3 lines to a one-line edit. Highlighting a hunk in the middle will still be incorrect.

Also, highlighting diff hunks may result in even more broken cases because added/removed lines are intermingled, so it will definitely confuse chroma.

@silverwind commented on GitHub (Mar 4, 2023): > Could we highlight every section? If you mean the diff hunks: It may be an improvement, but will not completely fix the issue. A complete fix can only to be to highlight the whole "before" and "after" files and map back the highlighted lines into the diff lines. Imagine a file that only consists of one big muliline string, spanning hundres of lines. git diff will give a context of +/- 3 lines to a one-line edit. Highlighting a hunk in the middle will still be incorrect. Also, highlighting diff hunks may result in even more broken cases because added/removed lines are intermingled, so it will definitely confuse chroma.
Author
Owner

@wxiaoguang commented on GitHub (Mar 4, 2023):

Could we highlight every section?

That's my "an intermediate solution" above. However it is only an intermediate solution, you would still lose the correct context because the diff hunk may only contain a few lines, there might be no /* in the hunk.

@wxiaoguang commented on GitHub (Mar 4, 2023): > Could we highlight every section? That's my "an intermediate solution" above. However it is only an intermediate solution, you would still lose the correct context because the diff hunk may only contain a few lines, there might be no `/*` in the hunk.
Author
Owner

@silverwind commented on GitHub (Mar 4, 2023):

I doubt highlighting hunks as a whole will improve the situation much, I fear it may only make it worse.

@silverwind commented on GitHub (Mar 4, 2023): I doubt highlighting hunks as a whole will improve the situation much, I fear it may only make it worse.
Author
Owner

@wxiaoguang commented on GitHub (Mar 4, 2023):

hunks as a whole ... I fear it may only make it worse.

Yup, if the a hunk only contains /* but doesn't contain */, the remaining hunks are all rendered as "comment", not good.

@wxiaoguang commented on GitHub (Mar 4, 2023): > hunks as a whole ... I fear it may only make it worse. Yup, if the a hunk only contains `/*` but doesn't contain `*/`, the remaining hunks are all rendered as "comment", not good.
Author
Owner

@silverwind commented on GitHub (Jan 22, 2025):

Let's continue in https://github.com/go-gitea/gitea/issues/33358

@silverwind commented on GitHub (Jan 22, 2025): Let's continue in https://github.com/go-gitea/gitea/issues/33358
Author
Owner

@wxiaoguang commented on GitHub (Jan 23, 2025):

No, that's different case. #33354 is just an upstream bug.

Oops, misread, 33358 is the right issue.

@wxiaoguang commented on GitHub (Jan 23, 2025): ~~No, that's different case. #33354 is just an upstream bug.~~ Oops, misread, 33358 is the right issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#10350