当前位置: 首页>>代码示例>>Java>>正文


Java DefaultCodeFormatter.createIndentationString方法代码示例

本文整理汇总了Java中org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.createIndentationString方法的典型用法代码示例。如果您正苦于以下问题:Java DefaultCodeFormatter.createIndentationString方法的具体用法?Java DefaultCodeFormatter.createIndentationString怎么用?Java DefaultCodeFormatter.createIndentationString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.eclipse.jdt.internal.formatter.DefaultCodeFormatter的用法示例。


在下文中一共展示了DefaultCodeFormatter.createIndentationString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: format

import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入方法依赖的package包/类
public static TextEdit format(IDocument document, TypedPosition partition,
    Map<String, String> javaFormattingPrefs,
    Map<String, String> javaScriptFormattingPrefs, String original) {
  try {
    // Extract the JSNI block out of the document
    int offset = partition.getOffset();
    int length = partition.getLength();

    // Determine the line delimiter, indent string, and tab/indent widths
    String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
    int tabWidth = IndentManipulation.getTabWidth(javaFormattingPrefs);
    int indentWidth = IndentManipulation.getIndentWidth(javaFormattingPrefs);

    // Get indentation level of the first line of the JSNI block (this should
    // be the line containing the JSNI method declaration)
    int methodDeclarationOffset = getMethodDeclarationOffset(document, offset);
    int jsniLine1 = document.getLineOfOffset(methodDeclarationOffset);
    int methodIndentLevel = getLineIndentLevel(document, jsniLine1, tabWidth,
        indentWidth);
    DefaultCodeFormatter defaultCodeFormatter = new DefaultCodeFormatter(
        javaFormattingPrefs);
    String indentLine = defaultCodeFormatter.createIndentationString(methodIndentLevel);

    // Extract the JSNI body out of the block and split it up by line
    String jsniSource = document.get(offset, length);
    String body = JsniParser.extractMethodBody(jsniSource);

    String formattedJs;

    // JSNI Java references mess up the JS formatter, so replace them
    // with place holder values
    JsniJavaRefReplacementResult replacementResults = replaceJsniJavaRefs(body);
    body = replacementResults.getJsni();

    TextEdit formatEdit = CodeFormatterUtil.format2(
        CodeFormatter.K_STATEMENTS, body, methodIndentLevel + 1,
        lineDelimiter, javaScriptFormattingPrefs);

    if (formatEdit != null) {

      body = restoreJsniJavaRefs(replacementResults);

      Document d = new Document(body);
      formatEdit.apply(d);

      formattedJs = d.get();

      if (!formattedJs.startsWith(lineDelimiter)) {
        formattedJs = lineDelimiter + formattedJs;
      }

      if (!formattedJs.endsWith(lineDelimiter)) {
        formattedJs = formattedJs + lineDelimiter;
      }

      formattedJs = formattedJs + indentLine;

      formattedJs = "/*-{" + formattedJs + "}-*/";

    } else {

      if (original == null) {
        return null;
      }

      formattedJs = original; // formatting failed, use the original string
    }

    return new ReplaceEdit(offset, length, formattedJs);

  } catch (Exception e) {
    GWTPluginLog.logError(e);
    return null;
  }
}
 
开发者ID:gwt-plugins,项目名称:gwt-eclipse-plugin,代码行数:76,代码来源:JsniFormattingUtil.java

示例2: format

import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; //导入方法依赖的package包/类
public TextEdit format(IDocument document, TypedPosition partition, Map<String, String> javaFormattingPrefs,
					   Map<String, String> javaScriptFormattingPrefs, String original) {
	try {
		// Extract the JSNI block out of the document
		int offset = partition.getOffset();
		int length = partition.getLength();

		// Determine the line delimiter, indent string, and tab/indent widths
		String lineDelimiter = Settings.LINE_SEPARATOR;
		int tabWidth = IndentManipulation.getTabWidth(javaFormattingPrefs);
		int indentWidth = IndentManipulation.getIndentWidth(javaFormattingPrefs);

		// Get indentation level of the first line of the JSNI block (this should
		// be the line containing the JSNI method declaration)
		int methodDeclarationOffset = getMethodDeclarationOffset(document, offset);

		int jsniLine1 = document.getLineOfOffset(methodDeclarationOffset);
		int methodIndentLevel = getLineIndentLevel(document, jsniLine1, tabWidth, indentWidth);
		DefaultCodeFormatter defaultCodeFormatter = new DefaultCodeFormatter(javaFormattingPrefs);
		String indentLine = defaultCodeFormatter.createIndentationString(methodIndentLevel);

		// Extract the JSNI body out of the block and split it up by line
		String jsniSource = document.get(offset, length);
		String body = JsniParser.extractMethodBody(jsniSource);

		String formattedJs;

		// JSNI Java references mess up the JS formatter, so replace them
		// with place holder values
		JsniJavaRefReplacementResult replacementResults = replaceJsniJavaRefs(body);
		body = replacementResults.getJsni();
		CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(javaScriptFormattingPrefs);

		TextEdit formatEdit = codeFormatter.format(CodeFormatter.K_STATEMENTS, body, 0, body.length(),
				methodIndentLevel + 1, lineDelimiter);

		if (formatEdit != null) {

			body = restoreJsniJavaRefs(replacementResults);

			Document d = new Document(body);
			formatEdit.apply(d);

			formattedJs = d.get();

			if (!formattedJs.startsWith(lineDelimiter)) {
				formattedJs = lineDelimiter + formattedJs;
			}

			if (!formattedJs.endsWith(lineDelimiter)) {
				formattedJs = formattedJs + lineDelimiter;
			}

			formattedJs = formattedJs + indentLine;

			formattedJs = "/*-{" + formattedJs + "}-*/";

		} else {

			if (original == null) {
				return null;
			}

			formattedJs = original; // formatting failed, use the original string
		}

		return new ReplaceEdit(offset, length, formattedJs);

	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
开发者ID:krasa,项目名称:EclipseCodeFormatter,代码行数:73,代码来源:JsniFormattingUtil.java


注:本文中的org.eclipse.jdt.internal.formatter.DefaultCodeFormatter.createIndentationString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。