From dfa1ffe6500881c74d71a109902478ee5a37a52f Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 25 Oct 2017 18:00:59 -0700 Subject: [PATCH] Cleanup and reordering --- src/compiler/core.ts | 55 ++++++++++++++----------- src/compiler/utilities.ts | 2 +- src/harness/unittests/compileOnSave.ts | 1 - src/services/navigateTo.ts | 6 +-- src/services/navigationBar.ts | 7 ++-- src/services/refactors/extractSymbol.ts | 31 +++----------- 6 files changed, 43 insertions(+), 59 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 94ab45a6f6a..3457af9d174 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1490,6 +1490,16 @@ namespace ts { return a === b; } + /** + * Compare equality between two strings using an ordinal comparison. + * + * Case-insensitive comparisons compare both strings after applying `toUpperCase` to + * each string. + */ + export function equateStrings(a: string, b: string, ignoreCase: boolean) { + return ignoreCase ? equateStringsCaseInsensitive(a, b) : equateStringsCaseSensitive(a, b); + } + export function equateStringsCaseInsensitive(a: string, b: string) { return a === b || a !== undefined @@ -1501,16 +1511,6 @@ namespace ts { return equateValues(a, b); } - /** - * Compare equality between two strings using an ordinal comparison. - * - * Case-insensitive comparisons compare both strings after applying `toUpperCase` to - * each string. - */ - export function equateStrings(a: string, b: string, ignoreCase: boolean) { - return ignoreCase ? equateStringsCaseInsensitive(a, b) : equateStringsCaseSensitive(a, b); - } - export function getStringEqualityComparer(ignoreCase: boolean) { return ignoreCase ? equateStringsCaseInsensitive : equateStringsCaseSensitive; } @@ -1557,6 +1557,20 @@ namespace ts { return ignoreCase ? compareStringsCaseInsensitive : compareStringsCaseSensitive; } + /** + * Compare two strings using the sort behavior of the UI locale. + * + * Ordering is not predictable between different host locales, but is best for displaying + * ordered data for UI presentation. Characters with multiple unicode representations may + * be considered equal. + * + * Case-insensitive comparisons compare strings that differ in only base characters or + * accents/diacritic marks as unequal. + */ + export function compareStringsUI(a: string, b: string, ignoreCase: boolean) { + return ignoreCase ? compareStringsCaseInsensitiveUI(a, b) : compareStringsCaseSensitiveUI(a, b); + } + /** * Creates a string comparer for use with string collation in the UI. */ @@ -1660,24 +1674,17 @@ namespace ts { return comparer(a, b); } - /** - * Compare two strings using the sort behavior of the UI locale. - * - * Ordering is not predictable between different host locales, but is best for displaying - * ordered data for UI presentation. Characters with multiple unicode representations may - * be considered equal. - * - * Case-insensitive comparisons compare strings that differ in only base characters or - * accents/diacritic marks as unequal. - */ - export function compareStringsUI(a: string, b: string, ignoreCase: boolean) { - return ignoreCase ? compareStringsCaseInsensitiveUI(a, b) : compareStringsCaseSensitiveUI(a, b); - } - export function getStringComparerUI(ignoreCase: boolean) { return ignoreCase ? compareStringsCaseInsensitiveUI : compareStringsCaseSensitiveUI; } + export function compareProperties(a: T, b: T, key: keyof T) { + return a === b ? Comparison.EqualTo : + a === undefined ? Comparison.LessThan : + b === undefined ? Comparison.GreaterThan : + compareValues(a[key], b[key]); + } + function getDiagnosticFileName(diagnostic: Diagnostic): string { return diagnostic.file ? diagnostic.file.fileName : undefined; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 4c75215acb5..a3a8ca3afc7 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -3949,7 +3949,7 @@ namespace ts { trySetLanguageAndTerritory(language, /*territory*/ undefined, errors); } - // Set the locale for UI collation + // Set the UI locale for string collation setUILocale(locale); function trySetLanguageAndTerritory(language: string, territory: string, errors?: Push): boolean { diff --git a/src/harness/unittests/compileOnSave.ts b/src/harness/unittests/compileOnSave.ts index 43becc3fdb1..d6d22b162bf 100644 --- a/src/harness/unittests/compileOnSave.ts +++ b/src/harness/unittests/compileOnSave.ts @@ -13,7 +13,6 @@ namespace ts.projectSystem { describe("CompileOnSave affected list", () => { function sendAffectedFileRequestAndCheckResult(session: server.Session, request: server.protocol.Request, expectedFileList: { projectFileName: string, files: FileOrFolder[] }[]) { const response = session.executeCommand(request).response as server.protocol.CompileOnSaveAffectedFileListSingleProject[]; - // File-system ordering should use a predictable order const comparer = getStringComparer(/*ignoreCase*/ false); const actualResult = response.sort((list1, list2) => comparer(list1.projectFileName, list2.projectFileName)); expectedFileList = expectedFileList.sort((list1, list2) => comparer(list1.projectFileName, list2.projectFileName)); diff --git a/src/services/navigateTo.ts b/src/services/navigateTo.ts index 603342df7a8..762726adb77 100644 --- a/src/services/navigateTo.ts +++ b/src/services/navigateTo.ts @@ -173,10 +173,10 @@ namespace ts.NavigateTo { return bestMatchKind; } - function compareNavigateToItems(i1: RawNavigateToItem, i2: RawNavigateToItem): number { + function compareNavigateToItems(i1: RawNavigateToItem, i2: RawNavigateToItem) { // TODO(cyrusn): get the gamut of comparisons that VS already uses here. - return i1.matchKind - i2.matchKind || - compareStringsCaseSensitiveUI(i1.name, i2.name); + return compareValues(i1.matchKind, i2.matchKind) + || compareStringsCaseSensitiveUI(i1.name, i2.name); } function createNavigateToItem(rawItem: RawNavigateToItem): NavigateToItem { diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index 9f4d37407eb..712a315bb41 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -366,10 +366,9 @@ namespace ts.NavigationBar { children.sort(compareChildren); } - function compareChildren(child1: NavigationBarNode, child2: NavigationBarNode): number { - const name1 = tryGetName(child1.node), name2 = tryGetName(child2.node); - return compareStringsCaseInsensitiveUI(name1, name2) - || navigationBarNodeKind(child1) - navigationBarNodeKind(child2); + function compareChildren(child1: NavigationBarNode, child2: NavigationBarNode) { + return compareStringsCaseInsensitiveUI(tryGetName(child1.node), tryGetName(child2.node)) + || compareValues(navigationBarNodeKind(child1), navigationBarNodeKind(child2)); } /** diff --git a/src/services/refactors/extractSymbol.ts b/src/services/refactors/extractSymbol.ts index 41b922c2003..e26cdd0f74f 100644 --- a/src/services/refactors/extractSymbol.ts +++ b/src/services/refactors/extractSymbol.ts @@ -1137,32 +1137,11 @@ namespace ts.refactor.extractSymbol { {type: type1, declaration: declaration1}: {type: Type, declaration?: Declaration}, {type: type2, declaration: declaration2}: {type: Type, declaration?: Declaration}) { - if (declaration1) { - if (declaration2) { - const positionDiff = declaration1.pos - declaration2.pos; - if (positionDiff !== 0) { - return positionDiff; - } - } - else { - return 1; // Sort undeclared type parameters to the front. - } - } - else if (declaration2) { - return -1; // Sort undeclared type parameters to the front. - } - - const name1 = type1.symbol ? type1.symbol.getName() : ""; - const name2 = type2.symbol ? type2.symbol.getName() : ""; - - // This is for code generation, use a predictable comparer. - const nameDiff = compareStringsCaseSensitive(name1, name2); - if (nameDiff !== 0) { - return nameDiff; - } - - // IDs are guaranteed to be unique, so this ensures a total ordering. - return type1.id - type2.id; + return compareProperties(declaration1, declaration2, "pos") + || compareStringsCaseSensitive( + type1.symbol ? type1.symbol.getName() : "", + type2.symbol ? type2.symbol.getName() : "") + || compareValues(type1.id, type2.id); } function getCalledExpression(scope: Node, range: TargetRange, functionNameText: string): Expression {