-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Adjust line detection in highlight.go #20495
Conversation
The code for detection of lines in highlight.go is somewhat too complex and doesn't take account of how chroma is actually splitting things into lines for us. Replace go-gitea#19967 Signed-off-by: Andrew Thornton <[email protected]>
@silverwind as I don't quite understand what #19967 is supposed to be doing or fixing I hope this is doing the right thing |
Original problem was #19331, but the PR was then rewritten way beyond the original fix. |
Can confirm this fixes #19331. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great. Fixes all copy/paste issues and eliminates those useless wrappers of chroma as well while being far simpler than that other PR, I like it.
Some thoughts (but not blocker):
func linesBytesCount(s []byte) int {
nl := []byte{'\n'}
n := bytes.Count(s, nl)
if len(s) > 0 && !bytes.HasSuffix(s, nl) {
n++
}
return n
}
func TestTemp(t *testing.T) {
test := func(fileName string, content string) {
b := []byte(content)
str, _ := json.Marshal(content)
fmt.Printf("for: %s\n%s\n----\n", str, strings.Join(File(linesBytesCount(b), fileName, "", b), "\n"))
}
test("a.py", "")
test("a.py", "\n")
test("a.py", "a=1")
test("a.py", "a=1\n")
test("a.py", "a=1\n\n")
}
|
ps: about "The code for detection of lines in highlight.go is somewhat too complex and doesn't take account of how chroma is actually splitting things into lines for us." I think that's not true. That code is not that complex, the core code is only about 30 lines for a simple tag matcher. Most changes are related to other issues, including fixing suspicious And the simple tag matcher already take account the Chroma's splitting result, it doesn't output inconsistent If you think that the simple tag matcher is not needed, the #19967 can also be rewritten to a simple Split+Trim (updated) |
So numlines isn't actually as suspect as you might think it's just pre computing the count. What's your worry with the function?
As I say I don't understand what the PR is trying to solve. @silverwind said that they wanted the span class w to remain so I've left them in. If we don't want them then let's get them out but explain why. The original code I am talking about is not the PR you wrote. I am talking about the code in that is currently there which is splitting on Chroma's formatter guarantees that code lines become Therefore splitting on @wxiaoguang I am not attacking you or your original PR - I just think that it's too complicated to write a tag parser to split this stuff and I think the |
See #19967 (comment) , and I do not think it's right to pass the uncertain Before,
Now it's not, just switched back to the Split+Trim method, and, the |
One more thing about: the old That's why the So #19967 removed such dirty behavior. In the future, if users want to hide the last empty line or show a block emoji (🚫) for no-newline when rendering, the new code is easier to do so #19967 (comment) |
OK, well it's clear that I don't understand the problem. So I'm going to close this PR. |
Sad to see such a quality PR go to waste. BTW we don't have all to agree to land a PR, just two approvals are enough :) |
The code for detection of lines in highlight.go is somewhat too complex
and doesn't take account of how chroma is actually splitting things into
lines for us.
Replace #19967
Signed-off-by: Andrew Thornton [email protected]