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

[GitHub] [netbeans] lkishalmi opened a new pull request, #5954: Navigator Support for HCL Files

lkishalmi opened a new pull request, #5954:
URL: https://github.com/apache/netbeans/pull/5954

   Well, this one is a bit more than the Navogator.
   
   The AST is kind of ready till Attribute level. Beyond that there is the HCL Expression sub-language which AST still has to be done.
   
   That makes possible to implement the structure scanner part for Navigator.
   
   I've realized that the HCL Expression language can be parsed by the HCL parser itself, no need to generate a separate HCLExpressionParser. That would just duplicate a lot of internal context classes, and so it would needed a duplicate implementation of the expression AST builder.
   
   I plan to remove the Terraform specific Lexer in the future, as that is a duplicate + small addition to the HCLLexer. That is used in the basic syntax Highlight only. It would be better to replace that with parser assisted depending on HCL language flavors.
   
   


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


[GitHub] [netbeans] lkishalmi merged pull request #5954: Navigator Support for HCL Files

Posted by "lkishalmi (via GitHub)" <gi...@apache.org>.
lkishalmi merged PR #5954:
URL: https://github.com/apache/netbeans/pull/5954


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


[GitHub] [netbeans] lkishalmi commented on pull request #5954: Navigator Support for HCL Files

Posted by "lkishalmi (via GitHub)" <gi...@apache.org>.
lkishalmi commented on PR #5954:
URL: https://github.com/apache/netbeans/pull/5954#issuecomment-1548718500

   ![image](https://github.com/apache/netbeans/assets/1381701/a1a18cce-a472-41f9-854d-e6ae4aef1e98)
   


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


[GitHub] [netbeans] lkishalmi commented on pull request #5954: Navigator Support for HCL Files

Posted by "lkishalmi (via GitHub)" <gi...@apache.org>.
lkishalmi commented on PR #5954:
URL: https://github.com/apache/netbeans/pull/5954#issuecomment-1553931426

   @matthiasblaesing Thank you for the insightful review!


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


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

Posted by "lkishalmi (via GitHub)" <gi...@apache.org>.
lkishalmi commented on code in PR #5954:
URL: https://github.com/apache/netbeans/pull/5954#discussion_r1198495256


##########
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:
   Nice catch! Thanks!



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


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

Posted by "matthiasblaesing (via GitHub)" <gi...@apache.org>.
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


[GitHub] [netbeans] lkishalmi commented on pull request #5954: Navigator Support for HCL Files

Posted by "lkishalmi (via GitHub)" <gi...@apache.org>.
lkishalmi commented on PR #5954:
URL: https://github.com/apache/netbeans/pull/5954#issuecomment-1556097485

   The red underlines are coming from the Lexer. Somehow I assigned the wrong coloring to the template content. Fixed in the last commit. Thanks for pointing that out!


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