Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 19, 2025

JavaScript in <script> blocks loses syntax highlighting when preceded by C# control structures without braces in Razor files. This occurs because the TextMate grammar pattern for control structures only ended after seeing a closing brace }, causing single-statement control structures to consume subsequent content.

Changes

  • Modified control structure end patterns: Updated end condition from '(?<=})' to '(?<=})|(?<=;)|(?=^\s*\})' for all C# control structures (if, else, for, foreach, using, lock, do, switch, try, catch, finally)
  • Added test coverage: Two new test cases verify JavaScript highlighting after control structures without braces

Example

Before (JavaScript not highlighted):

@{
    if (l == 1)
        Console.WriteLine("test");
}

<script>
$(document).on("ready", function() {
    console.log("not working");  // No highlighting
});
</script>

After (JavaScript properly highlighted):

@{
    if (l == 1)
        Console.WriteLine("test");
}

<script>
$(document).on("ready", function() {
    console.log("working");  // Full syntax highlighting
});
</script>

The pattern now terminates after semicolons (single statements) and before closing braces at line start (parent block boundaries), in addition to the existing behavior for control structure blocks.

Original prompt

This section details on the original issue you should resolve

<issue_title>JavaScript coloring does not work in all .cshtml files</issue_title>
<issue_description>This issue has been moved from a ticket on Developer Community.


[severity:It bothers me. A fix would be nice]
In some .cshtml files, JavaScript coloring does not work.

Examples:

Working
image.png

Not working
image.png

Both files are partial views. Checked code syntax - everything is good.
Tried re-opening file - nothing.
Tried solutions in this SO thread - nothing. (https://stackoverflow.com/questions/78189420/syntax-highlighting-visual-studio-2022-in-cshtml-files-not-working)


Original Comments

Feedback Bot on 11/11/2025, 00:44 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Jonas Gucu on 11/12/2025, 05:12 AM:

After a bit of experimentation, it looks like sometimes C# code breaks the rest of the code.
In this case it’s an if statement not having curly braces.

Working example:
Image

Not working example:
Image

<agent_instructions>Please fix our textmate grammar so script blocks after C# if statements, without braces, are colored correctly.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix JavaScript coloring in .cshtml files Fix JavaScript highlighting in Razor files after C# control structures without braces Dec 19, 2025
Copilot AI requested a review from davidwengier December 19, 2025 05:13
@davidwengier
Copy link
Member

If for no other reason, copilot is worth its weight in gold for textmate updates

@davidwengier davidwengier marked this pull request as ready for review December 19, 2025 05:20
@davidwengier davidwengier requested review from a team as code owners December 19, 2025 05:20
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.

JavaScript coloring does not work in all .cshtml files

2 participants