Skip to content

Commit

Permalink
Made HCL AST on records
Browse files Browse the repository at this point in the history
Use TextBlocks in formatting test, minor enhancements
  • Loading branch information
lkishalmi committed Mar 18, 2024
1 parent bbd18aa commit b1f2e5b
Show file tree
Hide file tree
Showing 33 changed files with 640 additions and 895 deletions.
2 changes: 1 addition & 1 deletion ide/languages.hcl/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ OpenIDE-Module: org.netbeans.modules.languages.hcl
OpenIDE-Module-Layer: org/netbeans/modules/languages/hcl/layer.xml
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/languages/hcl/Bundle.properties
OpenIDE-Module-Specification-Version: 1.4
OpenIDE-Module-Java-Dependencies: Java > 11
OpenIDE-Module-Java-Dependencies: Java > 17
AutoUpdate-Show-In-Client: true
4 changes: 2 additions & 2 deletions ide/languages.hcl/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
javac.source=11
javac.target=11
javac.source=17
javac.target=17

Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
import org.netbeans.modules.languages.hcl.ast.HCLAttribute;
import org.netbeans.modules.languages.hcl.ast.HCLBlock;
import org.netbeans.modules.languages.hcl.ast.HCLCollection;
import org.netbeans.modules.languages.hcl.ast.HCLDocument;
import org.netbeans.modules.languages.hcl.ast.HCLElement;
import org.netbeans.modules.languages.hcl.ast.HCLExpression;
import org.netbeans.modules.languages.hcl.ast.HCLFunction;
import org.netbeans.modules.languages.hcl.ast.HCLIdentifier;
import org.netbeans.modules.languages.hcl.ast.HCLTreeWalker;
import org.netbeans.modules.languages.hcl.ast.HCLVariable;
import org.netbeans.modules.parsing.spi.Scheduler;
import org.netbeans.modules.parsing.spi.SchedulerEvent;
Expand Down Expand Up @@ -64,8 +63,7 @@ public final void run(HCLParserResult result, SchedulerEvent event) {
resume();

Highlighter h = createHighlighter(result);
result.getDocument().accept(h);
highlights = h.work;
highlights = h.process(result.getDocument());
}

protected Highlighter createHighlighter(HCLParserResult result) {
Expand All @@ -74,7 +72,7 @@ protected Highlighter createHighlighter(HCLParserResult result) {

@Override
public int getPriority() {
return 0;
return 100;
}

@Override
Expand All @@ -87,25 +85,33 @@ public void cancel() {
cancelled = true;
}

protected abstract class Highlighter extends HCLElement.BAEVisitor {
protected abstract class Highlighter {
protected final Map<OffsetRange, Set<ColoringAttributes>> work = new HashMap<>();

protected final SourceRef refs;
protected Highlighter(SourceRef refs) {
this.refs = refs;
}

@Override
public final boolean visit(HCLElement e) {
protected abstract void highlight(HCLTreeWalker.Step step);

private boolean cancellableHighlight(HCLTreeWalker.Step step) {
if (isCancelled()) {
return true;
return false;
}
return super.visit(e);
highlight(step);
return true;
}

public Map<OffsetRange, Set<ColoringAttributes>> process(HCLElement element) {
HCLTreeWalker.depthFirst(element, this::cancellableHighlight);
return work;
}

protected final void mark(HCLElement e, Set<ColoringAttributes> attrs) {
refs.getOffsetRange(e).ifPresent((range) -> work.put(range, attrs));
}

}

protected class DefaultHighlighter extends Highlighter {
Expand All @@ -115,9 +121,12 @@ public DefaultHighlighter(SourceRef refs) {
}

@Override
protected boolean visitBlock(HCLBlock block) {
if (block.getParent() instanceof HCLDocument) {
List<HCLIdentifier> decl = block.getDeclaration();
protected void highlight(HCLTreeWalker.Step step) {

// TODO: Can use record patterns from Java 21
HCLElement e = step.node();
if (e instanceof HCLBlock block && step.depth() == 1) {
List<HCLIdentifier> decl = block.declaration();
HCLIdentifier type = decl.get(0);

mark(type, ColoringAttributes.CLASS_SET);
Expand All @@ -127,35 +136,17 @@ protected boolean visitBlock(HCLBlock block) {
mark(id, ColoringAttributes.CONSTRUCTOR_SET);
}
}
} else {
//TODO: Handle nested Blocks...
}
return false;
}

@Override
protected boolean visitAttribute(HCLAttribute attr) {
mark(attr.getName(), ColoringAttributes.FIELD_SET);
return false;
}

@Override
protected boolean visitExpression(HCLExpression expr) {
if (expr instanceof HCLFunction) {
HCLFunction func = (HCLFunction) expr;
mark(func.getName(), ColoringAttributes.CONSTRUCTOR_SET);
}

if (expr instanceof HCLCollection.Object) {
HCLCollection.Object obj = (HCLCollection.Object) expr;
for (HCLExpression key : obj.getKeys()) {
if (key instanceof HCLVariable) {
mark(key, ColoringAttributes.FIELD_SET);
} else if (e instanceof HCLAttribute attr) {
mark(attr.name(), ColoringAttributes.FIELD_SET);
} else if (e instanceof HCLFunction func) {
mark(func.name(), ColoringAttributes.CONSTRUCTOR_SET);
} else if (e instanceof HCLCollection.Object obj) {
for (HCLCollection.ObjectElement oe : obj.elements()) {
if (oe.key() instanceof HCLVariable) {
mark(oe.key(), ColoringAttributes.FIELD_SET);
}
}
}
return false;
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
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.HCLAddressableElement;
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.openide.filesystems.FileObject;

/**
Expand All @@ -42,11 +42,11 @@
*/
public class HCLStructureItem implements ElementHandle, StructureItem {

final HCLAddressableElement element;
final HCLElement element;
final SourceRef references;
private List<? extends StructureItem> nestedCache;

public HCLStructureItem(HCLAddressableElement element, SourceRef references) {
public HCLStructureItem(HCLElement element, SourceRef references) {
this.element = element;
this.references = references;
}
Expand All @@ -63,7 +63,12 @@ public String getMimeType() {

@Override
public String getName() {
return element.id();
if (element instanceof HCLAttribute a) {
return a.id();
} else if (element instanceof HCLBlock b) {
return b.id();
}
return "<" + element.getClass().getSimpleName() + ">";
}

@Override
Expand Down Expand Up @@ -113,10 +118,10 @@ public List<? extends StructureItem> getNestedItems() {
if (element instanceof HCLContainer) {
HCLContainer c = (HCLContainer) element;
List<HCLStructureItem> nested = new ArrayList<>();
for (HCLBlock block : c.getBlocks()) {
for (HCLBlock block : c.blocks()) {
nested.add(new HCLStructureItem(block, references));
}
for (HCLAttribute attribute : c.getAttributes()) {
for (HCLAttribute attribute : c.attributes()) {
nested.add(new HCLStructureItem(attribute, references));
}
nestedCache = nested;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.netbeans.modules.languages.hcl;

import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -28,6 +28,7 @@
import java.util.TreeMap;
import org.netbeans.modules.csl.api.OffsetRange;
import org.netbeans.modules.languages.hcl.ast.HCLElement;
import org.netbeans.modules.languages.hcl.ast.HCLElementFactory;
import org.netbeans.modules.parsing.api.Snapshot;
import org.openide.filesystems.FileObject;

Expand All @@ -37,7 +38,7 @@
*/
public class SourceRef {
public final Snapshot source;
private Map<HCLElement, OffsetRange> elementOffsets = new HashMap<>();
private Map<HCLElement, OffsetRange> elementOffsets = new IdentityHashMap<>();

private TreeMap<OffsetRange, HCLElement> elementAt = new TreeMap<>((o1, o2) -> o1.getStart() != o2.getStart() ? o1.getStart() - o2.getStart() : o2.getEnd() - o1.getEnd());

Expand All @@ -53,8 +54,8 @@ private void add(HCLElement e, OffsetRange r) {
}
}

void elementCreated(HCLElement.CreateContext ctx) {
add(ctx.element, new OffsetRange(ctx.start.getStartIndex(), ctx.stop.getStopIndex() + 1));
void elementCreated(HCLElementFactory.CreateContext ctx) {
add(ctx.element(), new OffsetRange(ctx.start().getStartIndex(), ctx.stop().getStopIndex() + 1));
}

public FileObject getFileObject() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*/
package org.netbeans.modules.languages.hcl.ast;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
*
* @author lkishalmi
*/
public abstract class HCLArithmeticOperation extends HCLExpression {
public sealed interface HCLArithmeticOperation extends HCLExpression {

Operator op();

public enum Operator {
NOT("!"),
Expand Down Expand Up @@ -61,51 +61,27 @@ public String toString() {
}
}

public final Operator op;

public HCLArithmeticOperation(Operator op) {
this.op = op;
}

public final static class Binary extends HCLArithmeticOperation {
public final HCLExpression left;
public final HCLExpression right;

public Binary(Operator op, HCLExpression left, HCLExpression right) {
super(op);
this.left = left;
this.right = right;
}

public record Binary(Operator op, HCLExpression left, HCLExpression right) implements HCLArithmeticOperation {
@Override
public String asString() {
return left.toString() + op.toString() + right.toString();
}

@Override
public List<? extends HCLExpression> getChildren() {
return Arrays.asList(left, right);
public List<? extends HCLExpression> elements() {
return List.of(left, right);
}

}

public final static class Unary extends HCLArithmeticOperation {
public final HCLExpression operand;

public Unary(Operator op, HCLExpression operand) {
super(op);
this.operand = operand;
}

public record Unary(Operator op, HCLExpression operand) implements HCLArithmeticOperation {
@Override
public String asString() {
return op.toString() + operand.toString();
}

@Override
public List<? extends HCLExpression> getChildren() {
return Collections.singletonList(operand);
public List<? extends HCLExpression> elements() {
return List.of(operand);
}

}
}
Loading

0 comments on commit b1f2e5b

Please sign in to comment.