mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[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:
@@ -539,6 +539,40 @@ static bool isVacuousName(StringRef name) {
|
|||||||
camel_case::sameWordIgnoreFirstCase(name, "with");
|
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,
|
static StringRef omitNeedlessWords(StringRef name,
|
||||||
OmissionTypeName typeName,
|
OmissionTypeName typeName,
|
||||||
NameRole role,
|
NameRole role,
|
||||||
@@ -705,6 +739,15 @@ static StringRef omitNeedlessWords(StringRef name,
|
|||||||
// type. For example, if we matched "ViewController" in
|
// type. For example, if we matched "ViewController" in
|
||||||
// "dismissViewControllerAnimated", stitch together
|
// "dismissViewControllerAnimated", stitch together
|
||||||
// "dismissAnimated".
|
// "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 =
|
SmallString<16> newName =
|
||||||
name.substr(0, nameWordRevIter.base().getPosition());
|
name.substr(0, nameWordRevIter.base().getPosition());
|
||||||
newName
|
newName
|
||||||
@@ -743,35 +786,11 @@ static StringRef omitNeedlessWords(StringRef name,
|
|||||||
case PartOfSpeech::Gerund:
|
case PartOfSpeech::Gerund:
|
||||||
// Don't prune redundant type information from the base name if
|
// Don't prune redundant type information from the base name if
|
||||||
// there is a corresponding property (either singular or plural).
|
// there is a corresponding property (either singular or plural).
|
||||||
if (allPropertyNames && role == NameRole::BaseName) {
|
if (role == NameRole::BaseName &&
|
||||||
SmallString<16> localScratch;
|
textMatchesPropertyName(
|
||||||
auto removedText = name.substr(nameWordRevIter.base().getPosition());
|
name.substr(nameWordRevIter.base().getPosition()),
|
||||||
auto removedName = camel_case::toLowercaseWord(removedText,
|
allPropertyNames))
|
||||||
localScratch);
|
return name;
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strip off the part of the name that is redundant with
|
// Strip off the part of the name that is redundant with
|
||||||
// type information.
|
// type information.
|
||||||
@@ -1173,7 +1192,7 @@ bool swift::omitNeedlessWords(StringRef &baseName,
|
|||||||
if (!isProperty) {
|
if (!isProperty) {
|
||||||
StringRef newBaseName = ::omitNeedlessWords(baseName, contextType,
|
StringRef newBaseName = ::omitNeedlessWords(baseName, contextType,
|
||||||
NameRole::BaseNameSelf,
|
NameRole::BaseNameSelf,
|
||||||
nullptr, scratch);
|
allPropertyNames, scratch);
|
||||||
if (newBaseName != baseName) {
|
if (newBaseName != baseName) {
|
||||||
baseName = newBaseName;
|
baseName = newBaseName;
|
||||||
anyChanges = true;
|
anyChanges = true;
|
||||||
|
|||||||
@@ -32,6 +32,11 @@
|
|||||||
-(void)drawGreebies:(nonnull SEGreebieArray*)greebies;
|
-(void)drawGreebies:(nonnull SEGreebieArray*)greebies;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@interface ABCDoodle : NSObject
|
||||||
|
@property (nonatomic,copy,nonnull) NSArray<ABCDoodle *> *doodles;
|
||||||
|
-(void)addDoodle:(nonnull ABCDoodle *)doodle;
|
||||||
|
@end
|
||||||
|
|
||||||
@protocol OMWLanding
|
@protocol OMWLanding
|
||||||
-(void)flipLanding;
|
-(void)flipLanding;
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -306,6 +306,9 @@
|
|||||||
// Non-parameterized Objective-C class ending in "Array".
|
// Non-parameterized Objective-C class ending in "Array".
|
||||||
// CHECK-OMIT-NEEDLESS-WORDS: func draw(_: SEGreebieArray)
|
// CHECK-OMIT-NEEDLESS-WORDS: func draw(_: SEGreebieArray)
|
||||||
|
|
||||||
|
// Property-name sensitivity in the base name "Self" stripping.
|
||||||
|
// CHECK-OMIT-NEEDLESS-WORDS: func addDoodle(_: ABCDoodle)
|
||||||
|
|
||||||
// Protocols as contexts
|
// Protocols as contexts
|
||||||
// CHECK-OMIT-NEEDLESS-WORDS: protocol OMWLanding {
|
// CHECK-OMIT-NEEDLESS-WORDS: protocol OMWLanding {
|
||||||
// CHECK-OMIT-NEEDLESS-WORDS-NEXT: func flip()
|
// CHECK-OMIT-NEEDLESS-WORDS-NEXT: func flip()
|
||||||
|
|||||||
Reference in New Issue
Block a user