Omit needless words: never reduce a name down to "using", "for", or

"with"

Treat these prepositions as vacuous names, so we never reduce any
parameter name down to one of them. If that would happen, leave the
entire parameter name alone. This makes our transformation a bit more conservative.

Swift SVN r32460
This commit is contained in:
Doug Gregor
2015-10-06 07:40:34 +00:00
parent e60e5a3605
commit 2dcc3afd34
4 changed files with 31 additions and 31 deletions

View File

@@ -467,7 +467,11 @@ static StringRef omitNeedlessWordsFromPrefix(StringRef name,
/// Identify certain vacuous names to which we do not want to reduce any name.
static bool isVacuousName(StringRef name) {
return name == "set" || name == "get";
return camel_case::sameWordIgnoreFirstCase(name, "get") ||
camel_case::sameWordIgnoreFirstCase(name, "for") ||
camel_case::sameWordIgnoreFirstCase(name, "set") ||
camel_case::sameWordIgnoreFirstCase(name, "using") ||
camel_case::sameWordIgnoreFirstCase(name, "with");
}
static StringRef omitNeedlessWords(StringRef name,
@@ -628,17 +632,7 @@ static StringRef omitNeedlessWords(StringRef name,
return origName;
break;
case NameRole::SubsequentParameter: {
// For subsequent parameters, drop useless leading prepositions such as
// "with" and "using".
StringRef firstWord = camel_case::getFirstWord(name);
if (firstWord.size() < name.size() &&
(firstWord == "with" || firstWord == "using")) {
name = toLowercaseWord(name.substr(firstWord.size()), scratch);
}
break;
}
case NameRole::SubsequentParameter:
case NameRole::FirstParameter:
case NameRole::Partial:
break;
@@ -716,6 +710,9 @@ static bool omitNeedlessWordsMatchingFirstArgumentLabel(
unsigned splitPos = nameWordRevIter.base().getPosition();
if (splitPos == 0) return false;
// If we would be left with a vacuous argument name, don't split.
if (isVacuousName(name.substr(splitPos))) return false;
// Split into base name/first argument label.
argName = ::toLowercaseWord(name.substr(splitPos), scratch);
name = name.substr(0, splitPos);
@@ -875,8 +872,14 @@ bool swift::omitNeedlessWords(StringRef &baseName,
afterSplitPos += 3;
// Create a first argument name with the remainder of the base name,
// lowercased.
argNames[0] = toLowercaseWord(newName.substr(afterSplitPos), scratch);
// lowercased. If we would end up with a vacuous name, go
// back and get the original.
StringRef newArgName = newName.substr(afterSplitPos);
if (isVacuousName(newArgName)) {
size_t pos = name.rfind(newArgName);
newArgName = name.substr(pos);
}
argNames[0] = toLowercaseWord(newArgName, scratch);
// Update the base name by splitting at the preposition.
newName = newName.substr(0, splitPos);