Skip to content

Instantly share code, notes, and snippets.

@thomasjm
Created December 5, 2018 21:31
Show Gist options
  • Save thomasjm/1b7e93e0966365ca9ffa76345fcaff08 to your computer and use it in GitHub Desktop.
Save thomasjm/1b7e93e0966365ca9ffa76345fcaff08 to your computer and use it in GitHub Desktop.
Repro for javascript-typescript-langserver issue 502
#!/usr/bin/python
import atexit
import os
import socket
import subprocess
from time import sleep
port = 5000
# Start the LSP server (send log to /tmp folder)
all_processes = []
def cleanup():
timeout_sec = 5
for p in all_processes: # list of your processes
p_sec = 0
for second in range(timeout_sec):
if p.poll() == None:
sleep(1)
p_sec += 1
if p_sec >= timeout_sec:
p.kill() # supported from python 2.6
print("cleaned up!")
atexit.register(cleanup)
all_processes.append(subprocess.Popen(["javascript-typescript-langserver", "-p", str(port), "-t", "-l", "/tmp/langserver_output.log"]))
# Sleep so it has time to come up (increase this if needed)
sleep(5)
# Connect to LSP server via TCP
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", port))
def send_command(sock, contents):
sock.send(b"Content-Length: %d\r\n\r\n" % len(contents))
sock.send(contents)
def receive_message(sock):
first_line = ""
while True:
first_line += sock.recv(1).decode("utf-8")
if first_line.endswith("\r\n"):
content_length = int(first_line[len("Content-Length:"):].strip())
break
sock.recv(2) # Read \r\n
return sock.recv(content_length)
# Send initialize message and get response
# Make /tmp/lsphome be the home folder for this test
if not os.path.exists("/tmp/lsphome"): os.mkdir("/tmp/lsphome")
send_command(sock, b'{"jsonrpc":"2.0","id":"0.ro2h94rs7jo","method":"initialize","params":{"rootPath":"/tmp/lsphome","rootUri":"file:///tmp/lsphome","initializationOptions":{},"trace":"verbose","capabilities":{"workspace":{"applyEdit":false,"workspaceEdit":{"documentChanges":false},"didChangeConfiguration":{"dynamicRegistration":false},"didChangeWatchedFiles":{"dynamicRegistration":false},"symbol":{"dynamicRegistration":false,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]}},"executeCommand":{"dynamicRegistration":false},"workspaceFolders":false,"configuration":false},"textDocument":{"synchronization":{"dynamicRegistration":false,"willSave":false,"willSaveWaitUntil":false,"didSave":false},"completion":{"dynamicRegistration":false,"completionItem":{"snippetSupport":true,"commitCharactersSupport":true,"documentationFormat":["markdown","plaintext"],"deprecatedSupport":true,"preselectSupport":true},"completionItemKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]},"contextSupport":true},"hover":{"dynamicRegistration":false,"contentFormat":["markdown","plaintext"]},"signatureHelp":{"dynamicRegistration":false,"signatureInformation":{}},"references":{"dynamicRegistration":false},"documentHighlight":{"dynamicRegistration":false},"documentSymbol":{"dynamicRegistration":false,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"hierarchicalDocumentSymbolSupport":true},"formatting":{"dynamicRegistration":false},"rangeFormatting":{"dynamicRegistration":false},"onTypeFormatting":{"dynamicRegistration":false},"typeDefinition":{"dynamicRegistration":false},"implementation":{"dynamicRegistration":false},"codeAction":{"dynamicRegistration":false,"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["quickfix","refactor"]}}},"codeLens":{"dynamicRegistration":false},"documentLink":{"dynamicRegistration":false},"colorProvider":{"dynamicRegistration":false},"rename":{"dynamicRegistration":false},"publishDiagnostics":{"relatedInformation":true},"foldingRange":{"dynamicRegistration":false,"rangeLimit":5,"lineFoldingOnly":false}}}}}')
response = receive_message(sock)
print("Initialize response: %s" % response)
# Create simple document and send textDocument/didOpen command
with open("/tmp/lsphome/test.js", "w") as f: f.write("con")
send_command(sock, b'{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file:///tmp/lsphome/test.js","languageId":"js","version":111,"text":"con"}}}')
# Send complete command
send_command(sock, b'{"jsonrpc":"2.0","id":"0.h7vhhjfuc16","method":"textDocument/completion","params":{"context":{"triggerKind":1},"textDocument":{"uri":"file:///tmp/lsphome/test.js"},"position":{"line":0,"character":3}}}')
complete_response = receive_message(sock)
print("\n\nComplete response", complete_response)
# Exit
sock.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment