Skip to content

Add basic semantic highlighting#84

Merged
dklilley merged 10 commits intomathworks:mainfrom
Gustaf-C:semantic_highlights
Apr 20, 2026
Merged

Add basic semantic highlighting#84
dklilley merged 10 commits intomathworks:mainfrom
Gustaf-C:semantic_highlights

Conversation

@Gustaf-C
Copy link
Copy Markdown
Contributor

@Gustaf-C Gustaf-C commented Apr 2, 2026

Fixes mathworks/MATLAB-extension-for-vscode#45

This adds basic support for semantic highlighting. Currently only variables is implemented, but I plan to do some more work before it is ready to be merged. I am opening a draft PR to allow for some initial feedback.

Results can be seen below where indexed vectors are now correctly identified as variables:
image

Changes

  • Added SemanticTokensProvider.ts which uses the pre-existing indexer to find semantic tokens
  • Updated server.ts to register the provider

Note
Depends on plumbing in mathworks/MATLAB-extension-for-vscode#321

@Gustaf-C
Copy link
Copy Markdown
Contributor Author

Gustaf-C commented Apr 7, 2026

I have identified a few issues that I am looking into. It seems to be possible to correctly identify variables, but class and function references, as well as methods and properties, are hard to differentiate, where I’d say that the latter is more annoying.

Since the current parser is in P-code, I can’t make any adjustments there on my own. If the current parser would be updated, the information could easily be used in the highlighter. Otherwise, the best option could be to update the textmate scopes so the highlighting is at least not actively misleading.

@dklilley
Copy link
Copy Markdown
Member

dklilley commented Apr 9, 2026

Hi @Gustaf-C , thanks for working on this!

Overall, this approach looks good to me. I agree that class, function, method, and property references are going to be difficult to differentiate at this time.

That being said, I think adding simple variable semantic highlighting is a good start which adds definite value! I will take a closer look at the code itself when this is transferred out of draft state.

The parsing logic currently uses some undocumented MATLAB APIs which are very difficult to work with, so we are trying to move away from their usage. We are currently waiting for a new API to become available in MATLAB which should provide us easier-to-use and more detailed info about the file contents. At that point, it should be possible to add more semantic token types.

@Gustaf-C
Copy link
Copy Markdown
Contributor Author

I am reasonably happy with the changes now. A small example:

image

The previous behaviour can be seen below:
image

I would however suggest that we remove the highlighting for methods/properties in textmate since the current behaviour is more confusing than helping.

Copy link
Copy Markdown
Member

@dklilley dklilley left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! Overall this is looking good - I just let a few comments with suggestions.

It would also be great to add some unit tests for SemanticTokensProvider. You should be able to use either FormatSupportProvider.test.ts or HighlightSymbolProvider.test.ts as starting points.

Comment thread src/providers/semanticTokens/SemanticTokensProvider.ts Outdated
Comment thread src/providers/semanticTokens/SemanticTokensProvider.ts Outdated
Comment thread src/providers/semanticTokens/SemanticTokensProvider.ts Outdated
Comment thread src/providers/semanticTokens/SemanticTokensProvider.ts Outdated
Comment thread src/server.ts Outdated
Comment on lines +379 to +394
// Ensures that semantic tokens are refreshed after indexing,
// so highlighting is updated after opening the editor.
documentIndexer.setOnIndexed(() => {
scheduleSemanticRefresh()
})

let refreshTimer: NodeJS.Timeout | undefined

function scheduleSemanticRefresh (): void {
if (refreshTimer != null) clearTimeout(refreshTimer)

// Delay sending the refresh notification to batch multiple indexing updates together
refreshTimer = setTimeout(() => {
void connection.sendRequest('workspace/semanticTokens/refresh')
}, 150)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this logic be moved within the SemanticTokenProvider? I think this can be set up in the constructor there, which should help encapsulate this logic.

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.

I have moved the logic into a separate function, which hides the logic from the server but keeps the provider focused on computing the tokens. I feel like that's a decent enough solution that doesn't mix responsibilities.

@Gustaf-C
Copy link
Copy Markdown
Contributor Author

Thanks for your comments, I have made some tweaks accordingly.

I'll add some basic unit tests.

@Gustaf-C Gustaf-C requested a review from dklilley April 18, 2026 20:08
@dklilley dklilley self-assigned this Apr 20, 2026
@dklilley dklilley merged commit 22edf92 into mathworks:main Apr 20, 2026
30 checks passed
@dklilley
Copy link
Copy Markdown
Member

Thanks for working on this!

@Gustaf-C Gustaf-C deleted the semantic_highlights branch April 20, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make different highlighting for function call and variable field access

2 participants