Skip to content
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

Semantic tokens highlighting #2919

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open

Semantic tokens highlighting #2919

wants to merge 15 commits into from

Conversation

MikaelMayer
Copy link
Member

@MikaelMayer MikaelMayer commented Oct 24, 2022

Before
image

After
image

Fixes dafny-lang/ide-vscode#299

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@@ -328,6 +328,7 @@ abstract public class ModuleDecl : TopLevelDecl {
return Signature;
}
public int Height;
public Token RootToken = new Token();
Copy link
Member

Choose a reason for hiding this comment

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

Root sounds like a tree is involved, but IIUC the tokens form a list not a tree. How about FirstToken as a name?

Copy link
Member

@keyboardDrummer keyboardDrummer left a comment

Choose a reason for hiding this comment

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

Awesome stuff, thanks @MikaelMayer and @cpitclaudel

Comment
}

private static TokenClass ClassifyToken(int kind) { // FIXME rewrite as a dictionary of lists
Copy link
Member

Choose a reason for hiding this comment

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

Please resolve the FIXME

#pragma warning disable CS8524
// [CS8524] The switch expression does not handle some values of its input type (it is not exhaustive) involving
// an unnamed enum value. For example, the pattern '(Microsoft.Dafny.Parser.TokenClass)16' is not covered.
Parser.TokenClassificationMap[tok.kind] switch {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we first map to Parser.TokenClass and then to (SemanticTokenType? tokenType, SemanticTokenModifier[] modifiers), instead of skipping Parser.TokenClass ?

Copy link
Member Author

Choose a reason for hiding this comment

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

There are two levels of abstraction here. TokenClass regroups tokens by more classes than SemanticTokenType, so that it will be easier to migrate groups to new semantics tokens when they appear.

var importNameTokens =
await ParseAndCollectTokens("module M {} module N { import M' = M }");

// FIXME
Copy link
Member

Choose a reason for hiding this comment

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

FIXME

logger.LogWarning("Tokens requested for unloaded document {DocumentUri}", identifier.TextDocument.Uri);
return;
}
var document = await documentManager.GetLastDocumentAsync();
Copy link
Member

Choose a reason for hiding this comment

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

Please don't use GetLastDocumentAsync. There's no need to wait for verification to have completed. I think we need a GetParsedDocumentAsync, although using GetResolvedDocumentAsync would already be better.

};
}

private void CollectBasicTokens(DafnySemanticTokensBuilder builder, Dafny.Program program) {
Copy link
Member

Choose a reason for hiding this comment

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

Basic doesn't mean much to me, and I think Semantic in CollectSemanticTokens is incorrect because I would say parsing doesn't establish semantics, even though LSP mentions the word Semantic in many of its token related classes (that we are also using for the 'Basic' tokens).

I'm not sure about the names though. Maybe PushTokensBasedOnLexing and PushTokensBasedOnParsing

Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed to "CollectScannerTokens" and "CollectResolutionBasedTokens"

}

public override void Visit(LiteralExpr literalExpression) {
//# builder.Push("literalExpression", literalExpression.tok, SemanticTokenType.Macro, SemanticTokenModifier.DefaultLibrary); //?Macro
Copy link
Member

Choose a reason for hiding this comment

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

What's going on here?

}

public override void VisitUnknown(object node, IToken token) {
// FIXME change to logging a warning
Copy link
Member

Choose a reason for hiding this comment

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

FIXME

// FIXME change to logging a warning
}

public override void Visit(ModuleDefinition moduleDefinition) { // TODO Keyword token
Copy link
Member

Choose a reason for hiding this comment

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

TODO

/// <summary>
/// Visitor responsible for populating a table of document tokens.
/// </summary>
public class LspSemanticTokensGeneratingVisitor : SyntaxTreeVisitor {
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to replace this visitor with an interface IHasHighlight { bool Highlight(DafnySemanticTokensBuilder builder) that the individual Node classes can implement ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Would it be possible to replace this visitor with an interface IHasHighlight { bool Highlight(DafnySemanticTokensBuilder builder) that the individual Node classes can implement ?

That would require to make DafnyCore aware of the language server, so I wouldn't go that route.

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.

expect :- does not highlight expect
3 participants