Skip to content

Commit

Permalink
[Omit needless words] Use the getter name for BOOL properties.
Browse files Browse the repository at this point in the history
Rather than employ complicated heuristics to determine when to add
"is" to the name of a Booleam property, just trust the name of the
getter.
  • Loading branch information
DougGregor committed Jan 5, 2016
1 parent ba2e534 commit 4d24800
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 66 deletions.
1 change: 0 additions & 1 deletion include/swift/Basic/StringExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ namespace swift {
Unknown,
Preposition,
Verb,
AuxiliaryVerb,
Gerund,
};

Expand Down
31 changes: 0 additions & 31 deletions lib/Basic/PartsOfSpeech.def
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
# define VERB(Word)
#endif

#ifndef AUXILIARY_VERB
# define AUXILIARY_VERB(Word)
#endif

DIRECTIONAL_PREPOSITION(above)
DIRECTIONAL_PREPOSITION(after)
DIRECTIONAL_PREPOSITION(along)
Expand Down Expand Up @@ -842,33 +838,6 @@ VERB(yell)
VERB(zip)
VERB(zoom)

AUXILIARY_VERB(am)
AUXILIARY_VERB(are)
AUXILIARY_VERB(been)
AUXILIARY_VERB(being)
AUXILIARY_VERB(can)
AUXILIARY_VERB(could)
AUXILIARY_VERB(did)
AUXILIARY_VERB(does)
AUXILIARY_VERB(had)
AUXILIARY_VERB(has)
AUXILIARY_VERB(have)
AUXILIARY_VERB(having)
AUXILIARY_VERB(is)
AUXILIARY_VERB(may)
AUXILIARY_VERB(might)
AUXILIARY_VERB(must)
AUXILIARY_VERB(need)
AUXILIARY_VERB(needs)
AUXILIARY_VERB(ought)
AUXILIARY_VERB(shall)
AUXILIARY_VERB(should)
AUXILIARY_VERB(was)
AUXILIARY_VERB(were)
AUXILIARY_VERB(will)
AUXILIARY_VERB(would)

#undef AUXILIARY_VERB
#undef VERB
#undef DIRECTIONAL_PREPOSITION
#undef PREPOSITION
32 changes: 0 additions & 32 deletions lib/Basic/StringExtras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ PartOfSpeech swift::getPartOfSpeech(StringRef word) {
#define VERB(Word) \
if (word.equals_lower(#Word)) \
return PartOfSpeech::Verb;
#define AUXILIARY_VERB(Word) \
if (word.equals_lower(#Word)) \
return PartOfSpeech::AuxiliaryVerb;
#include "PartsOfSpeech.def"

// Identify gerunds, which always end in "ing".
Expand Down Expand Up @@ -671,7 +668,6 @@ static StringRef omitNeedlessWords(StringRef name,
break;

case PartOfSpeech::Unknown:
case PartOfSpeech::AuxiliaryVerb:
// Assume it's a noun or adjective; don't strip anything.
break;
}
Expand Down Expand Up @@ -703,23 +699,6 @@ static StringRef omitNeedlessWords(StringRef name,
return name;
}

/// Determine whether the given word indicates a boolean result.
static bool nameIndicatesBooleanResult(StringRef name) {
for (auto word: camel_case::getWords(name)) {
// Auxiliary verbs indicate Boolean results.
if (getPartOfSpeech(word) == PartOfSpeech::AuxiliaryVerb)
return true;

// Words that end in "s" indicate either Boolean results---it
// could be a verb in the present continuous tense---or some kind
// of plural, for which "is" would be inappropriate anyway.
if (word.back() == 's')
return true;
}

return false;
}

/// A form of toLowercaseWord that also lowercases acronyms.
static StringRef toLowercaseWordAndAcronym(StringRef string,
StringScratchSpace &scratch) {
Expand Down Expand Up @@ -812,16 +791,6 @@ bool swift::omitNeedlessWords(StringRef &baseName,
}
}

// Boolean properties should start with "is", unless their
// first word already implies a Boolean result.
if (resultType.isBoolean() && isProperty &&
!nameIndicatesBooleanResult(baseName)) {
SmallString<32> newName("is");
camel_case::appendSentenceCase(newName, baseName);
baseName = scratch.copyString(newName);
anyChanges = true;
}

return lowercaseAcronymsForReturn();
}

Expand Down Expand Up @@ -871,7 +840,6 @@ bool swift::omitNeedlessWords(StringRef &baseName,
break;

case PartOfSpeech::Unknown:
case PartOfSpeech::AuxiliaryVerb:
++nameWordRevIter;
break;
}
Expand Down
10 changes: 10 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2209,6 +2209,16 @@ auto ClangImporter::Implementation::importFullName(
case clang::DeclarationName::Identifier:
// Map the identifier.
baseName = D->getDeclName().getAsIdentifierInfo()->getName();

if (OmitNeedlessWords) {
// For Objective-C BOOL properties, use the name of the getter
// which, conventionally, has an "is" prefix.
if (auto property = dyn_cast<clang::ObjCPropertyDecl>(D)) {
if (isBoolType(clangSema.Context, property->getType()))
baseName = property->getGetterName().getNameForSlot(0);
}
}

break;

case clang::DeclarationName::ObjCMultiArgSelector:
Expand Down
7 changes: 5 additions & 2 deletions test/IDE/print_omit_needless_words.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
// Note: Typedefs with a "_t" suffix".
// CHECK-FOUNDATION: func subtract(_: Int32) -> NSNumber

// Note: Respect the getter name for BOOL properties.
// CHECK-FOUNDATION: var isMakingHoney: Bool

// Note: multi-word enum name matching; "with" splits the first piece.
// CHECK-FOUNDATION: func someMethod(deprecatedOptions _: NSDeprecatedOptions = [])

Expand Down Expand Up @@ -138,8 +141,8 @@
// Collection element types.
// CHECK-FOUNDATION: func adding(_: AnyObject) -> Set<NSObject>

// Boolean properties get an "is" prefix.
// CHECK-FOUNDATION: var isEmpty: Bool { get }
// Boolean properties follow the getter.
// CHECK-FOUNDATION: var empty: Bool { get }
// CHECK-FOUNDATION: func nonEmpty() -> Bool
// CHECK-FOUNDATION: var isStringSet: Bool { get }
// CHECK-FOUNDATION: var wantsAUnion: Bool { get }
Expand Down

0 comments on commit 4d24800

Please sign in to comment.