Omit needless words: prefer removing leading redundant type information first.

We intentionally avoid collapsing a method name down to just a
preposition. When we end up in such cases, prefer to strip leading
redundant type information (which corresponds to both context and
return types) rather than trailing type information. This can help
keep close method families together, e.g., in NSFontDescriptor:

-  func fontDescriptorWith(_: UIFontDescriptorSymbolicTraits) -> UIFontDescriptor
-  func fontDescriptorWithSize(_: CGFloat) -> UIFontDescriptor
-  func fontDescriptorWithMatrix(_: CGAffineTransform) ->  UIFontDescriptor
-  func fontDescriptorWithFace(_: String) -> UIFontDescriptor
-  func fontDescriptorWithFamily(_: String) -> UIFontDescriptor
+  func withSymbolicTraits(_: UIFontDescriptorSymbolicTraits) ->  UIFontDescriptor
+  func withSize(_: CGFloat) -> UIFontDescriptor
+  func withMatrix(_: CGAffineTransform) -> UIFontDescriptor
+  func withFace(_: String) -> UIFontDescriptor
+  func withFamily(_: String) -> UIFontDescriptor

Note especially the first case, where we don't want to just go down to
"with" for the name, even though "SymbolicTraits" is redundant with
the parameter type.

Swift SVN r31702
This commit is contained in:
Doug Gregor
2015-09-04 23:57:09 +00:00
parent 60b770b60d
commit 5b0423c9f2
3 changed files with 27 additions and 13 deletions

View File

@@ -14,6 +14,7 @@
// names.
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/Fallthrough.h"
#include "swift/Basic/StringExtras.h"
#include "clang/Basic/CharInfo.h"
#include "llvm/ADT/ArrayRef.h"
@@ -526,13 +527,23 @@ StringRef swift::omitNeedlessWords(StringRef name, OmissionTypeName typeName,
// information we would strip off.
switch (getPartOfSpeech(*nameWordRevIter)) {
case PartOfSpeech::Preposition:
if (role == NameRole::BaseName) {
// Strip off the part of the name that is redundant with
// type information, so long as there's something preceding the
// preposition.
if (std::next(nameWordRevIter) != nameWordRevIterEnd)
name = name.substr(0, nameWordRevIter.base().getPosition());
break;
}
SWIFT_FALLTHROUGH;
case PartOfSpeech::Verb:
case PartOfSpeech::Gerund: {
case PartOfSpeech::Gerund:
// Strip off the part of the name that is redundant with
// type information.
name = name.substr(0, nameWordRevIter.base().getPosition());
break;
}
case PartOfSpeech::Unknown:
// Assume it's a noun or adjective; don't strip anything.
@@ -594,8 +605,17 @@ bool swift::omitNeedlessWords(StringRef &baseName,
return false;
}
// Omit needless words based on parameter types.
// Omit needless words in the base name based on the result type.
bool anyChanges = false;
StringRef newBaseName
= omitNeedlessWordsForResultType(baseName, resultType,
resultTypeMatchesContext, scratch);
if (newBaseName != baseName) {
baseName = newBaseName;
anyChanges = true;
}
// Omit needless words based on parameter types.
for (unsigned i = 0, n = argNames.size(); i != n; ++i) {
// If there is no corresponding parameter, there is nothing to
// omit.
@@ -621,14 +641,5 @@ bool swift::omitNeedlessWords(StringRef &baseName,
}
}
// Omit needless words in the base name based on the result type.
StringRef newBaseName
= omitNeedlessWordsForResultType(baseName, resultType,
resultTypeMatchesContext, scratch);
if (newBaseName != baseName) {
baseName = newBaseName;
anyChanges = true;
}
return anyChanges;
}