Skip to content

(java) Improve detection of types, including generic and array types - #4346

Merged
joshgoebel merged 14 commits into
highlightjs:mainfrom
hns:java-generics-and-arrays
Jul 14, 2026
Merged

joshgoebel merged 14 commits into
highlightjs:mainfrom
hns:java-generics-and-arrays

Conversation

@hns

@hns hns commented Dec 5, 2025 •

Copy link
Copy Markdown
Contributor

This improves detection of types in Java variable and method declarations, including support for generic and array types.

Resolves #3678
Resolves #4017
Resolves #4345

Changes

  • The regexes for type name and type parameters for generic types are split so they can be highlighted independently.
  • The regex for type parameters in generic types is improved to support wildcards and bounded types.
  • A regex for array brackets is added to support array types.
  • Regexes for type parameters and array brackets are added to variable and method declarations so they support generic and array types.
  • Like types of variable declarations, return types in methods are highlighted as hljs-type, including the void type.

Checklist

  • Added markup tests, or they don't apply here because...
  • Updated the changelog at CHANGES.md

@hns hns changed the title improve detection of types, including generic and array types (java) Improve detection of types, including generic and array types Dec 5, 2025
Comment thread src/languages/java.js Resolved
Comment thread src/languages/java.js Resolved

@nizarbenalla nizarbenalla left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Change looks good, I left a couple of inline comments.
The cases I mentioned are pretty harmless, you may only notice them if you're using snippets.
I also see that the copyright notices are being applied correctly.

Comment thread src/languages/java.js Resolved
Comment thread src/languages/java.js Resolved
@joshgoebel

Copy link
Copy Markdown
Member

I like where you're headed with this, just need to make it all a bit more readable, and I dropped a few questions.

@hns

hns commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for looking into this, Josh. I'll update the PR to address your feedback in the next few days.

hns added 2 commits July 9, 2026 16:16
 - recognize more array types
 - rename, untangle and comment regex patterns

@hns hns left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@joshgoebel @nizarbenalla I addressed your review feedback in a followup commit.

I also fixed a bug I discovered where a keyword was highlighted as type if it is followed by an assignment, such as in:

return foo = 3;

Let me know if you think this should be fixed as a separate issue.

Comment thread src/languages/java.js Resolved
Comment thread src/languages/java.js Resolved
Comment thread src/languages/java.js Resolved
Comment thread src/languages/java.js Resolved
Comment thread src/languages/java.js Resolved
@github-actions

Copy link
Copy Markdown

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

5 files changed

Total change +27 B

View Changes
file base pr diff
es/core.min.js 8.24 KB 8.24 KB -3 B
es/highlight.min.js 8.24 KB 8.24 KB -3 B
es/languages/java.min.js 1.32 KB 1.33 KB +17 B
highlight.min.js 8.28 KB 8.28 KB -3 B
languages/java.min.js 1.32 KB 1.34 KB +19 B

@joshgoebel

Copy link
Copy Markdown
Member

how you feeling? Ready to undraft? Anything further we need to do here?

@hns
hns marked this pull request as ready for review July 13, 2026 10:20
@hns

hns commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

I had kind of forgotten this was still in draft mode. I think it looks good so I marked it as ready.

@github-actions

Copy link
Copy Markdown

Build Size Report

Changes to minified artifacts in /build, after gzip compression.

2 files changed

Total change +36 B

View Changes
file base pr diff
es/languages/java.min.js 1.32 KB 1.33 KB +17 B
languages/java.min.js 1.32 KB 1.34 KB +19 B

@joshgoebel
joshgoebel merged commit b353518 into highlightjs:main Jul 14, 2026
18 checks passed
@hns

hns commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, Josh!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants