From 4085b06211100cd36acb2506bbf201953bac86b6 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Mon, 22 May 2017 13:29:28 -0400 Subject: [PATCH 26/31] FIXME: introduce lsp.py --- gcc/testsuite/gcc.dg/lsp/lsp.py | 121 ++++++++++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/lsp/test.py | 123 +-------------------------------------- 2 files changed, 123 insertions(+), 121 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/lsp/lsp.py diff --git a/gcc/testsuite/gcc.dg/lsp/lsp.py b/gcc/testsuite/gcc.dg/lsp/lsp.py new file mode 100644 index 0000000..f86aca6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/lsp/lsp.py @@ -0,0 +1,121 @@ +import json + +import jsonrpc # pip install json-rpc +import requests + +# Various types to model the LSP interface + +class Position: + def __init__(self, line, character): + self.line = line + self.character = character + + def __repr__(self): + return 'Position(%r, %r)' % (self.line, self.character) + + def __eq__(self, other): + return self.__dict__ == other.__dict__ + + def to_json(self): + return {'line': self.line, 'character': self.character} + + @staticmethod + def from_json(js): + return Position(js['line'], js['character']) + +class Range: + def __init__(self, start, end): + self.start = start + self.end = end + + def __repr__(self): + return 'Range(%r, %r)' % (self.start, self.end) + + def __eq__(self, other): + return self.__dict__ == other.__dict__ + + @staticmethod + def from_json(js): + return Range(Position.from_json(js['start']), + Position.from_json(js['end'])) + +class Location: + def __init__(self, uri, range): + self.uri = uri + self.range = range + + def __repr__(self): + return 'Location(%r, %r)' % (self.uri, self.range) + + def __eq__(self, other): + return self.__dict__ == other.__dict__ + + @staticmethod + def from_json(js): + return Location(js['uri'], Range.from_json(js['range'])) + + def dump(self, msg): + print('%s:%i:%i: %s' % (self.uri, self.range.start.line, + self.range.start.character, msg)) + # FIXME: underline + import linecache + line = linecache.getline(self.uri, self.range.start.line) + print('line: %r' % line) + +class TextDocumentIdentifier: + def __init__(self, uri): + self.uri = uri + + def __eq__(self, other): + return self.__dict__ == other.__dict__ + + def to_json(self): + return {'uri': self.uri} + +class TextDocumentPositionParams: + def __init__(self, textDocument, position): + self.textDocument = textDocument + self.position = position + + def __eq__(self, other): + return self.__dict__ == other.__dict__ + + def to_json(self): + return {"textDocument" : self.textDocument.to_json(), + "position" : self.position.to_json()} + +# A wrapper for making LSP calls against a server + +class Proxy: + def __init__(self, url): + self.url = url + self.next_id = 0 + + def make_request(self, method, params): + json_req = {"method": method, + "params": params, + "jsonrpc": "2.0", + "id": self.next_id} + self.next_id += 1 + return json_req + + def post_request(self, method, params): + payload = self.make_request(method, params) + headers = {'content-type': 'application/json'} + response = requests.post(self.url, data=json.dumps(payload), + headers=headers) + print('response: %r' % response) + print('response.json(): %r' % response.json()) + return response.json() + + def goto_definition(self, textDocument, position): + params = TextDocumentPositionParams(textDocument, position) + json_resp = self.post_request('textDocument/definition', + params.to_json()) + print(json_resp) + # Expect either a Location or a list of Location + if isinstance(json_resp, list): + return [Location.from_json(item) for item in json_resp] + else: + return Location.from_json(json_resp) + diff --git a/gcc/testsuite/gcc.dg/lsp/test.py b/gcc/testsuite/gcc.dg/lsp/test.py index 6da4218..52effc0 100644 --- a/gcc/testsuite/gcc.dg/lsp/test.py +++ b/gcc/testsuite/gcc.dg/lsp/test.py @@ -1,123 +1,4 @@ -import json - -import jsonrpc # pip install json-rpc -import requests - -# Various types to model the LSP interface - -class Position: - def __init__(self, line, character): - self.line = line - self.character = character - - def __repr__(self): - return 'Position(%r, %r)' % (self.line, self.character) - - def __eq__(self, other): - return self.__dict__ == other.__dict__ - - def to_json(self): - return {'line': self.line, 'character': self.character} - - @staticmethod - def from_json(js): - return Position(js['line'], js['character']) - -class Range: - def __init__(self, start, end): - self.start = start - self.end = end - - def __repr__(self): - return 'Range(%r, %r)' % (self.start, self.end) - - def __eq__(self, other): - return self.__dict__ == other.__dict__ - - @staticmethod - def from_json(js): - return Range(Position.from_json(js['start']), - Position.from_json(js['end'])) - -class Location: - def __init__(self, uri, range): - self.uri = uri - self.range = range - - def __repr__(self): - return 'Location(%r, %r)' % (self.uri, self.range) - - def __eq__(self, other): - return self.__dict__ == other.__dict__ - - @staticmethod - def from_json(js): - return Location(js['uri'], Range.from_json(js['range'])) - - def dump(self, msg): - print('%s:%i:%i: %s' % (self.uri, self.range.start.line, - self.range.start.character, msg)) - # FIXME: underline - import linecache - line = linecache.getline(self.uri, self.range.start.line) - print('line: %r' % line) - -class TextDocumentIdentifier: - def __init__(self, uri): - self.uri = uri - - def __eq__(self, other): - return self.__dict__ == other.__dict__ - - def to_json(self): - return {'uri': self.uri} - -class TextDocumentPositionParams: - def __init__(self, textDocument, position): - self.textDocument = textDocument - self.position = position - - def __eq__(self, other): - return self.__dict__ == other.__dict__ - - def to_json(self): - return {"textDocument" : self.textDocument.to_json(), - "position" : self.position.to_json()} - -# A wrapper for making LSP calls against a server - -class LspProxy: - def __init__(self, url): - self.url = url - self.next_id = 0 - - def make_request(self, method, params): - json_req = {"method": method, - "params": params, - "jsonrpc": "2.0", - "id": self.next_id} - self.next_id += 1 - return json_req - - def post_request(self, method, params): - payload = self.make_request(method, params) - headers = {'content-type': 'application/json'} - response = requests.post(self.url, data=json.dumps(payload), - headers=headers) - print('response: %r' % response) - print('response.json(): %r' % response.json()) - return response.json() - - def goto_definition(self, textDocument, position): - params = TextDocumentPositionParams(textDocument, position) - json_resp = self.post_request('textDocument/definition', - params.to_json()) - print(json_resp) - # Expect either a Location or a list of Location - if isinstance(json_resp, list): - return [Location.from_json(item) for item in json_resp] - else: - return Location.from_json(json_resp) +from lsp import Proxy, TextDocumentIdentifier, Position, Range, Location def main(): # This assumes that we're running this: @@ -126,7 +7,7 @@ def main(): # -flsp=4000 -fblt -wrapper gdb,--args url = "http://localhost:4000/jsonrpc" - proxy = LspProxy(url) + proxy = Proxy(url) # Ask for the location of a usage of "struct foo" result = proxy.goto_definition(TextDocumentIdentifier('test.c'), Position(9, 17)) -- 1.8.5.3