Skip to content

Formatting: sanitize formatting option of 0 tab-width. rdar://37835956 #14861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/IDE/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,15 @@ std::pair<LineRange, std::string> 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()
Expand Down
2 changes: 2 additions & 0 deletions test/SourceKit/CodeFormat/indent-with-tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {"
Expand All @@ -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) {}"