Skip to content

Commit 90af7e6

Browse files
committed
[Omit needless words] "Of" associates left except when determining properties of the result.
1 parent 4bbdec1 commit 90af7e6

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

lib/Basic/StringExtras.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,18 @@ static bool shouldPlacePrepositionOnArgLabel(StringRef beforePreposition,
873873
afterPreposition == "Z")
874874
return false;
875875

876+
// The preposition "of" binds tightly to the left word, except in
877+
// rare cases.
878+
if (camel_case::sameWordIgnoreFirstCase(preposition, "of")) {
879+
auto following = camel_case::getFirstWord(afterPreposition);
880+
if (!camel_case::sameWordIgnoreFirstCase(following, "type") &&
881+
!camel_case::sameWordIgnoreFirstCase(following, "types") &&
882+
!camel_case::sameWordIgnoreFirstCase(following, "kind") &&
883+
!camel_case::sameWordIgnoreFirstCase(following, "size") &&
884+
!camel_case::sameWordIgnoreFirstCase(following, "length"))
885+
return false;
886+
}
887+
876888
return true;
877889
}
878890

@@ -1143,12 +1155,9 @@ bool swift::omitNeedlessWords(StringRef &baseName,
11431155
}
11441156

11451157
// If needed, split the base name.
1146-
bool didSplitBaseName = false;
11471158
if (!argNames.empty() &&
1148-
splitBaseName(baseName, argNames[0], paramTypes[0], firstParamName)) {
1149-
didSplitBaseName = true;
1159+
splitBaseName(baseName, argNames[0], paramTypes[0], firstParamName))
11501160
anyChanges = true;
1151-
}
11521161

11531162
// Omit needless words based on parameter types.
11541163
for (unsigned i = 0, n = argNames.size(); i != n; ++i) {
@@ -1181,16 +1190,5 @@ bool swift::omitNeedlessWords(StringRef &baseName,
11811190
}
11821191
}
11831192

1184-
// Place a "lonely of" on the base name, rather than having it as
1185-
// the first argument label.
1186-
if (didSplitBaseName &&
1187-
camel_case::sameWordIgnoreFirstCase(argNames[0], "of")) {
1188-
SmallString<16> newBaseName;
1189-
newBaseName += baseName;
1190-
newBaseName += "Of";
1191-
baseName = scratch.copyString(newBaseName);
1192-
argNames[0] = StringRef();
1193-
}
1194-
11951193
return lowercaseAcronymsForReturn();
11961194
}

test/IDE/Inputs/custom-modules/OmitNeedlessWords.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
-(void)methodAndReturnError:(NSError **)error;
1414
-(nullable Class)typeOfString:(nonnull NSString *)string;
1515
-(nullable Class)typeOfNamedString:(nonnull NSString *)string;
16+
-(nullable Class)typeOfTypeNamed:(nonnull NSString *)string;
1617
-(void)appendWithContentsOfString:(nonnull NSString *)string;
1718
@end

test/IDE/print_omit_needless_words.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,10 @@
279279

280280
// "Of" associates left.
281281
// CHECK-OMIT-NEEDLESS-WORDS: func typeOf(_: String)
282-
// CHECK-OMIT-NEEDLESS-WORDS: func type(ofNamedString _: String)
282+
// CHECK-OMIT-NEEDLESS-WORDS: func typeOf(namedString _: String)
283+
284+
// ... except for some properties of the result.
285+
// CHECK-OMIT-NEEDLESS-WORDS: func type(ofTypeNamed _: String)
283286

284287
// Look for preposition prior to "of".
285288
// CHECK-OMIT-NEEDLESS-WORDS: func append(contentsOf _: String)

0 commit comments

Comments
 (0)