Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 15, 2025

Syntax highlighting breaks when using optional parameters in F# member signatures. After the ? character, all subsequent code is incorrectly highlighted as types/namespaces:

member _.memb(?optional:string) = optional
              ^^^^^^^^^^^^^^^^^^^^
              incorrectly highlighted as type

Root Cause

QuickParse.GetCompleteIdentifierIslandImplAux returns None when the cursor is positioned on ? because it's not an identifier character. This causes downstream language services to misinterpret context and apply incorrect semantic classifications.

Changes

src/Compiler/Service/QuickParse.fs

  • Added special case in fixup logic: when on ? followed by an identifier character, look ahead one position to extract the identifier

tests/FSharp.Compiler.Service.Tests/

  • Added tokenization test confirming QMARK and IDENT tokens are correctly produced
  • Added QuickParse tests for identifier extraction when cursor is on ? or on the parameter name
  • Added documentation explaining how QuickParse affects VS semantic coloring

vsintegration/tests/FSharp.Editor.Tests/

  • Added semantic classification test in SemanticClassificationServiceTests.fs verifying optional parameters are not misclassified as types/namespaces

Evidence of Fix

The fix addresses the Visual Studio coloring issue through the following call chain:

  • ClassificationService.fs (VS Editor) → FSharpCheckerResults.GetSemanticClassification
  • ServiceAnalysis.fs uses QuickParse.GetPartialLongNameEx to extract identifier context
  • When QuickParse returns None for ?, downstream services misinterpret context, causing incorrect semantic classifications

The semantic classification test directly demonstrates that with the fix, optional parameters are correctly classified and NOT misidentified as types/namespaces.

Testing

  • ✅ Compiler Service tests: 2025 passed, 29 skipped
  • ✅ QuickParse tests: 3 passed (verify identifier extraction with ? prefix)
  • ✅ Tokenizer tests: verify QMARK and IDENT tokens correctly produced
  • ✅ Semantic classification test: verifies optional parameters are not misclassified

The fix is minimal—7 lines of code handling the ? prefix pattern without affecting other identifier extraction logic.

Original prompt

This section details on the original issue you should resolve

<issue_title>F# Highlighting fails on optional parameters</issue_title>
<issue_description>This issue has been moved from a ticket on Developer Community.

Image
member _.memb(?optional:string) = optional

[severity:It bothers me. A fix would be nice]
When using optional arguments (prefixed with ?), all the member will be highlighted as a type or namespace, breaking coloring the to optional argument.

</issue_description>

<agent_instructions>Debug the services for syntax coloring provided by FSharp.Compiler.Services.
Check existing tests for tokenization as well as semantic coloring.

Set up a regression test to validate the reported issue.</agent_instructions>

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

Custom agent used: F# agent
Generic agent for F# coding following the coding guidelines of F# from MsLearn


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits December 15, 2025 17:13
…ctly

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
… prefix

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix F# highlighting for optional parameters Fix syntax highlighting for optional parameters with ? prefix Dec 15, 2025
Copilot AI requested a review from T-Gro December 15, 2025 17:45
@T-Gro
Copy link
Member

T-Gro commented Dec 16, 2025

/run fantomas

@github-actions
Copy link
Contributor

❗ Release notes required

@copilot,

Caution

No release notes found for the changed paths (see table below).

Please make sure to add an entry with an informative description of the change as well as link to this pull request, issue and language suggestion if applicable. Release notes for this repository are based on Keep A Changelog format.

The following format is recommended for this repository:

* <Informative description>. ([PR #XXXXX](https://github.com/dotnet/fsharp/pull/XXXXX))

See examples in the files, listed in the table below or in th full documentation at https://fsharp.github.io/fsharp-compiler-docs/release-notes/About.html.

If you believe that release notes are not necessary for this PR, please add NO_RELEASE_NOTES label to the pull request.

You can open this PR in browser to add release notes: open in github.dev

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/10.0.200.md No release notes found or release notes format is not correct

@github-actions
Copy link
Contributor

🔧 CLI Command Report

  • Command: /run fantomas
  • Outcome: success

✅ Patch applied:
- Files changed: 1
- Lines changed: 13

->
Some index
// Handle optional parameter syntax: if we're on '?' and the next char is an identifier, use the next position
| _ when
Copy link
Member

Choose a reason for hiding this comment

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

Provide evidence that this is indeed used for coloring in VS.
The same for testing evidence - the problem was reported with incorrect colors.

@copilot

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added evidence and tests in commit d8ccf99:

Evidence that QuickParse affects VS coloring:

The call chain is: ClassificationService.fs (VS Editor) → FSharpCheckerResults.GetSemanticClassificationSemanticClassification.fs (core logic) → ServiceAnalysis.fs uses QuickParse.GetPartialLongNameEx (lines 156, 342).

When QuickParse returns None for ?, downstream language services can't extract identifier context, causing misclassification.

Testing evidence:

  1. QuickParseTests.fs: Demonstrates the fix - GetCompleteIdentifierIsland now correctly extracts "optional" when cursor is on ?
  2. SemanticClassificationServiceTests.fs: Added test verifying optional parameters are NOT misclassified as ClassName/NamespaceName
  3. TokenizerTests.fs: Confirms QMARK and IDENT tokens are correctly produced

All tests pass, proving the fix resolves the reported coloring issue.

Copilot AI and others added 2 commits December 16, 2025 09:23
…c classification

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copilot AI requested a review from T-Gro December 16, 2025 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

F# Highlighting fails on optional parameters

3 participants