From afe2061451fd5286126dc48a02f24ecd5a959280 Mon Sep 17 00:00:00 2001 From: yaohaosen Date: Thu, 28 Aug 2025 10:46:50 +0800 Subject: [PATCH] [LSP] Fix Quick Info For struct with @component Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICV5PJ Signed-off-by: yaohaosen --- ets2panda/bindings/test/cases.ts | 3 ++- .../test/expected/getQuickInfoAtPosition.json | 23 +++++++++++++++++++ .../getQuickInfoAtPosition5.ets | 22 ++++++++++++++++++ ets2panda/lsp/include/internal_api.h | 10 ++++++++ ets2panda/lsp/src/quick_info.cpp | 2 +- ets2panda/lsp/src/rename.cpp | 2 +- .../test/unit/lsp/quick_info_api_test.cpp | 12 +++++++++- 7 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 ets2panda/bindings/test/testcases/getQuickInfoAtPosition/getQuickInfoAtPosition5.ets diff --git a/ets2panda/bindings/test/cases.ts b/ets2panda/bindings/test/cases.ts index 5f3273d7ea..88a60b9926 100644 --- a/ets2panda/bindings/test/cases.ts +++ b/ets2panda/bindings/test/cases.ts @@ -231,7 +231,8 @@ export const basicCases: TestCases = { '1': [resolveTestPath('test/testcases/getQuickInfoAtPosition/getQuickInfoAtPosition1.ets'), 626], '2': [resolveTestPath('test/testcases/getQuickInfoAtPosition/getQuickInfoAtPosition2.ets'), 618], '3': [resolveTestPath('test/testcases/getQuickInfoAtPosition/getQuickInfoAtPosition3.ets'), 663], - '4': [resolveTestPath('test/testcases/getQuickInfoAtPosition/getQuickInfoAtPosition4.ets'), 697] + '4': [resolveTestPath('test/testcases/getQuickInfoAtPosition/getQuickInfoAtPosition4.ets'), 697], + '5': [resolveTestPath('test/testcases/getQuickInfoAtPosition/getQuickInfoAtPosition5.ets'), 701] }, getDocumentHighlights: { expectedFilePath: resolveTestPath('test/expected/getDocumentHighlights.json'), diff --git a/ets2panda/bindings/test/expected/getQuickInfoAtPosition.json b/ets2panda/bindings/test/expected/getQuickInfoAtPosition.json index ab3f7ca7b7..b942b7b1ae 100644 --- a/ets2panda/bindings/test/expected/getQuickInfoAtPosition.json +++ b/ets2panda/bindings/test/expected/getQuickInfoAtPosition.json @@ -162,5 +162,28 @@ "kind": "returnType" } ] + }, + "5": { + "kind": "struct", + "kindModifier": "final", + "textSpan": { + "start": 699, + "length": 5 + }, + "fileName": "getQuickInfoAtPosition5.ets", + "displayParts": [ + { + "text": "struct", + "kind": "keyword" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "Index", + "kind": "structName" + } + ] } } diff --git a/ets2panda/bindings/test/testcases/getQuickInfoAtPosition/getQuickInfoAtPosition5.ets b/ets2panda/bindings/test/testcases/getQuickInfoAtPosition/getQuickInfoAtPosition5.ets new file mode 100644 index 0000000000..c701f3ff4a --- /dev/null +++ b/ets2panda/bindings/test/testcases/getQuickInfoAtPosition/getQuickInfoAtPosition5.ets @@ -0,0 +1,22 @@ +'use static' +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Entry, Component } from '@ohos.arkui.component'; + +@Component +struct Index { + build() {} +} diff --git a/ets2panda/lsp/include/internal_api.h b/ets2panda/lsp/include/internal_api.h index 84eebcc952..6777e3729e 100644 --- a/ets2panda/lsp/include/internal_api.h +++ b/ets2panda/lsp/include/internal_api.h @@ -94,6 +94,16 @@ public: return impl_->LogDiagnostic(context, ekind, args, argc, pos); } + void ClassDefinitionSetFromStructModifier(es2panda_Context *context, es2panda_AstNode *classInstance) + { + return impl_->ClassDefinitionSetFromStructModifier(context, classInstance); + } + + bool ClassDefinitionIsFromStructConst(es2panda_Context *context, es2panda_AstNode *classInstance) + { + return impl_->ClassDefinitionIsFromStructConst(context, classInstance); + } + NO_COPY_SEMANTIC(Initializer); NO_MOVE_SEMANTIC(Initializer); diff --git a/ets2panda/lsp/src/quick_info.cpp b/ets2panda/lsp/src/quick_info.cpp index 9b4c6bdf56..c7ac6bf677 100644 --- a/ets2panda/lsp/src/quick_info.cpp +++ b/ets2panda/lsp/src/quick_info.cpp @@ -643,7 +643,7 @@ std::vector CreateDisplayForClass(ir::AstNode *node) displayParts.emplace_back(CreateKeyword("namespace")); displayParts.emplace_back(CreateSpace()); displayParts.emplace_back(CreateNamespace(GetNameFromClassDefinition(node))); - } else if (node->Parent()->IsETSStructDeclaration()) { + } else if (node->AsClassDefinition()->IsFromStruct() || node->Parent()->IsETSStructDeclaration()) { displayParts.emplace_back(CreateKeyword("struct")); displayParts.emplace_back(CreateSpace()); displayParts.emplace_back(SignatureCreateStructName(GetNameFromClassDefinition(node))); diff --git a/ets2panda/lsp/src/rename.cpp b/ets2panda/lsp/src/rename.cpp index d5bab32a0b..6ae4022b8c 100644 --- a/ets2panda/lsp/src/rename.cpp +++ b/ets2panda/lsp/src/rename.cpp @@ -354,7 +354,7 @@ std::string GetKindOfClassDefinition(ir::AstNode *node) if (node->AsClassDefinition()->IsNamespaceTransformed()) { return "namespace"; } - if (node->Parent()->IsETSStructDeclaration()) { + if (node->AsClassDefinition()->IsFromStruct() || node->Parent()->IsETSStructDeclaration()) { return "struct"; } return "class"; diff --git a/ets2panda/test/unit/lsp/quick_info_api_test.cpp b/ets2panda/test/unit/lsp/quick_info_api_test.cpp index 2a71e8248f..67ebc36f98 100644 --- a/ets2panda/test/unit/lsp/quick_info_api_test.cpp +++ b/ets2panda/test/unit/lsp/quick_info_api_test.cpp @@ -463,11 +463,21 @@ TEST_F(LspQuickInfoTests, GetQuickInfoAtPositionClass) auto ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED); LSPAPI const *lspApi = GetImpl(); - size_t const offset1 = 8; // class A + size_t const offset1 = 8; // struct A auto quickInfo1 = lspApi->getQuickInfoAtPosition("GetQuickInfoAtPositionClass.ets", ctx, offset1); auto expectedQuickInfo1 = ExpectResultClass1(); AssertQuickInfo(expectedQuickInfo1, quickInfo1); + auto context = reinterpret_cast(ctx); + const ark::es2panda::ir::AstNode *ast = context->parserProgram->Ast(); + const auto *structDefNode = ast->FindChild( + [](const auto *node) { return node->IsClassDefinition() && node->Parent()->IsETSStructDeclaration(); }); + initializer.ClassDefinitionSetFromStructModifier(ctx, (es2panda_AstNode *)structDefNode); + auto isFromStruct = initializer.ClassDefinitionIsFromStructConst(ctx, (es2panda_AstNode *)structDefNode); + ASSERT_EQ(isFromStruct, true); + auto quickInfo3 = lspApi->getQuickInfoAtPosition("GetQuickInfoAtPositionClass.ets", ctx, offset1); + AssertQuickInfo(expectedQuickInfo1, quickInfo3); + size_t const offset2 = 42; // namespace S auto quickInfo2 = lspApi->getQuickInfoAtPosition("GetQuickInfoAtPositionClass.ets", ctx, offset2); auto expectedQuickInfo2 = ExpectResultClass2(); -- Gitee