From 89f63f062ada3c345babc20425a4e1c3aeaea3c7 Mon Sep 17 00:00:00 2001 From: Junichi Yamamoto Date: Fri, 19 Jul 2024 14:37:29 +0900 Subject: [PATCH] Add the `Error` class and the `Throwable` interface as completion items for exceptions [GH-7594] - https://github.com/apache/netbeans/issues/7594 - https://www.php.net/manual/en/language.errors.php7.php - https://www.php.net/manual/en/class.error.php - https://www.php.net/manual/en/class.throwable.php - Check the `Error` class and the `Throwable` interface - Add unit tests Example: ```php 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 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, @@ -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()); @@ -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()); @@ -914,6 +918,13 @@ private void autoCompleteExceptions(final PHPCompletionResult completionResult, } } } + final Set 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; @@ -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); diff --git a/php/php.editor/test/unit/data/testfiles/completion/lib/gh7594/gh7594.php b/php/php.editor/test/unit/data/testfiles/completion/lib/gh7594/gh7594.php new file mode 100644 index 000000000000..ca87dd1291b5 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/completion/lib/gh7594/gh7594.php @@ -0,0 +1,55 @@ +