[Mlir-commits] [mlir] 4c3adea - [mlir-lsp-server] Add support for hover on symbol references

River Riddle llvmlistbot at llvm.org
Mon Jun 7 14:10:56 PDT 2021


Author: River Riddle
Date: 2021-06-07T14:07:41-07:00
New Revision: 4c3adea7a4ab7c63010a953547152d4ad861f9de

URL: https://github.com/llvm/llvm-project/commit/4c3adea7a4ab7c63010a953547152d4ad861f9de
DIFF: https://github.com/llvm/llvm-project/commit/4c3adea7a4ab7c63010a953547152d4ad861f9de.diff

LOG: [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

Added: 
    

Modified: 
    mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp
    mlir/test/mlir-lsp-server/hover.test

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp b/mlir/lib/Tools/mlir-lsp-server/MLIRServer.cpp
index 3ff363374605..715bc279b0e1 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<lsp::Hover> findHover(const lsp::URIForFile &uri,
                                  const lsp::Position &hoverPos);
   Optional<lsp::Hover>
-  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<lsp::Hover> 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<lsp::Hover> MLIRDocument::findHover(const lsp::URIForFile &uri,
 }
 
 Optional<lsp::Hover> 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 9e5a89bc3b44..fea204dc2361 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"}


        


More information about the Mlir-commits mailing list