mbien commented on code in PR #8528:
URL: https://github.com/apache/netbeans/pull/8528#discussion_r2106269180


##########
webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/JsonSemanticAnalyzer.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.javascript2.editor;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import org.netbeans.modules.csl.api.ColoringAttributes;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.api.SemanticAnalyzer;
+import org.netbeans.modules.javascript2.doc.api.JsDocumentationSupport;
+import org.netbeans.modules.javascript2.doc.spi.JsComment;
+import org.netbeans.modules.javascript2.editor.parser.JsonParserResult;
+import org.netbeans.modules.javascript2.model.api.JsObject;
+import org.netbeans.modules.javascript2.model.api.JsReference;
+import org.netbeans.modules.javascript2.model.api.Model;
+import org.netbeans.modules.javascript2.model.api.ModelUtils;
+import org.netbeans.modules.parsing.spi.Scheduler;
+import org.netbeans.modules.parsing.spi.SchedulerEvent;
+
+public class JsonSemanticAnalyzer extends SemanticAnalyzer<JsonParserResult> {
+
+    private Map<OffsetRange, Set<ColoringAttributes>> semanticHighlights;
+    private volatile boolean canceled;
+
+    public JsonSemanticAnalyzer() {
+        this.canceled = false;
+        this.semanticHighlights = Collections.emptyMap();
+    }
+
+    @Override
+    @SuppressWarnings("ReturnOfCollectionOrArrayField")
+    public Map<OffsetRange, Set<ColoringAttributes>> getHighlights() {
+        return semanticHighlights;
+    }
+
+    @Override
+    public void run(JsonParserResult result, SchedulerEvent event) {
+        canceled = false;
+        
+        if (canceled) {
+            return;
+        }
+
+        Map<OffsetRange, Set<ColoringAttributes>> highlights = new 
HashMap<>(100);
+        Model model = Model.getModel(result, false);
+        highlights = count(result, model.getGlobalObject(), highlights, new 
HashSet<>());
+
+        assert highlights != null;
+
+        semanticHighlights = highlights;
+    }
+
+    @SuppressWarnings("AssignmentToMethodParameter")
+    private Map<OffsetRange, Set<ColoringAttributes>> count (JsonParserResult 
result, JsObject parent, Map<OffsetRange, Set<ColoringAttributes>> highlights, 
Set<String> processedObjects) {
+        if (ModelUtils.wasProcessed(parent, processedObjects)) {
+            return highlights;
+        }
+        for (Iterator<? extends JsObject> it = 
parent.getProperties().values().iterator(); it.hasNext();) {
+            JsObject object = it.next();
+            if (object.getDeclarationName() != null) {
+                switch (object.getJSKind()) {
+                    case OBJECT_LITERAL:
+                        if(object.getDeclarationName() != null) {
+                            addColoring(result, highlights, 
object.getDeclarationName().getOffsetRange(), ColoringAttributes.FIELD_SET);
+                        }
+                        break;
+                    case PROPERTY:
+                        addColoring(result, highlights, 
object.getDeclarationName().getOffsetRange(), ColoringAttributes.FIELD_SET);
+                        break;
+                }
+            }
+            if (canceled) {
+                highlights = Collections.emptyMap();
+                break;
+            }
+            if (!(object instanceof JsReference && 
ModelUtils.isDescendant(object, ((JsReference)object).getOriginal()))) {
+                highlights = count(result, object, highlights, 
processedObjects);
+            }
+        }
+
+        return highlights;
+    }
+
+    private void addColoring(JsonParserResult result, Map<OffsetRange, 
Set<ColoringAttributes>> highlights, OffsetRange astRange, 
Set<ColoringAttributes> coloring) {
+        int start = 
result.getSnapshot().getOriginalOffset(astRange.getStart());
+        int end = result.getSnapshot().getOriginalOffset(astRange.getEnd());
+        if (start > -1 && end > -1 && start < end && !isInComment(result, 
astRange)) {
+            OffsetRange range = start == astRange.getStart() ? astRange : new 
OffsetRange(start, end);
+            highlights.put(range, coloring);
+        }
+    }
+
+    private boolean isInComment(JsonParserResult result, OffsetRange range) {
+        for (JsComment comment : 
JsDocumentationSupport.getDocumentationHolder(result).getCommentBlocks().values())
 {
+            if (comment.getOffsetRange().containsInclusive(range.getStart())) {

Review Comment:
   interesting, thanks!
   
   performance wise:
   You have more experience there, but if someone would edit huge json files 
which have comments, this could be a performance problem. Since this scales 
currently with `prop_count * comment_count`. Not sure what the keys of the map 
are (Integer for line number?, offset?).
   
   It might be also worth to move 
`JsDocumentationSupport.getDocumentationHolder(result).getCommentBlocks()` out 
of the outer for loop. I had a similar case 
https://github.com/apache/netbeans/pull/8525#discussion_r2103682448 where 
moving it out made the inner loop 10x faster in large files (without hurting 
readability) since it allowed the JVM to inline everything.
   
   something like:
   ```diff
   diff --git 
a/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/JsonSemanticAnalyzer.java
 
b/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/JsonSemanticAnalyzer.java
   index 965f618..d71fde0 100644
   --- 
a/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/JsonSemanticAnalyzer.java
   +++ 
b/webcommon/javascript2.editor/src/org/netbeans/modules/javascript2/editor/JsonSemanticAnalyzer.java
   @@ -75,17 +75,20 @@
            if (ModelUtils.wasProcessed(parent, processedObjects)) {
                return highlights;
            }
   +        
   +        Map<Integer, ? extends JsComment> comments = 
JsDocumentationSupport.getDocumentationHolder(result).getCommentBlocks();
   +        
            for (Iterator<? extends JsObject> it = 
parent.getProperties().values().iterator(); it.hasNext();) {
                JsObject object = it.next();
                if (object.getDeclarationName() != null) {
                    switch (object.getJSKind()) {
                        case OBJECT_LITERAL:
                            if(object.getDeclarationName() != null) {
   -                            addColoring(result, highlights, 
object.getDeclarationName().getOffsetRange(), ColoringAttributes.FIELD_SET);
   +                            addColoring(result, comments, highlights, 
object.getDeclarationName().getOffsetRange(), ColoringAttributes.FIELD_SET);
                            }
                            break;
                        case PROPERTY:
   -                        addColoring(result, highlights, 
object.getDeclarationName().getOffsetRange(), ColoringAttributes.FIELD_SET);
   +                        addColoring(result, comments, highlights, 
object.getDeclarationName().getOffsetRange(), ColoringAttributes.FIELD_SET);
                            break;
                    }
                }
   @@ -101,17 +104,17 @@
            return highlights;
        }
    
   -    private void addColoring(JsonParserResult result, Map<OffsetRange, 
Set<ColoringAttributes>> highlights, OffsetRange astRange, 
Set<ColoringAttributes> coloring) {
   +    private void addColoring(JsonParserResult result, Map<Integer, ? 
extends JsComment> comments, Map<OffsetRange, Set<ColoringAttributes>> 
highlights, OffsetRange astRange, Set<ColoringAttributes> coloring) {
            int start = 
result.getSnapshot().getOriginalOffset(astRange.getStart());
            int end = result.getSnapshot().getOriginalOffset(astRange.getEnd());
   -        if (start > -1 && end > -1 && start < end && !isInComment(result, 
astRange)) {
   +        if (start > -1 && end > -1 && start < end && !isInComment(result, 
comments, astRange)) {
                OffsetRange range = start == astRange.getStart() ? astRange : 
new OffsetRange(start, end);
                highlights.put(range, coloring);
            }
        }
    
   -    private boolean isInComment(JsonParserResult result, OffsetRange range) 
{
   -        for (JsComment comment : 
JsDocumentationSupport.getDocumentationHolder(result).getCommentBlocks().values())
 {
   +    private boolean isInComment(JsonParserResult result, Map<Integer, ? 
extends JsComment> comments, OffsetRange range) {
   +        for (JsComment comment : comments.values()) {
                if 
(comment.getOffsetRange().containsInclusive(range.getStart())) {
                    return true;
                }
   
   ```



-- 
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-unsubscr...@netbeans.apache.org

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


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

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

Reply via email to