Distinguishing type identifiers from function/variable identifiers with SwiftSyntax

when using SwiftSyntax as a code highlighter, it seems that SyntaxClassifications does not correctly label identifiers as type identifiers unless they appear in the post-colon position. so a code snippet like the following wouldn’t have any distinguishing colors for the Foo and Bar tokens.

let x = Foo.Bar()

this is a pretty big regression from other highlighter implementations like Splash, that output a much “richer” set of syntax highlights.

can SwiftSyntax achieve something similar to this?

cc @ahoppen

No, types in expression position cannot be resolved syntactically in Swift (except from lexical context, basically by imitating lookup from the code provided and hoping it’s correct). let x = Foo.Bar() and let x = foo.bar() are the same as far as the language is concerned, and both Foo and foo could refer to something in another file or imported module.


okay, that’s more or less what i suspected, thanks for confirming. my next idea is to try and compile the snippets server-side and read the indexstore-db output to obtain the token classifications. would you recommend going down that path, or is there a simpler approach to take?

Someone more recently involved with the project might have better suggestions, but yeah, that or SourceKit. Even within the compiler types are treated as defining names just like var and func and case; restrictions like “types can’t appear as values without .self” are applied afterwards.

I would suggest that you start a sourcekit-lsp instance, open the document and run the textDocument/semanticTokens/full request. That aggregates syntax coloring from swift-syntax and sourcekitd.