Swift-format increases indent level for multi-line string literals each time it is run

The swift-format repo seems to have issues disabled so I cannot create an issue there.

It seems that whenever I run the swift-format -m format command, It increases the indentation of multi-line string literals by 1 each time.

It increases the indentation of each line following the initial """ including the closing """

here is my configuration file:

{
  "fileScopedDeclarationPrivacy" : {
    "accessLevel" : "private"
  },
  "indentation" : {
    "tabs" : 1
  },
  "indentConditionalCompilationBlocks" : true,
  "indentSwitchCaseLabels" : false,
  "lineBreakAroundMultilineExpressionChainComponents" : false,
  "lineBreakBeforeControlFlowKeywords" : false,
  "lineBreakBeforeEachArgument" : false,
  "lineBreakBeforeEachGenericRequirement" : false,
  "lineLength" : 120,
  "maximumBlankLines" : 1,
  "prioritizeKeepingFunctionOutputTogether" : false,
  "respectsExistingLineBreaks" : true,
  "rules" : {
    "AllPublicDeclarationsHaveDocumentation" : true,
    "AlwaysUseLowerCamelCase" : true,
    "AmbiguousTrailingClosureOverload" : true,
    "BeginDocumentationCommentWithOneLineSummary" : true,
    "DoNotUseSemicolons" : true,
    "DontRepeatTypeInStaticProperties" : true,
    "FileScopedDeclarationPrivacy" : true,
    "FullyIndirectEnum" : true,
    "GroupNumericLiterals" : true,
    "IdentifiersMustBeASCII" : true,
    "NeverForceUnwrap" : true,
    "NeverUseForceTry" : true,
    "NeverUseImplicitlyUnwrappedOptionals" : true,
    "NoAccessLevelOnExtensionDeclaration" : false,
    "NoBlockComments" : true,
    "NoCasesWithOnlyFallthrough" : true,
    "NoEmptyTrailingClosureParentheses" : true,
    "NoLabelsInCasePatterns" : true,
    "NoLeadingUnderscores" : false,
    "NoParensAroundConditions" : true,
    "NoVoidReturnOnFunctionSignature" : true,
    "OneCasePerLine" : true,
    "OneVariableDeclarationPerLine" : true,
    "OnlyOneTrailingClosureArgument" : false,
    "OrderedImports" : true,
    "ReturnVoidInsteadOfEmptyTuple" : true,
    "UseLetInEveryBoundCaseVariable" : true,
    "UseShorthandTypeNames" : true,
    "UseSingleLinePropertyGetter" : true,
    "UseSynthesizedInitializer" : true,
    "UseTripleSlashForDocumentationComments" : true,
    "UseLetInEveryBoundCaseVariable": true,
    "ValidateDocumentationComments" : true
  },
  "tabWidth" : 4,
  "version" : 1
}

example swift code that is experiencing this issue:

import Foundation

class TestClass {
	var multilineString: String = """
		This is a test.
		Each line of this string, including its closing quotes should get indented with every run of swift-format.
		Test test
		Test Test
		"""
}

so if I run swift-format 5 times, this is the output:

import Foundation

class TestClass {
	var multilineString: String = """
												This is a test.
												Each line of this string, including its closing quotes should get indented with every run of swift-format.
												Test test
												Test Test
												"""
}

Maybe there should be a test case for idempotence?

Bugs for swift-format can be filed at bugs.swift.org with the "swift-format" component.

1 Like

As a quick test, if you change your configuration to use spaces instead of tabs, does it still exhibit the same problem? My hunch is that the code that tries to preserve/correct the leading space in the string isn't dealing with tabs correctly.

The test harness automatically checks idempotence for all of the pretty-printer tests. That's why I think it's more likely an issue related to tabs, which we just don't have as much test coverage for.

Nope it works perfectly fine with spaces.