Skip to content

Fix hex color detection in strings by adding lookbehind patterns #251589

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

Merged
merged 6 commits into from
Jun 16, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 16, 2025

The color picker was not detecting hex colors when they appeared directly after quote characters or other common delimiters in code. For example:

// This didn't work - no color picker shown
const color = '#ff0000';

// But this worked - color picker shown
const color = ' #ff0000';

The issue was in the regex pattern in defaultDocumentColorsComputer.ts which only matched hex colors that were preceded by whitespace (\s+) or at the start of a line (^).

Solution:
Added positive lookbehind patterns to detect hex colors after common programming delimiters:

  • Single quotes: '#ff0000'
  • Double quotes: "#ff0000"
  • Equals signs: background-color=#ff0000
  • Brackets: colors = [#ff0000]
  • Parentheses: rgb(#ff0000)
  • Colons: style: {color: #ff0000}

Changes:

  • Modified the regex in defaultDocumentColorsComputer.ts to include (?<=['":=\[({])(#)([A-Fa-f0-9]{6})\b and (?<=['":=\[({])(#)([A-Fa-f0-9]{8})\b patterns
  • Added comprehensive test suite to ensure the fix works and doesn't break existing functionality

Testing:

  • ✅ Original issue cases now work: '#ff0000', "#ff0000"
  • ✅ Multiple hex colors in arrays: ['#ff0000', '#00ff00', '#0000ff']
  • ✅ Hex colors after common delimiters: =, [, (, :
  • ✅ Both 6-digit and 8-digit hex colors supported
  • ✅ All existing functionality preserved (whitespace, start-of-line cases)
  • ✅ RGB/HSL color detection unaffected

This is a minimal change (1 line modified) that significantly improves the color picker's usability in common coding scenarios.

Fixes #251582.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • electronjs.org
    • Triggering command: node-gyp (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: aiday-mar <61460952+aiday-mar@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Hex color inside string is not detected Fix hex color detection in strings by adding lookbehind patterns Jun 16, 2025
@Copilot Copilot AI requested a review from aiday-mar June 16, 2025 11:07
Copilot finished work on behalf of aiday-mar June 16, 2025 11:07
@@ -101,7 +101,7 @@ function _findMatches(model: IDocumentColorComputerTarget | string, regex: RegEx
function computeColors(model: IDocumentColorComputerTarget): IColorInformation[] {
const result: IColorInformation[] = [];
// Early validation for RGB and HSL
const initialValidationRegex = /\b(rgb|rgba|hsl|hsla)(\([0-9\s,.\%]*\))|\s+(#)([A-Fa-f0-9]{6})\b|\s+(#)([A-Fa-f0-9]{8})\b|^(#)([A-Fa-f0-9]{6})\b|^(#)([A-Fa-f0-9]{8})\b/gm;
const initialValidationRegex = /\b(rgb|rgba|hsl|hsla)(\([0-9\s,.\%]*\))|\s+(#)([A-Fa-f0-9]{6})\b|\s+(#)([A-Fa-f0-9]{8})\b|^(#)([A-Fa-f0-9]{6})\b|^(#)([A-Fa-f0-9]{8})\b|(?<=['":=\[({])(#)([A-Fa-f0-9]{6})\b|(?<=['":=\[({])(#)([A-Fa-f0-9]{8})\b/gm;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi thanks for the change. I want the hex regex to be changed so that is simply adds the case when the length 6 or 8 color is inside of a string (or after a single or double quote). You don't need to cover the case where it follows other delimiters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! I've simplified the regex to only support hex colors after single and double quotes. Removed the lookbehind patterns for other delimiters (=, [, (, {, :) and updated the tests accordingly. The regex now only includes (?<=['"])(#)([A-Fa-f0-9]{6})\b|(?<=['"])(#)([A-Fa-f0-9]{8})\b for the quote cases. Commit: 55e10f1

…imiters

Co-authored-by: aiday-mar <61460952+aiday-mar@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 16, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • electronjs.org
    • Triggering command: node-gyp (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of aiday-mar June 16, 2025 11:42
@aiday-mar aiday-mar marked this pull request as ready for review June 16, 2025 16:09
@aiday-mar aiday-mar enabled auto-merge (squash) June 16, 2025 16:09
@vs-code-engineering vs-code-engineering bot added this to the June 2025 milestone Jun 16, 2025
@aiday-mar aiday-mar merged commit 24c0ff1 into main Jun 16, 2025
8 checks passed
@aiday-mar aiday-mar deleted the copilot/fix-251582 branch June 16, 2025 17:23
Subham-KRLX pushed a commit to Subham-KRLX/vscode that referenced this pull request Jun 17, 2025
…rosoft#251589)

* Initial plan for issue

* Fix hex color detection in strings by adding lookbehind patterns

Co-authored-by: aiday-mar <61460952+aiday-mar@users.noreply.github.com>

* Simplify hex color detection to only support quotes, remove other delimiters

Co-authored-by: aiday-mar <61460952+aiday-mar@users.noreply.github.com>

* polish

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: aiday-mar <61460952+aiday-mar@users.noreply.github.com>
Co-authored-by: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com>
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.

Hex color inside string is not detected
3 participants