diff --git a/lib/IDE/Formatting.cpp b/lib/IDE/Formatting.cpp index 977d256ce8d83..18d7786b25c2e 100644 --- a/lib/IDE/Formatting.cpp +++ b/lib/IDE/Formatting.cpp @@ -982,6 +982,15 @@ std::pair swift::ide::reformat(LineRange Range, CodeFormatOptions Options, SourceManager &SM, SourceFile &SF) { + // Sanitize 0-width tab + if (Options.UseTabs && !Options.TabWidth) { + // If IndentWidth is specified, use it as the tab width. + if (Options.IndentWidth) + Options.TabWidth = Options.IndentWidth; + // Otherwise, use the default value, + else + Options.TabWidth = 4; + } FormatWalker walker(SF, SM); auto SourceBufferID = SF.getBufferID().getValue(); StringRef Text = SM.getLLVMSourceMgr() diff --git a/test/SourceKit/CodeFormat/indent-with-tab.swift b/test/SourceKit/CodeFormat/indent-with-tab.swift index 0cdcaa67171b3..fdc928d1dba80 100644 --- a/test/SourceKit/CodeFormat/indent-with-tab.swift +++ b/test/SourceKit/CodeFormat/indent-with-tab.swift @@ -21,6 +21,7 @@ b: Int) {} // RUN: %sourcekitd-test -req=format -line=8 -length=1 -req-opts=usetabs=1 %s >>%t.response // RUN: %sourcekitd-test -req=format -line=9 -length=1 -req-opts=usetabs=1 %s >>%t.response // RUN: %sourcekitd-test -req=format -line=12 -length=1 -req-opts=usetabs=1 %s >>%t.response +// RUN: %sourcekitd-test -req=format -line=12 -length=1 -req-opts=usetabs=1 -req-opts=tabwidth=0 %s >> %t.response // RUN: %FileCheck --strict-whitespace %s <%t.response // CHECK: key.sourcetext: "class Foo {" @@ -33,3 +34,4 @@ b: Int) {} // CHECK: key.sourcetext: "\t" // CHECK: key.sourcetext: "}" // CHECK: key.sourcetext: "\t\t b: Int) {}" +// CHECK: key.sourcetext: "\t\t b: Int) {}"