diff --git a/modules/language-server/src/main/java/nextflow/lsp/services/script/ScriptFormattingProvider.java b/modules/language-server/src/main/java/nextflow/lsp/services/script/ScriptFormattingProvider.java index 82b3b2c..06f8b27 100644 --- a/modules/language-server/src/main/java/nextflow/lsp/services/script/ScriptFormattingProvider.java +++ b/modules/language-server/src/main/java/nextflow/lsp/services/script/ScriptFormattingProvider.java @@ -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 ) @@ -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)); } diff --git a/modules/language-server/src/test/groovy/nextflow/lsp/services/script/ScriptFormattingTest.groovy b/modules/language-server/src/test/groovy/nextflow/lsp/services/script/ScriptFormattingTest.groovy index ba55ae3..7fd8eb4 100644 --- a/modules/language-server/src/test/groovy/nextflow/lsp/services/script/ScriptFormattingTest.groovy +++ b/modules/language-server/src/test/groovy/nextflow/lsp/services/script/ScriptFormattingTest.groovy @@ -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) @@ -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() @@ -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() + } + }