performance: do not rewrite the whole file

This commit is contained in:
Alexey Kudravtsev
2013-12-26 12:55:33 +04:00
parent f3bf9d08b7
commit 56354fd643
5 changed files with 44 additions and 33 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -544,27 +544,35 @@ public class ExternalAnnotationsManagerImpl extends ReadableExternalAnnotationsM
final XmlTag rootTag = document.getRootTag();
final String externalName = getExternalName(listOwner, false);
if (rootTag != null) {
XmlTag anchor = null;
for (XmlTag item : rootTag.getSubTags()) {
if (Comparing.strEqual(StringUtil.unescapeXml(item.getAttributeValue("name")), externalName)) {
int compare = Comparing.compare(externalName, StringUtil.unescapeXml(item.getAttributeValue("name")));
if (compare == 0) {
anchor = null;
for (XmlTag annotation : item.getSubTags()) {
if (Comparing.strEqual(annotation.getAttributeValue("name"), annotationFQName)) {
compare = Comparing.compare(annotationFQName, annotation.getAttributeValue("name"));
if (compare == 0) {
annotation.delete();
break;
}
if (compare < 0) break;
anchor = annotation;
}
XmlTag newTag = XmlElementFactory.getInstance(myPsiManager.getProject()).createTagFromText(
createAnnotationTag(annotationFQName, values));
item.add(newTag);
item.addAfter(newTag, anchor);
commitChanges(xmlFile);
notifyAfterAnnotationChanging(listOwner, annotationFQName, true);
return;
}
if (compare < 0) break;
anchor = item;
}
@NonNls String text =
"<item name=\'" + StringUtil.escapeXml(externalName) + "\'>\n";
text += createAnnotationTag(annotationFQName, values);
text += "</item>";
rootTag.add(XmlElementFactory.getInstance(myPsiManager.getProject()).createTagFromText(text));
rootTag.addAfter(XmlElementFactory.getInstance(myPsiManager.getProject()).createTagFromText(text), anchor);
}
}
commitChanges(xmlFile);
@@ -604,7 +612,8 @@ public class ExternalAnnotationsManagerImpl extends ReadableExternalAnnotationsM
}
}
Collections.sort(itemTags, new Comparator<XmlTag>() {
List<XmlTag> sorted = new ArrayList<XmlTag>(itemTags);
Collections.sort(sorted, new Comparator<XmlTag>() {
@Override
public int compare(XmlTag item1, XmlTag item2) {
String externalName1 = item1.getAttributeValue("name");
@@ -613,9 +622,11 @@ public class ExternalAnnotationsManagerImpl extends ReadableExternalAnnotationsM
return externalName1.compareTo(externalName2);
}
});
for (XmlTag item : itemTags) {
rootTag.addAfter(item, null);
item.delete();
if (!sorted.equals(itemTags)) {
for (XmlTag item : sorted) {
rootTag.addAfter(item, null);
item.delete();
}
}
}
@@ -1,8 +1,8 @@
<root>
<item name='p.TestDeannotation java.lang.String get()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='p.TestDeannotation1 java.lang.String get(java.lang.String) 0'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='p.TestDeannotation java.lang.String get()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='p.TestDeannotation1 java.lang.String get(java.lang.String) 0'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
</root>
@@ -1,10 +1,10 @@
<root>
<item name='suppressed.ExistingExternalName java.lang.String foo()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='suppressed.SecondSuppression void second()'>
<annotation name='java.lang.SuppressWarnings'>
<val val="{DoesntMatterWhat}"/>
</annotation>
</item>
<item name='suppressed.ExistingExternalName java.lang.String foo()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='suppressed.SecondSuppression void second()'>
<annotation name='java.lang.SuppressWarnings'>
<val val="{DoesntMatterWhat}"/>
</annotation>
</item>
</root>
@@ -1,7 +1,7 @@
<root>
<item name='multiRoot.Test java.lang.String get(java.lang.String) 0'>
<annotation name='org.jetbrains.annotations.NotNull'>
<val name="value" val="&quot;foo&quot;"/>
</annotation>
</item>
<item name='multiRoot.Test java.lang.String get(java.lang.String) 0'>
<annotation name='org.jetbrains.annotations.NotNull'>
<val name="value" val="&quot;foo&quot;"/>
</annotation>
</item>
</root>
@@ -1,7 +1,7 @@
<root>
<item name='multiRoot.Test java.lang.String get(java.lang.String)'>
<annotation name='org.jetbrains.annotations.Nullable'>
<val name="value" val="&quot;foo&quot;"/>
</annotation>
</item>
<item name='multiRoot.Test java.lang.String get(java.lang.String)'>
<annotation name='org.jetbrains.annotations.Nullable'>
<val name="value" val="&quot;foo&quot;"/>
</annotation>
</item>
</root>