[Omit needless words] Use the property lists to restrict BaseNameSelf pruning.

This eliminates some apparent inconsistencies in the translation,
fixing rdar://problem/24860176.
This commit is contained in:
Doug Gregor
2016-02-29 12:46:25 -08:00
parent 56785e81a6
commit b890e4c29f
3 changed files with 57 additions and 30 deletions

View File

@@ -539,6 +539,40 @@ static bool isVacuousName(StringRef name) {
camel_case::sameWordIgnoreFirstCase(name, "with");
}
/// Determine whether the given text matches a property name.
static bool textMatchesPropertyName(StringRef text,
const InheritedNameSet *allPropertyNames) {
if (!allPropertyNames) return false;
SmallString<16> localScratch;
auto name = camel_case::toLowercaseWord(text, localScratch);
// A property with exactly this name.
if (allPropertyNames->contains(name)) return true;
// From here on, we'll be working with scratch space.
if (name.data() != localScratch.data())
localScratch = name;
if (localScratch.back() == 'y') {
// If the last letter is a 'y', try 'ies'.
localScratch.pop_back();
localScratch += "ies";
if (allPropertyNames->contains(localScratch)) return true;
} else {
// Otherwise, add an 's' and try again.
localScratch += 's';
if (allPropertyNames->contains(localScratch)) return true;
// Alternatively, try to add 'es'.
localScratch.pop_back();
localScratch += "es";
if (allPropertyNames->contains(localScratch)) return true;
}
return false;
}
static StringRef omitNeedlessWords(StringRef name,
OmissionTypeName typeName,
NameRole role,
@@ -705,6 +739,15 @@ static StringRef omitNeedlessWords(StringRef name,
// type. For example, if we matched "ViewController" in
// "dismissViewControllerAnimated", stitch together
// "dismissAnimated".
// Don't prune redundant type information from the base name if
// there is a corresponding property (either singular or plural).
StringRef removedText =
name.substr(nameWordRevIter.base().getPosition(),
firstMatchingNameWordRevIter.base().getPosition());
if (textMatchesPropertyName(removedText, allPropertyNames))
return name;
SmallString<16> newName =
name.substr(0, nameWordRevIter.base().getPosition());
newName
@@ -743,35 +786,11 @@ static StringRef omitNeedlessWords(StringRef name,
case PartOfSpeech::Gerund:
// Don't prune redundant type information from the base name if
// there is a corresponding property (either singular or plural).
if (allPropertyNames && role == NameRole::BaseName) {
SmallString<16> localScratch;
auto removedText = name.substr(nameWordRevIter.base().getPosition());
auto removedName = camel_case::toLowercaseWord(removedText,
localScratch);
// A property with exactly this name.
if (allPropertyNames->contains(removedName)) return name;
// From here on, we'll be working with scratch space.
if (removedName.data() != localScratch.data())
localScratch = removedName;
if (localScratch.back() == 'y') {
// If the last letter is a 'y', try 'ies'.
localScratch.pop_back();
localScratch += "ies";
if (allPropertyNames->contains(localScratch)) return name;
} else {
// Otherwise, add an 's' and try again.
localScratch += 's';
if (allPropertyNames->contains(localScratch)) return name;
// Alternatively, try to add 'es'.
localScratch.pop_back();
localScratch += "es";
if (allPropertyNames->contains(localScratch)) return name;
}
}
if (role == NameRole::BaseName &&
textMatchesPropertyName(
name.substr(nameWordRevIter.base().getPosition()),
allPropertyNames))
return name;
// Strip off the part of the name that is redundant with
// type information.
@@ -1173,7 +1192,7 @@ bool swift::omitNeedlessWords(StringRef &baseName,
if (!isProperty) {
StringRef newBaseName = ::omitNeedlessWords(baseName, contextType,
NameRole::BaseNameSelf,
nullptr, scratch);
allPropertyNames, scratch);
if (newBaseName != baseName) {
baseName = newBaseName;
anyChanges = true;