com.vladsch.flexmark.ext.macros.internal.MacroDefinitionRepository Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flexmark-ext-macros Show documentation
Show all versions of flexmark-ext-macros Show documentation
flexmark-java extension for processing macros
package com.vladsch.flexmark.ext.macros.internal;
import com.vladsch.flexmark.ext.macros.MacroDefinitionBlock;
import com.vladsch.flexmark.ext.macros.MacroReference;
import com.vladsch.flexmark.ext.macros.MacrosExtension;
import com.vladsch.flexmark.util.ast.KeepType;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.ast.NodeRepository;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.data.DataKey;
import org.jetbrains.annotations.NotNull;
import java.util.*;
@SuppressWarnings("WeakerAccess")
public class MacroDefinitionRepository extends NodeRepository {
final private ArrayList myReferencedMacroDefinitionBlocks = new ArrayList<>();
public void addMacrosReference(MacroDefinitionBlock macroDefinitionBlock, MacroReference macros) {
if (!macroDefinitionBlock.isReferenced()) {
myReferencedMacroDefinitionBlocks.add(macroDefinitionBlock);
}
macroDefinitionBlock.setFirstReferenceOffset(macros.getStartOffset());
}
public void resolveMacrosOrdinals() {
// need to sort by first referenced offset then set each to its ordinal position in the array+1
myReferencedMacroDefinitionBlocks.sort(Comparator.comparing(MacroDefinitionBlock::getFirstReferenceOffset));
int ordinal = 0;
for (MacroDefinitionBlock macroDefinitionBlock : myReferencedMacroDefinitionBlocks) {
macroDefinitionBlock.setOrdinal(++ordinal);
}
}
public List getReferencedMacroDefinitionBlocks() {
return myReferencedMacroDefinitionBlocks;
}
public MacroDefinitionRepository(DataHolder options) {
super(MacrosExtension.MACRO_DEFINITIONS_KEEP.get(options));
}
@NotNull
@Override
public DataKey getDataKey() {
return MacrosExtension.MACRO_DEFINITIONS;
}
@NotNull
@Override
public DataKey getKeepDataKey() {
return MacrosExtension.MACRO_DEFINITIONS_KEEP;
}
@NotNull
@Override
public Set getReferencedElements(Node parent) {
HashSet references = new HashSet<>();
visitNodes(parent, value -> {
if (value instanceof MacroReference) {
MacroDefinitionBlock reference = ((MacroReference) value).getReferenceNode(MacroDefinitionRepository.this);
if (reference != null) {
references.add(reference);
}
}
}, MacroReference.class);
return references;
}
}