本文整理汇总了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);
}
}
示例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
}
}
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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;
}
}
示例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)));
}
示例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());
}
示例14: getFormatterForFile
import com.google.googlejavaformat.java.Formatter; //导入依赖的package包/类
@Override
protected Optional<Formatter> getFormatterForFile(@NotNull PsiFile file) {
return Optional.of(formatter);
}
示例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);