Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP: implemented the ability to run a test method when clicking the run icon in the gutter editor. #8142

Merged
merged 1 commit into from
Jan 23, 2025

Conversation

troizet
Copy link
Collaborator

@troizet troizet commented Jan 12, 2025

Before:

before

After:

after

Algorithm Description:

Each time a file is opened or edited, it checks for test methods in the file and uses TestMethodController.setTestMethods to create annotations that use the RunDebugTestGutterAction.java action to execute the test method.

Peculiarities of implementation:

  • The implementation was done by analogy with the implementation of annotations for java (ComputeTestMethodsImpl.java).

  • Due to the fact that I have not found an analog of EditorAwareJavaSourceTaskFactory for php, I decided to make a binding to the event of getting the focus of the editor tab and changes in the edited document.
    The php project opening hook seemed to me to be a less appropriate place to register a listener. Since events are handled for each editor tab regardless of the project and file type, I made the ComputeTestMethodAnnotations class a singleton to avoid having to register the listener multiple times if multiple php projects are open.

  • TestMethodController.setTestMethods implements all the work of creating an annotation to run a test method.
    Apparently, this method should update the list of annotations at each call of this method, when the passed collection of methods changes. Вut I didn't manage to achieve correct work in this case. While debugging the ComputeTestMethodsImpl.java class for java, I noticed that the method is called twice: first for an empty collection,
    then for a collection with new computed methods.
    So I made the method call first with the empty collection, to clear the annotation list, then already with the method collection.

  • The algorithm of searching for test methods was made by analogy as in the implementation of action for launching a test method for execution from the context menu of the editor.

@troizet troizet added the PHP [ci] enable extra PHP tests (php/php.editor) label Jan 12, 2025
@troizet troizet requested review from tmysik and junichi11 January 12, 2025 06:07
Copy link
Member

@tmysik tmysik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, see my comments, thank you.

Copy link
Member

@junichi11 junichi11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your work.

@troizet troizet force-pushed the php_add_run_test_method_annotation branch from 96cf166 to 2c2d7bc Compare January 14, 2025 05:53
@troizet
Copy link
Collaborator Author

troizet commented Jan 14, 2025

Made the suggested corrections. Slightly changed the logic of the getTestMethods method.

@junichi11
Copy link
Member

@jlahoda Could you tell us the way to implement this feature other than Java properly if you know? (Is this implementation OK?)

@troizet troizet force-pushed the php_add_run_test_method_annotation branch from 2c2d7bc to 75bd763 Compare January 18, 2025 10:29
@troizet
Copy link
Collaborator Author

troizet commented Jan 18, 2025

Made the change. I hope I have done everything correctly now.

@junichi11
Copy link
Member

I'll test it later. Thanks!

* then already with the method collection.
*/
TestMethodController.setTestMethods(handledDocument, Collections.emptyList());
TestMethodController.setTestMethods(handledDocument, testMethods);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps better:

Suggested change
TestMethodController.setTestMethods(handledDocument, testMethods);
if (!testMethods.isEmpty()) {
TestMethodController.setTestMethods(handledDocument, testMethods);
}

Right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've verified that we cannot get the expected behavior if we don't set an empty list...
(A popup list is not shown after we change a method's name.)

nb-php-gh8142

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation

private static void setTestMethodsImpl(StyledDocument doc, List<TestMethod> methods) {
doc.putProperty(TestMethodAnnotation.DOCUMENT_METHODS_KEY, methods);
Map<TestMethod, TestMethodAnnotation> annotations = (Map<TestMethod, TestMethodAnnotation>) doc.getProperty(TestMethodAnnotation.DOCUMENT_ANNOTATIONS_KEY);
if (annotations == null) {
annotations = new HashMap<>();
doc.putProperty(TestMethodAnnotation.DOCUMENT_ANNOTATIONS_KEY, annotations);
}
Map<Integer, TestMethod> annotationLines = (Map<Integer, TestMethod>) doc.getProperty(TestMethodAnnotation.DOCUMENT_ANNOTATION_LINES_KEY);
if (annotationLines == null) {
annotationLines = new HashMap<>();
doc.putProperty(TestMethodAnnotation.DOCUMENT_ANNOTATION_LINES_KEY, annotationLines);
}
Set<TestMethod> added = new HashSet<>(methods);
Map<TestMethod, TestMethodAnnotation> removed = new HashMap<>(annotations);
removed.keySet().removeAll(added);
added.removeAll(annotations.keySet());
for (TestMethod method : added) {
TestMethodAnnotation a = new TestMethodAnnotation(method);
NbDocument.addAnnotation(doc, method.preferred, 0, a);
annotations.put(method, a);
int line = NbDocument.findLineNumber(doc, method.preferred.getOffset());
annotationLines.put(line, method);
}
for (Entry<TestMethod, TestMethodAnnotation> e : removed.entrySet()) {
NbDocument.removeAnnotation(doc, e.getValue());
annotations.remove(e.getKey());
int line = NbDocument.findLineNumber(doc, e.getKey().preferred.getOffset());
annotationLines.remove(line);
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pinged @jlahoda, maybe he will have time to look into it and verify it.

Copy link
Member

@tmysik tmysik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nearly OK to me. Please, address all the unresolved comment so we can merge it. Thank you for your work.

@tmysik
Copy link
Member

tmysik commented Jan 19, 2025

@troizet I did another round of reviews, please, look at the comments. Once they are resolved, I am Ok with the merge. Let's wait also for @junichi11. Thank you for your work! 👍

@junichi11
Copy link
Member

Please also attach screenshots(before & after).

@troizet troizet force-pushed the php_add_run_test_method_annotation branch from 75bd763 to 6df55e1 Compare January 23, 2025 07:10
@troizet
Copy link
Collaborator Author

troizet commented Jan 23, 2025

Made the change. Also rebased to the latest master.

Copy link
Member

@tmysik tmysik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Please, wait also for approval from @junichi11.

Thank you for your work!

Copy link
Member

@junichi11 junichi11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thank you for your work!

@junichi11
Copy link
Member

BTW, please avoid rebasing until the review is done because it's hard to see the diff (https://github.com/apache/netbeans/compare/75bd763c66853242a7dcbb5a26c4877bcd1dc87d..6df55e14fd086cdef16f870b1e7696e5616fa733)

@junichi11 junichi11 added this to the NB25 milestone Jan 23, 2025
@troizet
Copy link
Collaborator Author

troizet commented Jan 23, 2025

@junichi11 @tmysik Thanks so much for the review!

@troizet troizet force-pushed the php_add_run_test_method_annotation branch from 6df55e1 to 50f1c09 Compare January 23, 2025 16:02
@junichi11
Copy link
Member

Let's merge this after CI passes because the feature freeze is on January 24th.

@junichi11 junichi11 merged commit b1c24b2 into apache:master Jan 23, 2025
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PHP [ci] enable extra PHP tests (php/php.editor)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants