Delombok:保留换行符
Delombok: Preserve line breaks
我在发布 jar 文件的同时编译 delmboked java 文件。
有如下代码:
getSomeStream()
.map(v->...)
.filter(v->...)
.flatMap(v->...)
.reduce((s,a)->....)
被删除为单行:
getSomeStream().map(v->...).filter(v->...).flatMap(v->...).reduce((s,a)->....)
这是无法调试的。
运行 使用 -f pretty
的 delombok 不能解决这个问题。
是否有任何选项可以使代码易于阅读?
谢谢
披露:我是 lombok 的维护者。
我理解您的要求,但很遗憾,我们无法轻松访问原始换行符。
Delombok 根据抽象语法树 (AST) 和一些格式化选项生成输出。 AST 由解析器生成。
由于我们有 AST,delombok 可以使用相对简单的格式化规则生成新文件。
我并不是说不可能尝试在原始源文件中找到行结尾,但这并非微不足道。
根据您对调试的评论,您是说您获得的唯一有用信息是行号吗?
我可以想象我们添加一个选项总是在“select”之后插入换行符,这样调用链总是分布在多行上。我们甚至可以在参数中包含方法调用,并将其放在自己的行中。
我能够通过 运行 google java 格式在 delmboked 来源上实现这一点:
build.gradle
plugins {
id 'java'
id 'com.github.sherter.google-java-format' version '0.9'
id "io.franzbecker.gradle-lombok" version "4.0.0"
}
task delombok(type: DelombokTask) {
ext.outputDir = file("$buildDir/delombok")
outputs.dir(outputDir)
sourceSets.main.java.srcDirs.each {
inputs.dir(it)
args(it, "-d", outputDir)
}
doFirst {
outputDir.deleteDir()
}
}
googleJavaFormat {
toolVersion = '1.9'
source = "$buildDir/delombok"
include '**/*.java'
}
tasks.googleJavaFormat{
dependsOn delombok
}
结果:
原始代码片段:
for (Map.Entry<String,QueryBuilder> e : aliases.entrySet()) {
if (!restTemplate
.indexOps(resourcesIndices)
.getAliases(e.getKey())
.containsKey(resourcesIndices.getIndexName())) {
final boolean acknowledged = restTemplate
.indexOps(resourcesIndices)
.alias(new AliasActions(new AliasAction.Add(
AliasActionParameters.builder()
.withAliases(e.getKey())
.withIndices(resourcesIndices.getIndexName())
.withFilterQuery(new NativeSearchQuery(e.getValue()))
.build()
)));
if (!acknowledged) {
throw new BeanCreationException("Failed to create alias");
}
}
}
德隆博克:
for (Map.Entry<String, QueryBuilder> e : aliases.entrySet()) {
if (!restTemplate.indexOps(resourcesIndices).getAliases(e.getKey()).containsKey(resourcesIndices.getIndexName())) {
final boolean acknowledged = restTemplate.indexOps(resourcesIndices).alias(new AliasActions(new AliasAction.Add(AliasActionParameters.builder().withAliases(e.getKey()).withIndices(resourcesIndices.getIndexName()).withFilterQuery(new NativeSearchQuery(e.getValue())).build())));
if (!acknowledged) {
throw new BeanCreationException("Failed to create alias");
}
}
}
Google 格式化
for (Map.Entry<String, QueryBuilder> e : aliases.entrySet()) {
if (!restTemplate
.indexOps(resourcesIndices)
.getAliases(e.getKey())
.containsKey(resourcesIndices.getIndexName())) {
final boolean acknowledged =
restTemplate
.indexOps(resourcesIndices)
.alias(
new AliasActions(
new AliasAction.Add(
AliasActionParameters.builder()
.withAliases(e.getKey())
.withIndices(resourcesIndices.getIndexName())
.withFilterQuery(new NativeSearchQuery(e.getValue()))
.build())));
if (!acknowledged) {
throw new BeanCreationException("Failed to create alias");
}
}
}
我在发布 jar 文件的同时编译 delmboked java 文件。 有如下代码:
getSomeStream()
.map(v->...)
.filter(v->...)
.flatMap(v->...)
.reduce((s,a)->....)
被删除为单行:
getSomeStream().map(v->...).filter(v->...).flatMap(v->...).reduce((s,a)->....)
这是无法调试的。
运行 使用 -f pretty
的 delombok 不能解决这个问题。
是否有任何选项可以使代码易于阅读? 谢谢
披露:我是 lombok 的维护者。
我理解您的要求,但很遗憾,我们无法轻松访问原始换行符。
Delombok 根据抽象语法树 (AST) 和一些格式化选项生成输出。 AST 由解析器生成。
由于我们有 AST,delombok 可以使用相对简单的格式化规则生成新文件。
我并不是说不可能尝试在原始源文件中找到行结尾,但这并非微不足道。
根据您对调试的评论,您是说您获得的唯一有用信息是行号吗?
我可以想象我们添加一个选项总是在“select”之后插入换行符,这样调用链总是分布在多行上。我们甚至可以在参数中包含方法调用,并将其放在自己的行中。
我能够通过 运行 google java 格式在 delmboked 来源上实现这一点:
build.gradle
plugins {
id 'java'
id 'com.github.sherter.google-java-format' version '0.9'
id "io.franzbecker.gradle-lombok" version "4.0.0"
}
task delombok(type: DelombokTask) {
ext.outputDir = file("$buildDir/delombok")
outputs.dir(outputDir)
sourceSets.main.java.srcDirs.each {
inputs.dir(it)
args(it, "-d", outputDir)
}
doFirst {
outputDir.deleteDir()
}
}
googleJavaFormat {
toolVersion = '1.9'
source = "$buildDir/delombok"
include '**/*.java'
}
tasks.googleJavaFormat{
dependsOn delombok
}
结果:
原始代码片段:
for (Map.Entry<String,QueryBuilder> e : aliases.entrySet()) {
if (!restTemplate
.indexOps(resourcesIndices)
.getAliases(e.getKey())
.containsKey(resourcesIndices.getIndexName())) {
final boolean acknowledged = restTemplate
.indexOps(resourcesIndices)
.alias(new AliasActions(new AliasAction.Add(
AliasActionParameters.builder()
.withAliases(e.getKey())
.withIndices(resourcesIndices.getIndexName())
.withFilterQuery(new NativeSearchQuery(e.getValue()))
.build()
)));
if (!acknowledged) {
throw new BeanCreationException("Failed to create alias");
}
}
}
德隆博克:
for (Map.Entry<String, QueryBuilder> e : aliases.entrySet()) {
if (!restTemplate.indexOps(resourcesIndices).getAliases(e.getKey()).containsKey(resourcesIndices.getIndexName())) {
final boolean acknowledged = restTemplate.indexOps(resourcesIndices).alias(new AliasActions(new AliasAction.Add(AliasActionParameters.builder().withAliases(e.getKey()).withIndices(resourcesIndices.getIndexName()).withFilterQuery(new NativeSearchQuery(e.getValue())).build())));
if (!acknowledged) {
throw new BeanCreationException("Failed to create alias");
}
}
}
Google 格式化
for (Map.Entry<String, QueryBuilder> e : aliases.entrySet()) {
if (!restTemplate
.indexOps(resourcesIndices)
.getAliases(e.getKey())
.containsKey(resourcesIndices.getIndexName())) {
final boolean acknowledged =
restTemplate
.indexOps(resourcesIndices)
.alias(
new AliasActions(
new AliasAction.Add(
AliasActionParameters.builder()
.withAliases(e.getKey())
.withIndices(resourcesIndices.getIndexName())
.withFilterQuery(new NativeSearchQuery(e.getValue()))
.build())));
if (!acknowledged) {
throw new BeanCreationException("Failed to create alias");
}
}
}