com.intellij.formatting.FormattingModel Java Examples

The following examples show how to use com.intellij.formatting.FormattingModel. 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: CsvFormattingModelBuilder.java    From intellij-csv-validator with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
    ASTNode root = CsvFormatHelper.getRoot(element.getNode());
    CsvFormattingInfo formattingInfo = new CsvFormattingInfo(
            settings,
            CsvFormatHelper.createSpaceBuilder(settings),
            CsvFormatHelper.createColumnInfoMap(root, settings)
    );

    return FormattingModelProvider.createFormattingModelForPsiFile(
            element.getContainingFile(),
            new CsvBlock(root, formattingInfo),
            settings
    );
}
 
Example #2
Source File: GraphQLInjectedFormattingModelBuilder.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
    if(element instanceof JSFile || element.getContainingFile() instanceof JSFile) {
        final JSFile file = (JSFile)(element instanceof JSFile ? element : element.getContainingFile());
        file.putUserData(WANT_DEFAULT_FORMATTER_KEY, true);
        try {
            final FormattingModelBuilder formattingModelBuilder = LanguageFormatting.INSTANCE.forContext(file.getLanguage(), element);
            if (formattingModelBuilder != null) {
                final FormattingModel model = formattingModelBuilder.createModel(element, settings);
                final Block rootBlock = model.getRootBlock();
                return new DelegatingFormattingModel(model, new GraphQLBlockWrapper(rootBlock, null, element.getNode(), rootBlock.getWrap(), rootBlock.getAlignment(), createSpaceBuilder(settings, element), settings));
            }
        } finally {
            file.putUserData(WANT_DEFAULT_FORMATTER_KEY, null);
        }
    }
    throw new IllegalArgumentException("Unsupported element '" + element + "'. It must be an element in a JSFile with its own default formatter to support injected GraphQL formatting");
}
 
Example #3
Source File: BashFormattingModelBuilder.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@NotNull
public FormattingModel createModel(final PsiElement element, final CodeStyleSettings settings) {
    ASTNode node = element.getNode();
    assert node != null;

    PsiFile containingFile = element.getContainingFile();
    ASTNode astNode = containingFile.getNode();
    assert astNode != null;

    BashProjectSettings projectSettings = BashProjectSettings.storedSettings(containingFile.getProject());
    if (!projectSettings.isFormatterEnabled()) {
        return FormattingModelProvider.createFormattingModelForPsiFile(containingFile,
                new NoOpBlock(astNode), settings);
    }

    return FormattingModelProvider.createFormattingModelForPsiFile(containingFile,
            new BashBlock(astNode, null, Indent.getAbsoluteNoneIndent(), null, settings), settings);
}
 
Example #4
Source File: JoinLinesHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
private int[] getSpacesToAdd(List<RangeMarker> markers) {
  int size = markers.size();
  int[] spacesToAdd = new int[size];
  Arrays.fill(spacesToAdd, -1);
  CharSequence text = myDoc.getCharsSequence();
  FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(myFile);
  CodeStyleSettings settings = CodeStyle.getSettings(myFile);
  FormattingModel model = builder == null ? null : builder.createModel(myFile, settings);
  FormatterEx formatter = FormatterEx.getInstance();
  for (int i = 0; i < size; i++) {
    myIndicator.checkCanceled();
    myIndicator.setFraction(0.7 + 0.25 * i / size);
    RangeMarker marker = markers.get(i);
    if (!marker.isValid()) continue;
    int end = StringUtil.skipWhitespaceForward(text, marker.getStartOffset());
    int spacesToCreate = end >= text.length() || text.charAt(end) == '\n' ? 0 : model == null ? 1 : formatter.getSpacingForBlockAtOffset(model, end);
    spacesToAdd[i] = spacesToCreate < 0 ? 1 : spacesToCreate;
  }
  return spacesToAdd;
}
 
Example #5
Source File: SoyFormattingModelBuilder.java    From bamboo-soy with Apache License 2.0 5 votes vote down vote up
@NotNull
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
  final PsiFile file = element.getContainingFile();

  if (element.getNode().getElementType() == SoyTypes.OTHER) {
    return new SimpleTemplateLanguageFormattingModelBuilder().createModel(element, settings);
  } else {
    return new DocumentBasedFormattingModel(
        getRootBlock(file, file.getViewProvider(), settings),
        element.getProject(),
        settings,
        file.getFileType(),
        file);
  }
}
 
Example #6
Source File: FormattingModelBuilder.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
    PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(ProtoLanguage.INSTANCE);
    ASTNode fileNode = containingFile.getNode();
    Wrap wrap = Wrap.createWrap(WrapType.NONE, false);
    Alignment alignment = Alignment.createAlignment();
    ProtoFileBlock block = new ProtoFileBlock(fileNode, wrap, alignment, settings);
    return FormattingModelProvider.createFormattingModelForPsiFile(containingFile, block, settings);
}
 
Example #7
Source File: CypherFormattingModelBuilder.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
    CypherBlock block = new CypherBlock(element.getNode(), Alignment.createAlignment(),
            Indent.getNoneIndent(), Wrap.createWrap(WrapType.NONE, false),
            settings,
            CypherFormattingModelBuilder.createSpacingBuilder(settings)
    );
    return FormattingModelProvider.createFormattingModelForPsiFile(element.getContainingFile(), block, settings);
}
 
Example #8
Source File: JSGraphQLEndpointFormattingModelBuilder.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
    return FormattingModelProvider.createFormattingModelForPsiFile(
            element.getContainingFile(),
            new JSGraphQLEndpointBlock(null, element.getNode(), null, null, createSpaceBuilder(settings)),
            settings);
}
 
Example #9
Source File: BuckFormattingModelBuilder.java    From Buck-IntelliJ-Plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(@NotNull PsiElement element,
                                   @NotNull CodeStyleSettings settings,
                                   @NotNull FormattingMode mode) {
  final BuckBlock block =
      new BuckBlock(null, element.getNode(), settings, null, Indent.getNoneIndent(), null);
  return FormattingModelProvider.createFormattingModelForPsiFile(
      element.getContainingFile(),
      block,
      settings);
}
 
Example #10
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 #11
Source File: HaxeFormattingModelBuilder.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
  return new HaxeFormattingModel(
    element.getContainingFile(),
    settings,
    new HaxeBlock(element.getNode(), null, null, settings)
  );
}
 
Example #12
Source File: XQueryFormattingModelBuilder.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
    CommonCodeStyleSettings commonSettings = settings.getCommonSettings(XQueryLanguage.INSTANCE);
    XQueryCodeStyleSettings xQuerySettings = settings.getCustomSettings(XQueryCodeStyleSettings.class);
    final XQueryFormattingBlock block = new XQueryFormattingBlock(element.getNode(), null, null, commonSettings,
            createSpacingBuilder(commonSettings, xQuerySettings, settings));
    FormattingModel result = createFormattingModelForPsiFile(element.getContainingFile(), block, settings);
    return result;
}
 
Example #13
Source File: PsiViewerDialog.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Block buildBlocks(@Nonnull PsiElement rootElement) {
  FormattingModelBuilder formattingModelBuilder = LanguageFormatting.INSTANCE.forContext(rootElement);
  CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(rootElement.getProject());
  if (formattingModelBuilder != null) {
    FormattingModel formattingModel = formattingModelBuilder.createModel(rootElement, settings);
    return formattingModel.getRootBlock();
  }
  else {
    return null;
  }
}
 
Example #14
Source File: DocumentBasedFormattingModel.java    From consulo with Apache License 2.0 5 votes vote down vote up
public DocumentBasedFormattingModel(@Nonnull final FormattingModel originalModel,
                                    @Nonnull final Document document,
                                    final Project project,
                                    final CodeStyleSettings settings,
                                    final FileType fileType,
                                    final PsiFile file) {
  myOriginalFormattingModel = originalModel;
  myRootBlock = originalModel.getRootBlock();
  myDocument = document;
  myProject = project;
  mySettings = settings;
  myFileType = fileType;
  myFile = file;
  myDocumentModel = new FormattingDocumentModelImpl(document,file);
}
 
Example #15
Source File: IndentOptionsDetectorImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private List<LineIndentInfo> calcLineIndentInfo() {
  if (myDocument == null) return null;
  CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(myProject);
  FormattingModelBuilder modelBuilder = LanguageFormatting.INSTANCE.forContext(myFile);
  if (modelBuilder == null) return null;

  FormattingModel model = modelBuilder.createModel(myFile, settings);
  Block rootBlock = model.getRootBlock();
  return new FormatterBasedLineIndentInfoBuilder(myDocument, rootBlock).build();
}
 
Example #16
Source File: BuckFormattingModelBuilder.java    From Buck-IntelliJ-Plugin with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
  return createModel(element, settings, FormattingMode.REFORMAT);
}