Skip to content

Commit

Permalink
fix MPS-19456: Illegal access on smart completion
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihail Muhin authored and Mihail Muhin committed Apr 21, 2014
1 parent d95d28e commit d6732f2
Showing 1 changed file with 100 additions and 91 deletions.
Expand Up @@ -24,8 +24,6 @@
import jetbrains.mps.editor.runtime.impl.NodeSubstituteActionsComparator;
import jetbrains.mps.ide.icons.IconManager;
import jetbrains.mps.ide.icons.IdeIcons;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import jetbrains.mps.nodeEditor.CellSide;
import jetbrains.mps.nodeEditor.EditorComponent;
import jetbrains.mps.nodeEditor.EditorContext;
Expand All @@ -47,6 +45,8 @@
import jetbrains.mps.util.Computable;
import jetbrains.mps.util.NameUtil;
import jetbrains.mps.util.WindowsUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.mps.openapi.model.SNode;

Expand Down Expand Up @@ -221,116 +221,125 @@ public List<SubstituteAction> compute(TypeCheckingContext context) {
}

private void rebuildMenuEntries() {
ModelAccess.instance().runReadAction(new Runnable() {
Runnable todo = new Runnable() {
@Override
public void run() {
myMenuEmpty = false;
final String pattern = getPatternEditor().getPattern();
doRebuildMenuEntries();
}
};
if (myIsSmart) {
ModelAccess.instance().runWriteActionInCommand(todo);
} else {
ModelAccess.instance().runReadAction(todo);
}
}

List<SubstituteAction> matchingActions = getMatchingActions(pattern, false);
if (matchingActions.isEmpty()) {
matchingActions = getMatchingActions(IntelligentInputUtil.trimLeft(pattern), false);
}
private void doRebuildMenuEntries() {
myMenuEmpty = false;
final String pattern = getPatternEditor().getPattern();

try {
Collections.sort(matchingActions, new Comparator<SubstituteAction>() {
private Map<SubstituteAction, Integer> mySortPriorities = new HashMap<SubstituteAction, Integer>();
private Map<SubstituteAction, String> myVisibleMatchingTexts = new HashMap<SubstituteAction, String>();

private int getSortPriority(SubstituteAction a) {
Integer result = mySortPriorities.get(a);
if (result == null) {
if (a.getParameterObject() instanceof SNode) {
result = NodePresentationUtil.getSortPriority(a.getSourceNode(), (SNode) a.getParameterObject());
} else {
result = 0;
}
mySortPriorities.put(a, result);
}
return result;
}
List<SubstituteAction> matchingActions = getMatchingActions(pattern, false);
if (matchingActions.isEmpty()) {
matchingActions = getMatchingActions(IntelligentInputUtil.trimLeft(pattern), false);
}

private String getVisibleMatchingText(SubstituteAction a) {
String result = myVisibleMatchingTexts.get(a);
if (result == null) {
result = a.getVisibleMatchingText(pattern);
myVisibleMatchingTexts.put(a, result);
}
return result;
try {
Collections.sort(matchingActions, new Comparator<SubstituteAction>() {
private Map<SubstituteAction, Integer> mySortPriorities = new HashMap<SubstituteAction, Integer>();
private Map<SubstituteAction, String> myVisibleMatchingTexts = new HashMap<SubstituteAction, String>();

private int getSortPriority(SubstituteAction a) {
Integer result = mySortPriorities.get(a);
if (result == null) {
if (a.getParameterObject() instanceof SNode) {
result = NodePresentationUtil.getSortPriority(a.getSourceNode(), (SNode) a.getParameterObject());
} else {
result = 0;
}
mySortPriorities.put(a, result);
}
return result;
}

@Override
public int compare(SubstituteAction i1, SubstituteAction i2) {
boolean strictly1 = i1.canSubstituteStrictly(pattern);
boolean strictly2 = i2.canSubstituteStrictly(pattern);
if (strictly1 != strictly2) {
return strictly1 ? -1 : 1;
}
private String getVisibleMatchingText(SubstituteAction a) {
String result = myVisibleMatchingTexts.get(a);
if (result == null) {
result = a.getVisibleMatchingText(pattern);
myVisibleMatchingTexts.put(a, result);
}
return result;
}

int p1 = getSortPriority(i1);
int p2 = getSortPriority(i2);
if (p1 != p2) {
return p1 - p2;
}
@Override
public int compare(SubstituteAction i1, SubstituteAction i2) {
boolean strictly1 = i1.canSubstituteStrictly(pattern);
boolean strictly2 = i2.canSubstituteStrictly(pattern);
if (strictly1 != strictly2) {
return strictly1 ? -1 : 1;
}

String s1 = getVisibleMatchingText(i1);
String s2 = getVisibleMatchingText(i2);
int p1 = getSortPriority(i1);
int p2 = getSortPriority(i2);
if (p1 != p2) {
return p1 - p2;
}

boolean null_s1 = (s1 == null || s1.length() == 0);
boolean null_s2 = (s2 == null || s2.length() == 0);
if (null_s1 && null_s2) return 0;
if (null_s1) return 1;
if (null_s2) return -1;
int comparisonResult = s1.compareTo(s2);
String s1 = getVisibleMatchingText(i1);
String s2 = getVisibleMatchingText(i2);

if (comparisonResult == 0) {
return 0;
}
boolean null_s1 = (s1 == null || s1.length() == 0);
boolean null_s2 = (s2 == null || s2.length() == 0);
if (null_s1 && null_s2) return 0;
if (null_s1) return 1;
if (null_s2) return -1;
int comparisonResult = s1.compareTo(s2);

return comparisonResult;
}
});

if (myIsSmart /*&& false*/) {
sortSmartActions(matchingActions);
if (comparisonResult == 0) {
return 0;
}
} catch (Exception e) {
LOG.error(e, e);

return comparisonResult;
}
});

mySubstituteActions = matchingActions;
if (mySubstituteActions.size() == 0) {
myMenuEmpty = true;
mySubstituteActions.add(new AbstractNodeSubstituteAction() {
@Override
public String getMatchingText(String pattern) {
return "No variants for \"" + getPatternEditor().getPattern() + "\"";
}
if (myIsSmart /*&& false*/) {
sortSmartActions(matchingActions);
}
} catch (Exception e) {
LOG.error(e, e);
}

@Override
public String getVisibleMatchingText(String pattern) {
return getMatchingText(pattern);
}
mySubstituteActions = matchingActions;
if (mySubstituteActions.size() == 0) {
myMenuEmpty = true;
mySubstituteActions.add(new AbstractNodeSubstituteAction() {
@Override
public String getMatchingText(String pattern) {
return "No variants for \"" + getPatternEditor().getPattern() + "\"";
}

@Override
public SNode doSubstitute(@Nullable final jetbrains.mps.openapi.editor.EditorContext editorContext, String pattern) {
return null;
}
});
@Override
public String getVisibleMatchingText(String pattern) {
return getMatchingText(pattern);
}

int textLength = 0;
int descriptionLength = 0;
for (SubstituteAction item : mySubstituteActions) {
try {
textLength = Math.max(textLength, getTextLength(item, pattern));
descriptionLength = Math.max(descriptionLength, getDescriptionLength(item, pattern));
} catch (Throwable t) {
LOG.error(t, t);
}
@Override
public SNode doSubstitute(@Nullable final jetbrains.mps.openapi.editor.EditorContext editorContext, String pattern) {
return null;
}
});
}

int textLength = 0;
int descriptionLength = 0;
for (SubstituteAction item : mySubstituteActions) {
try {
textLength = Math.max(textLength, getTextLength(item, pattern));
descriptionLength = Math.max(descriptionLength, getDescriptionLength(item, pattern));
} catch (Throwable t) {
LOG.error(t, t);
}
});
}
}

private void sortSmartActions(List<SubstituteAction> matchingActions) {
Expand Down

0 comments on commit d6732f2

Please sign in to comment.