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

Use semantic highlighting #1019

Open
2 tasks
edvgui opened this issue Mar 13, 2023 · 0 comments
Open
2 tasks

Use semantic highlighting #1019

edvgui opened this issue Mar 13, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@edvgui
Copy link
Contributor

edvgui commented Mar 13, 2023

The language server protocol allows a server to provide information about semantic tokens, which can, from the editor side, be mapped to some nice coloring. It would for example allow us to color the entity names, plugin names, namespaces path, variable, etc.

I started exploring this, and I have a really short PoC, but it misses some elements to work correctly.

  1. The server should communicate that it can provide semantic tokens, and give the list of tokens it might return
--- a/server/src/inmantals/server.py
+++ b/server/src/inmantals/server.py
@@ -122,6 +122,14 @@ class InmantaLSHandler(JsonRpcHandler):
                     # the language server does not report work done progress for workspace symbol requests
                     "workDoneProgress": False,
                 },
+                "semanticTokensProvider": {
+                    "legend": {
+                        "tokenTypes": ["type"],
+                        "tokenModifiers": [],
+                    },
+                    "range": False,
+                    "full": True,
+                }
             }
         }
 
  1. The server will then receive textDocument/semanticTokens/full requests, which it can answer to with a list containing the the position of tokens, their length, and the token in question (its index in the list provided in the capabilities config returned by the server)
    async def textDocument_semanticTokens_full(self, textDocument):
        """
        https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide#scope-inspector
        https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide
        https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_semanticTokens
        """
        uri = textDocument["uri"]

        url = os.path.realpath(uri.replace("file://", ""))

        # line, start char, length, tokenType

        logger.info("Providing semantic tokens for file %s", url)

        # This says that at the first line of the file, at position zero and for a length
        # of 6 characters, the token present there is a type
        return {"data": [1, 0, 6, 0]}
  1. When running the extension, and using the token inspector, you can then see that the information is passed through and accessible by vscode. We just need to setup some coloring for it.
Screenshare.-.2023-03-13.8_03_42.AM.mp4

Here is a list of things that should still be figured out:

  • How to extract the semantic tokens information from the compiler, this should ideally be done after parsing, and before the compiler runs completely (we want semantic coloring even if the compile fails)
  • How to setup the coloring in the extension

Some useful references:

@edvgui edvgui added the enhancement New feature or request label Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant