From e6cbcb2d2c5d9abe0da40c8d48d1c58aac628b96 Mon Sep 17 00:00:00 2001 From: Junichi Yamamoto Date: Sun, 11 Feb 2024 13:02:37 +0900 Subject: [PATCH] Fix incorrect code completion for use function #7041 - https://github.com/apache/netbeans/issues/7041 - Don't add parameter parts if the context is use functions - Add unit tests --- .../editor/completion/PHPCompletionItem.java | 10 ++++ .../testGroupUseFunction01.php | 29 +++++++++++ ...n01.php.testGroupUseFunction01.cccustomtpl | 5 ++ .../testUseFunction01/testUseFunction01.php | 28 +++++++++++ ...nction01.php.testUseFunction01.cccustomtpl | 5 ++ .../completion/PHP71CodeCompletionTest.java | 9 ---- .../completion/PHP72CodeCompletionTest.java | 9 ---- .../completion/PHP74CodeCompletionTest.java | 9 ---- .../completion/PHP80CodeCompletionTest.java | 9 ---- .../completion/PHP81CodeCompletionTest.java | 9 ---- .../completion/PHP82CodeCompletionTest.java | 9 ---- .../completion/PHP83CodeCompletionTest.java | 9 ---- .../PHPCodeCompletionAutoImportTest.java | 9 ---- .../PHPCodeCompletionGH7041Test.java | 48 +++++++++++++++++++ .../PHPCodeCompletionMagicMethodTest.java | 9 ---- .../completion/PHPCodeCompletionTestBase.java | 9 ++++ 16 files changed, 134 insertions(+), 81 deletions(-) create mode 100644 php/php.editor/test/unit/data/testfiles/completion/lib/gh7041/testGroupUseFunction01/testGroupUseFunction01.php create mode 100644 php/php.editor/test/unit/data/testfiles/completion/lib/gh7041/testGroupUseFunction01/testGroupUseFunction01.php.testGroupUseFunction01.cccustomtpl create mode 100644 php/php.editor/test/unit/data/testfiles/completion/lib/gh7041/testUseFunction01/testUseFunction01.php create mode 100644 php/php.editor/test/unit/data/testfiles/completion/lib/gh7041/testUseFunction01/testUseFunction01.php.testUseFunction01.cccustomtpl create mode 100644 php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionGH7041Test.java diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCompletionItem.java b/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCompletionItem.java index 5772b0112bbe..2ec01950f2ce 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCompletionItem.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCompletionItem.java @@ -561,6 +561,11 @@ public static boolean insertOnlyMethodsName(CompletionRequest request) { if (request.insertOnlyMethodsName != null) { return request.insertOnlyMethodsName; } + if (isUseFunctionContext(request.context)) { + // GH-7041 + // e.g. use function Vendor\Package\myFunction; + return true; + } boolean result = false; TokenHierarchy tokenHierarchy = request.result.getSnapshot().getTokenHierarchy(); TokenSequence tokenSequence = (TokenSequence) tokenHierarchy.tokenSequence(); @@ -595,6 +600,11 @@ public static boolean insertOnlyMethodsName(CompletionRequest request) { return result; } + private static boolean isUseFunctionContext(CompletionContext context) { + return context == CompletionContext.USE_FUNCTION_KEYWORD + || context == CompletionContext.GROUP_USE_FUNCTION_KEYWORD; + } + private boolean isNewClassContext(CompletionContext context) { return context.equals(CompletionContext.NEW_CLASS) || context.equals(CompletionContext.THROW_NEW) diff --git a/php/php.editor/test/unit/data/testfiles/completion/lib/gh7041/testGroupUseFunction01/testGroupUseFunction01.php b/php/php.editor/test/unit/data/testfiles/completion/lib/gh7041/testGroupUseFunction01/testGroupUseFunction01.php new file mode 100644 index 000000000000..f327bf8afbd7 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/completion/lib/gh7041/testGroupUseFunction01/testGroupUseFunction01.php @@ -0,0 +1,29 @@ + createClassPathsForTest() { ); } - private String getTestDirName() { - String name = getName(); - int indexOf = name.indexOf("_"); - if (indexOf != -1) { - name = name.substring(0, indexOf); - } - return name; - } - private String getTestPath(String fileName) { return String.format("testfiles/completion/lib/php71/%s/%s.php", getTestDirName(), fileName); } diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP72CodeCompletionTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP72CodeCompletionTest.java index f3d9c62a16e0..9e863c02d572 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP72CodeCompletionTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP72CodeCompletionTest.java @@ -44,15 +44,6 @@ protected Map createClassPathsForTest() { ); } - private String getTestDirName() { - String name = getName(); - int indexOf = name.indexOf("_"); - if (indexOf != -1) { - name = name.substring(0, indexOf); - } - return name; - } - private String getTestPath(String fileName) { return String.format("testfiles/completion/lib/php72/%s/%s.php", getTestDirName(), fileName); } diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP74CodeCompletionTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP74CodeCompletionTest.java index 438e54ebd9c2..0e40aba71106 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP74CodeCompletionTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP74CodeCompletionTest.java @@ -51,15 +51,6 @@ protected Map createClassPathsForTest() { ); } - private String getTestDirName() { - String name = getName(); - int indexOf = name.indexOf("_"); - if (indexOf != -1) { - name = name.substring(0, indexOf); - } - return name; - } - private String getTestPath(String fileName) { return String.format("testfiles/completion/lib/php74/%s/%s.php", getTestDirName(), fileName); } diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP80CodeCompletionTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP80CodeCompletionTest.java index 76ea5d4b7e09..a7cd6632d849 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP80CodeCompletionTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP80CodeCompletionTest.java @@ -40,15 +40,6 @@ protected FileObject[] createSourceClassPathsForTest() { return new FileObject[]{FileUtil.toFileObject(new File(getDataDir(), "/testfiles/completion/lib/php80/" + getTestDirName()))}; } - private String getTestDirName() { - String name = getName(); - int indexOf = name.indexOf("_"); - if (indexOf != -1) { - name = name.substring(0, indexOf); - } - return name; - } - private String getTestPath() { return String.format("testfiles/completion/lib/php80/%s/%s.php", getTestDirName(), getTestDirName()); } diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP81CodeCompletionTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP81CodeCompletionTest.java index f3014c2c4aa9..c69c0d874687 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP81CodeCompletionTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP81CodeCompletionTest.java @@ -44,15 +44,6 @@ protected Map createClassPathsForTest() { ); } - private String getTestDirName() { - String name = getName(); - int indexOf = name.indexOf("_"); - if (indexOf != -1) { - name = name.substring(0, indexOf); - } - return name; - } - private String getTestPath(String fileName) { return String.format("testfiles/completion/lib/php81/%s/%s.php", getTestDirName(), fileName); } diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP82CodeCompletionTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP82CodeCompletionTest.java index d3a35db2e7e0..7e39aae737e3 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP82CodeCompletionTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP82CodeCompletionTest.java @@ -44,15 +44,6 @@ protected Map createClassPathsForTest() { ); } - private String getTestDirName() { - String name = getName(); - int indexOf = name.indexOf("_"); - if (indexOf != -1) { - name = name.substring(0, indexOf); - } - return name; - } - private String getTestPath(String fileName) { return String.format("testfiles/completion/lib/php82/%s/%s.php", getTestDirName(), fileName); } diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP83CodeCompletionTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP83CodeCompletionTest.java index 3d851482be5c..c50329786616 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP83CodeCompletionTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHP83CodeCompletionTest.java @@ -44,15 +44,6 @@ protected Map createClassPathsForTest() { ); } - private String getTestDirName() { - String name = getName(); - int indexOf = name.indexOf("_"); - if (indexOf != -1) { - name = name.substring(0, indexOf); - } - return name; - } - private String getTestPath(String fileName) { return String.format("testfiles/completion/lib/php83/%s/%s.php", getTestDirName(), fileName); } diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionAutoImportTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionAutoImportTest.java index 58bab4ef101d..94be5257fa12 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionAutoImportTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionAutoImportTest.java @@ -34,15 +34,6 @@ protected FileObject[] createSourceClassPathsForTest() { return new FileObject[]{FileUtil.toFileObject(new File(getDataDir(), "/testfiles/completion/lib/autoImport/" + getTestDirName()))}; } - private String getTestDirName() { - String name = getName(); - int indexOf = name.indexOf("_"); - if (indexOf != -1) { - name = name.substring(0, indexOf); - } - return name; - } - private String getTestPath() { return String.format("testfiles/completion/lib/autoImport/%s/%s.php", getTestDirName(), getTestDirName()); } diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionGH7041Test.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionGH7041Test.java new file mode 100644 index 000000000000..587acc542afe --- /dev/null +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionGH7041Test.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.netbeans.modules.php.editor.completion; + +import java.io.File; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; + +public class PHPCodeCompletionGH7041Test extends PHPCodeCompletionTestBase { + + public PHPCodeCompletionGH7041Test(String testName) { + super(testName); + } + + @Override + protected FileObject[] createSourceClassPathsForTest() { + return new FileObject[]{FileUtil.toFileObject(new File(getDataDir(), "/testfiles/completion/lib/gh7041/" + getTestDirName()))}; + } + + private String getTestPath() { + return String.format("testfiles/completion/lib/gh7041/%s/%s.php", getTestDirName(), getTestDirName()); + } + + public void testUseFunction01() throws Exception { + checkCompletionCustomTemplateResult(getTestPath(), "use function GH7041\\^", null, true); + } + + public void testGroupUseFunction01() throws Exception { + checkCompletionCustomTemplateResult(getTestPath(), " ^// test", null, true); + } + +} diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionMagicMethodTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionMagicMethodTest.java index bece42d07b1d..ad7045653fe7 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionMagicMethodTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionMagicMethodTest.java @@ -104,15 +104,6 @@ protected Map createClassPathsForTest() { ); } - private String getTestDirName() { - String name = getName(); - int indexOf = name.indexOf("_"); - if (indexOf != -1) { - name = name.substring(0, indexOf); - } - return name; - } - private String getTestPath(String fileName) { return String.format("testfiles/completion/lib/magicMethods/%s/%s.php", getTestDirName(), fileName); } diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionTestBase.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionTestBase.java index afa24d451ac2..72be89893d90 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionTestBase.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionTestBase.java @@ -81,6 +81,15 @@ protected FileObject[] createSourceClassPathsForTest() { return null; } + protected String getTestDirName() { + String name = getName(); + int indexOf = name.indexOf("_"); + if (indexOf != -1) { + name = name.substring(0, indexOf); + } + return name; + } + protected void checkCompletionCustomTemplateResult(final String file, final String caretLine, CompletionProposalFilter filter, boolean checkAllItems) throws Exception { checkCompletionCustomTemplateResult(file, caretLine, filter, checkAllItems, null); }