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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

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

Reply via email to