mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Revert "Omit needless words: split base names on the last preposition."
This reverts commit r31976; this was still not a good idea. Swift SVN r32079
This commit is contained in:
@@ -440,7 +440,7 @@ StringRef swift::omitNeedlessWords(StringRef name, OmissionTypeName typeName,
|
||||
StringScratchSpace &scratch) {
|
||||
if (name.empty() || typeName.empty()) return name;
|
||||
|
||||
// "usingBlock" -> "body" and "withBlock" -> "body" for block parameters.
|
||||
// "usingBlock" -> "body" for block parameters.
|
||||
if ((role == NameRole::FirstParameter ||
|
||||
role == NameRole::SubsequentParameter) &&
|
||||
(name == "usingBlock" || name == "withBlock") &&
|
||||
@@ -567,6 +567,19 @@ StringRef swift::omitNeedlessWords(StringRef name, OmissionTypeName typeName,
|
||||
break;
|
||||
}
|
||||
|
||||
// If removing the type information would leave us with a
|
||||
// parameter named "with", lowercase the type information and
|
||||
// keep that instead.
|
||||
if ((role == NameRole::FirstParameter ||
|
||||
role == NameRole::SubsequentParameter) &&
|
||||
*nameWordRevIter == "with" &&
|
||||
std::next(nameWordRevIter) == nameWordRevIterEnd) {
|
||||
name = toLowercaseWord(
|
||||
name.substr(nameWordRevIter.base().getPosition()),
|
||||
scratch);
|
||||
break;
|
||||
}
|
||||
|
||||
SWIFT_FALLTHROUGH;
|
||||
|
||||
case PartOfSpeech::Verb:
|
||||
@@ -659,62 +672,66 @@ bool swift::omitNeedlessWords(StringRef &baseName,
|
||||
: NameRole::FirstParameter;
|
||||
|
||||
StringRef name = role == NameRole::BaseName ? baseName : argNames[i];
|
||||
StringRef newName = omitNeedlessWords(name, paramTypes[i], role, scratch);
|
||||
|
||||
// If there is a preposition in the base name, split the base name
|
||||
// at that preposition.
|
||||
// Split the base name on the preposition "with", if it's available.
|
||||
if (role == NameRole::BaseName) {
|
||||
// Scan backwards for a preposition.
|
||||
auto nameWords = camel_case::getWords(name);
|
||||
// Scan backwards for the preposition "With".
|
||||
auto nameWords = camel_case::getWords(newName);
|
||||
auto nameWordRevIter = nameWords.rbegin(),
|
||||
nameWordRevIterEnd = nameWords.rend();
|
||||
bool found = false, done = false;
|
||||
while (nameWordRevIter != nameWordRevIterEnd && !done) {
|
||||
switch (getPartOfSpeech(*nameWordRevIter)) {
|
||||
case PartOfSpeech::Preposition:
|
||||
found = true;
|
||||
done = true;
|
||||
break;
|
||||
|
||||
case PartOfSpeech::Verb:
|
||||
case PartOfSpeech::Gerund:
|
||||
// Don't skip over verbs or gerunds.
|
||||
done = true;
|
||||
break;
|
||||
|
||||
case PartOfSpeech::Unknown:
|
||||
++nameWordRevIter;
|
||||
break;
|
||||
}
|
||||
while (nameWordRevIter != nameWordRevIterEnd &&
|
||||
!(*nameWordRevIter == "With")) {
|
||||
++nameWordRevIter;
|
||||
}
|
||||
|
||||
// If we found a preposition that's not at the beginning of the
|
||||
// name, split there.
|
||||
if (found) {
|
||||
++nameWordRevIter;
|
||||
unsigned splitPos = nameWordRevIter.base().getPosition();
|
||||
if (splitPos > 0) {
|
||||
// Update the base name by splitting at the preposition.
|
||||
anyChanges = true;
|
||||
baseName = name.substr(0, splitPos);
|
||||
// If we found "With" somewhere...
|
||||
if (nameWordRevIter != nameWordRevIterEnd) {
|
||||
// That isn't at the end...
|
||||
unsigned afterWithPos = nameWordRevIter.base().getPosition();
|
||||
if (afterWithPos > 4) {
|
||||
// Find the text that follows "with". If there is nothing
|
||||
// following "With" in the new name, use the original name.
|
||||
StringRef remainingName;
|
||||
if (afterWithPos == newName.size())
|
||||
remainingName = name.substr(afterWithPos);
|
||||
else
|
||||
remainingName = newName.substr(afterWithPos);
|
||||
|
||||
// Create a first argument name with the remainder of the base name,
|
||||
// lowercased.
|
||||
name = toLowercaseWord(name.substr(splitPos), scratch);
|
||||
argNames[0] = name;
|
||||
role = NameRole::FirstParameter;
|
||||
// The first argument name is either "body" (for
|
||||
// "WithBlock") or the lowercased text that followed "with".
|
||||
if (remainingName == "Block")
|
||||
argNames[0] = "body";
|
||||
else
|
||||
argNames[0] = toLowercaseWord(remainingName, scratch);
|
||||
|
||||
// The base name is everything up to (but not including) the "with".
|
||||
baseName = name.substr(0, afterWithPos-4);
|
||||
|
||||
anyChanges = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Omit needless words from the name.
|
||||
StringRef newName = omitNeedlessWords(name, paramTypes[i], role, scratch);
|
||||
if (name == newName) continue;
|
||||
|
||||
anyChanges = true;
|
||||
|
||||
// Record this change.
|
||||
if (role == NameRole::BaseName) {
|
||||
baseName = newName;
|
||||
// If, after dropping type information, the last word of the
|
||||
// base name is "Using", drop the "Using" from the base name and
|
||||
// use "body" as a label for the first parameter.
|
||||
StringRef remainingName = name.substr(newName.size());
|
||||
if (camel_case::getLastWord(newName) == "Using" &&
|
||||
remainingName == "Block") {
|
||||
argNames[0] = "body";
|
||||
baseName = newName.substr(0, newName.size() - 5);
|
||||
} else {
|
||||
// Otherwise, adopt the new base name.
|
||||
baseName = newName;
|
||||
}
|
||||
} else {
|
||||
argNames[i] = newName;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user