Skip to content

Commit

Permalink
Move formatters to shared library
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Sherman <[email protected]>
  • Loading branch information
bentsherman committed Nov 22, 2024
1 parent 9fc23d0 commit cf785cc
Show file tree
Hide file tree
Showing 12 changed files with 638 additions and 590 deletions.
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.lsp.services.util;
package nextflow.script.formatter;

import java.util.List;
import java.util.stream.Stream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.lsp.services.util;
package nextflow.script.formatter;

public record FormattingOptions(
int tabSize,
Expand Down
Loading

0 comments on commit cf785cc

Please sign in to comment.