troizet commented on code in PR #8142:
URL: https://github.com/apache/netbeans/pull/8142#discussion_r1929484735


##########
php/php.project/src/org/netbeans/modules/php/project/ComputeTestMethodAnnotations.java:
##########
@@ -0,0 +1,189 @@
+/*
+ * 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.project;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.JTextComponent;
+import org.netbeans.api.editor.EditorRegistry;
+import org.netbeans.modules.editor.NbEditorUtilities;
+import org.netbeans.modules.gsf.testrunner.ui.api.TestMethodController;
+import 
org.netbeans.modules.gsf.testrunner.ui.api.TestMethodController.TestMethod;
+import org.netbeans.modules.php.api.editor.EditorSupport;
+import org.netbeans.modules.php.api.editor.PhpClass;
+import org.netbeans.modules.php.api.editor.PhpType;
+import org.netbeans.modules.php.api.phpmodule.PhpModule;
+import org.netbeans.modules.php.api.util.FileUtils;
+import org.netbeans.modules.php.project.ui.actions.support.CommandUtils;
+import org.netbeans.modules.php.project.util.PhpProjectUtils;
+import org.netbeans.modules.php.spi.testing.PhpTestingProvider;
+import org.netbeans.spi.project.SingleMethod;
+import org.openide.filesystems.FileObject;
+import org.openide.util.Lookup;
+import org.openide.util.RequestProcessor;
+
+
+public class ComputeTestMethodAnnotations implements DocumentListener, 
PropertyChangeListener, Runnable {
+
+    private static final Logger LOGGER = 
Logger.getLogger(ComputeTestMethodAnnotations.class.getName());
+
+    private static final RequestProcessor RP = new 
RequestProcessor(ComputeTestMethodAnnotations.class);
+    private final RequestProcessor.Task task = RP.create(this);
+
+    private static final ComputeTestMethodAnnotations INSTANCE = new 
ComputeTestMethodAnnotations();
+
+    private static final AtomicInteger usagesCount = new AtomicInteger(0);
+
+    private volatile Document handledDocument;
+
+    public static ComputeTestMethodAnnotations getInstance() {
+        return INSTANCE;
+    }
+
+    private ComputeTestMethodAnnotations() {
+    }
+
+    public void register() {
+        if (usagesCount.getAndIncrement() == 0) {
+            EditorRegistry.addPropertyChangeListener(this);
+        }
+    }
+
+    public void unregister() {
+        if (usagesCount.decrementAndGet() == 0) {
+            EditorRegistry.removePropertyChangeListener(this);
+        }
+    }
+
+    @Override
+    public void propertyChange(PropertyChangeEvent event) {
+        JTextComponent textComponent = EditorRegistry.lastFocusedComponent();
+        if (textComponent != null) {
+            Document document = textComponent.getDocument();
+            if (document != null) {
+                FileObject fileObject = 
NbEditorUtilities.getFileObject(document);
+                if (fileObject != null) {
+                    if (FileUtils.isPhpFile(fileObject)) {
+                        if 
(event.getPropertyName().equals(EditorRegistry.FOCUS_GAINED_PROPERTY)) {
+                            handleFileChange(document);
+                            
document.addDocumentListener(ComputeTestMethodAnnotations.this);
+                        } else if 
(event.getPropertyName().equals(EditorRegistry.FOCUS_LOST_PROPERTY)) {
+                            document.removeDocumentListener(this);
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    @Override
+    public void insertUpdate(DocumentEvent event) {
+        handleFileChange(event.getDocument());
+    }
+
+    @Override
+    public void removeUpdate(DocumentEvent event) {
+        handleFileChange(event.getDocument());
+    }
+
+    @Override
+    public void changedUpdate(DocumentEvent event) {
+        handleFileChange(event.getDocument());
+    }
+
+    private void handleFileChange(Document doc) {
+        handledDocument = doc;
+        task.schedule(500);
+    }
+
+    @Override
+    public void run() {
+        List<TestMethod> testMethods = new ArrayList<>();
+
+        List<TestMethod> methods = getTestMethods(handledDocument);
+        testMethods.addAll(methods);
+
+        /*
+         * 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.
+         * So I made the method call first with the empty collection, to clear 
the annotation list,
+         * then already with the method collection.
+         */
+        TestMethodController.setTestMethods(handledDocument, 
Collections.emptyList());
+        TestMethodController.setTestMethods(handledDocument, testMethods);

Review Comment:
   If I understand correctly, he has a different account now. @lahodaj 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
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

Reply via email to