Skip to content

Commit

Permalink
grammar: enum/macro folding & structure
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiro committed Feb 26, 2023
1 parent b62889b commit 4cba813
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public class RustASTNode {
* The inner traits in this node.
*/
private final TreeMap<String, RustASTNode> traits;
/**
* The inner enums in this node.
*/
private final TreeMap<String, RustASTNode> enums;
/**
* The inner macros in this node.
*/
private final TreeMap<String, RustASTNode> macros;
/**
* Any nested folds this node may have.
*/
Expand All @@ -88,6 +96,8 @@ public RustASTNode(RustASTNodeKind kind) {
this.structs = new TreeMap<>();
this.impls = new TreeMap<>();
this.traits = new TreeMap<>();
this.enums = new TreeMap<>();
this.macros = new TreeMap<>();
this.codeblockFolds = new ArrayList<>();
}

Expand Down Expand Up @@ -251,4 +261,30 @@ public Collection<RustASTNode> traits() {
return traits.values();
}

public void addEnum(RustASTNode e) {
e.setParent(this);
enums.put(e.getName(), e);
}

public RustASTNode getEnum(String name) {
return enums.get(name);
}

public Collection<RustASTNode> enums() {
return enums.values();
}

public void addMacro(RustASTNode macro) {
macro.setParent(this);
macros.put(macro.getName(), macro);
}

public RustASTNode getMacro(String name) {
return macros.get(name);
}

public Collection<RustASTNode> macros() {
return macros.values();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public enum RustASTNodeKind {
/**
* A Rust trait.
*/
TRAIT

TRAIT,
/**
* A Rust macro rules
*/
MACRO,
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,61 @@ public RustASTNode visitCrate(RustParser.CrateContext ctx) {
return crate;
}

@Override
public RustASTNode visitEnumeration(RustParser.EnumerationContext ctx) {
if (cancelled.get()) {
return null;
}
RustASTNode current = peek();

RustASTNode e = new RustASTNode(RustASTNodeKind.ENUM);
e.setName(ctx.identifier().getText());
e.setRange(ctx.start, ctx.stop);
e.setFold(ctx.LCURLYBRACE(), ctx.RCURLYBRACE());

current.addEnum(e);

LOG.log(LOGLEVEL, String.format("Visiting enum %s%n", e.getName()));

push(e);
// Visit children to seek for functions
for (ParseTree tree : ctx.children) {
tree.accept(this);
}
pop();

return e;
}

@Override
public RustASTNode visitMacroRulesDefinition(RustParser.MacroRulesDefinitionContext ctx) {
if (cancelled.get()) {
return null;
}
RustASTNode current = peek();

RustASTNode macro = new RustASTNode(RustASTNodeKind.MACRO);
macro.setName(ctx.identifier().getText());
macro.setRange(ctx.start, ctx.stop);
RustParser.MacroRulesDefContext def = ctx.macroRulesDef();
if (def != null) {
macro.setFold(def.LCURLYBRACE(), def.RCURLYBRACE());
}

current.addMacro(macro);

LOG.log(LOGLEVEL, String.format("Visiting macro %s%n", macro.getName()));

push(macro);
// Visit children to seek for functions
for (ParseTree tree : ctx.children) {
tree.accept(this);
}
pop();

return macro;
}

@Override
public RustASTNode visitTrait_(RustParser.Trait_Context ctx) {
if (cancelled.get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class RustFoldingProvider implements FoldTypeProvider {
TYPES.add(RustFoldingScanner.TYPE_FUNCTION);
TYPES.add(RustFoldingScanner.TYPE_IMPL);
TYPES.add(RustFoldingScanner.TYPE_TRAIT);
TYPES.add( RustFoldingScanner.TYPE_ENUM);
TYPES.add( RustFoldingScanner.TYPE_MACRO);
// TODO: Add more fold types
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ public static RustFoldingScanner create() {
getBundleString("FT_Traits"), // NOI18N
FoldTemplate.DEFAULT_BLOCK);

/**
* FoldType for Rust enums
*/
@NbBundle.Messages("FT_Enums=Enums")
public static final FoldType TYPE_ENUM = FoldType.MEMBER.derive("enum", // NOI18N
getBundleString("FT_Enums"), // NOI18N
FoldTemplate.DEFAULT_BLOCK);

/**
* FoldType for Rust enums
*/
@NbBundle.Messages("FT_Macros=Macros")
public static final FoldType TYPE_MACRO = FoldType.MEMBER.derive("macro", // NOI18N
getBundleString("FT_Macros"), // NOI18N
FoldTemplate.DEFAULT_BLOCK);

/**
* Returns the codeblockFolds from the giveh RustLanguageParserResult
*
Expand Down Expand Up @@ -139,6 +155,20 @@ public Map<String, List<OffsetRange>> folds(RustLanguageParserResult rustLanguag
crate.traits().forEach(addFunctionsInNode);
}

// Enums
{
List<OffsetRange> enumFolds = crate.enums().stream().map(RustASTNode::getFold).filter(Objects::nonNull).collect(Collectors.toList());
folds.put(TYPE_ENUM.code(), enumFolds);
crate.enums().forEach(addFunctionsInNode);
}

// Macros
{
List<OffsetRange> macroFolds = crate.macros().stream().map(RustASTNode::getFold).filter(Objects::nonNull).collect(Collectors.toList());
folds.put(TYPE_MACRO.code(), macroFolds);
crate.macros().forEach(addFunctionsInNode);
}

// Function folds
{
crate.functions().forEach(addFunction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ private static final String getIconBase(RustASTNode node) {
case FUNCTION:
return "org/netbeans/modules/rust/grammar/structure/resources/structure-function.png"; // NOI18N
case STRUCT:
return "org/netbeans/modules/rust/grammar/structure/resources/structure-struct.png"; // NOI18N
case IMPL:
case TRAIT:
return "org/netbeans/modules/rust/grammar/structure/resources/structure-struct.png"; // NOI18N
return "org/netbeans/modules/rust/grammar/structure/resources/structure-impl.png"; // NOI18N
case MACRO:
return "org/netbeans/modules/rust/grammar/structure/resources/structure-macro.png"; // NOI18N
default:
return "org/netbeans/modules/rust/grammar/structure/resources/structure-struct.png"; // NOI18N
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ private List<? extends StructureItem> createStructureFromAST(final RustAST ast)
crate.structs().forEach(adder);
crate.traits().forEach(adder);
crate.functions().forEach(adder);
crate.enums().forEach(adder);
crate.macros().forEach(adder);

return items;
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4cba813

Please sign in to comment.