Skip to content

Commit

Permalink
Merge pull request apache#7599 from junichi11/php-gh-7594-cc-throwabl…
Browse files Browse the repository at this point in the history
…e-error

Add the `Error` class and the `Throwable` interface as completion items for exceptions [apacheGH-7594]
  • Loading branch information
junichi11 authored Jul 20, 2024
2 parents cf4a54d + 89f63f0 commit b1a4514
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ private static enum UseType {
private static final List<String> INHERITANCE_KEYWORDS =
Arrays.asList(new String[]{"extends", "implements"}); //NOI18N
private static final String EXCEPTION_CLASS_NAME = "\\Exception"; // NOI18N
private static final String ERROR_CLASS_NAME = "\\Error"; // NOI18N
private static final String THROWABLE_INTERFACE_NAME = "\\Throwable"; // NOI18N
private static final List<PHPTokenId> VALID_UNION_TYPE_TOKENS = Arrays.asList(
PHPTokenId.WHITESPACE, PHPTokenId.PHP_STRING, PHPTokenId.PHP_NS_SEPARATOR,
PHPTokenId.PHP_TYPE_BOOL, PHPTokenId.PHP_TYPE_FLOAT, PHPTokenId.PHP_TYPE_INT, PHPTokenId.PHP_TYPE_STRING, PHPTokenId.PHP_TYPE_VOID,
Expand Down Expand Up @@ -891,7 +893,8 @@ private void autoCompleteExceptions(final PHPCompletionResult completionResult,
if (CancelSupport.getDefault().isCancelled()) {
return;
}
if (isExceptionClass(classElement)) {
if (isExceptionClass(classElement)
|| isErrorClass(classElement)) {
completionResult.add(new PHPCompletionItem.ClassItem(classElement, request, false, null));
if (withConstructors) {
constructorClassNames.add(classElement.getFullyQualifiedName());
Expand All @@ -904,7 +907,8 @@ private void autoCompleteExceptions(final PHPCompletionResult completionResult,
if (CancelSupport.getDefault().isCancelled()) {
return;
}
if (isExceptionClass(inheritedClass)) {
if (isExceptionClass(inheritedClass)
|| isErrorClass(inheritedClass)) {
completionResult.add(new PHPCompletionItem.ClassItem(classElement, request, false, null));
if (withConstructors) {
constructorClassNames.add(classElement.getFullyQualifiedName());
Expand All @@ -914,6 +918,13 @@ private void autoCompleteExceptions(final PHPCompletionResult completionResult,
}
}
}
final Set<InterfaceElement> interfaces = request.index.getInterfaces(nameQuery);
for (InterfaceElement interfaceElement : interfaces) {
if (isThrowableInterface(interfaceElement)) {
completionResult.add(new PHPCompletionItem.InterfaceItem(interfaceElement, request, false));
break;
}
}
for (QualifiedName qualifiedName : constructorClassNames) {
if (CancelSupport.getDefault().isCancelled()) {
return;
Expand All @@ -926,6 +937,14 @@ private boolean isExceptionClass(ClassElement classElement) {
return classElement.getFullyQualifiedName().toString().equals(EXCEPTION_CLASS_NAME);
}

private boolean isErrorClass(ClassElement classElement) {
return classElement.getFullyQualifiedName().toString().equals(ERROR_CLASS_NAME);
}

private boolean isThrowableInterface(InterfaceElement interfaceElement) {
return interfaceElement.getFullyQualifiedName().toString().equals(THROWABLE_INTERFACE_NAME);
}

private void autoCompleteClassNames(final PHPCompletionResult completionResult,
PHPCompletionItem.CompletionRequest request, boolean endWithDoubleColon) {
autoCompleteClassNames(completionResult, request, endWithDoubleColon, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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.
*/

try {

} catch(Thr) {

}

try {

} catch(\Thr) {

}

try {

} catch(Err) {

}

try {

} catch(\Err) {

}

try {

} catch(TypeE) {

}

try {

} catch(\TypeE) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
} catch(Thr|) {
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS Throwable [PUBLIC] stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
} catch(\Thr|) {
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS Throwable [PUBLIC] stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
} catch(Err|) {
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS Error [PUBLIC] stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
} catch(\Err|) {
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS Error [PUBLIC] stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
} catch(TypeE|) {
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS TypeError [PUBLIC] stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Code completion result for source line:
} catch(\TypeE|) {
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
CLASS TypeError [PUBLIC] stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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.
*/

interface Throwable extends Stringable {}

class Error implements \Throwable {}

class TypeError extends \Error {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 PHPCodeCompletionGH7594Test extends PHPCodeCompletionTestBase {

public PHPCodeCompletionGH7594Test(String testName) {
super(testName);
}

@Override
protected FileObject[] createSourceClassPathsForTest() {
return new FileObject[]{FileUtil.toFileObject(new File(getDataDir(), "/testfiles/completion/lib/gh7594/"))};
}

public void testGH7594_01a() throws Exception {
checkCompletion("testfiles/completion/lib/gh7594/gh7594.php", "} catch(Thr^) {", false);
}

public void testGH7594_01b() throws Exception {
checkCompletion("testfiles/completion/lib/gh7594/gh7594.php", "} catch(\\Thr^) {", false);
}

public void testGH7594_02a() throws Exception {
checkCompletion("testfiles/completion/lib/gh7594/gh7594.php", "} catch(Err^) {", false);
}

public void testGH7594_02b() throws Exception {
checkCompletion("testfiles/completion/lib/gh7594/gh7594.php", "} catch(\\Err^) {", false);
}

public void testGH7594_03a() throws Exception {
checkCompletion("testfiles/completion/lib/gh7594/gh7594.php", "} catch(TypeE^) {", false);
}

public void testGH7594_03b() throws Exception {
checkCompletion("testfiles/completion/lib/gh7594/gh7594.php", "} catch(\\TypeE^) {", false);
}
}

0 comments on commit b1a4514

Please sign in to comment.