[Omit needless words] Match type/name words ignoring case

This commit is contained in:
Doug Gregor
2016-02-05 22:14:55 -08:00
parent 10c2a48ffc
commit bd8fc9614e
4 changed files with 22 additions and 4 deletions

View File

@@ -355,7 +355,9 @@ static bool matchNameWordToTypeWord(StringRef nameWord, StringRef typeWord) {
// We can match the suffix of the type so long as everything preceding the // We can match the suffix of the type so long as everything preceding the
// match is neither a lowercase letter nor a '_'. This ignores type // match is neither a lowercase letter nor a '_'. This ignores type
// prefixes for acronyms, e.g., the 'NS' in 'NSURL'. // prefixes for acronyms, e.g., the 'NS' in 'NSURL'.
if (typeWord.endswith(nameWord)) { if (typeWord.endswith(nameWord) ||
(typeWord.endswith_lower(nameWord) &&
!clang::isLowercase(typeWord[typeWord.size()-nameWord.size()]))) {
// Check that everything preceding the match is neither a lowercase letter // Check that everything preceding the match is neither a lowercase letter
// nor a '_'. // nor a '_'.
for (unsigned i = 0, n = nameWord.size(); i != n; ++i) { for (unsigned i = 0, n = nameWord.size(); i != n; ++i) {
@@ -367,7 +369,7 @@ static bool matchNameWordToTypeWord(StringRef nameWord, StringRef typeWord) {
// We can match a prefix so long as everything following the match is // We can match a prefix so long as everything following the match is
// a number. // a number.
if (camel_case::startsWithIgnoreFirstCase(typeWord, nameWord)) { if (typeWord.startswith_lower(nameWord)) {
for (unsigned i = nameWord.size(), n = typeWord.size(); i != n; ++i) { for (unsigned i = nameWord.size(), n = typeWord.size(); i != n; ++i) {
if (!clang::isDigit(typeWord[i])) return false; if (!clang::isDigit(typeWord[i])) return false;
} }
@@ -379,7 +381,7 @@ static bool matchNameWordToTypeWord(StringRef nameWord, StringRef typeWord) {
} }
// Check for an exact match. // Check for an exact match.
return camel_case::sameWordIgnoreFirstCase(nameWord, typeWord); return nameWord.equals_lower(typeWord);
} }
/// Match the beginning of the name to the given type name. /// Match the beginning of the name to the given type name.
@@ -520,7 +522,6 @@ static StringRef omitNeedlessWords(StringRef name,
auto typeWordRevIter = typeWords.rbegin(), auto typeWordRevIter = typeWords.rbegin(),
typeWordRevIterEnd = typeWords.rend(); typeWordRevIterEnd = typeWords.rend();
bool anyMatches = false; bool anyMatches = false;
auto matched = [&] { auto matched = [&] {
if (anyMatches) return; if (anyMatches) return;

View File

@@ -0,0 +1,7 @@
@import Foundation;
@interface OmitNeedlessWords : NSObject
-(void)jumpToUrl:(nonnull NSURL *)url;
-(BOOL)objectIsCompatibleWithObject:(nonnull id)other;
-(void)insetByX:(NSInteger)x y:(NSInteger)y;
@end

View File

@@ -0,0 +1,4 @@
module OmitNeedlessWords {
export *
header "OmitNeedlessWords.h"
}

View File

@@ -21,6 +21,9 @@
// RUN: %target-swift-ide-test(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t -I %S/../ClangModules/Inputs/custom-modules) -print-module -source-filename %s -module-to-print=CoreCooling -function-definitions=false -prefer-type-repr=true -enable-omit-needless-words -skip-parameter-names -enable-infer-default-arguments > %t.CoreCooling.txt // RUN: %target-swift-ide-test(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t -I %S/../ClangModules/Inputs/custom-modules) -print-module -source-filename %s -module-to-print=CoreCooling -function-definitions=false -prefer-type-repr=true -enable-omit-needless-words -skip-parameter-names -enable-infer-default-arguments > %t.CoreCooling.txt
// RUN: FileCheck %s -check-prefix=CHECK-CORECOOLING -strict-whitespace < %t.CoreCooling.txt // RUN: FileCheck %s -check-prefix=CHECK-CORECOOLING -strict-whitespace < %t.CoreCooling.txt
// RUN: %target-swift-ide-test(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t -I %S/Inputs/custom-modules) -print-module -source-filename %s -module-to-print=OmitNeedlessWords -function-definitions=false -prefer-type-repr=true -enable-omit-needless-words -skip-parameter-names -enable-infer-default-arguments > %t.OmitNeedlessWords.txt
// RUN: FileCheck %s -check-prefix=CHECK-OMIT-NEEDLESS-WORDS -strict-whitespace < %t.OmitNeedlessWords.txt
// RUN: %target-swift-ide-test(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -print-module -source-filename %s -module-to-print=errors -function-definitions=false -prefer-type-repr=true -enable-omit-needless-words -skip-parameter-names -enable-infer-default-arguments > %t.errors.txt // RUN: %target-swift-ide-test(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -print-module -source-filename %s -module-to-print=errors -function-definitions=false -prefer-type-repr=true -enable-omit-needless-words -skip-parameter-names -enable-infer-default-arguments > %t.errors.txt
// RUN: FileCheck %s -check-prefix=CHECK-ERRORS -strict-whitespace < %t.errors.txt // RUN: FileCheck %s -check-prefix=CHECK-ERRORS -strict-whitespace < %t.errors.txt
@@ -249,5 +252,8 @@
// CHECK-APPKIT: func add(_: Rect) // CHECK-APPKIT: func add(_: Rect)
// CHECK-APPKIT: class func conjureRect(_: Rect) // CHECK-APPKIT: class func conjureRect(_: Rect)
// CHECK-OMIT-NEEDLESS-WORDS: func jumpTo(_: URL)
// CHECK-OMIT-NEEDLESS-WORDS: func objectIsCompatibleWith(_: AnyObject) -> Bool
// CHECK-OMIT-NEEDLESS-WORDS: func insetByX(_: Int, y: Int)
// Don't drop the 'error'. // Don't drop the 'error'.
// CHECK-ERRORS: func tryAndReturnError(_: ()) throws // CHECK-ERRORS: func tryAndReturnError(_: ()) throws