com.intellij.psi.codeStyle.CodeStyleSettings Java Examples

The following examples show how to use com.intellij.psi.codeStyle.CodeStyleSettings. 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: FormatProcessor.java    From consulo with Apache License 2.0 6 votes vote down vote up
public FormatProcessor(final FormattingDocumentModel model, Block block, FormatOptions options, @Nonnull FormattingProgressCallback callback) {
  myProgressCallback = callback;

  CommonCodeStyleSettings.IndentOptions defaultIndentOption = options.myIndentOptions;
  CodeStyleSettings settings = options.mySettings;
  BlockIndentOptions blockIndentOptions = new BlockIndentOptions(settings, defaultIndentOption, block);

  myDocument = model.getDocument();
  myReformatContext = options.isReformatWithContext();

  final InitialInfoBuilder builder = prepareToBuildBlocksSequentially(block, model, options, defaultIndentOption, myProgressCallback);
  myWrapState = new WrapBlocksState(builder, blockIndentOptions);

  FormatTextRanges ranges = options.myAffectedRanges;

  if (ranges != null && myReformatContext) {
    AdjustFormatRangesState adjustRangesState = new AdjustFormatRangesState(block, ranges, model);
    myStateProcessor = new StateProcessor(adjustRangesState);
    myStateProcessor.setNextState(myWrapState);
  }
  else {
    myStateProcessor = new StateProcessor(myWrapState);
  }
}
 
Example #2
Source File: DustFormattingModelBuilder.java    From Intellij-Dust with MIT License 6 votes vote down vote up
/**
 * We have to override {@link com.intellij.formatting.templateLanguages.TemplateLanguageFormattingModelBuilder#createModel}
 * since after we delegate to some templated languages, those languages (xml/html for sure, potentially others)
 * delegate right back to us to format the DustTypes.OUTER_TYPE token we tell them to ignore,
 * causing an stack-overflowing loop.
 */
@NotNull
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {

  final PsiFile file = element.getContainingFile();
  Block rootBlock;

  ASTNode node = element.getNode();

  if (node.getElementType() == DustFileViewProvider.OUTER_TYPE) {
    // If we're looking at a DustTypes.HTML element, then we've been invoked by our templated
    // language.  Make a dummy block to allow that formatter to continue
    return new SimpleTemplateLanguageFormattingModelBuilder().createModel(element, settings);
  } else {
    rootBlock = getRootBlock(file, file.getViewProvider(), settings);
  }

  return new DocumentBasedFormattingModel(rootBlock, element.getProject(), settings, file.getFileType(), file);
}
 
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: FormatterImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void format(final FormattingModel model, final CodeStyleSettings settings, final CommonCodeStyleSettings.IndentOptions indentOptions, final FormatTextRanges affectedRanges)
        throws IncorrectOperationException {
  try {
    validateModel(model);
    SequentialTask task = new MyFormattingTask() {
      @Nonnull
      @Override
      protected FormatProcessor buildProcessor() {
        FormatOptions options = new FormatOptions(settings, indentOptions, affectedRanges);
        FormatProcessor processor = new FormatProcessor(model.getDocumentModel(), model.getRootBlock(), options, getProgressCallback());
        processor.format(model, true);
        return processor;
      }
    };
    execute(task);
  }
  catch (FormattingModelInconsistencyException e) {
    LOG.error(e);
  }
}
 
Example #5
Source File: FormatterTestCase.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected void defaultSettings() {
  CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());

  settings.ALIGN_MULTILINE_PARAMETERS = true;
  settings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS = false;
  settings.ALIGN_MULTILINE_FOR = true;

  settings.ALIGN_MULTILINE_BINARY_OPERATION = false;
  settings.ALIGN_MULTILINE_TERNARY_OPERATION = false;
  settings.ALIGN_MULTILINE_THROWS_LIST = false;
  settings.ALIGN_MULTILINE_EXTENDS_LIST = false;
  settings.ALIGN_MULTILINE_PARENTHESIZED_EXPRESSION = false;
  settings.DO_NOT_INDENT_TOP_LEVEL_CLASS_MEMBERS = false;

  getSettings().SPACE_BEFORE_ANOTATION_PARAMETER_LIST = false;
  getSettings().SPACE_AROUND_ASSIGNMENT_OPERATORS = true;
  getSettings().SPACE_WITHIN_ANNOTATION_PARENTHESES = false;
  getSettings().SPACE_AROUND_ASSIGNMENT_OPERATORS = true;
}
 
Example #6
Source File: FormattingModelBuilder.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Create spacing builder using given settings.
 */
public static SpacingBuilder createSpacingBuilder(CodeStyleSettings settings) {
    CommonCodeStyleSettings protoSettings = settings.getCommonSettings(ProtoLanguage.INSTANCE);
    SpacingBuilder builder = new SpacingBuilder(settings, ProtoLanguage.INSTANCE);
    builder.around(ASSIGN).spacing(1, 1, 0, false, 0);
    builder.before(SEMICOLON).spacing(0, 0, 0, false, 0);
    builder.after(LINE_COMMENT).spacing(0, 0, 1, true, 2);
    builder.after(LCURLY).spacing(0, 0, 1, true, 2);
    builder.before(RCURLY).spacing(0, 0, 1, true, 2);
    builder.after(LPAREN).spacing(0, 0, 0, false, 0);
    builder.before(RPAREN).spacing(0, 0, 0, false, 0);
    builder.after(LSQUARE).spacing(0, 0, 0, false, 0);
    builder.before(RSQUARE).spacing(0, 0, 0, false, 0);
    builder.before(LT).spacing(0, 0, 0, false, 0);
    builder.after(LT).spacing(0, 0, 0, false, 0);
    builder.before(GT).spacing(0, 0, 0, false, 0);
    builder.before(COMMA).spacing(0, 0, 0, false, 0);
    builder.before(SEMICOLON).spacing(0, 0, 0, false, 0);
    builder.after(COMMA).spacing(1, 1, 0, false, 0);
    return builder;
}
 
Example #7
Source File: CypherBlock.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
CypherBlock(@NotNull ASTNode node,
            @Nullable Alignment alignment,
            @Nullable Indent indent,
            @Nullable Wrap wrap,
            @NotNull CodeStyleSettings settings,
            @NotNull SpacingBuilder spacingBuilder) {
    this.node = node;
    this.codeStyleSettings = settings;
    this.wrap = wrap;
    this.indent = indent;
    this.alignment = alignment;
    this.spacingBuilder = spacingBuilder;
}
 
Example #8
Source File: CsvFormatHelper.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
public static Map<Integer, CsvColumnInfo<ASTNode>> createColumnInfoMap(ASTNode root, CodeStyleSettings settings) {
    Map<Integer, CsvColumnInfo<ASTNode>> columnInfoMap = new HashMap<>();
    ASTNode child = root.getFirstChildNode();
    int row = 0;
    while (child != null) {
        if (child.getElementType() == CsvTypes.RECORD) {
            Integer column = 0;
            ASTNode subChild = child.getFirstChildNode();
            while (subChild != null) {
                if (subChild.getElementType() == CsvTypes.FIELD) {
                    int length = getTextLength(subChild, settings);
                    if (!columnInfoMap.containsKey(column)) {
                        columnInfoMap.put(column, new CsvColumnInfo(column, length, row));
                    } else if (columnInfoMap.get(column).getMaxLength() < length) {
                        columnInfoMap.get(column).setMaxLength(length, row);
                    }
                    columnInfoMap.get(column).addElement(subChild);
                    ++column;
                }
                subChild = subChild.getTreeNext();
            }
            ++row;
        }
        child = child.getTreeNext();
    }
    return columnInfoMap;
}
 
Example #9
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 #10
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 #11
Source File: OptionTableWithPreviewPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void resetImpl(final CodeStyleSettings settings) {
  TreeModel treeModel = myTreeTable.getTree().getModel();
  TreeNode root = (TreeNode)treeModel.getRoot();
  resetNode(root, settings);
  ((DefaultTreeModel)treeModel).nodeChanged(root);
}
 
Example #12
Source File: CppFormattingModelBuilder.java    From CppTools with Apache License 2.0 5 votes vote down vote up
@NotNull
public FormattingModel createModel(final PsiElement psiElement, final CodeStyleSettings codeStyleSettings) {
  return new FormattingModel() {
    private FormattingModel myModel;

    {
      myModel = FormattingModelProvider.createFormattingModelForPsiFile(psiElement.getContainingFile(), new CppBlock(psiElement), codeStyleSettings);
    }

    @NotNull
    public Block getRootBlock() {
      return myModel.getRootBlock();
    }

    @NotNull
    public FormattingDocumentModel getDocumentModel() {
      return myModel.getDocumentModel();
    }

    public TextRange replaceWhiteSpace(TextRange textRange, String whiteSpace) {
      return myModel.replaceWhiteSpace(textRange, whiteSpace);
    }

    public TextRange shiftIndentInsideRange(TextRange range, int indent) {
      return myModel.shiftIndentInsideRange(range, indent);
    }

    public void commitChanges() {
      myModel.commitChanges();
    }
  };
}
 
Example #13
Source File: TemplateLanguageBlock.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected TemplateLanguageBlock(@Nonnull ASTNode node, @Nullable Wrap wrap, @Nullable Alignment alignment,
                                @Nonnull TemplateLanguageBlockFactory blockFactory,
                                @Nonnull CodeStyleSettings settings,
                                @Nullable List<DataLanguageBlockWrapper> foreignChildren) {
  super(node, wrap, alignment);
  myBlockFactory = blockFactory;
  myForeignChildren = foreignChildren;
  mySettings = settings;
}
 
Example #14
Source File: AbstractWhiteSpaceFormattingStrategy.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public CharSequence adjustWhiteSpaceIfNecessary(@Nonnull CharSequence whiteSpaceText,
                                                @Nonnull CharSequence text,
                                                int startOffset,
                                                int endOffset, CodeStyleSettings codeStyleSettings, ASTNode nodeAfter)
{
  // Does nothing
  return whiteSpaceText;
}
 
Example #15
Source File: CoreFormatterUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static FormattingModel buildModel(@Nonnull FormattingModelBuilder builder,
                                         @Nonnull PsiElement element,
                                         @Nonnull TextRange range,
                                         @Nonnull CodeStyleSettings settings,
                                         @Nonnull FormattingMode mode) {
  if (builder instanceof FormattingModelBuilderEx) {
    return ((FormattingModelBuilderEx)builder).createModel(element, range, settings, mode);
  }
  else {
    return builder.createModel(element, settings);
  }
}
 
Example #16
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 #17
Source File: CodeStyleBlankLinesPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private int getFieldValue(CodeStyleSettings settings) {
  try {
    if (myTargetClass != null) {
      return myTarget.getInt(settings.getCustomSettings(myTargetClass));
    }
    CommonCodeStyleSettings commonSettings = settings.getCommonSettings(getDefaultLanguage());
    return myTarget.getInt(commonSettings);
  }
  catch (IllegalAccessException e) {
    throw new RuntimeException(e);
  }
}
 
Example #18
Source File: SoyBlock.java    From bamboo-soy with Apache License 2.0 5 votes vote down vote up
public SoyBlock(
    @NotNull TemplateLanguageBlockFactory blockFactory,
    @NotNull CodeStyleSettings settings,
    @NotNull ASTNode node,
    @Nullable List<DataLanguageBlockWrapper> foreignChildren,
    HtmlPolicy htmlPolicy) {
  super(blockFactory, settings, node, foreignChildren);
  myHtmlPolicy = htmlPolicy;
}
 
Example #19
Source File: MemberChooser.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void customizeOptionsPanel() {
  if (myInsertOverrideAnnotationCheckbox != null && myIsInsertOverrideVisible) {
    CodeStyleSettings styleSettings = CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings();
    myInsertOverrideAnnotationCheckbox.setSelected(styleSettings.INSERT_OVERRIDE_ANNOTATION);
  }
  if (myCopyJavadocCheckbox != null) {
    myCopyJavadocCheckbox.setSelected(PropertiesComponent.getInstance().isTrueValue(PROP_COPYJAVADOC));
  }
}
 
Example #20
Source File: OptionTableWithPreviewPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void setValue(Object value, CodeStyleSettings settings) {
  //noinspection EmptyCatchBlock
  try {
    if (value instanceof Integer) {
      field.setInt(getSettings(settings), ((Integer)value).intValue());
    }
    else {
      field.setInt(getSettings(settings), myDefaultValue);
    }
  }
  catch (IllegalAccessException e) {
  }
}
 
Example #21
Source File: OptionTreeWithPreviewPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean isToggleNodeModified(MyToggleTreeNode childNode, final CodeStyleSettings settings) {
  try {
    BooleanOptionKey key = (BooleanOptionKey)childNode.getKey();
    return childNode.isSelected() != key.getValue(settings);
  }
  catch (IllegalArgumentException | IllegalAccessException e) {
    LOG.error(e);
  }
  return false;
}
 
Example #22
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 #23
Source File: ExcludedFileFormattingRestriction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isFormatterAllowed(@Nonnull PsiElement context) {
  try {
    PsiFile file = context.getContainingFile();
    CodeStyleSettings settings = CodeStyle.getSettings(file);
    return !settings.getExcludedFiles().contains(file);
  }
  catch (PsiInvalidElementAccessException e) {
    return false;
  }
}
 
Example #24
Source File: OptionTableWithPreviewPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void setValue(Object value, CodeStyleSettings settings) {
  try {
    for (int i = 0; i < values.length; i++) {
      if (options[i].equals(value)) {
        field.setInt(getSettings(settings), values[i]);
        return;
      }
    }
    LOG.error("Invalid option value " + value + " for " + field.getName());
  }
  catch (IllegalAccessException ignore) {
  }
}
 
Example #25
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 #26
Source File: HaxeCodeInsightFixtureTestCase.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
public void setTestStyleSettings(int indent) {
  Project project = getProject();
  CodeStyleSettings currSettings = CodeStyleSettingsManager.getSettings(project);
  assertNotNull(currSettings);
  CodeStyleSettings tempSettings = currSettings.clone();
  CodeStyleSettings.IndentOptions indentOptions = tempSettings.getIndentOptions(HaxeFileType.HAXE_FILE_TYPE);
  indentOptions.INDENT_SIZE = indent;
  assertNotNull(indentOptions);
  CodeStyleSettingsManager.getInstance(project).setTemporarySettings(tempSettings);
}
 
Example #27
Source File: OptionTreeWithPreviewPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void resetMyTreeNode(MyToggleTreeNode childNode, final CodeStyleSettings settings) {
  try {
    BooleanOptionKey key = (BooleanOptionKey)childNode.getKey();
    childNode.setSelected(key.getValue(settings));
    childNode.setEnabled(key.isEnabled());
  }
  catch (IllegalArgumentException | IllegalAccessException e) {
    LOG.error(e);
  }
}
 
Example #28
Source File: OptionTableWithPreviewPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Object getValue(CodeStyleSettings settings) {
  try {
    int value = field.getInt(getSettings(settings));
    for (int i = 0; i < values.length; i++) {
      if (values[i] == value) return options[i];
    }
    LOG.error("Invalid option value " + value + " for " + field.getName());
  }
  catch (IllegalAccessException ignore) {
  }
  return null;
}
 
Example #29
Source File: IdeHelper.java    From yiistorm with MIT License 5 votes vote down vote up
public static XmlCodeStyleSettings getXmlSettings(Project project) {
    CodeStyleSettings settings = getSettings(project);
    if (settings != null) {
        return settings.getCustomSettings(XmlCodeStyleSettings.class);
    }
    return null;
}
 
Example #30
Source File: GLSLFormattingModelBuilder.java    From glsl4idea with GNU Lesser General Public License v3.0 5 votes vote down vote up
@NotNull
@Override
public FormattingModel createModel(PsiElement psiElement, CodeStyleSettings codeStyleSettings) {
    return FormattingModelProvider.createFormattingModelForPsiFile(
            psiElement.getContainingFile(),
            new GLSLFormattingBlock(psiElement.getNode(), null, null, createSpacingBuilder(codeStyleSettings)),
            codeStyleSettings);
}