From 22c7d380ee011d02b461f6c28662f697bb6ba05b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Kon=C3=AD=C4=8Dek?= Date: Sat, 7 Apr 2018 00:18:15 +0200 Subject: [PATCH] AsyncAwaitHighlighter --- .../AsyncAnonymousFunctionHighlighter.cs | 29 ---------------- ...ighlighter.cs => AsyncAwaitHighlighter.cs} | 16 +++++++-- .../AsyncLocalFunctionHighlighter.cs | 29 ---------------- .../AsyncMethodHighlighter.cs | 29 ---------------- .../KeywordHighlighters/AwaitHighlighter.cs | 34 ------------------- .../AsyncAnonymousFunctionHighlighterTests.cs | 2 +- .../AsyncLocalFunctionHighlighterTests.cs | 2 +- .../AsyncMethodHighlighterTests.cs | 2 +- .../AwaitHighlighterTests.cs | 2 +- .../AbstractKeywordHighlighter.cs | 21 +++++++++--- 10 files changed, 34 insertions(+), 132 deletions(-) delete mode 100644 src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncAnonymousFunctionHighlighter.cs rename src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/{AbstractAsyncHighlighter.cs => AsyncAwaitHighlighter.cs} (83%) delete mode 100644 src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncLocalFunctionHighlighter.cs delete mode 100644 src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncMethodHighlighter.cs delete mode 100644 src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AwaitHighlighter.cs diff --git a/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncAnonymousFunctionHighlighter.cs b/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncAnonymousFunctionHighlighter.cs deleted file mode 100644 index f327c0aeaed..00000000000 --- a/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncAnonymousFunctionHighlighter.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Threading; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Text; -using Roslyn.Utilities; - -namespace Microsoft.CodeAnalysis.Editor.CSharp.KeywordHighlighting.KeywordHighlighters -{ - [ExportHighlighter(LanguageNames.CSharp)] - internal class AsyncAnonymousFunctionHighlighter : AbstractAsyncHighlighter - { - protected override IEnumerable GetHighlights(AnonymousFunctionExpressionSyntax node, CancellationToken cancellationToken) - { - if (node.AsyncKeyword.Kind() != SyntaxKind.AsyncKeyword) - { - return SpecializedCollections.EmptyEnumerable(); - } - - var spans = new List(); - - HighlightRelatedKeywords(node, spans); - - return spans; - } - } -} diff --git a/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AbstractAsyncHighlighter.cs b/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncAwaitHighlighter.cs similarity index 83% rename from src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AbstractAsyncHighlighter.cs rename to src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncAwaitHighlighter.cs index de673cdbf76..1e8a55046b8 100644 --- a/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AbstractAsyncHighlighter.cs +++ b/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncAwaitHighlighter.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; +using System.Threading; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -10,9 +11,20 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.KeywordHighlighting.KeywordHighlighters { - internal abstract class AbstractAsyncHighlighter : AbstractKeywordHighlighter where TNode : SyntaxNode + [ExportHighlighter(LanguageNames.CSharp)] + internal class AsyncAwaitHighlighter : AbstractKeywordHighlighter { - protected void HighlightRelatedKeywords(SyntaxNode node, List spans) + protected override bool HighlightNode(SyntaxNode node) + => node.IsReturnableConstruct(); + + protected override IEnumerable GetHighlightsForNode(SyntaxNode node, CancellationToken cancellationToken) + { + var spans = new List(); + HighlightRelatedKeywords(node, spans); + return spans; + } + + private static void HighlightRelatedKeywords(SyntaxNode node, List spans) { // Highlight async keyword switch (node) diff --git a/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncLocalFunctionHighlighter.cs b/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncLocalFunctionHighlighter.cs deleted file mode 100644 index 0ee77c5516e..00000000000 --- a/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncLocalFunctionHighlighter.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Threading; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Text; -using Roslyn.Utilities; - -namespace Microsoft.CodeAnalysis.Editor.CSharp.KeywordHighlighting.KeywordHighlighters -{ - [ExportHighlighter(LanguageNames.CSharp)] - internal class AsyncLocalFunctionHighlighter : AbstractAsyncHighlighter - { - protected override IEnumerable GetHighlights(LocalFunctionStatementSyntax node, CancellationToken cancellationToken) - { - if (!node.Modifiers.Any(SyntaxKind.AsyncKeyword)) - { - return SpecializedCollections.EmptyEnumerable(); - } - - var spans = new List(); - - HighlightRelatedKeywords(node, spans); - - return spans; - } - } -} diff --git a/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncMethodHighlighter.cs b/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncMethodHighlighter.cs deleted file mode 100644 index 9b2652a782e..00000000000 --- a/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AsyncMethodHighlighter.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Threading; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Text; -using Roslyn.Utilities; - -namespace Microsoft.CodeAnalysis.Editor.CSharp.KeywordHighlighting.KeywordHighlighters -{ - [ExportHighlighter(LanguageNames.CSharp)] - internal class AsyncMethodHighlighter : AbstractAsyncHighlighter - { - protected override IEnumerable GetHighlights(MethodDeclarationSyntax node, CancellationToken cancellationToken) - { - if (!node.Modifiers.Any(SyntaxKind.AsyncKeyword)) - { - return SpecializedCollections.EmptyEnumerable(); - } - - var spans = new List(); - - HighlightRelatedKeywords(node, spans); - - return spans; - } - } -} diff --git a/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AwaitHighlighter.cs b/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AwaitHighlighter.cs deleted file mode 100644 index 664dba64d1e..00000000000 --- a/src/EditorFeatures/CSharp/Highlighting/KeywordHighlighters/AwaitHighlighter.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using Microsoft.CodeAnalysis.CSharp.Extensions; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Text; -using Roslyn.Utilities; - -namespace Microsoft.CodeAnalysis.Editor.CSharp.KeywordHighlighting.KeywordHighlighters -{ - [ExportHighlighter(LanguageNames.CSharp)] - internal class AwaitHighlighter : AbstractAsyncHighlighter - { - protected override IEnumerable GetHighlights(AwaitExpressionSyntax awaitExpression, CancellationToken cancellationToken) - { - var parent = awaitExpression - .AncestorsAndSelf() - .FirstOrDefault(n => n.IsReturnableConstruct()); - - if (parent == null) - { - return SpecializedCollections.EmptyEnumerable(); - } - - var spans = new List(); - - HighlightRelatedKeywords(parent, spans); - - return spans; - } - } -} diff --git a/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncAnonymousFunctionHighlighterTests.cs b/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncAnonymousFunctionHighlighterTests.cs index 1bf6212974d..9403260209c 100644 --- a/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncAnonymousFunctionHighlighterTests.cs +++ b/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncAnonymousFunctionHighlighterTests.cs @@ -11,7 +11,7 @@ public class AsyncAnonymousFunctionHighlighterTests : AbstractCSharpKeywordHighl { internal override IHighlighter CreateHighlighter() { - return new AsyncAnonymousFunctionHighlighter(); + return new AsyncAwaitHighlighter(); } [Fact, Trait(Traits.Feature, Traits.Features.KeywordHighlighting)] diff --git a/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncLocalFunctionHighlighterTests.cs b/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncLocalFunctionHighlighterTests.cs index 39c7db6786a..70aad952baf 100644 --- a/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncLocalFunctionHighlighterTests.cs +++ b/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncLocalFunctionHighlighterTests.cs @@ -11,7 +11,7 @@ public class AsyncLocalFunctionHighlighterTests : AbstractCSharpKeywordHighlight { internal override IHighlighter CreateHighlighter() { - return new AsyncLocalFunctionHighlighter(); + return new AsyncAwaitHighlighter(); } [Fact, Trait(Traits.Feature, Traits.Features.KeywordHighlighting)] diff --git a/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncMethodHighlighterTests.cs b/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncMethodHighlighterTests.cs index db51a41d7c9..39e1856e1aa 100644 --- a/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncMethodHighlighterTests.cs +++ b/src/EditorFeatures/CSharpTest/KeywordHighlighting/AsyncMethodHighlighterTests.cs @@ -11,7 +11,7 @@ public class AsyncMethodHighlighterTests : AbstractCSharpKeywordHighlighterTests { internal override IHighlighter CreateHighlighter() { - return new AsyncMethodHighlighter(); + return new AsyncAwaitHighlighter(); } [Fact, Trait(Traits.Feature, Traits.Features.KeywordHighlighting)] diff --git a/src/EditorFeatures/CSharpTest/KeywordHighlighting/AwaitHighlighterTests.cs b/src/EditorFeatures/CSharpTest/KeywordHighlighting/AwaitHighlighterTests.cs index b11a1d6e055..2aa1c06a569 100644 --- a/src/EditorFeatures/CSharpTest/KeywordHighlighting/AwaitHighlighterTests.cs +++ b/src/EditorFeatures/CSharpTest/KeywordHighlighting/AwaitHighlighterTests.cs @@ -12,7 +12,7 @@ public class AwaitHighlighterTests : AbstractCSharpKeywordHighlighterTests { internal override IHighlighter CreateHighlighter() { - return new AwaitHighlighter(); + return new AsyncAwaitHighlighter(); } [Fact, Trait(Traits.Feature, Traits.Features.KeywordHighlighting)] diff --git a/src/EditorFeatures/Core/Implementation/KeywordHighlighting/AbstractKeywordHighlighter.cs b/src/EditorFeatures/Core/Implementation/KeywordHighlighting/AbstractKeywordHighlighter.cs index db73c50e531..b936e5f3e5e 100644 --- a/src/EditorFeatures/Core/Implementation/KeywordHighlighting/AbstractKeywordHighlighter.cs +++ b/src/EditorFeatures/Core/Implementation/KeywordHighlighting/AbstractKeywordHighlighter.cs @@ -8,9 +8,20 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.Highlighting { - internal abstract class AbstractKeywordHighlighter : IHighlighter - where TNode : SyntaxNode + internal abstract class AbstractKeywordHighlighter : AbstractKeywordHighlighter where TNode : SyntaxNode { + protected sealed override bool HighlightNode(SyntaxNode node) => node is TNode; + + protected sealed override IEnumerable GetHighlightsForNode(SyntaxNode node, CancellationToken cancellationToken) + => GetHighlights((TNode)node, cancellationToken); + + protected abstract IEnumerable GetHighlights(TNode node, CancellationToken cancellationToken); + } + + internal abstract class AbstractKeywordHighlighter : IHighlighter + { + protected abstract bool HighlightNode(SyntaxNode node); + public IEnumerable GetHighlights( SyntaxNode root, int position, CancellationToken cancellationToken) { @@ -18,9 +29,9 @@ internal abstract class AbstractKeywordHighlighter : IHighlighter { for (var parent = token.Parent; parent != null; parent = parent.Parent) { - if (parent is TNode parentTNode) + if (HighlightNode(parent)) { - var highlights = GetHighlights(parentTNode, cancellationToken); + var highlights = GetHighlightsForNode(parent, cancellationToken); // Only return them if any of them matched if (highlights.Any(span => span.IntersectsWith(position))) @@ -35,7 +46,7 @@ internal abstract class AbstractKeywordHighlighter : IHighlighter return SpecializedCollections.EmptyEnumerable(); } - protected abstract IEnumerable GetHighlights(TNode node, CancellationToken cancellationToken); + protected abstract IEnumerable GetHighlightsForNode(SyntaxNode node, CancellationToken cancellationToken); protected TextSpan EmptySpan(int position) { -- GitLab