Java Code Examples for com.intellij.formatting.FormattingModelBuilder#createModel()

The following examples show how to use com.intellij.formatting.FormattingModelBuilder#createModel() . 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: 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 2
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 3
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 4
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();
}