Skip to content

Commit

Permalink
Improve formatting of include declaration
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Sherman <bentshermann@gmail.com>
  • Loading branch information
bentsherman committed Nov 22, 2024
1 parent 70c5c32 commit 9fc23d0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void visitFeatureFlag(FeatureFlagNode node) {

@Override
public void visitInclude(IncludeNode node) {
var wrap = node.modules.size() > 1;
var wrap = node.getLineNumber() < node.getLastLineNumber();
fmt.appendLeadingComments(node);
fmt.append("include {");
if( wrap )
Expand All @@ -207,7 +207,7 @@ public void visitInclude(IncludeNode node) {
fmt.append(" as ");
fmt.append(module.alias);
}
if( !wrap && options.harshilAlignment() ) {
if( !wrap && node.modules.size() == 1 && options.harshilAlignment() ) {
var padding = maxIncludeWidth - getIncludeWidth(module);
fmt.append(" ".repeat(padding));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ import spock.lang.Specification
*/
class ScriptFormattingTest extends Specification {

Path getWorkspaceRoot() {
def workspaceRoot = Path.of(System.getProperty('user.dir')).resolve('build/test_workspace/')
if( !Files.exists(workspaceRoot) )
workspaceRoot.toFile().mkdirs()
return workspaceRoot
}

ScriptService getService(Path workspaceRoot) {
def service = new ScriptService()
service.connect(new TestLanguageClient())
service.initialize(workspaceRoot.toUri().toString(), Collections.emptyList(), false)
return service
}

String openAndFormat(ScriptService service, Path filePath, String contents) {
def uri = filePath.toUri()
def textDocumentItem = new TextDocumentItem(uri.toString(), 'nextflow', 1, contents)
Expand All @@ -42,16 +56,11 @@ class ScriptFormattingTest extends Specification {

def 'should format a script' () {
given:
def workspaceRoot = Path.of(System.getProperty('user.dir')).resolve('build/test_workspace/')
if( !Files.exists(workspaceRoot) )
workspaceRoot.toFile().mkdirs()

def service = new ScriptService()
service.connect(new TestLanguageClient())
service.initialize(workspaceRoot.toUri().toString(), Collections.emptyList(), false)
def workspaceRoot = getWorkspaceRoot()
def service = getService(workspaceRoot)
def filePath = workspaceRoot.resolve('main.nf')

when:
def filePath = workspaceRoot.resolve('main.nf')
def contents = '''\
workflow { println 'Hello!' }
'''.stripIndent()
Expand All @@ -76,4 +85,34 @@ class ScriptFormattingTest extends Specification {
'''.stripIndent()
}

def 'should format an include declaration' () {
given:
def workspaceRoot = getWorkspaceRoot()
def service = getService(workspaceRoot)
def filePath = workspaceRoot.resolve('main.nf')

when:
def contents = '''\
include{foo;bar}from'./foobar.nf'
'''.stripIndent()
then:
openAndFormat(service, filePath, contents) == '''\
include { foo ; bar } from './foobar.nf'
'''.stripIndent()

when:
contents = '''\
include{
foo;bar
}from'./foobar.nf'
'''.stripIndent()
then:
openAndFormat(service, filePath, contents) == '''\
include {
foo ;
bar
} from './foobar.nf'
'''.stripIndent()
}

}

0 comments on commit 9fc23d0

Please sign in to comment.