Skip to content

PHP Syntax Highlighting does not working in review mode #21970

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

Closed
dionisvl opened this issue Nov 29, 2022 · 13 comments · Fixed by #33766
Closed

PHP Syntax Highlighting does not working in review mode #21970

dionisvl opened this issue Nov 29, 2022 · 13 comments · Fixed by #33766
Labels
issue/workaround it is or has a workaround topic/ui Change the appearance of the Gitea UI type/bug

Comments

@dionisvl
Copy link

Description

Hello
PHP syntax highlighting does not working in Code-Review mode.
But i see that its works for TypeScript, Kotlin etc.

Current example on try.gitea.io:
https://try.gitea.io/den/test/pulls/1/files

Screenshots

2022-11-29_10-52-08

Gitea Version

1.19.0+dev-135-gf047ee0a4

Can you reproduce the bug on the Gitea demo site?

Yes

Operating System

Windows 10 Pro

Browser Version

Chrome 107

@dionisvl dionisvl added type/bug topic/ui Change the appearance of the Gitea UI labels Nov 29, 2022
@dionisvl dionisvl changed the title PHP Syntax Highlighing does not working in review mode PHP Syntax Highlighting does not working in review mode Nov 29, 2022
@lehag92
Copy link

lehag92 commented Dec 8, 2022

Syntax highlight works on edit/view file page: https://try.gitea.io/den/test/src/commit/8877ea0e117b428f22e72ee3f2908603ef88182c/testphp2.php
but same changes in PR page does not highlighting.

@wxiaoguang
Copy link
Contributor

IMO it's similar to this one: #19684 (comment)

When Gitea renders the diff page, it calls the syntax highlighter line by line, then the highlighter can not work properly.

Just my guess.

@lehag92
Copy link

lehag92 commented Dec 8, 2022

Can syntax highlighter check at least file name extension and try to highlight it line by line?

IMO it's similar to this one: #19684 (comment)

When Gitea renders the diff page, it calls the syntax highlighter line by line, then the highlighter can not work properly.

Just my guess.

@wxiaoguang
Copy link
Contributor

My guess is: the PHP syntax highlighter needs to see a <?php tag first to start colorizing the code, so it won't highlight the lines after <?php in diff mode, while other languages do not have such mechanism.

I haven't looked into the code. Just FYI.

@zeripath
Copy link
Contributor

zeripath commented Dec 9, 2022

@wxiaoguang is right here.

I am fairly certain that the correct lexer will be being selected here - the issue is that without the lexer seeing the <?php it won't switch in to php mode. So it will simply call of this stuff "HTML"

Now, the only way to fix that would be to search backwards until you find the start of the block, which essentially requires one to lex the whole of the document and not just the fragment. Whilst that would be fine for small files it's not gonna work very nicely for big files.

@dionisvl
Copy link
Author

@zeripath
ok, let's at least analyze all PHP files except > 1 MB

@lehag92
Copy link

lehag92 commented Dec 20, 2022

@wxiaoguang is right here.

I am fairly certain that the correct lexer will be being selected here - the issue is that without the lexer seeing the <?php it won't switch in to php mode. So it will simply call of this stuff "HTML"

Now, the only way to fix that would be to search backwards until you find the start of the block, which essentially requires one to lex the whole of the document and not just the fragment. Whilst that would be fine for small files it's not gonna work very nicely for big files.

If missing <?php was the issue, new added\created files should be highlighted in this case, but they are not.

@progserg
Copy link

progserg commented Feb 3, 2023

same problem

@ABOryn
Copy link

ABOryn commented Feb 3, 2023

+1
Linux Mint 21.1, FireFox 109.0.1 (64-bit)

@gempir
Copy link
Contributor

gempir commented Feb 9, 2023

I debugged it.

It doesn't know the correct lexer to use for the file. So it let's chroma guess. But Chroma prefers phtml over php. Which results in no real syntax highlighting.

alecthomas/chroma#577

@gempir
Copy link
Contributor

gempir commented Feb 9, 2023

Workaround for now:

Create a .gitattributes file in your repo with:

*.php linguist-language=php

Then the syntax highlighting works!

image

@silverwind
Copy link
Member

Lets continue in #33358 regarding the problem with line-by-line syntax highlighting.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Jan 23, 2025

It is another different case than #33358, because chroma requires php file to start with <?php.

Although 33358 could help, but I think for this case, upstream or we could have a quick solution.

@wxiaoguang wxiaoguang reopened this Jan 23, 2025
hiifong pushed a commit to hiifong/gitea that referenced this issue Mar 10, 2025
Fix go-gitea#33358, fix go-gitea#21970

This adds a step in the `GitDiffForRender` that does syntax highlighting for the
entire file and then only references lines from that syntax highlighted
code. This allows things like multi-line comments to be syntax
highlighted correctly.

---------

Co-authored-by: wxiaoguang <[email protected]>

(cherry picked from commit 3f1f808)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue/workaround it is or has a workaround topic/ui Change the appearance of the Gitea UI type/bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants