From 4c3adea7a4ab7c63010a953547152d4ad861f9de Mon Sep 17 00:00:00 2001 From: River Riddle Date: Mon, 7 Jun 2021 13:59:50 -0700 Subject: [PATCH] [mlir-lsp-server] Add support for hover on symbol references For now the hover simply shows the same information as hovering on the operation name. If necessary this can be tweaked to something symbol specific later. Differential Revision: https://reviews.llvm.org/D103728 --- mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp | 14 ++++++++++---- mlir/test/mlir-lsp-server/hover.test | 26 +++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp b/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp index 3ff36337..715bc27 100644 --- a/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp +++ b/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp @@ -286,7 +286,8 @@ struct MLIRDocument { Optional findHover(const lsp::URIForFile &uri, const lsp::Position &hoverPos); Optional - buildHoverForOperation(const AsmParserState::OperationDefinition &op); + buildHoverForOperation(llvm::SMRange hoverRange, + const AsmParserState::OperationDefinition &op); lsp::Hover buildHoverForOperationResult(llvm::SMRange hoverRange, Operation *op, unsigned resultStart, unsigned resultEnd, @@ -439,7 +440,12 @@ Optional MLIRDocument::findHover(const lsp::URIForFile &uri, for (const AsmParserState::OperationDefinition &op : asmState.getOpDefs()) { // Check if the position points at this operation. if (contains(op.loc, posLoc)) - return buildHoverForOperation(op); + return buildHoverForOperation(op.loc, op); + + // Check if the position points at the symbol name. + for (auto &use : op.symbolUses) + if (contains(use, posLoc)) + return buildHoverForOperation(use, op); // Check if the position points at a result group. for (unsigned i = 0, e = op.resultGroups.size(); i < e; ++i) { @@ -473,8 +479,8 @@ Optional MLIRDocument::findHover(const lsp::URIForFile &uri, } Optional MLIRDocument::buildHoverForOperation( - const AsmParserState::OperationDefinition &op) { - lsp::Hover hover(getRangeFromLoc(sourceMgr, op.loc)); + llvm::SMRange hoverRange, const AsmParserState::OperationDefinition &op) { + lsp::Hover hover(getRangeFromLoc(sourceMgr, hoverRange)); llvm::raw_string_ostream os(hover.contents.value); // Add the operation name to the hover. diff --git a/mlir/test/mlir-lsp-server/hover.test b/mlir/test/mlir-lsp-server/hover.test index 9e5a89b..fea204d 100644 --- a/mlir/test/mlir-lsp-server/hover.test +++ b/mlir/test/mlir-lsp-server/hover.test @@ -128,6 +128,30 @@ // CHECK-NEXT: } // CHECK-NEXT: } // ----- -{"jsonrpc":"2.0","id":6,"method":"shutdown"} +// Hover on a symbol reference. +{"jsonrpc":"2.0","id":6,"method":"textDocument/hover","params":{ + "textDocument":{"uri":"test:///foo.mlir"}, + "position":{"line":0,"character":8} +}} +// CHECK: "id": 6, +// CHECK-NEXT: "jsonrpc": "2.0", +// CHECK-NEXT: "result": { +// CHECK-NEXT: "contents": { +// CHECK-NEXT: "kind": "markdown", +// CHECK-NEXT: "value": "\"func\" : public @foo\n\nGeneric Form:\n\n```mlir\n\"func\"() ( {\n}) {sym_name = \"foo\", type = (i1) -> ()} : () -> ()\n```\n" +// CHECK-NEXT: }, +// CHECK-NEXT: "range": { +// CHECK-NEXT: "end": { +// CHECK-NEXT: "character": 9, +// CHECK-NEXT: "line": 0 +// CHECK-NEXT: }, +// CHECK-NEXT: "start": { +// CHECK-NEXT: "character": 5, +// CHECK-NEXT: "line": 0 +// CHECK-NEXT: } +// CHECK-NEXT: } +// CHECK-NEXT: } +// ----- +{"jsonrpc":"2.0","id":7,"method":"shutdown"} // ----- {"jsonrpc":"2.0","method":"exit"} -- 2.7.4