You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "matthiasblaesing (via GitHub)" <gi...@apache.org> on 2023/05/16 18:32:01 UTC

[GitHub] [netbeans] matthiasblaesing commented on a diff in pull request #5954: Navigator Support for HCL Files

matthiasblaesing commented on code in PR #5954:
URL: https://github.com/apache/netbeans/pull/5954#discussion_r1195540732


##########
ide/languages.hcl/src/org/netbeans/modules/languages/hcl/HCLStructureItem.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.languages.hcl;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import javax.swing.ImageIcon;
+import org.netbeans.modules.csl.api.ElementHandle;
+import org.netbeans.modules.csl.api.ElementKind;
+import org.netbeans.modules.csl.api.HtmlFormatter;
+import org.netbeans.modules.csl.api.Modifier;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.api.StructureItem;
+import org.netbeans.modules.csl.spi.ParserResult;
+import org.netbeans.modules.languages.hcl.ast.HCLAttribute;
+import org.netbeans.modules.languages.hcl.ast.HCLBlock;
+import org.netbeans.modules.languages.hcl.ast.HCLContainer;
+import org.netbeans.modules.languages.hcl.ast.HCLElement;
+import org.netbeans.modules.languages.hcl.ast.SourceRef;
+import org.openide.filesystems.FileObject;
+
+/**
+ *
+ * @author lkishalmi
+ */
+public class HCLStructureItem implements ElementHandle, StructureItem {
+
+    final HCLElement element;
+    final SourceRef references;
+
+    public HCLStructureItem(HCLElement element, SourceRef references) {
+        this.element = element;
+        this.references = references;
+    }
+
+    @Override
+    public FileObject getFileObject() {
+        return references.getFileObject();
+    }
+
+    @Override
+    public String getMimeType() {
+        return getFileObject().getMIMEType();
+    }
+
+    @Override
+    public String getName() {
+        return element.id();
+    }
+
+    @Override
+    public String getIn() {
+        return null;
+    }
+
+
+    @Override
+    public Set<Modifier> getModifiers() {
+        return Collections.emptySet();
+    }
+
+    @Override
+    public boolean signatureEquals(ElementHandle handle) {
+        return false;
+    }
+
+    @Override
+    public OffsetRange getOffsetRange(ParserResult result) {
+        Optional<OffsetRange> range = references.getOffsetRange(element);
+        return range.isPresent() ? range.get(): OffsetRange.NONE;

Review Comment:
   The construct looks more like the normal `null`-Check idiom, for `Optional`, I would use:
   
   ```suggestion
           return references.getOffsetRange(element).orElse(OffsetRange.NONE);
   ```



##########
ide/languages.hcl/src/org/netbeans/modules/languages/hcl/HCLStructureItem.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.languages.hcl;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import javax.swing.ImageIcon;
+import org.netbeans.modules.csl.api.ElementHandle;
+import org.netbeans.modules.csl.api.ElementKind;
+import org.netbeans.modules.csl.api.HtmlFormatter;
+import org.netbeans.modules.csl.api.Modifier;
+import org.netbeans.modules.csl.api.OffsetRange;
+import org.netbeans.modules.csl.api.StructureItem;
+import org.netbeans.modules.csl.spi.ParserResult;
+import org.netbeans.modules.languages.hcl.ast.HCLAttribute;
+import org.netbeans.modules.languages.hcl.ast.HCLBlock;
+import org.netbeans.modules.languages.hcl.ast.HCLContainer;
+import org.netbeans.modules.languages.hcl.ast.HCLElement;
+import org.netbeans.modules.languages.hcl.ast.SourceRef;
+import org.openide.filesystems.FileObject;
+
+/**
+ *
+ * @author lkishalmi
+ */
+public class HCLStructureItem implements ElementHandle, StructureItem {
+
+    final HCLElement element;
+    final SourceRef references;
+
+    public HCLStructureItem(HCLElement element, SourceRef references) {
+        this.element = element;
+        this.references = references;
+    }
+
+    @Override
+    public FileObject getFileObject() {
+        return references.getFileObject();
+    }
+
+    @Override
+    public String getMimeType() {
+        return getFileObject().getMIMEType();
+    }
+
+    @Override
+    public String getName() {
+        return element.id();
+    }
+
+    @Override
+    public String getIn() {
+        return null;
+    }
+
+
+    @Override
+    public Set<Modifier> getModifiers() {
+        return Collections.emptySet();
+    }
+
+    @Override
+    public boolean signatureEquals(ElementHandle handle) {
+        return false;
+    }
+
+    @Override
+    public OffsetRange getOffsetRange(ParserResult result) {
+        Optional<OffsetRange> range = references.getOffsetRange(element);
+        return range.isPresent() ? range.get(): OffsetRange.NONE;
+    }
+
+    @Override
+    public String getSortText() {
+        return getName();
+    }
+
+    @Override
+    public String getHtml(HtmlFormatter formatter) {
+        return getName();
+    }
+
+    @Override
+    public ElementHandle getElementHandle() {
+        return this;
+    }
+
+    @Override
+    public boolean isLeaf() {
+        return element instanceof HCLAttribute;
+    }
+
+    @Override
+    public List<? extends StructureItem> getNestedItems() {
+        if (element instanceof HCLContainer) {

Review Comment:
   This method is called often, we should cache the child items.



##########
ide/languages.hcl/src/org/netbeans/modules/languages/hcl/ast/ASTBuilderListener.java:
##########
@@ -32,48 +33,73 @@
 public class ASTBuilderListener extends HCLParserBaseListener {
 
     final HCLDocument document = new HCLDocument();
+    final SourceRef references;
+
+    private HCLContainer current = document;
+    
+    public ASTBuilderListener(Snapshot source) {
+        this.references = new SourceRef(source);
+    }
 
     public HCLDocument getDocument() {
         return document;
     }
 
-    int blockDepth = 0;
+    public SourceRef getReferences() {
+        return references;
+    }
+
+    
+    private void addReference(HCLElement e, Token token) {
+        references.add(e, token.getStartIndex(), token.getStopIndex() + 1);
+    }
+
+    private void addReference(HCLElement e, ParserRuleContext ctx) {
+        
+        references.add(e, ctx.start.getStartIndex(), ctx.stop.getStopIndex() + 1);
+    }
+
 
     @Override
     public void exitBlock(HCLParser.BlockContext ctx) {
-        if (blockDepth == 1) {
-            ArrayList<HCLIdentifier> decl = new ArrayList<>(4);
-            for (TerminalNode idn : ctx.IDENTIFIER()) {
-                Token token = idn.getSymbol();
-                SourceRef src = new SourceRef(null, token.getStartIndex(), token.getStopIndex());
-                HCLIdentifier id = new HCLIdentifier.SimpleId(src, token.getText());
-                decl.add(id);
-            }
-            for (HCLParser.StringLitContext idn : ctx.stringLit()) {
-                String sid = idn.getText();
-                sid = sid.substring(1, sid.length() - (sid.endsWith("\"") ? 1 : 0));
-                SourceRef src = new SourceRef(null, idn.getStart().getStartIndex(), idn.getStop().getStopIndex());
-                /*
-                StringBuilder sb = new StringBuilder(idn.getStop().getStopIndex() - idn.getStart().getStartIndex());
-                for (HCLParser.StringContentContext scontent : idn.stringContent()) {
-                    for (TerminalNode tn : scontent.STRING_CONTENT()) {
-                        sb.append(tn.getText());
-                    }
-                }
-                HCLIdentifier id = new HCLIdentifier.StringId(src, sb.toString());
-                */
-                HCLIdentifier id = new HCLIdentifier.StringId(src, sid);
-                decl.add(id);
-            }
-            Collections.sort(decl, HCLElement.SOURCE_ORDER);
-            document.add(new HCLBlock(decl));
+        HCLBlock block = (HCLBlock) current;
+
+        ArrayList<HCLIdentifier> decl = new ArrayList<>(4);
+
+        for (TerminalNode idn : ctx.IDENTIFIER()) {
+            Token token = idn.getSymbol();
+            HCLIdentifier id = new HCLIdentifier.SimpleId(block, token.getText());
+            addReference(id, token);
+            decl.add(id);
+        }
+        for (HCLParser.StringLitContext idn : ctx.stringLit()) {
+            String sid = idn.getText();
+            sid = sid.substring(1, sid.length() - (sid.endsWith("\"") ? 1 : 0));
+            HCLIdentifier id = new HCLIdentifier.StringId(block, sid);
+            addReference(id, idn);
+            decl.add(id);
+        }
+        block.setDeclaration(references.sortBySource(decl));
+
+        current = current.getContainer();
+        current.add(block);
+        addReference(block, ctx);
+    }
+
+    @Override
+    public void exitBody(HCLParser.BodyContext ctx) {
+        for (HCLParser.AttributeContext actx : ctx.attribute()) {
+            HCLAttribute attr = new HCLAttribute(current);
+            attr.name = new HCLIdentifier.SimpleId(attr, actx.IDENTIFIER().getText());
+            addReference(attr.name, actx.IDENTIFIER().getSymbol());
+            addReference(attr, ctx);

Review Comment:
   I noticed, that double clicks in navigator always jumped to the first element in the block, not the right one. Turns out this typo is responsible.
   
   ```suggestion
               addReference(attr, actx);
   ```



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