All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aspectj.org.eclipse.jdt.core.formatter.CodeFormatter Maven / Gradle / Ivy

There is a newer version: 1.9.22.1
Show newest version
/*******************************************************************************
 * Copyright (c) 2000, 2014 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Harry Terkelsen ([email protected]) - Bug 449262 - Allow the use of third-party Java formatters
 *******************************************************************************/
package org.aspectj.org.eclipse.jdt.core.formatter;

import org.aspectj.org.eclipse.jdt.internal.compiler.util.Util;
import org.eclipse.jface.text.IRegion;
import org.eclipse.text.edits.TextEdit;

import java.util.Map;

/**
 * Specification for a generic source code formatter.
 *
 * @since 3.0
 */
public abstract class CodeFormatter {

	/**
	 * Unknown kind
	 *

* Since 3.6, if the corresponding comment options are set to * true then it is also possible to format the comments on the fly * by adding the {@link #F_INCLUDE_COMMENTS} flag to this kind of format. *

*/ public static final int K_UNKNOWN = 0x00; /** * Kind used to format an expression *

* Note that using this constant, the comments encountered while formatting * the expression may be shifted to match the correct indentation but are not * formatted. *

* Since 3.6, if the corresponding comment options are set to * true then it is also possible to format the comments on the fly * by adding the {@link #F_INCLUDE_COMMENTS} flag to this kind of format. *

*/ public static final int K_EXPRESSION = 0x01; /** * Kind used to format a set of statements *

* Note that using this constant, the comments encountered while formatting * the statements may be shifted to match the correct indentation but are not * formatted. *

* Since 3.6, if the corresponding comment options are set to * true then it is also possible to format the comments on the fly * by adding the {@link #F_INCLUDE_COMMENTS} flag to this kind of format. *

*/ public static final int K_STATEMENTS = 0x02; /** * Kind used to format a set of class body declarations *

* Note that using this constant, the comments encountered while formatting * the body declarations may be shifted to match the correct indentation but * are not formatted. *

* Since 3.6, if the corresponding comment options are set to * true then it is also possible to format the comments on the fly * by adding the {@link #F_INCLUDE_COMMENTS} flag to this kind of format. *

*/ public static final int K_CLASS_BODY_DECLARATIONS = 0x04; /** * Kind used to format a compilation unit *

* Note that using this constant, the comments are only indented while * formatting the compilation unit. *

* Since 3.4, if the corresponding comment option is set to * true then it is also possible to format the comments on the fly * by adding the {@link #F_INCLUDE_COMMENTS} flag to this kind of format. *

*/ public static final int K_COMPILATION_UNIT = 0x08; /** * Kind used to format a single-line comment * @since 3.1 */ public static final int K_SINGLE_LINE_COMMENT = 0x10; /** * Kind used to format a multi-line comment * @since 3.1 */ public static final int K_MULTI_LINE_COMMENT = 0x20; /** * Kind used to format a Javadoc comment * * @since 3.1 */ public static final int K_JAVA_DOC = 0x40; /** * Flag used to include the comments during the formatting of the code * snippet. *

* This flag can be combined with the following kinds: *

    *
  • {@link #K_COMPILATION_UNIT}
  • *
  • {@link #K_UNKNOWN}
  • *
  • {@link #K_CLASS_BODY_DECLARATIONS} (since 3.6)
  • *
  • {@link #K_EXPRESSION} (since 3.6)
  • *
  • {@link #K_STATEMENTS} (since 3.6)
  • *
*

* Note also that it has an effect only when one or several format comments * options for * {@link DefaultCodeFormatterConstants#FORMATTER_COMMENT_FORMAT_JAVADOC_COMMENT javadoc} * , * {@link DefaultCodeFormatterConstants#FORMATTER_COMMENT_FORMAT_BLOCK_COMMENT block} * or * {@link DefaultCodeFormatterConstants#FORMATTER_COMMENT_FORMAT_LINE_COMMENT line} * are set to {@link DefaultCodeFormatterConstants#TRUE true} while calling * {@link #format(int, String, int, int, int, String)} or * {@link #format(int, String, IRegion[], int, String)} methods *

* For example, with the Eclipse default formatter options, the formatting * of the following code snippet using {@link #K_COMPILATION_UNIT}: *

	 * public class X {
	 * /**
	 *  * This is just a simple example to show that comments will be formatted while processing a compilation unit only if the constant flag F_INCLUDE_COMMENT flag is set.
	 *  * @param str The input string
	 *  */
	 *  void foo(String str){}}
	 * 
* will produce the following output: *
	 * public class X {
	 * 	/**
	 *  	 * This is just a simple example to show that comments will be formatted while processing a compilation unit only if the constant flag F_INCLUDE_COMMENT flag is set.
	 *  	 *
	 *  	 * @param str The input string
	 *  	 */
	 *  	void foo(String str){
	 *  	}
	 *  }
	 * 
* Adding this flavor to the kind given while formatting the same source * (e.g. {@link #K_COMPILATION_UNIT} | {@link #F_INCLUDE_COMMENTS}) * will produce the following output instead: *
	 * public class X {
	 * 	/**
	 *  	 * This is just a simple example to show that comments will be formatted
	 *  	 * while processing a compilation unit only if the constant flag
	 *  	 * F_INCLUDE_COMMENT flag is set.
	 *  	 *
	 *  	 * @param str
	 *  	 * 		The input string
	 *  	 */
	 *  	void foo(String str){
	 *  	}
	 *  }
	 * 
*

* Note: Although we're convinced that the formatter should * always include the comments while processing a * {@link #K_COMPILATION_UNIT kind of compilation unit}, we * have decided to add a specific flag to fix this formatter incorrect behavior. * This will prevent all existing clients (e.g. 3.3 and previous versions) using * the {@link #K_COMPILATION_UNIT} kind to be broken while formatting. *

* @since 3.4 */ public static final int F_INCLUDE_COMMENTS = 0x1000; /** * Format source, * and returns a text edit that correspond to the difference between the given * string and the formatted string. *

* It returns null if the given string cannot be formatted. *

* If the offset position is matching a whitespace, the result can include * whitespaces. It would be up to the caller to get rid of preceding * whitespaces. *

* * @param kind Use to specify the kind of the code snippet to format. It can * be any of these: *
    *
  • {@link #K_EXPRESSION}
  • *
  • {@link #K_STATEMENTS}
  • *
  • {@link #K_CLASS_BODY_DECLARATIONS}
  • *
  • {@link #K_COMPILATION_UNIT}
    * Since 3.4, the comments can be formatted on the fly while * using this kind of code snippet
    * (see {@link #F_INCLUDE_COMMENTS} for more detailed explanation on * this flag) *
  • *
  • {@link #K_UNKNOWN}
  • *
  • {@link #K_SINGLE_LINE_COMMENT}
  • *
  • {@link #K_MULTI_LINE_COMMENT}
  • *
  • {@link #K_JAVA_DOC}
  • *
* @param source the source to format * @param offset the given offset to start recording the edits (inclusive). * @param length the given length to stop recording the edits (exclusive). * @param indentationLevel the initial indentation level, used * to shift left/right the entire source fragment. An initial indentation * level of zero or below has no effect. * @param lineSeparator the line separator to use in formatted source, * if set to null, then the platform default one will be used. * @return the text edit * @throws IllegalArgumentException if offset is lower than 0, length is lower than 0 or * length is greater than source length. */ public abstract TextEdit format(int kind, String source, int offset, int length, int indentationLevel, String lineSeparator); /** * Format source, * and returns a text edit that correspond to the difference between the given string and the formatted string. *

It returns null if the given string cannot be formatted.

* *

If an offset position is matching a whitespace, the result can include whitespaces. It would be up to the * caller to get rid of preceding whitespaces.

* *

No region in regions must overlap with any other region in regions. * Each region must be within source. There must be at least one region. Regions must be sorted * by their offsets, smaller offset first.

* * @param kind Use to specify the kind of the code snippet to format. It can * be any of these: *
    *
  • {@link #K_EXPRESSION}
  • *
  • {@link #K_STATEMENTS}
  • *
  • {@link #K_CLASS_BODY_DECLARATIONS}
  • *
  • {@link #K_COMPILATION_UNIT}
    * Since 3.4, the comments can be formatted on the fly while * using this kind of code snippet
    * (see {@link #F_INCLUDE_COMMENTS} for more detailed explanation on * this flag) *
  • *
  • {@link #K_UNKNOWN}
  • *
  • {@link #K_SINGLE_LINE_COMMENT}
  • *
  • {@link #K_MULTI_LINE_COMMENT}
  • *
  • {@link #K_JAVA_DOC}
  • *
* @param source the source to format * @param regions a set of regions in source to format * @param indentationLevel the initial indentation level, used * to shift left/right the entire source fragment. An initial indentation * level of zero or below has no effect. * @param lineSeparator the line separator to use in formatted source, * if set to null, then the platform default one will be used. * @return the text edit * @throws IllegalArgumentException if there is no region, a region overlaps with another region, or the regions are not * sorted in the ascending order. * @since 3.4 */ public abstract TextEdit format(int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator); /** * Answers the string that corresponds to the indentation to the given indentation level or an empty string * if the indentation cannot be computed. *

This method needs to be overridden in a subclass.

* *

The default implementation returns an empty string.

* * @param indentationLevel the given indentation level * @return the string corresponding to the right indentation level * @exception IllegalArgumentException if the given indentation level is lower than zero * @since 3.2 */ public String createIndentationString(int indentationLevel) { return Util.EMPTY_STRING; } /** * Sets the formatting options for this formatter. *

The default implementation ignores the options. * * @param options the options for the formatter * @since 3.11 */ public void setOptions(Map options) { // Do nothing by default } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy