From c61be112d94bfac19484fc0b279d0a582cae9d0d Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 29 Sep 2014 14:38:03 -0700 Subject: [PATCH] getOccurrences for 'get' and 'set' keywords. 'get'/'set' keywords are highlighted for analogous accessors. --- src/services/services.ts | 24 +++++++++++++ .../fourslash/getOccurrencesSetAndGet.ts | 34 +++++++++++++++++++ .../fourslash/getOccurrencesSetAndGet2.ts | 34 +++++++++++++++++++ .../fourslash/getOccurrencesSetAndGet3.ts | 34 +++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 tests/cases/fourslash/getOccurrencesSetAndGet.ts create mode 100644 tests/cases/fourslash/getOccurrencesSetAndGet2.ts create mode 100644 tests/cases/fourslash/getOccurrencesSetAndGet3.ts diff --git a/src/services/services.ts b/src/services/services.ts index 04e63f6dbe1..b8a757cfb65 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2418,6 +2418,11 @@ module ts { return getConstructorOccurrences(node.parent); } break; + case SyntaxKind.GetKeyword: + case SyntaxKind.SetKeyword: + if (hasKind(node.parent, SyntaxKind.GetAccessor) || hasKind(node.parent, SyntaxKind.SetAccessor)) { + return getGetAndSetOccurrences(node.parent); + } } return undefined; @@ -2648,6 +2653,25 @@ module ts { return map(keywords, getReferenceEntryFromNode); } + function getGetAndSetOccurrences(accessorDeclaration: AccessorDeclaration): ReferenceEntry[] { + var keywords: Node[] = []; + + tryPushAccessorKeyword(accessorDeclaration.symbol, SyntaxKind.GetAccessor); + tryPushAccessorKeyword(accessorDeclaration.symbol, SyntaxKind.SetAccessor); + + return map(keywords, getReferenceEntryFromNode); + + function tryPushAccessorKeyword(accessorSymbol: Symbol, accessorKind: SyntaxKind): void { + var accessor = getDeclarationOfKind(accessorSymbol, accessorKind); + + if (!accessor) { + return; + } + + forEach(accessor.getChildren(), child => pushKeywordIf(keywords, child, SyntaxKind.GetKeyword, SyntaxKind.SetKeyword)); + } + } + // returns true if 'node' is defined and has a matching 'kind'. function hasKind(node: Node, kind: SyntaxKind) { return node !== undefined && node.kind === kind; diff --git a/tests/cases/fourslash/getOccurrencesSetAndGet.ts b/tests/cases/fourslash/getOccurrencesSetAndGet.ts new file mode 100644 index 00000000000..b1ab6f44c6b --- /dev/null +++ b/tests/cases/fourslash/getOccurrencesSetAndGet.ts @@ -0,0 +1,34 @@ +/// + +////class Foo { +//// [|set|] bar(b: any) { +//// } +//// +//// public [|get|] bar(): any { +//// return undefined; +//// } +//// +//// public set set(s: any) { +//// } +//// +//// public get set(): any { +//// return undefined; +//// } +//// +//// public set get(g: any) { +//// } +//// +//// public get get(): any { +//// return undefined; +//// } +////} + +test.ranges().forEach(r => { + goTo.position(r.start); + + test.ranges().forEach(range => { + verify.occurrencesAtPositionContains(range, false); + }); + + verify.occurrencesAtPositionCount(test.ranges().length); +}); diff --git a/tests/cases/fourslash/getOccurrencesSetAndGet2.ts b/tests/cases/fourslash/getOccurrencesSetAndGet2.ts new file mode 100644 index 00000000000..1345394b8ee --- /dev/null +++ b/tests/cases/fourslash/getOccurrencesSetAndGet2.ts @@ -0,0 +1,34 @@ +/// + +////class Foo { +//// set bar(b: any) { +//// } +//// +//// public get bar(): any { +//// return undefined; +//// } +//// +//// public [|set|] set(s: any) { +//// } +//// +//// public [|get|] set(): any { +//// return undefined; +//// } +//// +//// public set get(g: any) { +//// } +//// +//// public get get(): any { +//// return undefined; +//// } +////} + +test.ranges().forEach(r => { + goTo.position(r.start); + + test.ranges().forEach(range => { + verify.occurrencesAtPositionContains(range, false); + }); + + verify.occurrencesAtPositionCount(test.ranges().length); +}); diff --git a/tests/cases/fourslash/getOccurrencesSetAndGet3.ts b/tests/cases/fourslash/getOccurrencesSetAndGet3.ts new file mode 100644 index 00000000000..4105a407571 --- /dev/null +++ b/tests/cases/fourslash/getOccurrencesSetAndGet3.ts @@ -0,0 +1,34 @@ +/// + +////class Foo { +//// set bar(b: any) { +//// } +//// +//// public get bar(): any { +//// return undefined; +//// } +//// +//// public set set(s: any) { +//// } +//// +//// public get set(): any { +//// return undefined; +//// } +//// +//// public [|set|] get(g: any) { +//// } +//// +//// public [|get|] get(): any { +//// return undefined; +//// } +////} + +test.ranges().forEach(r => { + goTo.position(r.start); + + test.ranges().forEach(range => { + verify.occurrencesAtPositionContains(range, false); + }); + + verify.occurrencesAtPositionCount(test.ranges().length); +});