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


Java Formatter类代码示例

本文整理汇总了Java中com.google.googlejavaformat.java.Formatter的典型用法代码示例。如果您正苦于以下问题:Java Formatter类的具体用法?Java Formatter怎么用?Java Formatter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: validate

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
@Override
public boolean validate(Path file) {
  if (!isJavaFile(file)) {
    log.get().debug(file + " is not a java file");
    return true;
  }

  log.get().info("Validating '" + file + "'");
  try (InputStream inputStream = Files.newInputStream(file)) {
    String unformatterContent = IOUtils.toString(inputStream);
    String formattedContent = new Formatter().formatSource(unformatterContent);
    return unformatterContent.equals(formattedContent);
  } catch (IOException | FormatterException e) {
    throw new RuntimeException(e);
  }
}
 
开发者ID:Cosium,项目名称:maven-git-code-format,代码行数:17,代码来源:JavaFormatter.java

示例2: formatInternal

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
private void formatInternal(Formatter formatter, PsiFile file, List<Range<Integer>> ranges)
    throws IncorrectOperationException {
  ApplicationManager.getApplication().assertWriteAccessAllowed();
  PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
  CheckUtil.checkWritable(file);

  Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
  if (document != null) {
    try {
      List<Replacement> replacements =
          formatter
              .getFormatReplacements(document.getText(), ranges)
              .stream()
              .sorted(
                  comparing((Replacement r) -> r.getReplaceRange().lowerEndpoint()).reversed())
              .collect(Collectors.toList());
      performReplacements(document, replacements);
    } catch (FormatterException e) {
      // Do not format on errors
    }
  }
}
 
开发者ID:google,项目名称:google-java-format,代码行数:23,代码来源:GoogleJavaFormatCodeStyleManager.java

示例3: formatJava

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
private static String formatJava(Context context, String src) throws FormatterException {
    AppSetting setting = new AppSetting(context);
    JavaFormatterOptions.Builder builder = JavaFormatterOptions.builder();
    builder.style(setting.getFormatType() == 0
            ? JavaFormatterOptions.Style.GOOGLE : JavaFormatterOptions.Style.AOSP);
    return new Formatter(builder.build()).formatSource(src);
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:8,代码来源:FormatFactory.java

示例4: FormattingJavaFileObject

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
/**
 * Create a new {@link FormattingJavaFileObject}.
 *
 * @param delegate {@link JavaFileObject} to decorate
 * @param messager to log messages with.
 */
FormattingJavaFileObject(
    JavaFileObject delegate, Formatter formatter, @Nullable Messager messager) {
  super(checkNotNull(delegate));
  this.formatter = checkNotNull(formatter);
  this.messager = messager;
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:13,代码来源:FormattingJavaFileObject.java

示例5: formatJavaSource

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
private static String formatJavaSource(
    final Formatter formatter, final Path javaFile, final String source) {
  final String formattedSource;
  try {
    formattedSource = formatter.formatSource(source);
  } catch (final FormatterException e) {
    throw new IllegalStateException("Could not format source in file " + javaFile, e);
  }
  return formattedSource;
}
 
开发者ID:spotify,项目名称:bazel-tools,代码行数:11,代码来源:Main.java

示例6: execute

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
public List<String> execute(Parser parser, String astJson) {
    List<String> synthesizedPrograms = new LinkedList<>();
    List<DSubTree> asts = getASTsFromNN(astJson);

    classLoader = URLClassLoader.newInstance(parser.classpathURLs);

    CompilationUnit cu = parser.cu;
    List<String> programs = new ArrayList<>();
    for (DSubTree ast : asts) {
        Visitor visitor = new Visitor(ast, new Document(parser.source), cu, mode);
        try {
            cu.accept(visitor);
            if (visitor.synthesizedProgram == null)
                continue;
            String program = visitor.synthesizedProgram.replaceAll("\\s", "");
            if (! programs.contains(program)) {
                String formattedProgram = new Formatter().formatSource(visitor.synthesizedProgram);
                programs.add(program);
                synthesizedPrograms.add(formattedProgram);
            }
        } catch (SynthesisException|FormatterException e) {
            // do nothing and try next ast
        }
    }

    return synthesizedPrograms;
}
 
开发者ID:capergroup,项目名称:bayou,代码行数:28,代码来源:Synthesizer.java

示例7: generateSource

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
private static String generateSource(Metadata metadata, Feature<?>... features) {
  SourceBuilder sourceBuilder = SourceStringBuilder.simple(features);
  new CodeGenerator().writeBuilderSource(sourceBuilder, metadata);
  try {
    return new Formatter().formatSource(sourceBuilder.toString());
  } catch (FormatterException e) {
    throw new RuntimeException(e);
  }
}
 
开发者ID:google,项目名称:FreeBuilder,代码行数:10,代码来源:DefaultedPropertiesSourceTest.java

示例8: reformatText

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
@Override
public void reformatText(@NotNull PsiFile file, int startOffset, int endOffset)
    throws IncorrectOperationException {
  Optional<Formatter> formatter = getFormatterForFile(file);
  if (formatter.isPresent() && StdFileTypes.JAVA.equals(file.getFileType())) {
    formatInternal(
        formatter.get(), file, ImmutableList.of(Range.closedOpen(startOffset, endOffset)));
  } else {
    super.reformatText(file, startOffset, endOffset);
  }
}
 
开发者ID:google,项目名称:google-java-format,代码行数:12,代码来源:GoogleJavaFormatCodeStyleManager.java

示例9: reformatTextWithContext

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
@Override
public void reformatTextWithContext(@NotNull PsiFile file, @NotNull Collection<TextRange> ranges)
    throws IncorrectOperationException {
  Optional<Formatter> formatter = getFormatterForFile(file);
  if (formatter.isPresent() && StdFileTypes.JAVA.equals(file.getFileType())) {
    formatInternal(formatter.get(), file, convertToRanges(ranges));
  } else {
    super.reformatTextWithContext(file, ranges);
  }
}
 
开发者ID:google,项目名称:google-java-format,代码行数:11,代码来源:GoogleJavaFormatCodeStyleManager.java

示例10: maybeFormat

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
private String maybeFormat(String input) {
  try {
    return new Formatter().formatSource(input);
  } catch (FormatterException e) {
    return input;
  }
}
 
开发者ID:google,项目名称:error-prone,代码行数:8,代码来源:BugCheckerRefactoringTestHelper.java

示例11: formatJavaFile

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
private static FormattingResult formatJavaFile(final Formatter formatter, final Path javaFile) {
  return FormattingResult.create(
      javaFile, formatJavaSource(formatter, javaFile, readFile(javaFile)));
}
 
开发者ID:spotify,项目名称:bazel-tools,代码行数:5,代码来源:Main.java

示例12: FormattableTDDCodeArea

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
public FormattableTDDCodeArea(){
    super();
    JavaFormatterOptions options = new JavaFormatterOptions(JavaFormatterOptions.JavadocFormatter.NONE, JavaFormatterOptions.Style.AOSP, JavaFormatterOptions.SortImports.NO);
    this.formatter = new Formatter(options);
    this.plainTextChanges().subscribe(this::bracketInsertListener);
}
 
开发者ID:ProPra16,项目名称:programmierpraktikum-abschlussprojekt-amigos,代码行数:7,代码来源:FormattableTDDCodeArea.java

示例13: BasicGoogleJavaFormatCodeStyleManager

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
public BasicGoogleJavaFormatCodeStyleManager(
    @NotNull CodeStyleManager original, JavaFormatterOptions.Style style) {
  super(original);
  this.formatter = new Formatter(JavaFormatterOptions.builder().style(style).build());
}
 
开发者ID:google,项目名称:google-java-format,代码行数:6,代码来源:BasicGoogleJavaFormatCodeStyleManager.java

示例14: getFormatterForFile

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
@Override
protected Optional<Formatter> getFormatterForFile(@NotNull PsiFile file) {
  return Optional.of(formatter);
}
 
开发者ID:google,项目名称:google-java-format,代码行数:5,代码来源:BasicGoogleJavaFormatCodeStyleManager.java

示例15: getFormatterForFile

import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
/**
 * Get the {@link Formatter} to be used with the given file, or absent to use the built-in
 * IntelliJ formatter.
 */
protected abstract Optional<Formatter> getFormatterForFile(PsiFile file);
 
开发者ID:google,项目名称:google-java-format,代码行数:6,代码来源:GoogleJavaFormatCodeStyleManager.java


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