IDEA-21113: Formatting for auto-generated CDATA section in mx:Script blocks

This commit is contained in:
Rustam.Vishnyakov
2010-08-09 13:02:36 +04:00
parent e1afb2e2d9
commit 18700e5963
3 changed files with 23 additions and 8 deletions
@@ -22,10 +22,12 @@ import com.intellij.lang.LanguageFormatting;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiLanguageInjectionHost;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.impl.source.tree.FileElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -105,6 +107,17 @@ public abstract class InjectedLanguageBlockBuilder {
public void addInjectedLanguageBlockWrapper(final List<Block> result, final ASTNode injectedNode,
final Indent indent, int offset, @Nullable TextRange range) {
//
// Do not create a block for an empty range
//
if (range != null) {
if (range.getLength() == 0) return;
if(StringUtil.isEmptyOrSpaces(range.substring(injectedNode.getText()))) {
return;
}
}
final PsiElement childPsi = injectedNode.getPsi();
final Language childLanguage = childPsi.getLanguage();
final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(childLanguage, childPsi);
@@ -26,6 +26,7 @@ import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.html.HtmlTag;
@@ -216,35 +217,36 @@ public class XmlGtTypedHandler extends TypedHandlerDelegate {
if (hasBalance) return Result.CONTINUE;
}
boolean insertedCData = false;
TextRange cdataReformatRange = null;
final XmlElementDescriptor descriptor = tag.getDescriptor();
if (descriptor instanceof XmlElementDescriptorWithCDataContent) {
final XmlElementDescriptorWithCDataContent cDataContainer = (XmlElementDescriptorWithCDataContent)descriptor;
if (cDataContainer.requiresCdataBracesInContext(tag)) {
@NonNls final String cDataStart = "><![CDATA[\n";
int rangeStart = offset;
@NonNls final String cDataStart = "><![CDATA[";
final String inserted = cDataStart + "\n]]>";
editor.getDocument().insertString(offset, inserted);
final int newoffset = offset + cDataStart.length();
editor.getCaretModel().moveToOffset(newoffset);
offset += inserted.length();
insertedCData = true;
cdataReformatRange = new TextRange(rangeStart, offset + 1);
}
}
editor.getDocument().insertString(offset, "</" + name + ">");
if (insertedCData) {
if (cdataReformatRange != null) {
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
try {
CodeStyleManager.getInstance(project).adjustLineIndent(file, editor.getCaretModel().getOffset());
try {
CodeStyleManager.getInstance(project).reformatText(file, cdataReformatRange.getStartOffset(), cdataReformatRange.getEndOffset());
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
}
return insertedCData ? Result.STOP : Result.CONTINUE;
return cdataReformatRange != null ? Result.STOP : Result.CONTINUE;
}
return Result.CONTINUE;
}
@@ -67,7 +67,7 @@ public class SyntheticBlock extends AbstractSyntheticBlock implements Block, Rea
return Spacing.getReadOnlySpacing();
}
if (type1 == XmlElementType.XML_CDATA_START && type2 == XmlElementType.XML_CDATA_END) {
return Spacing.createSpacing(0, 0, 0, myXmlFormattingPolicy.getShouldKeepLineBreaks(), 0);
return Spacing.createSpacing(0, 0, 0, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
}
if (type1 == XmlElementType.XML_CDATA_START && child2 instanceof AnotherLanguageBlockWrapper ||
type2 == XmlElementType.XML_CDATA_END && child1 instanceof AnotherLanguageBlockWrapper) {