Skip to content

Fix Shiki hang on inline struct type highlighting#2962

Merged
nighca merged 2 commits intogoplus:devfrom
nighca:issue-2932
Mar 17, 2026
Merged

Fix Shiki hang on inline struct type highlighting#2962
nighca merged 2 commits intogoplus:devfrom
nighca:issue-2932

Conversation

@nighca
Copy link
Collaborator

@nighca nighca commented Mar 17, 2026

Summary

  • narrow the inline struct semicolon field rule in the xgo TextMate grammar
  • avoid catastrophic regex behavior when Shiki tokenizes nested function types with inline struct parameters
  • preserve normal highlighting for representative semicolon-separated struct fields

Testing

  • verified the updated grammar file parses without errors
  • reproduced the original pathological input in an isolated Shiki harness and confirmed codeToTokens and codeToHtml complete successfully after the grammar change
  • verified a representative type T struct{X_0 int; X_1 float64} sample still produces highlighted HTML

Closes #2932

Copilot AI review requested due to automatic review settings March 17, 2026 10:11
@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical performance issue in the Shiki syntax highlighter, specifically targeting a regular expression within the Go+ TextMate grammar. The previous regex caused significant delays or hangs when processing complex inline struct type definitions, particularly those involving nested function parameters. The updated grammar rule prevents this catastrophic backtracking, ensuring efficient and correct highlighting for Go+ code without impacting standard struct field highlighting.

Highlights

  • Grammar Refinement: Narrowed the inline struct semicolon field rule within the xgo TextMate grammar.
  • Performance Fix: Addressed catastrophic regex behavior that occurred when Shiki tokenized nested function types containing inline struct parameters.
  • Highlighting Preservation: Ensured normal highlighting is preserved for representative semicolon-separated struct fields.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • spx-gui/src/utils/xgo/gop-tm-language.json
    • Modified the regex pattern for inline struct field declarations to be more specific and prevent catastrophic backtracking.
    • Simplified the capture group structure for the inline struct field highlighting rule.
Activity
  • Verified that the updated grammar file parses without errors.
  • Reproduced the original pathological input in an isolated Shiki harness and confirmed successful completion of codeToTokens and codeToHtml after the grammar change.
  • Verified that a representative type T struct{X_0 int; X_1 float64} sample still produces correctly highlighted HTML.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively resolves a performance issue with the syntax highlighter by replacing a complex and inefficient regular expression. This simplification prevents catastrophic backtracking and is a significant improvement. I have identified a minor issue in the new regex concerning the matching of variable lists, which could result in incorrect highlighting for certain valid code formats. My suggestion aims to enhance the robustness of this matching.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the xgo (GoPlus) TextMate grammar used by Shiki in spx-gui to avoid catastrophic regex behavior when highlighting inline struct{...} types containing semicolon-separated fields, addressing a browser hang during tokenization.

Changes:

  • Replaced the inline-struct “semicolon field” matching rule with a simpler regex intended to avoid catastrophic backtracking.
  • Kept capture-based highlighting for struct field names and types via existing #type-declarations patterns.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@xgopilot
Copy link
Contributor

xgopilot bot commented Mar 17, 2026

Solid fix. The catastrophic backtracking (ReDoS) in the old regex was caused by nested quantifiers around [\\S]+ with an optional terminator — a textbook ambiguity that forced exponential backtracking on nested struct types. Replacing it with an unambiguous negated character class [^; `\"/}]+eliminates the ambiguity entirely. The simplification of the capture structure (flat vs. nested sub-patterns) is correct. Two minor notes left inline: the removed(?<=\{)lookbehind assumption and the stale"comment"` field.

@nighca nighca merged commit 041172c into goplus:dev Mar 17, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to addSun function to stage

3 participants