-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ben Sherman <[email protected]>
- Loading branch information
1 parent
9fc23d0
commit cf785cc
Showing
12 changed files
with
638 additions
and
590 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
modules/compiler/src/main/java/config/formatter/ConfigFormattingVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright 2024, Seqera Labs | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package nextflow.config.formatter; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import nextflow.config.ast.ConfigAppendNode; | ||
import nextflow.config.ast.ConfigAssignNode; | ||
import nextflow.config.ast.ConfigBlockNode; | ||
import nextflow.config.ast.ConfigIncludeNode; | ||
import nextflow.config.ast.ConfigNode; | ||
import nextflow.config.ast.ConfigVisitorSupport; | ||
import nextflow.script.formatter.FormattingOptions; | ||
import nextflow.script.formatter.Formatter; | ||
import org.codehaus.groovy.control.SourceUnit; | ||
import org.codehaus.groovy.runtime.IOGroovyMethods; | ||
|
||
/** | ||
* | ||
* @author Ben Sherman <[email protected]> | ||
*/ | ||
public class ConfigFormattingVisitor extends ConfigVisitorSupport { | ||
|
||
private SourceUnit sourceUnit; | ||
|
||
private FormattingOptions options; | ||
|
||
private Formatter fmt; | ||
|
||
public ConfigFormattingVisitor(SourceUnit sourceUnit, FormattingOptions options) { | ||
this.sourceUnit = sourceUnit; | ||
this.options = options; | ||
this.fmt = new Formatter(options); | ||
} | ||
|
||
@Override | ||
protected SourceUnit getSourceUnit() { | ||
return sourceUnit; | ||
} | ||
|
||
public void visit() { | ||
var moduleNode = sourceUnit.getAST(); | ||
if( moduleNode instanceof ConfigNode cn ) | ||
super.visit(cn); | ||
} | ||
|
||
public String toString() { | ||
return fmt.toString(); | ||
} | ||
|
||
// config statements | ||
|
||
@Override | ||
public void visitConfigAssign(ConfigAssignNode node) { | ||
fmt.appendLeadingComments(node); | ||
fmt.appendIndent(); | ||
var name = String.join(".", node.names); | ||
fmt.append(name); | ||
if( currentAlignmentWidth > 0 ) { | ||
var padding = currentAlignmentWidth - name.length(); | ||
fmt.append(" ".repeat(padding)); | ||
} | ||
fmt.append(node instanceof ConfigAppendNode ? " " : " = "); | ||
fmt.visit(node.value); | ||
fmt.appendNewLine(); | ||
} | ||
|
||
private static final Pattern IDENTIFIER = Pattern.compile("[a-zA-Z_]+[a-zA-Z0-9_]*"); | ||
|
||
private int currentAlignmentWidth = 0; | ||
|
||
@Override | ||
public void visitConfigBlock(ConfigBlockNode node) { | ||
fmt.appendLeadingComments(node); | ||
fmt.appendIndent(); | ||
if( node.kind != null ) { | ||
fmt.append(node.kind); | ||
fmt.append(": "); | ||
} | ||
var name = node.name; | ||
if( IDENTIFIER.matcher(name).matches() ) { | ||
fmt.append(name); | ||
} | ||
else { | ||
fmt.append('\''); | ||
fmt.append(name); | ||
fmt.append('\''); | ||
} | ||
fmt.append(" {"); | ||
fmt.appendNewLine(); | ||
|
||
int caw = currentAlignmentWidth; | ||
if( options.harshilAlignment() ) { | ||
int maxWidth = 0; | ||
for( var stmt : node.statements ) { | ||
if( stmt instanceof ConfigAssignNode can ) { | ||
var width = String.join(".", can.names).length(); | ||
if( maxWidth < width ) | ||
maxWidth = width; | ||
} | ||
} | ||
currentAlignmentWidth = maxWidth; | ||
} | ||
|
||
fmt.incIndent(); | ||
super.visitConfigBlock(node); | ||
fmt.decIndent(); | ||
|
||
if( options.harshilAlignment() ) | ||
currentAlignmentWidth = caw; | ||
|
||
fmt.appendIndent(); | ||
fmt.append('}'); | ||
fmt.appendNewLine(); | ||
} | ||
|
||
@Override | ||
public void visitConfigInclude(ConfigIncludeNode node) { | ||
fmt.appendLeadingComments(node); | ||
fmt.appendIndent(); | ||
fmt.append("includeConfig "); | ||
fmt.visit(node.source); | ||
fmt.appendNewLine(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.