You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TLDR
I don't see any way to enable semanticTokens (seems off by default) when running gopls from the CLI. How does one pass configurations equivalent to the settings.json when in such a setting?
Long Version
I am trying to extract semantic tokens from gopls in the terminal. Towards this, I have written up the following rudimentary JsonRPC client that inits the server, opens the document, queries semantic tokens, closes the document and finally shuts down the server connection.
importjsonimportosimportasyncioasyncdeflsp_client():
host='0.0.0.0'# The host where gopls is runningport=56063# The port where gopls is listeningreader, writer=awaitasyncio.open_connection(host, port)
# Function to send a JSON RPC messageasyncdefsend_message(message):
message_json=json.dumps(message).encode('utf-8')
# Prepend message with Content-Length header required by LSPmessage_length=len(message_json)
writer.write(f"Content-Length: {message_length}\r\n\r\n".encode('utf-8') +message_json)
awaitwriter.drain()
asyncdefreceive_message():
whileTrue:
content_length=int((awaitreader.readline()).decode('utf-8').split(":")[1].strip())
awaitreader.readline() # Read the next two CRLF charactersmessage_text= (awaitreader.readexactly(content_length)).decode('utf-8')
message=json.loads(message_text)
if'method'inmessageandmessage['method'] =='window/showMessage':
print("Notification from server:", message['params']['message'])
elif'method'inmessageandmessage['method'] =='window/logMessage':
print("Log from server:", message['params']['message'])
else:
returnmessage# Initialize the serverinit_msg= {
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"rootUri": f"file://{os.getcwd()}/Go/Tests/",
"capabilities": {
"textDocument": {
"semanticTokens": {
"tokenTypes": [
"namespace", "type", "class", "enum", "interface",
"struct", "typeParameter", "parameter", "variable",
"property", "enumMember", "event", "function", "method",
"macro", "keyword", "modifier", "comment", "string",
"number", "regexp", "operator"
],
"tokenModifiers": [
"declaration", "definition", "readonly", "static",
"deprecated", "abstract", "async", "modification",
"documentation", "defaultLibrary"
],
"formats": ["relative"],
"requests": {
"full": True
}
},
"initializationOptions": {
"semanticTokens": True
}
}
},
},
}
awaitsend_message(init_msg)
print(f"Initialization Response: {awaitreceive_message()}")
# Notify the server that we're initializedinitialized_msg= {
"jsonrpc": "2.0",
"id": 2,
"method": "initialized",
"params": {},
}
awaitsend_message(initialized_msg)
print(f"Initialized Response: {awaitreceive_message()}")
# Open a documentopen_doc_msg= {
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {
"textDocument": {
"uri": "file:///path/to/your/file.go",
"languageId": "go",
"version": 1,
"text": "package main\n\nfunc main() {\n\t// Your Go code here\n}"
}
}
}
awaitsend_message(open_doc_msg)
print(f"Open Document Response: {awaitreceive_message()}")
# Request semantic tokenssemantic_tokens_msg= {
"jsonrpc": "2.0",
"id": 2,
"method": "textDocument/semanticTokens/full",
"params": {
"textDocument": {
"uri": "file:///path/to/your/file.go"
}
}
}
awaitsend_message(semantic_tokens_msg)
print(f"Semantic Tokens Response: {awaitreceive_message()}")
# Close the documentclose_doc_msg= {
"jsonrpc": "2.0",
"method": "textDocument/didClose",
"params": {
"textDocument": {
"uri": "file:///path/to/your/file.go"
}
}
}
awaitsend_message(close_doc_msg)
print(f"Close Document Response: {awaitreceive_message()}")
# Close the servershutdown_msg= {
"jsonrpc": "2.0",
"id": 3,
"method": "shutdown",
}
awaitsend_message(shutdown_msg)
print(f"Shutdown Response: {awaitreceive_message()}")
writer.close()
awaitwriter.wait_closed()
asyncio.run(lsp_client())
It appears the semantic tokens are disabled by default and can only be enabled by editing the settings.json file which only seems to apply when running the language server from within the IDE. How does one go about enabling this when running it on the CLI? I don't see any way to pass a settings file to the CLI executable.
The text was updated successfully, but these errors were encountered:
iNeil77
added
gopls
Issues related to the Go language server, gopls.
Tools
This label describes issues relating to any tools in the x/tools repository.
labels
Dec 28, 2023
I wished to use semantic tokenization to test the code-understanding ability of code language models. The unified interface provided by the LSP has been a huge leg up towards this end. So far I have used clangd for C, C++ and rust-analyzer for Rust from the terminal. I was wishing I could do the same for Go without instrumenting a VS code extension.
Would it be possible for you to allow for a way for a settings.json to be passed to the gopls CLI executable?
Is the init_param correct? From LSP spec, initializationOptions is a top-level field of the InitializeParams message, not the TextDocumentClientCapabilities message.
gopls version
v0.11.0
go env
What did you do?
TLDR
I don't see any way to enable
semanticTokens
(seems off by default) when runninggopls
from the CLI. How does one pass configurations equivalent to thesettings.json
when in such a setting?Long Version
I am trying to extract semantic tokens from
gopls
in the terminal. Towards this, I have written up the following rudimentary JsonRPC client that inits the server, opens the document, queries semantic tokens, closes the document and finally shuts down the server connection.However on running this I get the following logs:
It appears the semantic tokens are disabled by default and can only be enabled by editing the
settings.json
file which only seems to apply when running the language server from within the IDE. How does one go about enabling this when running it on the CLI? I don't see any way to pass a settings file to the CLI executable.What did you expect to see?
Semantic tokenization of Go source file.
What did you see instead?
{'jsonrpc': '2.0', 'error': {'code': 0, 'message': 'semantictokens are disabled'}, 'id': 2}
Editor and settings
No response
Logs
No response
The text was updated successfully, but these errors were encountered: