com.intellij.formatting.FormattingMode Java Examples

The following examples show how to use com.intellij.formatting.FormattingMode. 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: ManualCodeStyleManagerDelegator.java    From EclipseCodeFormatter with Apache License 2.0 5 votes vote down vote up
@Override
public int adjustLineIndent(@NotNull Document document, int offset, FormattingMode formattingMode) {
	if (original instanceof FormattingModeAwareIndentAdjuster) {
		return ((FormattingModeAwareIndentAdjuster) original).adjustLineIndent(document, offset, formattingMode);
	} else {
		return offset;
	}
}
 
Example #2
Source File: FormatterBasedLineIndentProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public String getLineIndent(@Nonnull Project project, @Nonnull Editor editor, Language language, int offset) {
  Document document = editor.getDocument();
  final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
  documentManager.commitDocument(document);
  PsiFile file = documentManager.getPsiFile(document);
  if (file == null) return "";
  return CodeStyleManager.getInstance(project).getLineIndent(file, offset, FormattingMode.ADJUST_INDENT_ON_ENTER);
}
 
Example #3
Source File: FormatterBasedIndentAdjuster.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  int lineStart = myDocument.getLineStartOffset(myLine);
  CommandProcessor.getInstance().runUndoTransparentAction(() -> ApplicationManager.getApplication().runWriteAction(() -> {
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myProject);
    if (codeStyleManager instanceof FormattingModeAwareIndentAdjuster) {
      ((FormattingModeAwareIndentAdjuster)codeStyleManager).adjustLineIndent(myDocument, lineStart, FormattingMode.ADJUST_INDENT_ON_ENTER);
    }
  }));
}
 
Example #4
Source File: CodeStyleManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the current formatting mode.
 *
 * @param project The current project used to obtain {@code CodeStyleManager} instance.
 * @return The current formatting mode.
 * @see FormattingMode
 */
public static FormattingMode getCurrentFormattingMode(@Nonnull Project project) {
  if (!project.isDisposed()) {
    CodeStyleManager instance = getInstance(project);
    if (instance instanceof FormattingModeAwareIndentAdjuster) {
      return ((FormattingModeAwareIndentAdjuster)instance).getCurrentFormattingMode();
    }
  }
  return FormattingMode.REFORMAT;
}
 
Example #5
Source File: ManualCodeStyleManagerDelegator.java    From EclipseCodeFormatter with Apache License 2.0 5 votes vote down vote up
@Override
public FormattingMode getCurrentFormattingMode() {
	if (original instanceof FormattingModeAwareIndentAdjuster) {
		return ((FormattingModeAwareIndentAdjuster) original).getCurrentFormattingMode();
	} else {
		return FormattingMode.REFORMAT;
	}
}
 
Example #6
Source File: DelegatingCodeStyleManager.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
@Override
public int adjustLineIndent(Document document, int offset, FormattingMode mode) {
	if (this.delegate instanceof FormattingModeAwareIndentAdjuster) {
		return ((FormattingModeAwareIndentAdjuster) this.delegate).adjustLineIndent(document, offset, mode);
	}
	return offset;
}
 
Example #7
Source File: CodeStyleManagerDecorator.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
@Override
public int adjustLineIndent(final Document document, final int offset, FormattingMode mode)
    throws IncorrectOperationException {
  if (delegate instanceof FormattingModeAwareIndentAdjuster) {
    return ((FormattingModeAwareIndentAdjuster) delegate)
        .adjustLineIndent(document, offset, mode);
  }
  return offset;
}
 
Example #8
Source File: CodeStyleManagerDecorator.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/** Uses same fallback as {@link CodeStyleManager#getCurrentFormattingMode}. */
@Override
public FormattingMode getCurrentFormattingMode() {
  if (delegate instanceof FormattingModeAwareIndentAdjuster) {
    return ((FormattingModeAwareIndentAdjuster) delegate).getCurrentFormattingMode();
  }
  return FormattingMode.REFORMAT;
}
 
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: DelegatingCodeStyleManager.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public int adjustLineIndent(final Document document, final int offset, FormattingMode mode)
    throws IncorrectOperationException {
  if (delegate instanceof FormattingModeAwareIndentAdjuster) {
    return ((FormattingModeAwareIndentAdjuster) delegate)
        .adjustLineIndent(document, offset, mode);
  }
  return offset;
}
 
Example #11
Source File: DelegatingCodeStyleManager.java    From intellij with Apache License 2.0 5 votes vote down vote up
/** Uses same fallback as {@link CodeStyleManager#getCurrentFormattingMode}. */
@Override
public FormattingMode getCurrentFormattingMode() {
  if (delegate instanceof FormattingModeAwareIndentAdjuster) {
    return ((FormattingModeAwareIndentAdjuster) delegate).getCurrentFormattingMode();
  }
  return FormattingMode.REFORMAT;
}
 
Example #12
Source File: DelegatingCodeStyleManager.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
@Override
public FormattingMode getCurrentFormattingMode() {
	if (this.delegate instanceof FormattingModeAwareIndentAdjuster) {
		return ((FormattingModeAwareIndentAdjuster) this.delegate).getCurrentFormattingMode();
	}
	return FormattingMode.REFORMAT;
}
 
Example #13
Source File: CodeStyleManagerDecorator.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public String getLineIndent(PsiFile file, int offset, FormattingMode mode) {
  return delegate.getLineIndent(file, offset, mode);
}
 
Example #14
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);
}
 
Example #15
Source File: CodeStyleManagerRunnable.java    From consulo with Apache License 2.0 4 votes vote down vote up
CodeStyleManagerRunnable(CodeStyleManagerImpl codeStyleManager, @Nonnull FormattingMode mode) {
  myCodeStyleManager = codeStyleManager;
  myMode = mode;
}
 
Example #16
Source File: CodeStyleManagerRunnable.java    From consulo with Apache License 2.0 4 votes vote down vote up
public T perform(PsiFile file, int offset, @Nullable TextRange range, T defaultValue) {
  if (file instanceof PsiCompiledFile) {
    file = ((PsiCompiledFile)file).getDecompiledPsiFile();
  }

  PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myCodeStyleManager.getProject());
  Document document = documentManager.getDocument(file);
  if (document instanceof DocumentWindow) {
    final DocumentWindow documentWindow = (DocumentWindow)document;
    final PsiFile topLevelFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
    if (!file.equals(topLevelFile)) {
      if (range != null) {
        range = documentWindow.injectedToHost(range);
      }
      if (offset != -1) {
        offset = documentWindow.injectedToHost(offset);
      }
      return adjustResultForInjected(perform(topLevelFile, offset, range, defaultValue), documentWindow);
    }
  }

  final PsiFile templateFile = PsiUtilCore.getTemplateLanguageFile(file);
  if (templateFile != null) {
    file = templateFile;
    document = documentManager.getDocument(templateFile);
  }

  PsiElement element = null;
  if (offset != -1) {
    element = CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset);
    if (element == null && offset != file.getTextLength()) {
      return defaultValue;
    }
    if (isInsidePlainComment(offset, element)) {
      return computeValueInsidePlainComment(file, offset, defaultValue);
    }
  }

  final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
  FormattingModelBuilder elementBuilder = element != null ? LanguageFormatting.INSTANCE.forContext(element) : builder;
  if (builder != null && elementBuilder != null) {
    mySettings = CodeStyle.getSettings(file);

    mySignificantRange = offset != -1 ? getSignificantRange(file, offset) : null;
    myIndentOptions = mySettings.getIndentOptionsByFile(file, mySignificantRange);

    FormattingMode currentMode = myCodeStyleManager.getCurrentFormattingMode();
    myCodeStyleManager.setCurrentFormattingMode(myMode);
    try {
      myModel = buildModel(builder, file, document);
      T result = doPerform(offset, range);
      if (result != null) {
        return result;
      }
    }
    finally {
      myCodeStyleManager.setCurrentFormattingMode(currentMode);
    }
  }
  return defaultValue;
}
 
Example #17
Source File: FormattingModeAwareIndentAdjuster.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Adjust line indent at document offset using {@code mode}.
 *
 * @param document The document to adjust line indent in.
 * @param offset   The offset in the document.
 * @param mode     The mode: {@link FormattingMode#ADJUST_INDENT} or {@link FormattingMode#ADJUST_INDENT_ON_ENTER}
 * @return Adjusted offset.
 */
int adjustLineIndent(@Nonnull final Document document, final int offset, FormattingMode mode);
 
Example #18
Source File: CodeStyleManager.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Calculates the indent that should be used for the specified line in
 * the specified file with the given formatting mode. Default implementation falls back to
 * {@link #getLineIndent(PsiFile, int)}
 *
 * @param file   the file for which the indent should be calculated.
 * @param offset the offset for the line at which the indent should be calculated.
 * @param mode   the formatting mode {@link FormattingMode}
 * @return the indent string (containing of tabs and/or whitespaces), or {@code null} if it
 * was not possible to calculate the indent.
 */
@Nullable
public String getLineIndent(@Nonnull PsiFile file, int offset, FormattingMode mode) {
  return getLineIndent(file, offset);
}
 
Example #19
Source File: FormattingModeAwareIndentAdjuster.java    From consulo with Apache License 2.0 votes vote down vote up
FormattingMode getCurrentFormattingMode();