Skip to content

Commit

Permalink
Detached ruleset unlocked from mixin did not worked right (did not seen
Browse files Browse the repository at this point in the history
mixins parameters).  Putting body scope joiner on one place to find out
how to refactor them. #186
  • Loading branch information
jurcovicovam committed Jul 1, 2014
1 parent aef9cff commit 28d4897
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public boolean seesLocalDataOf(IScope otherScope) {
return getParent().seesLocalDataOf(otherScope);
}

//FIXME ! this method might be wasteful, we are looking for the same tree, so there should be no need to restart search from top for each parent
public boolean seesAllDataOf(IScope otherScope) {
boolean isLocalImport = seesLocalDataOf(otherScope);
IScope parent = otherScope.getParent();
while (isLocalImport && parent != null) {
isLocalImport = seesLocalDataOf(parent);
parent = parent.getParent();
}

return isLocalImport;
}

public IScope getRootScope() {
if (!hasParent())
return this;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface IScope extends ILocalScope, IScopesTree {

public boolean seesLocalDataOf(IScope otherScope);

public boolean seesAllDataOf(IScope otherScope);

//smart util methods
public IScope firstChild();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.github.sommeri.less4j.core.compiler.scopes;

import java.util.ArrayList;
import java.util.List;

import com.github.sommeri.less4j.core.compiler.scopes.view.ScopeView;

public class ScopeManipulation {

//FIXME !!!! refactor and clean, unify with references solver joiner
public ScopeView constructImportedBodyScope(IScope importTargetScope, IScope bodyToBeImportedScope) {
ScopeView newScope = null;
// locally defined mixin does not require any other action
boolean isLocalImport = bodyToBeImportedScope.seesAllDataOf(importTargetScope);

if (isLocalImport) {
// we need to copy the whole tree, because this runs inside referenced mixin scope
// snapshot and imported mixin needs to remember the scope as it is now
newScope = ScopeFactory.createJoinedScopesView(null, bodyToBeImportedScope);
newScope.saveLocalDataForTheWholeWayUp();
} else {
// since this is non-local import, we need to join reference scope and imported mixins scope
// imported mixin needs to have access to variables defined in caller
newScope = ScopeFactory.createJoinedScopesView(importTargetScope, bodyToBeImportedScope);
newScope.saveLocalDataForTheWholeWayUp();
}
return newScope;
}

public IScope calculateBodyWorkingScope(IScope callerScope, IScope bodyScope) {
//FIXME !!!!!! find the case where this is needed for mixins and create test case for it
// locally defined mixin does not require any other action
boolean isLocallyDefined = bodyScope.seesAllDataOf(callerScope);

if (isLocallyDefined) {
return bodyScope;
}

//join scopes
IScope result = ScopeFactory.createJoinedScopesView(callerScope, bodyScope);
return result;
}

//FIXME: !!! evaluate need for this
public IScope calculateMixinsWorkingScope(IScope callerScope, IScope arguments, IScope mixinScope) {
// add arguments
IScope mixinDeclarationScope = mixinScope.getParent();
mixinDeclarationScope.add(arguments);

// locally defined mixin does not require any other action
boolean isLocallyDefined = mixinDeclarationScope.seesLocalDataOf(callerScope);
if (isLocallyDefined) {
return mixinScope;
}

//join scopes
IScope result = ScopeFactory.createJoinedScopesView(callerScope, mixinScope);
return result;
}

public List<FullMixinDefinition> mixinsToImport(IScope referenceScope, IScope referencedMixinScope, List<FullMixinDefinition> unmodifiedMixinsToImport) {
List<FullMixinDefinition> result = new ArrayList<FullMixinDefinition>();
for (FullMixinDefinition mixinToImport : unmodifiedMixinsToImport) {
boolean isLocalImport = mixinToImport.getScope().seesLocalDataOf(referenceScope);
if (isLocalImport) {
// we need to copy the whole tree, because this runs inside referenced mixin scope
// snapshot and imported mixin needs to remember the scope as it is now
ScopeView newWay = ScopeFactory.createJoinedScopesView(null, mixinToImport.getScope());
newWay.saveLocalDataForTheWholeWayUp();
result.add(new FullMixinDefinition(mixinToImport.getMixin(), newWay));
} else {
// since this is non-local import, we need to join reference scope and imported mixins scope
// imported mixin needs to have access to variables defined in caller
ScopeView newWay = ScopeFactory.createJoinedScopesView(referencedMixinScope, mixinToImport.getScope());
newWay.saveLocalDataForTheWholeWayUp();
result.add(new FullMixinDefinition(mixinToImport.getMixin(), newWay));
}

}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package com.github.sommeri.less4j.core.compiler.scopes.local;

import java.util.HashMap;
import java.util.Map;

import com.github.sommeri.less4j.core.ast.DetachedRuleset;
import com.github.sommeri.less4j.core.compiler.scopes.FullDetachedRulesetDefinition;


public class LocalScopeData implements Cloneable {

private VariablesDeclarationsStorage variables = new VariablesDeclarationsStorage();
private MixinsDefinitionsStorage mixins = new MixinsDefinitionsStorage();
private Map<DetachedRuleset, FullDetachedRulesetDefinition> detachedRulesetsScopes = new HashMap<DetachedRuleset, FullDetachedRulesetDefinition>();

@Override
public LocalScopeData clone() {
try {
LocalScopeData clone = (LocalScopeData) super.clone();
clone.variables = variables.clone();
clone.mixins = mixins.clone();
clone.detachedRulesetsScopes = new HashMap<DetachedRuleset, FullDetachedRulesetDefinition>(detachedRulesetsScopes);
return clone;

} catch (CloneNotSupportedException e) {
Expand All @@ -40,7 +33,6 @@ public String toString() {
StringBuilder result = new StringBuilder(getClass().getSimpleName()).append("\n");
result.append("**Variables storage: ").append(variables).append("\n\n");
result.append("**Mixins storage: ").append(mixins).append("\n\n");
result.append("**DetachedRulesets size: ").append(detachedRulesetsScopes.size()).append("\n\n");
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ public void addChild(IScope child) {
throw new IllegalStateException("Scopes view does not accept new childs.");
}

// @Override
// public void setParent(IScope parent) {
// throw new IllegalStateException("Scopes view does not accept new parents.");
// }

@Override
public void setParent(IScope parent) {
throw new IllegalStateException("Scopes view does not accept new parents.");
Expand Down
Loading

0 comments on commit 28d4897

Please sign in to comment.