diff --git a/ets2panda/bindings/native/src/lsp.cpp b/ets2panda/bindings/native/src/lsp.cpp index f4f29b2440981e65e48a9cbecd5a41f18b3774e9..94fc4a9ca536e10611d27282154081ad6bcb2fc5 100644 --- a/ets2panda/bindings/native/src/lsp.cpp +++ b/ets2panda/bindings/native/src/lsp.cpp @@ -1832,6 +1832,14 @@ KNativePointer impl_getProgramAst(KNativePointer contextPtr) } TS_INTEROP_1(getProgramAst, KNativePointer, KNativePointer) +KNativePointer impl_getMethodDefinition(KNativePointer astNodePtr, KStringPtr &nodeNamePtr, KStringPtr &mthodNamePtr) +{ + auto ast = reinterpret_cast(astNodePtr); + LSPAPI const *impl = GetImpl(); + return impl->getMethodDefinition(ast, nodeNamePtr.data(), mthodNamePtr.data()); +} +TS_INTEROP_3(getMethodDefinition, KNativePointer, KNativePointer, KStringPtr, KStringPtr) + KNativePointer impl_getDefinitionDataFromNode(KNativePointer astNodePtr, KStringPtr &nodeNamePtr) { auto ast = reinterpret_cast(astNodePtr); @@ -1839,7 +1847,6 @@ KNativePointer impl_getDefinitionDataFromNode(KNativePointer astNodePtr, KString return new DefinitionInfo(impl->getDefinitionDataFromNode(ast, nodeNamePtr.data())); } TS_INTEROP_2(getDefinitionDataFromNode, KNativePointer, KNativePointer, KStringPtr) - KInt impl_getSourceLocationLine(KNativePointer locationPtr) { auto *location = reinterpret_cast *>(locationPtr); @@ -1860,3 +1867,4 @@ KNativePointer impl_getColAndLineByOffset(KStringPtr &sourceCodePtr, KInt offset return new std::pair(impl->getColAndLineByOffset(sourceCodePtr.data(), offset)); } TS_INTEROP_2(getColAndLineByOffset, KNativePointer, KStringPtr, KInt) + diff --git a/ets2panda/lsp/include/api.h b/ets2panda/lsp/include/api.h index 8feb3be6823fd80ca46f771ce37666b22a39fd09..373f50ebd1858413b5ca78055035275c4393a7bc 100644 --- a/ets2panda/lsp/include/api.h +++ b/ets2panda/lsp/include/api.h @@ -556,6 +556,8 @@ typedef struct LSPAPI { es2panda_AstNode *(*getProgramAst)(es2panda_Context *context); es2panda_AstNode *(*getClassDefinition)(es2panda_AstNode *astNode, const std::string &nodeName); es2panda_AstNode *(*getIdentifier)(es2panda_AstNode *astNode, const std::string &nodeName); + es2panda_AstNode *(*getMethodDefinition)(es2panda_AstNode *astNode, const std::string &nodeName, + const std::string &methodName); DefinitionInfo (*getDefinitionDataFromNode)(es2panda_AstNode *astNode, const std::string &nodeName); } LSPAPI; CAPI_EXPORT LSPAPI const *GetImpl(); diff --git a/ets2panda/lsp/include/get_node.h b/ets2panda/lsp/include/get_node.h index b2c63afacb5d3c1240009be2d22ccbcfbda6feb4..9992bdf906a6158a37222d815d6c04f4c8f1fa13 100644 --- a/ets2panda/lsp/include/get_node.h +++ b/ets2panda/lsp/include/get_node.h @@ -22,6 +22,8 @@ namespace ark::es2panda::lsp { es2panda_AstNode *GetProgramAstImpl(es2panda_Context *context); es2panda_AstNode *GetClassDefinitionImpl(es2panda_AstNode *astNode, const std::string &nodeName); es2panda_AstNode *GetIdentifierImpl(es2panda_AstNode *astNode, const std::string &nodeName); +es2panda_AstNode *GetMethodDefinitionImpl(es2panda_AstNode *astNode, const std::string &nodeName, + const std::string &methodName); } // namespace ark::es2panda::lsp #endif \ No newline at end of file diff --git a/ets2panda/lsp/src/api.cpp b/ets2panda/lsp/src/api.cpp index 8f942b3daae489bcdcb9bc76be00cfbe08342607..16c382c72f8bb9505a0ba5f8f258453cd09c99d4 100644 --- a/ets2panda/lsp/src/api.cpp +++ b/ets2panda/lsp/src/api.cpp @@ -483,6 +483,12 @@ es2panda_AstNode *GetIdentifier(es2panda_AstNode *astNode, const std::string &no return GetIdentifierImpl(astNode, nodeName); } +es2panda_AstNode *GetMethodDefinition(es2panda_AstNode *astNode, const std::string &nodeName, + const std::string &methodName) +{ + return GetMethodDefinitionImpl(astNode, nodeName, methodName); +} + DefinitionInfo GetDefinitionDataFromNode(es2panda_AstNode *astNode, const std::string &nodeName) { DefinitionInfo result; @@ -556,6 +562,7 @@ LSPAPI g_lspImpl = {GetDefinitionAtPosition, GetProgramAst, GetClassDefinition, GetIdentifier, + GetMethodDefinition, GetDefinitionDataFromNode}; } // namespace ark::es2panda::lsp diff --git a/ets2panda/lsp/src/get_node.cpp b/ets2panda/lsp/src/get_node.cpp index fa9ea5fea6026f135008bce38bcce5cdd328db09..c0ea59a2bcfd263eb4b6c9fb5780fbd3ddb7ed0e 100644 --- a/ets2panda/lsp/src/get_node.cpp +++ b/ets2panda/lsp/src/get_node.cpp @@ -52,4 +52,18 @@ es2panda_AstNode *GetIdentifierImpl(es2panda_AstNode *astNode, const std::string return reinterpret_cast(targetNode); } +es2panda_AstNode *GetMethodDefinitionImpl(es2panda_AstNode *astNode, const std::string &nodeName, + const std::string &methodName) +{ + if (astNode == nullptr) { + return nullptr; + } + + auto ast = reinterpret_cast(GetClassDefinitionImpl(astNode, nodeName)); + auto targetNode = ast->FindChild([&methodName](ir::AstNode *childNode) { + return childNode->IsMethodDefinition() && + std::string(childNode->AsMethodDefinition()->Function()->Id()->Name()) == methodName; + }); + return reinterpret_cast(targetNode); +} } // namespace ark::es2panda::lsp \ No newline at end of file diff --git a/ets2panda/test/unit/lsp/get_node_test.cpp b/ets2panda/test/unit/lsp/get_node_test.cpp index 140f44ff2208943e776fb662e39c298cb8bd4bb4..3cd2a2aeff2383c1a899afb7cb852cf7ab9c981a 100644 --- a/ets2panda/test/unit/lsp/get_node_test.cpp +++ b/ets2panda/test/unit/lsp/get_node_test.cpp @@ -41,6 +41,7 @@ protected: { contexts_ = initializer.CreateContext("GetNodeTest.ets", ES2PANDA_STATE_CHECKED, R"(class Foo { Foo = 1; + bar() {} })"); } // NOLINTBEGIN(fuchsia-statically-constructed-objects, cert-err58-cpp) @@ -81,4 +82,16 @@ TEST_F(LspGetNodeTests, GetIdentifier1) ASSERT_EQ(reinterpret_cast(res)->AsIdentifier()->Name(), nodeName.data()); } +TEST_F(LspGetNodeTests, GetMethodDefinition1) +{ + auto ctx = reinterpret_cast(contexts_); + auto ast = ctx->parserProgram->Ast(); + LSPAPI const *lspApi = GetImpl(); + const std::string nodeName = "Foo"; + const std::string methodName = "bar"; + auto res = lspApi->getMethodDefinition(reinterpret_cast(ast), nodeName, methodName); + ASSERT_TRUE(reinterpret_cast(res)->IsMethodDefinition()); + ASSERT_EQ(reinterpret_cast(res)->AsMethodDefinition()->Function()->Id()->Name(), + methodName.data()); +} } // namespace \ No newline at end of file