This is an automated email from the ASF dual-hosted git repository.
tmysik pushed a commit to branch delivery
in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/delivery by this push:
new 01639b6 Fix code completion for parent's trait #3486
new b645e98 Merge pull request #3521 from junichi11/gh-3486
01639b6 is described below
commit 01639b6f44cb160cb899de31f7b99ed60b30104b
Author: Junichi Yamamoto <[email protected]>
AuthorDate: Thu Jan 27 15:53:44 2022 +0900
Fix code completion for parent's trait #3486
---
.../php/editor/completion/PHPCodeCompletion.java | 19 +++-
.../php/editor/elements/IndexQueryImpl.java | 4 +
.../testfiles/completion/lib/gh3486/gh3486.php | 103 +++++++++++++++++++++
.../lib/gh3486/gh3486.php.testGH3486_01.completion | 16 ++++
.../lib/test207345/bar.php.testUseCase1.completion | 1 -
.../completion/PHPCodeCompletionGH3486Test.java | 49 ++++++++++
6 files changed, 188 insertions(+), 4 deletions(-)
diff --git
a/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletion.java
b/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletion.java
index 309a774..3fb9e88 100644
---
a/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletion.java
+++
b/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletion.java
@@ -1347,6 +1347,7 @@ public class PHPCodeCompletion implements
CodeCompletionHandler2 {
List<String> invalidProposalsForClsMembers =
INVALID_PROPOSALS_FOR_CLS_MEMBERS;
Model model = request.result.getModel();
+ boolean parentContext = false;
boolean selfContext = false;
boolean staticLateBindingContext = false;
boolean specialVariable = false;
@@ -1361,6 +1362,7 @@ public class PHPCodeCompletion implements
CodeCompletionHandler2 {
staticContext = true;
instanceContext = true;
specialVariable = true;
+ parentContext = true;
} else if (TokenUtilities.textEquals(varName, "static")) { //
NOI18N
staticContext = true;
instanceContext = false;
@@ -1381,7 +1383,7 @@ public class PHPCodeCompletion implements
CodeCompletionHandler2 {
return;
}
final ElementFilter staticFlagFilter =
!completeAccessPrefix
- ? new StaticOrInstanceMembersFilter(staticContext,
instanceContext, selfContext, staticLateBindingContext)
+ ? new StaticOrInstanceMembersFilter(staticContext,
instanceContext, selfContext, staticLateBindingContext, parentContext)
: new ElementFilter() { // NETBEANS-1855
@Override
public boolean isAccepted(PhpElement element) {
@@ -1495,6 +1497,7 @@ public class PHPCodeCompletion implements
CodeCompletionHandler2 {
List<String> invalidProposalsForClsMembers =
INVALID_PROPOSALS_FOR_CLS_MEMBERS;
Model model = request.result.getModel();
+ boolean parentContext = false;
boolean selfContext = false;
boolean staticLateBindingContext = false;
boolean specialVariable = false;
@@ -1509,6 +1512,7 @@ public class PHPCodeCompletion implements
CodeCompletionHandler2 {
isStaticContext = true;
isInstanceContext = true;
specialVariable = true;
+ parentContext = true;
} else if (TokenUtilities.textEquals(varName, "static")) { //
NOI18N
isStaticContext = true;
isInstanceContext = false;
@@ -1523,7 +1527,7 @@ public class PHPCodeCompletion implements
CodeCompletionHandler2 {
if (CancelSupport.getDefault().isCancelled()) {
return;
}
- final ElementFilter staticFlagFilter = new
StaticOrInstanceMembersFilter(isStaticContext, isInstanceContext, selfContext,
staticLateBindingContext);
+ final ElementFilter staticFlagFilter = new
StaticOrInstanceMembersFilter(isStaticContext, isInstanceContext, selfContext,
staticLateBindingContext, true);
final ElementFilter methodsFilter = ElementFilter.allOf(
ElementFilter.forKind(PhpElementKind.METHOD),
ElementFilter.forName(NameKind.exact(functionName.text().toString())),
@@ -2321,15 +2325,17 @@ public class PHPCodeCompletion implements
CodeCompletionHandler2 {
private final boolean staticAllowed;
private final boolean nonstaticAllowed;
private final boolean forStaticLateBinding;
+ private final boolean forParentContext;
public StaticOrInstanceMembersFilter(final boolean forStaticContext,
final boolean forInstanceContext,
- final boolean forSelfContext, final boolean
forStaticLateBinding) {
+ final boolean forSelfContext, final boolean
forStaticLateBinding, final boolean forParentContext) {
this.forStaticContext = forStaticContext;
this.forInstanceContext = forInstanceContext;
this.forSelfContext = forSelfContext;
this.forStaticLateBinding = forStaticLateBinding;
this.staticAllowed = OptionsUtils.codeCompletionStaticMethods();
this.nonstaticAllowed =
OptionsUtils.codeCompletionNonStaticMethods();
+ this.forParentContext = forParentContext;
}
@Override
@@ -2348,6 +2354,13 @@ public class PHPCodeCompletion implements
CodeCompletionHandler2 {
private boolean isAcceptedForNotStaticContext(final PhpElement
element) {
final boolean isStatic = element.getPhpModifiers().isStatic();
+ if (forParentContext
+ && !isStatic
+ &&
element.getPhpElementKind().equals(PhpElementKind.FIELD)) {
+ // parent::fieldName is invalid
+ // this is constant
+ return false;
+ }
return !isStatic || (staticAllowed &&
element.getPhpElementKind().equals(PhpElementKind.METHOD));
}
diff --git
a/php/php.editor/src/org/netbeans/modules/php/editor/elements/IndexQueryImpl.java
b/php/php.editor/src/org/netbeans/modules/php/editor/elements/IndexQueryImpl.java
index 0936f89..14d42da 100644
---
a/php/php.editor/src/org/netbeans/modules/php/editor/elements/IndexQueryImpl.java
+++
b/php/php.editor/src/org/netbeans/modules/php/editor/elements/IndexQueryImpl.java
@@ -940,6 +940,10 @@ public final class IndexQueryImpl implements
ElementQuery.Index {
final Set<TypeElement> inheritedTypes =
elementQueryIndex.getInheritedTypes(enclosingType);
for (final TypeElement nextType : inheritedTypes) {
filters.add(ElementFilter.forMembersOfType(nextType));
+ // GH #3486
+ for (TypeElement trait : getAllUsedTraits(nextType)) {
+ filters.add(ElementFilter.forMembersOfType(trait));
+ }
}
}
return filters.toArray(new ElementFilter[filters.size()]);
diff --git
a/php/php.editor/test/unit/data/testfiles/completion/lib/gh3486/gh3486.php
b/php/php.editor/test/unit/data/testfiles/completion/lib/gh3486/gh3486.php
new file mode 100644
index 0000000..ea3d6e4
--- /dev/null
+++ b/php/php.editor/test/unit/data/testfiles/completion/lib/gh3486/gh3486.php
@@ -0,0 +1,103 @@
+<?php
+/*
+ * 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.
+ */
+
+trait TestTrait {
+
+ public int $publicTraitField = 1;
+ private int $privateTraitField = 1;
+ protected int $protectedTraitField = 1;
+ public static int $publicStaticTraitField = 1;
+ private static int $privateStaticTraitField = 1;
+ protected static int $protectedStaticTraitField = 1;
+
+ public function publicTraitMethod() {
+ echo "publicTraitMethod" . PHP_EOL;
+ }
+
+ private function privateTraitMethod() {
+ echo "privateTraitMethod" . PHP_EOL;
+ }
+
+ protected function protectedTraitMethod() {
+ echo "protectedTraitMethod" . PHP_EOL;
+ }
+
+ public static function publicStaticTraitMethod() {
+ echo "publicStaticTraitMethod" . PHP_EOL;
+ }
+
+ private static function privateStaticTraitMethod() {
+ echo "privateStaticTraitMethod" . PHP_EOL;
+ }
+
+ protected static function protectedStaticTraitMethod() {
+ echo "protectedStaticTraitMethod" . PHP_EOL;
+ }
+
+}
+
+class A {
+
+ use TestTrait;
+
+ public int $publicClassField = 1;
+ private int $privateClassField = 1;
+ protected int $protectedClassField = 1;
+ public static int $publicStaticClassField = 1;
+ private static int $privateStaticClassField = 1;
+ protected static int $protectedStaticClassField = 1;
+
+ public function publicClassMethod() {
+ echo "publicClassMethod" . PHP_EOL;
+ }
+
+ private function privateClassMethod() {
+ echo "privateClassMethod" . PHP_EOL;
+ }
+
+ protected function protectedClassMethod() {
+ echo "protectedClassMethod" . PHP_EOL;
+ }
+
+ public static function publicStaticClassMethod() {
+ echo "publicStaticClassMethod" . PHP_EOL;
+ }
+
+ private static function privateStaticClassMethod() {
+ echo "privateStaticClassMethod" . PHP_EOL;
+ }
+
+ protected static function protectedStaticClassMethod() {
+ echo "protectedStaticClassMethod" . PHP_EOL;
+ }
+
+ protected function protectedTraitMethod() {
+ echo "protectedTraitMethod" . PHP_EOL;
+ }
+
+}
+
+class B extends A {
+
+ protected function protectedTraitMethod() {
+ parent::protectedTraitMethod();
+ }
+
+}
diff --git
a/php/php.editor/test/unit/data/testfiles/completion/lib/gh3486/gh3486.php.testGH3486_01.completion
b/php/php.editor/test/unit/data/testfiles/completion/lib/gh3486/gh3486.php.testGH3486_01.completion
new file mode 100644
index 0000000..367cf52
--- /dev/null
+++
b/php/php.editor/test/unit/data/testfiles/completion/lib/gh3486/gh3486.php.testGH3486_01.completion
@@ -0,0 +1,16 @@
+Code completion result for source line:
+parent::|protectedTraitMethod();
+(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
+METHOD protectedClassMethod() [PROTECTE A
+METHOD protectedStaticClassMethod() [PROTECTE A
+METHOD protectedStaticTraitMethod() [PROTECTE TestTrait
+METHOD protectedTraitMethod() [PROTECTE A
+METHOD publicClassMethod() [PUBLIC] A
+METHOD publicStaticClassMethod() [STATIC] A
+METHOD publicStaticTraitMethod() [STATIC] TestTrait
+METHOD publicTraitMethod() [PUBLIC] TestTrait
+VARIABLE int $protectedStaticClassField [PROTECTE A
+VARIABLE int $protectedStaticTraitField [PROTECTE TestTrait
+VARIABLE int $publicStaticClassField [STATIC] A
+VARIABLE int $publicStaticTraitField [STATIC] TestTrait
+CONSTANT class \A [PUBLIC] Magic Constant
diff --git
a/php/php.editor/test/unit/data/testfiles/completion/lib/test207345/bar.php.testUseCase1.completion
b/php/php.editor/test/unit/data/testfiles/completion/lib/test207345/bar.php.testUseCase1.completion
index 5adc7ba..0b5efc5 100644
---
a/php/php.editor/test/unit/data/testfiles/completion/lib/test207345/bar.php.testUseCase1.completion
+++
b/php/php.editor/test/unit/data/testfiles/completion/lib/test207345/bar.php.testUseCase1.completion
@@ -4,5 +4,4 @@ parent::|
------------------------------------
METHOD __construct() [PUBLIC] \Testing\Ns\Blah
METHOD baz($param) [PUBLIC] \Testing\Ns\Blah
-VARIABLE ? field [PUBLIC] \Testing\Ns\Blah
CONSTANT class \Testing\Ns\Blah [PUBLIC] Magic Constant
diff --git
a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionGH3486Test.java
b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionGH3486Test.java
new file mode 100644
index 0000000..3084aa9
--- /dev/null
+++
b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionGH3486Test.java
@@ -0,0 +1,49 @@
+/*
+ * 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 java.util.Collections;
+import java.util.Map;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.modules.php.project.api.PhpSourcePath;
+import org.netbeans.spi.java.classpath.support.ClassPathSupport;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+
+public class PHPCodeCompletionGH3486Test extends PHPCodeCompletionTestBase {
+
+ public PHPCodeCompletionGH3486Test(String testName) {
+ super(testName);
+ }
+
+ public void testGH3486_01() throws Exception {
+ checkCompletion("testfiles/completion/lib/gh3486/gh3486.php", "
parent::^protectedTraitMethod();", false);
+ }
+
+ @Override
+ protected Map<String, ClassPath> createClassPathsForTest() {
+ return Collections.singletonMap(
+ PhpSourcePath.SOURCE_CP,
+ ClassPathSupport.createClassPath(new FileObject[] {
+ FileUtil.toFileObject(new File(getDataDir(),
"/testfiles/completion/lib/gh3486"))
+ })
+ );
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists