diff --git a/editor/editor-runtime/source/jetbrains/mps/nodeEditor/cellMenu/NodeSubstituteChooser.java b/editor/editor-runtime/source/jetbrains/mps/nodeEditor/cellMenu/NodeSubstituteChooser.java index 1f59e65b1724..06256f1cc2f8 100644 --- a/editor/editor-runtime/source/jetbrains/mps/nodeEditor/cellMenu/NodeSubstituteChooser.java +++ b/editor/editor-runtime/source/jetbrains/mps/nodeEditor/cellMenu/NodeSubstituteChooser.java @@ -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; @@ -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; @@ -221,116 +221,125 @@ public List 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 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() { - private Map mySortPriorities = new HashMap(); - private Map myVisibleMatchingTexts = new HashMap(); - - 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 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() { + private Map mySortPriorities = new HashMap(); + private Map myVisibleMatchingTexts = new HashMap(); + + 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 matchingActions) {