Skip to content

Commit

Permalink
Clean up #126
Browse files Browse the repository at this point in the history
  • Loading branch information
meri committed Feb 15, 2014
1 parent 3faa562 commit 84da0c9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public ASTCssNode getParent() {
return parent;
}

public boolean hasParent() {
return getParent()!=null;
}

/**
* WARNING: it is up to the programmer to keep parent and childs getters and
* setters consistent. Members of this hierarchy are not responsible for that.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.github.sommeri.less4j.core.ast.ASTCssNode;
import com.github.sommeri.less4j.core.ast.ASTCssNodeType;
import com.github.sommeri.less4j.core.ast.Body;
import com.github.sommeri.less4j.core.ast.FixedNamePart;
import com.github.sommeri.less4j.core.ast.InterpolableNamePart;
import com.github.sommeri.less4j.core.ast.annotations.NotAstProperty;
import com.github.sommeri.less4j.core.problems.BugHappened;

Expand All @@ -30,6 +32,18 @@ public ASTCssNode findParentOfType(ASTCssNode node, ASTCssNodeType... type) {
return candidate;
}

public void replaceMemberAndSynchronizeSilentness(InterpolableNamePart oldMember, FixedNamePart newMember) {
if (oldMember.hasParent())
oldMember.getParent().replaceMember(oldMember, newMember);

setTreeSilentness(newMember, oldMember.isSilent());
}

public void replaceAndSynchronizeSilentness(ASTCssNode oldChild, ASTCssNode newChild) {
setTreeSilentness(newChild, oldChild.isSilent());
replace(oldChild, newChild);
}

public void replace(ASTCssNode oldChild, ASTCssNode newChild) {
if (oldChild == newChild)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.github.sommeri.less4j.core.ast.GeneralBody;
import com.github.sommeri.less4j.core.ast.IndirectVariable;
import com.github.sommeri.less4j.core.ast.InterpolableName;
import com.github.sommeri.less4j.core.ast.InterpolableNamePart;
import com.github.sommeri.less4j.core.ast.MixinReference;
import com.github.sommeri.less4j.core.ast.RuleSet;
import com.github.sommeri.less4j.core.ast.SimpleSelector;
Expand Down Expand Up @@ -192,58 +191,48 @@ private boolean solveIfVariableReference(ASTCssNode node, IScope scope) {
switch (node.getType()) {
case VARIABLE: {
Expression replacement = expressionEvaluator.evaluate((Variable) node);
replaceAndSynchronizeSilentness(node, replacement);
manipulator.replaceAndSynchronizeSilentness(node, replacement);
return true;
}
case INDIRECT_VARIABLE: {
Expression replacement = expressionEvaluator.evaluate((IndirectVariable) node);
replaceAndSynchronizeSilentness(node, replacement);
manipulator.replaceAndSynchronizeSilentness(node, replacement);
return true;
}
case STRING_EXPRESSION: {
Expression replacement = expressionEvaluator.evaluate((CssString) node);
replaceAndSynchronizeSilentness(node, replacement);
manipulator.replaceAndSynchronizeSilentness(node, replacement);
return true;
}
case ESCAPED_VALUE: {
Expression replacement = expressionEvaluator.evaluate((EscapedValue) node);
replaceAndSynchronizeSilentness(node, replacement);
manipulator.replaceAndSynchronizeSilentness(node, replacement);
return true;
}
case ESCAPED_SELECTOR: {
SimpleSelector replacement = interpolateEscapedSelector((EscapedSelector) node, expressionEvaluator);
replaceAndSynchronizeSilentness(node, replacement);
manipulator.replaceAndSynchronizeSilentness(node, replacement);
return true;
}
case FIXED_NAME_PART: {
FixedNamePart part = (FixedNamePart) node;
FixedNamePart replacement = interpolateFixedNamePart(part, expressionEvaluator);
replaceMemberAndSynchronizeSilentness(part, replacement);
manipulator.replaceMemberAndSynchronizeSilentness(part, replacement);
return true;
}
case VARIABLE_NAME_PART: {
VariableNamePart part = (VariableNamePart) node;
Expression value = expressionEvaluator.evaluate(part.getVariable());
FixedNamePart fixedName = toFixedName(value, node.getUnderlyingStructure(), part);
FixedNamePart replacement = interpolateFixedNamePart(fixedName, expressionEvaluator);
replaceMemberAndSynchronizeSilentness(part, replacement);
manipulator.replaceMemberAndSynchronizeSilentness(part, replacement);
return true;
}
default: // nothing
}
return false;
}

private void replaceMemberAndSynchronizeSilentness(InterpolableNamePart part, FixedNamePart replacement) {
part.getParent().replaceMember(part, replacement);
manipulator.setTreeSilentness(replacement, part.isSilent());
}

private void replaceAndSynchronizeSilentness(ASTCssNode node, ASTCssNode replacement) {
manipulator.setTreeSilentness(replacement, node.isSilent());
manipulator.replace(node, replacement);
}

private FixedNamePart toFixedName(Expression value, HiddenTokenAwareTree parent, VariableNamePart part) {
CssPrinter printer = new QuotesKeepingInStringCssPrinter();
printer.append(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import (reference) "import/import-as-reference.less";
@import (reference) "import/import-as-reference-mixin.less";
.importer {
.returnedMixin() {
returned: mixin;
Expand Down

0 comments on commit 84da0c9

Please sign in to comment.