mbien commented on code in PR #8528: URL: https://github.com/apache/netbeans/pull/8528#discussion_r2105348287
########## 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()))) { Review Comment: if you like the new `instanceof` feature the cast can be eliminated which makes it a little bit shorter ```java if (!(object instanceof JsReference jsr && ModelUtils.isDescendant(jsr, jsr.getOriginal()))) { ``` ########## 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: question: how does this work? JS comment blocks in a json? ########## 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(); Review Comment: this looks like this could be changed into: ```java for (JsObject object : parent.getProperties().values()) { ``` -- 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