matthiasblaesing commented on code in PR #8528: URL: https://github.com/apache/netbeans/pull/8528#discussion_r2105894424
########## 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: Sure - it is a web-era format. Of course they don't follow a specification. An example from a freshly generated angular project: ```json /* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */ /* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */ { "extends": "./tsconfig.json", "compilerOptions": { "outDir": "./out-tsc/app", "types": [] }, "files": [ "src/main.ts" ], "include": [ "src/**/*.d.ts" ] } ``` Not sure which side is wrong here. The short sighted specification or the specification ignoring usage. -- 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