You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2021/10/02 12:27:54 UTC

[GitHub] [netbeans] matthiasblaesing commented on a change in pull request #3171: [jackpot] hint reload on save + run action registration + small cleanup

matthiasblaesing commented on a change in pull request #3171:
URL: https://github.com/apache/netbeans/pull/3171#discussion_r720670604



##########
File path: java/java.hints.declarative/src/org/netbeans/modules/java/hints/declarative/idebinding/UpdateGlobalHintsOnSaveTask.java
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.java.hints.declarative.idebinding;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import javax.swing.SwingUtilities;
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
+import org.netbeans.modules.java.hints.declarative.DeclarativeHintTokenId;
+import org.netbeans.modules.java.hints.spiimpl.RulesManager;
+import org.netbeans.spi.editor.document.OnSaveTask;
+import org.netbeans.spi.editor.document.OnSaveTask.Factory;
+
+/**
+ * Reloads the global hint if the file changed after a save action.
+ * @author mbien
+ */
+@MimeRegistration(mimeType=DeclarativeHintTokenId.MIME_TYPE, service=OnSaveTask.Factory.class, position = 3000)
+public final class UpdateGlobalHintsOnSaveTask implements Factory {
+
+//    private static final Executor delay = CompletableFuture.delayedExecutor(200, TimeUnit.MILLISECONDS); // 9+
+    private static final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(0);
+
+    @Override
+    public OnSaveTask createTask(OnSaveTask.Context context) {
+
+        return new OnSaveTask() {
+
+            @Override
+            public void performTask() {
+                // stolen from org.netbeans.modules.java.hints.spiimpl.options.HintsPanel#saveButtonActionPerformed
+                // must be on EDT, reloads global hints from hint files
+                // todo: this reloads *all* global hints, which isn't optimal
+                SwingUtilities.invokeLater(() -> RulesManager.getInstance().reload());
+            }
+
+            @Override
+            public void runLocked(Runnable task) {
+                // the file isn't actually saved yet, so we delay the task a bit
+                // it seems to be working fine even without a delay due to the timing of the EDT
+//                CompletableFuture.runAsync(task, delay); // 9+
+                scheduler.schedule(task, 200, TimeUnit.MILLISECONDS);

Review comment:
       If a `RequestProcessor` is used, `RequestProcessor#post` can be used, which can then be used to cancel the task. Please Remove the commented code.

##########
File path: java/java.hints.declarative/src/org/netbeans/modules/java/hints/declarative/idebinding/UpdateGlobalHintsOnSaveTask.java
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.java.hints.declarative.idebinding;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import javax.swing.SwingUtilities;
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
+import org.netbeans.modules.java.hints.declarative.DeclarativeHintTokenId;
+import org.netbeans.modules.java.hints.spiimpl.RulesManager;
+import org.netbeans.spi.editor.document.OnSaveTask;
+import org.netbeans.spi.editor.document.OnSaveTask.Factory;
+
+/**
+ * Reloads the global hint if the file changed after a save action.
+ * @author mbien
+ */
+@MimeRegistration(mimeType=DeclarativeHintTokenId.MIME_TYPE, service=OnSaveTask.Factory.class, position = 3000)
+public final class UpdateGlobalHintsOnSaveTask implements Factory {
+
+//    private static final Executor delay = CompletableFuture.delayedExecutor(200, TimeUnit.MILLISECONDS); // 9+
+    private static final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(0);

Review comment:
       I would use a `org.openide.util.RequestProcessor` here - that is the canonical way to handle delayed tasks in the IDE from my POV. (the commented code should be removed)




-- 
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: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists