Skip to content

Commit 6131743

Browse files
authored
Formatting: sanitize formatting option of 0 tab-width. rdar://37835956 (#14861)
If the format option specifies using tab of zero width, we reset the tab width to a non-zero value.
1 parent b74ed23 commit 6131743

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/IDE/Formatting.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,15 @@ std::pair<LineRange, std::string> swift::ide::reformat(LineRange Range,
982982
CodeFormatOptions Options,
983983
SourceManager &SM,
984984
SourceFile &SF) {
985+
// Sanitize 0-width tab
986+
if (Options.UseTabs && !Options.TabWidth) {
987+
// If IndentWidth is specified, use it as the tab width.
988+
if (Options.IndentWidth)
989+
Options.TabWidth = Options.IndentWidth;
990+
// Otherwise, use the default value,
991+
else
992+
Options.TabWidth = 4;
993+
}
985994
FormatWalker walker(SF, SM);
986995
auto SourceBufferID = SF.getBufferID().getValue();
987996
StringRef Text = SM.getLLVMSourceMgr()

test/SourceKit/CodeFormat/indent-with-tab.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ b: Int) {}
2121
// RUN: %sourcekitd-test -req=format -line=8 -length=1 -req-opts=usetabs=1 %s >>%t.response
2222
// RUN: %sourcekitd-test -req=format -line=9 -length=1 -req-opts=usetabs=1 %s >>%t.response
2323
// RUN: %sourcekitd-test -req=format -line=12 -length=1 -req-opts=usetabs=1 %s >>%t.response
24+
// RUN: %sourcekitd-test -req=format -line=12 -length=1 -req-opts=usetabs=1 -req-opts=tabwidth=0 %s >> %t.response
2425
// RUN: %FileCheck --strict-whitespace %s <%t.response
2526

2627
// CHECK: key.sourcetext: "class Foo {"
@@ -33,3 +34,4 @@ b: Int) {}
3334
// CHECK: key.sourcetext: "\t"
3435
// CHECK: key.sourcetext: "}"
3536
// CHECK: key.sourcetext: "\t\t b: Int) {}"
37+
// CHECK: key.sourcetext: "\t\t b: Int) {}"

0 commit comments

Comments
 (0)