本文整理汇总了Java中com.google.googlejavaformat.java.FormatterException类的典型用法代码示例。如果您正苦于以下问题:Java FormatterException类的具体用法?Java FormatterException怎么用?Java FormatterException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FormatterException类属于com.google.googlejavaformat.java包,在下文中一共展示了FormatterException类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validate
import com.google.googlejavaformat.java.FormatterException; //导入依赖的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.FormatterException; //导入依赖的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.FormatterException; //导入依赖的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: openWriter
import com.google.googlejavaformat.java.FormatterException; //导入依赖的package包/类
@Override
public Writer openWriter() throws IOException {
final StringBuilder stringBuilder = new StringBuilder(DEFAULT_FILE_SIZE);
return new Writer() {
@Override
public void write(char[] chars, int start, int end) throws IOException {
stringBuilder.append(chars, start, end - start);
}
@Override
public void flush() throws IOException {}
@Override
public void close() throws IOException {
try {
formatter.formatSource(
CharSource.wrap(stringBuilder),
new CharSink() {
@Override
public Writer openStream() throws IOException {
return fileObject.openWriter();
}
});
} catch (FormatterException e) {
// An exception will happen when the code being formatted has an error. It's better to
// log the exception and emit unformatted code so the developer can view the code which
// caused a problem.
try (Writer writer = fileObject.openWriter()) {
writer.append(stringBuilder.toString());
}
if (messager != null) {
messager.printMessage(Diagnostic.Kind.NOTE, "Error formatting " + getName());
}
}
}
};
}
示例5: formatJavaSource
import com.google.googlejavaformat.java.FormatterException; //导入依赖的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.FormatterException; //导入依赖的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: formatGoogleStyle
import com.google.googlejavaformat.java.FormatterException; //导入依赖的package包/类
public static String formatGoogleStyle(final String content) {
try {
return getGoogleFormatter().formatSource(content);
} catch (FormatterException e) {
return content;
}
}
示例8: generateSource
import com.google.googlejavaformat.java.FormatterException; //导入依赖的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);
}
}
示例9: maybeFormat
import com.google.googlejavaformat.java.FormatterException; //导入依赖的package包/类
private String maybeFormat(String input) {
try {
return new Formatter().formatSource(input);
} catch (FormatterException e) {
return input;
}
}