several quickedit (sql in xml) exception fixes

This commit is contained in:
Gregory Shrago
2010-07-12 14:34:44 +04:00
parent 708a7973ea
commit 613205a804
3 changed files with 40 additions and 15 deletions
@@ -148,8 +148,8 @@ public class ContainerUtil {
return hashMap;
}
public static <K, V> HashMap<K, Set<V>> classify(Iterator<V> iterator, Convertor<V, K> keyConvertor) {
HashMap<K, Set<V>> hashMap = new HashMap<K, Set<V>>();
public static <K, V> Map<K, Set<V>> classify(Iterator<V> iterator, Convertor<V, K> keyConvertor) {
Map<K, Set<V>> hashMap = new LinkedHashMap<K, Set<V>>();
while (iterator.hasNext()) {
V value = iterator.next();
final K key = keyConvertor.convert(value);
@@ -45,6 +45,7 @@ import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.intellij.psi.impl.source.resolve.FileContextUtil;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import com.intellij.psi.impl.source.tree.injected.Place;
@@ -292,7 +293,12 @@ public class QuickEditAction implements IntentionAction {
new WriteCommandAction.Simple(myProject, origFile) {
@Override
protected void run() throws Throwable {
commitToOriginalInner();
PostprocessReformattingAspect.getInstance(myProject).disablePostprocessFormattingInside(new Runnable() {
@Override
public void run() {
commitToOriginalInner();
}
});
}
}.execute();
}
@@ -307,11 +313,14 @@ public class QuickEditAction implements IntentionAction {
.classify(myMarkers.entrySet().iterator(),
new Convertor<Map.Entry<SmartPsiElementPointer, Pair<RangeMarker, RangeMarker>>, PsiLanguageInjectionHost>() {
public PsiLanguageInjectionHost convert(final Map.Entry<SmartPsiElementPointer, Pair<RangeMarker, RangeMarker>> o) {
return (PsiLanguageInjectionHost)o.getKey().getElement();
final PsiElement element = o.getKey().getElement();
return (PsiLanguageInjectionHost)element;
}
});
PsiDocumentManager.getInstance(myProject).commitDocument(myOrigDocument);
int localInsideFileCursor = 0;
for (PsiLanguageInjectionHost host : map.keySet()) {
if (host == null) continue;
final String hostText = host.getText();
TextRange insideHost = null;
final StringBuilder sb = new StringBuilder();
@@ -321,12 +330,13 @@ public class QuickEditAction implements IntentionAction {
final TextRange localInsideHost =
new TextRange(origMarker.getStartOffset() - hostOffset, origMarker.getEndOffset() - hostOffset);
final RangeMarker rangeMarker = entry.getValue().second;
final TextRange localInsideFile = new TextRange(rangeMarker.getStartOffset(), rangeMarker.getEndOffset());
final TextRange localInsideFile = new TextRange(Math.max(localInsideFileCursor, rangeMarker.getStartOffset()), rangeMarker.getEndOffset());
if (insideHost != null) {
//append unchanged inter-markers fragment
sb.append(hostText.substring(insideHost.getEndOffset(), localInsideHost.getStartOffset()));
}
sb.append(localInsideFile.getEndOffset() <= text.length()? localInsideFile.substring(text) : "");
sb.append(localInsideFile.getEndOffset() <= text.length() && !localInsideFile.isEmpty()? localInsideFile.substring(text) : "");
localInsideFileCursor = localInsideFile.getEndOffset();
insideHost = insideHost == null ? localInsideHost : insideHost.union(localInsideHost);
}
assert insideHost != null;
@@ -16,8 +16,10 @@
package com.intellij.psi.impl.source.resolve.reference.impl.manipulators;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.AbstractElementManipulator;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlText;
import com.intellij.util.IncorrectOperationException;
@@ -27,14 +29,27 @@ import com.intellij.util.IncorrectOperationException;
public class XmlTextManipulator extends AbstractElementManipulator<XmlText> {
public XmlText handleContentChange(XmlText text, TextRange range, String newContent) throws IncorrectOperationException {
final StringBuilder replacement = new StringBuilder(text.getValue());
replacement.replace(
range.getStartOffset(),
range.getEndOffset(),
newContent
);
text.setValue(replacement.toString());
final String newValue;
final String value = text.getValue();
if (range.equals(getRangeInElement(text))) {
newValue = newContent;
}
else {
final StringBuilder replacement = new StringBuilder(value);
replacement.replace(
range.getStartOffset(),
range.getEndOffset(),
newContent
);
newValue = replacement.toString();
}
if (Comparing.equal(value, newValue)) return text;
if (newValue.length() > 0) {
text.setValue(newValue);
}
else {
text.deleteChildRange(text.getFirstChild(), text.getLastChild());
}
return text;
}