下面列出了怎么用com.intellij.psi.formatter.FormattingDocumentModelImpl的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public TemplateLanguageBlock createTemplateLanguageBlock(
@NotNull ASTNode node,
@Nullable Wrap wrap,
@Nullable Alignment alignment,
@Nullable List<DataLanguageBlockWrapper> foreignChildren,
@NotNull CodeStyleSettings codeStyleSettings) {
final FormattingDocumentModelImpl documentModel =
FormattingDocumentModelImpl.createOn(node.getPsi().getContainingFile());
if (node.getPsi() instanceof TagElement) {
return new SoyTagBlock(
this,
codeStyleSettings,
node,
foreignChildren,
new HtmlPolicy(codeStyleSettings, documentModel));
} else if(node.getPsi() instanceof TagBlockElement) {
return new SoyTagBlockBlock(
this,
codeStyleSettings,
node,
foreignChildren,
new HtmlPolicy(codeStyleSettings, documentModel));
} else if (node.getPsi() instanceof SoyStatementList) {
return new SoyStatementListBlock(
this,
codeStyleSettings,
node,
foreignChildren,
new HtmlPolicy(codeStyleSettings, documentModel));
} else {
return new SoyBlock(
this,
codeStyleSettings,
node,
foreignChildren,
new HtmlPolicy(codeStyleSettings, documentModel));
}
}
@Nonnull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings)
{
final PsiFile file = element.getContainingFile();
FormattingDocumentModelImpl model = FormattingDocumentModelImpl.createOn(element.getContainingFile());
Block rootBlock = new CSharpFormattingBlock(file.getNode(), null, null, settings);
return new PsiBasedFormattingModel(file, rootBlock, model);
}
@Override
public FormattingModel createFormattingModelForPsiFile(@Nonnull final PsiFile file, @Nonnull final Block rootBlock, final CodeStyleSettings settings) {
return new PsiBasedFormattingModel(file, rootBlock, FormattingDocumentModelImpl.createOn(file));
}
public void assertInvalidRanges(final int startOffset, final int newEndOffset, FormattingDocumentModel model, String message) {
final StringBuilder buffer = new StringBuilder();
int minOffset = Math.max(Math.min(startOffset, newEndOffset), 0);
int maxOffset = Math.min(Math.max(startOffset, newEndOffset), model.getTextLength());
final StringBuilder messageBuffer = new StringBuilder();
messageBuffer.append(message);
Class<?> problematicLanguageClass;
if (model instanceof FormattingDocumentModelImpl) {
Language language = ((FormattingDocumentModelImpl)model).getFile().getLanguage();
messageBuffer.append(" in #").append(language.getDisplayName());
problematicLanguageClass = language.getClass();
}
else {
problematicLanguageClass = null;
}
messageBuffer.append(" #formatter");
messageBuffer.append("\nRange: [").append(startOffset).append(",").append(newEndOffset).append("], ").append("text fragment: [").append(minOffset).append(",").append(maxOffset).append("]\n");
buffer.append("Fragment text: '").append(model.getText(new TextRange(minOffset, maxOffset))).append("'\n");
buffer.append("File text:(").append(model.getTextLength()).append(")\n'");
buffer.append(model.getText(new TextRange(0, model.getTextLength())).toString());
buffer.append("'\n");
buffer.append("model (").append(model.getClass()).append("): ").append(model);
if (model instanceof FormattingDocumentModelImpl) {
final FormattingDocumentModelImpl modelImpl = (FormattingDocumentModelImpl)model;
buffer.append("Psi Tree:\n");
final PsiFile file = modelImpl.getFile();
final List<PsiFile> roots = file.getViewProvider().getAllFiles();
for (PsiFile root : roots) {
buffer.append("Root ");
DebugUtil.treeToBuffer(buffer, root.getNode(), 0, false, true, true, true);
}
buffer.append('\n');
}
LogMessageEx.error(LOG, messageBuffer.toString(), new Throwable(), buffer.toString());
}
/**
* Allows to check if {@code 'myInitial'} property value stands for continuous white space text.
* <p/>
* The text is considered to be continuous {@code 'white space'} at following cases:
* <ul>
* <li>{@code 'myInitial'} is empty string or string that contains white spaces only;</li>
* <li>{@code 'myInitial'} is a {@code CDATA} string which content is empty or consists from white spaces only;</li>
* <li>{@code 'myInitial'} string belongs to the same {@link PsiWhiteSpace} element;</li>
* </ul>
*
* @param model formatting model that is used to provide access to the {@code PSI API} if necessary
* @return {@code true} if {@code 'myInitial'} property value stands for white space;
* {@code false} otherwise
*/
private boolean coveredByBlock(final FormattingDocumentModel model) {
if (myInitial == null) return true;
if (model.containsWhiteSpaceSymbolsOnly(myStart, myEnd)) return true;
if (!(model instanceof FormattingDocumentModelImpl)) return false;
PsiFile psiFile = ((FormattingDocumentModelImpl)model).getFile();
if (psiFile == null) return false;
PsiElement start = psiFile.findElementAt(myStart);
PsiElement end = psiFile.findElementAt(myEnd-1);
return start == end && start instanceof PsiWhiteSpace; // there maybe non-white text inside CDATA-encoded injected elements
}