From 622a919e7644d24f832000e89631e057f7661924 Mon Sep 17 00:00:00 2001 From: yeger Date: Tue, 18 Jun 2019 15:44:46 -0400 Subject: [PATCH] adding defintion and declaration support --- classes.dot | 82 ++++++++++++++++++++------------------ examples/clangd.py | 1 + pylspclient/lsp_client.py | 38 +++++++++++++++++- pylspclient/lsp_structs.py | 25 +++++++++++- 4 files changed, 105 insertions(+), 41 deletions(-) diff --git a/classes.dot b/classes.dot index 4327fe5..b88ad9f 100644 --- a/classes.dot +++ b/classes.dot @@ -31,43 +31,47 @@ rankdir=BT "27" [label="Key", shape="record"]; "28" [label="LANGUAGE_IDENTIFIER", shape="record"]; "29" [label="Location", shape="record"]; -"30" [label="LspClient", shape="record"]; -"31" [label="LspEndpoint", shape="record"]; -"32" [label="Method", shape="record"]; -"33" [label="MethodNotFound", shape="record"]; -"34" [label="Module", shape="record"]; -"35" [label="MyEncoder", shape="record"]; -"36" [label="Namespace", shape="record"]; -"37" [label="Null", shape="record"]; -"38" [label="Number", shape="record"]; -"39" [label="Object", shape="record"]; -"40" [label="Operator", shape="record"]; -"41" [label="Package", shape="record"]; -"42" [label="ParameterInformation", shape="record"]; -"43" [label="ParseError", shape="record"]; -"44" [label="Position", shape="record"]; -"45" [label="Property", shape="record"]; -"46" [label="Range", shape="record"]; -"47" [label="RequestCancelled", shape="record"]; -"48" [fontcolor="red", label="ResponseError", shape="record"]; -"49" [label="ServerNotInitialized", shape="record"]; -"50" [label="SignatureHelp", shape="record"]; -"51" [label="SignatureInformation", shape="record"]; -"52" [label="String", shape="record"]; -"53" [label="Struct", shape="record"]; -"54" [label="SymbolInformation", shape="record"]; -"55" [label="SymbolKind", shape="record"]; -"56" [label="TextDocumentIdentifier", shape="record"]; -"57" [label="TextDocumentItem", shape="record"]; -"58" [label="TextDocumentPositionParams", shape="record"]; -"59" [label="TextEdit", shape="record"]; -"60" [label="TypeParameter", shape="record"]; -"61" [label="UnknownErrorCode", shape="record"]; -"62" [label="Variable", shape="record"]; -"63" [label="serverErrorEnd", shape="record"]; -"64" [label="serverErrorStart", shape="record"]; -"29" -> "54" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="location", style="solid"]; -"44" -> "46" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="start", style="solid"]; -"44" -> "46" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="end", style="solid"]; -"46" -> "29" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="range", style="solid"]; +"30" [label="LocationLink", shape="record"]; +"31" [label="LspClient", shape="record"]; +"32" [label="LspEndpoint", shape="record"]; +"33" [label="Method", shape="record"]; +"34" [label="MethodNotFound", shape="record"]; +"35" [label="Module", shape="record"]; +"36" [label="MyEncoder", shape="record"]; +"37" [label="Namespace", shape="record"]; +"38" [label="Null", shape="record"]; +"39" [label="Number", shape="record"]; +"40" [label="Object", shape="record"]; +"41" [label="Operator", shape="record"]; +"42" [label="Package", shape="record"]; +"43" [label="ParameterInformation", shape="record"]; +"44" [label="ParseError", shape="record"]; +"45" [label="Position", shape="record"]; +"46" [label="Property", shape="record"]; +"47" [label="Range", shape="record"]; +"48" [label="RequestCancelled", shape="record"]; +"49" [fontcolor="red", label="ResponseError", shape="record"]; +"50" [label="ServerNotInitialized", shape="record"]; +"51" [label="SignatureHelp", shape="record"]; +"52" [label="SignatureInformation", shape="record"]; +"53" [label="String", shape="record"]; +"54" [label="Struct", shape="record"]; +"55" [label="SymbolInformation", shape="record"]; +"56" [label="SymbolKind", shape="record"]; +"57" [label="TextDocumentIdentifier", shape="record"]; +"58" [label="TextDocumentItem", shape="record"]; +"59" [label="TextDocumentPositionParams", shape="record"]; +"60" [label="TextEdit", shape="record"]; +"61" [label="TypeParameter", shape="record"]; +"62" [label="UnknownErrorCode", shape="record"]; +"63" [label="Variable", shape="record"]; +"64" [label="serverErrorEnd", shape="record"]; +"65" [label="serverErrorStart", shape="record"]; +"29" -> "55" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="location", style="solid"]; +"45" -> "47" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="start", style="solid"]; +"45" -> "47" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="end", style="solid"]; +"47" -> "29" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="range", style="solid"]; +"47" -> "30" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="originSelectionRange", style="solid"]; +"47" -> "30" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="targetRange", style="solid"]; +"47" -> "30" [arrowhead="diamond", arrowtail="none", fontcolor="green", label="targetSelectionRange", style="solid"]; } diff --git a/examples/clangd.py b/examples/clangd.py index 922c7e8..f0c258c 100644 --- a/examples/clangd.py +++ b/examples/clangd.py @@ -160,6 +160,7 @@ if __name__ == "__main__": lsp_client.definition(pylspclient.lsp_structs.TextDocumentIdentifier(uri), pylspclient.lsp_structs.Position(14, 4)) lsp_client.signatureHelp(pylspclient.lsp_structs.TextDocumentIdentifier(uri), pylspclient.lsp_structs.Position(14, 4)) + lsp_client.definition(pylspclient.lsp_structs.TextDocumentIdentifier(uri), pylspclient.lsp_structs.Position(14, 4)) lsp_client.completion(pylspclient.lsp_structs.TextDocumentIdentifier(uri), pylspclient.lsp_structs.Position(14, 4), pylspclient.lsp_structs.CompletionContext(pylspclient.lsp_structs.CompletionTriggerKind.Invoked)) lsp_client.shutdown() lsp_client.exit() diff --git a/pylspclient/lsp_client.py b/pylspclient/lsp_client.py index 024e512..2edae43 100644 --- a/pylspclient/lsp_client.py +++ b/pylspclient/lsp_client.py @@ -141,6 +141,42 @@ class LspClient(object): """ result_dict = self.lsp_endpoint.call_method("textDocument/completion", textDocument=textDocument, position=position, context=context) if "isIncomplete" in result_dict: - return lsp_structs.CompletionList (**result_dict) + return lsp_structs.CompletionList(**result_dict) return [lsp_structs.CompletionItem(**l) for l in result_dict] + + + def declaration(self, textDocument, position): + """ + The go to declaration request is sent from the client to the server to resolve the declaration location of a + symbol at a given text document position. + + The result type LocationLink[] got introduce with version 3.14.0 and depends in the corresponding client + capability `clientCapabilities.textDocument.declaration.linkSupport`. + + :param TextDocumentItem textDocument: The text document. + :param Position position: The position inside the text document. + """ + result_dict = self.lsp_endpoint.call_method("textDocument/declaration", textDocument=textDocument, position=position) + if "uri" in result_dict: + return lsp_structs.Location(**result_dict) + + return [lsp_structs.Location(**l) if "uri" in l else lsp_structs.LinkLocation(**l) for l in result_dict] + + + def definition(self, textDocument, position): + """ + The go to definition request is sent from the client to the server to resolve the declaration location of a + symbol at a given text document position. + + The result type LocationLink[] got introduce with version 3.14.0 and depends in the corresponding client + capability `clientCapabilities.textDocument.declaration.linkSupport`. + + :param TextDocumentItem textDocument: The text document. + :param Position position: The position inside the text document. + """ + result_dict = self.lsp_endpoint.call_method("textDocument/definition", textDocument=textDocument, position=position) + if "uri" in result_dict: + return lsp_structs.Location(**result_dict) + + return [lsp_structs.Location(**l) if "uri" in l else lsp_structs.LinkLocation(**l) for l in result_dict] diff --git a/pylspclient/lsp_structs.py b/pylspclient/lsp_structs.py index 6e53448..d5f09ed 100644 --- a/pylspclient/lsp_structs.py +++ b/pylspclient/lsp_structs.py @@ -44,7 +44,7 @@ class Location(object): """ def __init__(self, uri, range): """ - Constructs a new Range instance. + Constructs a new Location instance. :param str uri: Resource file. :param Range range: The range inside the file @@ -52,6 +52,29 @@ class Location(object): self.uri = uri self.range = to_type(range, Range) + +class LocationLink(object): + """ + Represents a link between a source and a target location. + """ + def __init__(self, originSelectionRange, targetUri, targetRange, targetSelectionRange): + """ + Constructs a new LocationLink instance. + + :param Range originSelectionRange: Span of the origin of this link. + Used as the underlined span for mouse interaction. Defaults to the word range at the mouse position. + :param str targetUri: The target resource identifier of this link. + :param Range targetRange: The full target range of this link. If the target for example is a symbol then target + range is the range enclosing this symbol not including leading/trailing whitespace but everything else + like comments. This information is typically used to highlight the range in the editor. + :param Range targetSelectionRange: The range that should be selected and revealed when this link is being followed, + e.g the name of a function. Must be contained by the the `targetRange`. See also `DocumentSymbol#range` + """ + self.originSelectionRange = to_type(originSelectionRange, Range) + self.targetUri = targetUri + self.targetRange = to_type(targetRange, Range) + self.targetSelectionRange = to_type(targetSelectionRange, Range) + class Diagnostic(object): def __init__(self, range, severity, code, source, message, relatedInformation):