Java Code Examples for org.eclipse.jdt.core.ToolFactory#createCodeFormatter()

The following examples show how to use org.eclipse.jdt.core.ToolFactory#createCodeFormatter() . 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: TypeCreator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private String formatJava(IType type) throws JavaModelException {
  String source = type.getCompilationUnit().getSource();
  CodeFormatter formatter = ToolFactory.createCodeFormatter(type.getJavaProject().getOptions(true));
  TextEdit formatEdit = formatter.format(CodeFormatterFlags.getFlagsForCompilationUnitFormat(), source, 0,
      source.length(), 0, lineDelimiter);
  if (formatEdit == null) {
    CorePluginLog.logError("Could not format source for " + type.getCompilationUnit().getElementName());
    return source;
  }

  Document document = new Document(source);
  try {
    formatEdit.apply(document);
    source = document.get();
  } catch (BadLocationException e) {
    CorePluginLog.logError(e);
  }

  source = Strings.trimLeadingTabsAndSpaces(source);
  return source;
}
 
Example 2
Source File: JavaCodeFormatter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a JavaCodeFormatter using the default formatter options and
 * optionally applying user provided options on top.
 *
 * @param overrideOptions user provided options to apply on top of defaults
 */
public JavaCodeFormatter(final Map<String, Object> overrideOptions) {
    Map formatterOptions = new HashMap<>(DEFAULT_FORMATTER_OPTIONS);
    if (overrideOptions != null) {
        formatterOptions.putAll(overrideOptions);
    }

    this.codeFormatter = ToolFactory.createCodeFormatter(formatterOptions,
                                                         ToolFactory.M_FORMAT_EXISTING);
}
 
Example 3
Source File: JavaCodeFormatter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a JavaCodeFormatter using the default formatter options and
 * optionally applying user provided options on top.
 *
 * @param overrideOptions user provided options to apply on top of defaults
 */
public JavaCodeFormatter(final Map<String, Object> overrideOptions) {
    Map formatterOptions = new HashMap<>(DEFAULT_FORMATTER_OPTIONS);
    if (overrideOptions != null) {
        formatterOptions.putAll(overrideOptions);
    }

    this.codeFormatter = ToolFactory.createCodeFormatter(formatterOptions,
                                                         ToolFactory.M_FORMAT_EXISTING);
}
 
Example 4
Source File: JavaFormatter.java    From meghanada-server with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("try")
public static String formatEclipseStyle(final Properties prop, final String content) {
  try (TelemetryUtils.ScopedSpan scope =
      TelemetryUtils.startScopedSpan("JavaFormatter.formatEclipseStyle")) {
    TelemetryUtils.ScopedSpan.addAnnotation(
        TelemetryUtils.annotationBuilder().put("size", content.length()).build("args"));

    final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(prop);
    final IDocument document = new Document(content);
    final TextEdit textEdit =
        codeFormatter.format(
            CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS,
            content,
            0,
            content.length(),
            0,
            null);
    if (nonNull(textEdit)) {
      textEdit.apply(document);
      return ensureCorrectNewLines(document.get());
    } else {
      return content;
    }
  } catch (Throwable e) {
    return content;
  }
}
 
Example 5
Source File: EclipseCodeFormatter.java    From celerio with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "deprecation"})
public void setFormatterSettings(List<Setting> settings) {

    // // change the option to wrap each enum constant on a new line
    // options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS,
    // DefaultCodeFormatterConstants.createAlignmentValue(true,
    // DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE,
    // DefaultCodeFormatterConstants.INDENT_ON_COLUMN));
    //
    if (settings != null) {
        options = newHashMap();
        for (Setting s : settings) {
            options.put(s.getId(), s.getValue());
        }
    } else {
        options = DefaultCodeFormatterConstants.getEclipseDefaultSettings();

        options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8);
        options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8);
        options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);

        options.put(JavaCore.FORMATTER_LINE_SPLIT, "160");
        options.put(JavaCore.FORMATTER_TAB_CHAR, JavaCore.SPACE);
        options.put(JavaCore.FORMATTER_TAB_SIZE, "4");
    }

    // instanciate the default code formatter with the given options
    codeFormatter = ToolFactory.createCodeFormatter(options);
}
 
Example 6
Source File: CodeFormatterApplication.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Runs the Java code formatter application
 */
public Object start(IApplicationContext context) throws Exception {
	File[] filesToFormat = processCommandLine((String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS));

	if (filesToFormat == null) {
		return IApplication.EXIT_OK;
	}

	if (!this.quiet) {
		if (this.configName != null) {
			System.out.println(Messages.bind(Messages.CommandLineConfigFile, this.configName));
		}
		System.out.println(Messages.bind(Messages.CommandLineStart));
	}

	final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(this.options);
	// format the list of files and/or directories
	for (int i = 0, max = filesToFormat.length; i < max; i++) {
		final File file = filesToFormat[i];
		if (file.isDirectory()) {
			formatDirTree(file, codeFormatter);
		} else if (Util.isJavaLikeFileName(file.getPath())) {
			formatFile(file, codeFormatter);
		}
	}
	if (!this.quiet) {
		System.out.println(Messages.bind(Messages.CommandLineDone));
	}

	return IApplication.EXIT_OK;
}
 
Example 7
Source File: JavaFormatter.java    From formatter-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void init(Map<String, String> options, ConfigurationSource cfg) {
    super.initCfg(cfg);

    this.formatter = ToolFactory.createCodeFormatter(options, ToolFactory.M_FORMAT_EXISTING);
}