com.intellij.psi.formatter.FormattingDocumentModelImpl Java Examples

The following examples show how to use com.intellij.psi.formatter.FormattingDocumentModelImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SoyFormattingModelBuilder.java    From bamboo-soy with Apache License 2.0 5 votes vote down vote up
@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));
  }
}
 
Example #2
Source File: CSharpFormattingModelBuilder.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #3
Source File: FormatterImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public FormattingModel createFormattingModelForPsiFile(@Nonnull final PsiFile file, @Nonnull final Block rootBlock, final CodeStyleSettings settings) {
  return new PsiBasedFormattingModel(file, rootBlock, FormattingDocumentModelImpl.createOn(file));
}
 
Example #4
Source File: RangesAssert.java    From consulo with Apache License 2.0 4 votes vote down vote up
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());
}
 
Example #5
Source File: WhiteSpace.java    From consulo with Apache License 2.0 3 votes vote down vote up
/**
 * 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
}