Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
namespace Microsoft.CodeAnalysis.Editor.CSharp.EmbeddedLanguages
{
[ExportEmbeddedLanguageBraceMatcher(
PredefinedEmbeddedLanguageBraceMatcherNames.Json, LanguageNames.CSharp, supportsUnannotatedAPIs: true, "Json"), Shared]
internal sealed class CSharpJsonEmbeddedLanguageBraceMatcher :
AbstractJsonEmbeddedLanguageBraceMatcher
PredefinedEmbeddedLanguageNames.Json, LanguageNames.CSharp, supportsUnannotatedAPIs: true, "Json"), Shared]
internal sealed class CSharpJsonBraceMatcher : AbstractJsonBraceMatcher
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having names for each embeded-language/feature-name pair, we just have the names of hte embedded languages once.

{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpJsonEmbeddedLanguageBraceMatcher()
public CSharpJsonBraceMatcher()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to a simpler and more consistent naming pattern. Instead of repeating 'EmbeddedLanguage' in teh name, we just state the name of the embedded language. Regex/Json are already clearly embedded langauges, so this cut down on verbosity everywhere.

: base(CSharpEmbeddedLanguagesProvider.Info)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
namespace Microsoft.CodeAnalysis.Editor.CSharp.EmbeddedLanguages
{
[ExportEmbeddedLanguageBraceMatcher(
PredefinedEmbeddedLanguageBraceMatcherNames.Regex, LanguageNames.CSharp, supportsUnannotatedAPIs: true, "Regex", "Regexp"), Shared]
internal sealed class CSharpRegexEmbeddedLanguageBraceMatcher :
AbstractRegexEmbeddedLanguageBraceMatcher
PredefinedEmbeddedLanguageNames.Regex, LanguageNames.CSharp, supportsUnannotatedAPIs: true, "Regex", "Regexp"), Shared]
internal sealed class CSharpRegexBraceMatcher : AbstractRegexBraceMatcher
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpRegexEmbeddedLanguageBraceMatcher()
public CSharpRegexBraceMatcher()
: base(CSharpEmbeddedLanguagesProvider.Info)
{
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ namespace Microsoft.CodeAnalysis.EmbeddedLanguages.Json
/// <summary>
/// Brace matcher impl for embedded json strings.
/// </summary>
internal abstract class AbstractJsonEmbeddedLanguageBraceMatcher : IEmbeddedLanguageBraceMatcher
internal abstract class AbstractJsonBraceMatcher : IEmbeddedLanguageBraceMatcher
{
private readonly EmbeddedLanguageInfo _info;

public AbstractJsonEmbeddedLanguageBraceMatcher(EmbeddedLanguageInfo info)
public AbstractJsonBraceMatcher(EmbeddedLanguageInfo info)
=> _info = info;

public BraceMatchingResult? FindBraces(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ namespace Microsoft.CodeAnalysis.EmbeddedLanguages.RegularExpressions
/// <summary>
/// Brace matching impl for embedded regex strings.
/// </summary>
internal abstract class AbstractRegexEmbeddedLanguageBraceMatcher : IEmbeddedLanguageBraceMatcher
internal abstract class AbstractRegexBraceMatcher : IEmbeddedLanguageBraceMatcher
{
private readonly EmbeddedLanguageInfo _info;

protected AbstractRegexEmbeddedLanguageBraceMatcher(EmbeddedLanguageInfo info)
protected AbstractRegexBraceMatcher(EmbeddedLanguageInfo info)
=> _info = info;

public BraceMatchingResult? FindBraces(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Imports Microsoft.CodeAnalysis.VisualBasic.EmbeddedLanguages.LanguageServices

Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.EmbeddedLanguages
<ExportEmbeddedLanguageBraceMatcher(
PredefinedEmbeddedLanguageBraceMatcherNames.Json, LanguageNames.VisualBasic, True, "Json"), [Shared]>
Friend Class VisualBasicJsonEmbeddedLanguageBraceMatcher
Inherits AbstractJsonEmbeddedLanguageBraceMatcher
PredefinedEmbeddedLanguageNames.Json, LanguageNames.VisualBasic, True, "Json"), [Shared]>
Friend Class VisualBasicJsonBraceMatcher
Inherits AbstractJsonBraceMatcher

<ImportingConstructor>
<Obsolete(MefConstruction.ImportingConstructorMessage, True)>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Imports Microsoft.CodeAnalysis.VisualBasic.EmbeddedLanguages.LanguageServices

Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.EmbeddedLanguages
<ExportEmbeddedLanguageBraceMatcher(
PredefinedEmbeddedLanguageBraceMatcherNames.Regex, LanguageNames.VisualBasic, True, "Regex", "Regexp"), [Shared]>
Friend Class VisualBasicRegexEmbeddedLanguageBraceMatcher
Inherits AbstractRegexEmbeddedLanguageBraceMatcher
PredefinedEmbeddedLanguageNames.Regex, LanguageNames.VisualBasic, True, "Regex", "Regexp"), [Shared]>
Friend Class VisualBasicRegexBraceMatcher
Inherits AbstractRegexBraceMatcher

<ImportingConstructor>
<Obsolete(MefConstruction.ImportingConstructorMessage, True)>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
#nullable disable

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.EmbeddedLanguages.LanguageServices;
using Microsoft.CodeAnalysis.CSharp.LanguageServices;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.DocumentHighlighting;
using Microsoft.CodeAnalysis.EmbeddedLanguages;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.PooledObjects;

Expand All @@ -22,7 +26,12 @@ internal class CSharpDocumentHighlightsService : AbstractDocumentHighlightsServi
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpDocumentHighlightsService()
public CSharpDocumentHighlightsService(
[ImportMany] IEnumerable<Lazy<IEmbeddedLanguageDocumentHighlighter, EmbeddedLanguageMetadata>> services)
: base(LanguageNames.CSharp,
CSharpEmbeddedLanguagesProvider.Info,
CSharpSyntaxKinds.Instance,
services)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
namespace Microsoft.CodeAnalysis.CSharp.Features.EmbeddedLanguages
{
[ExportEmbeddedLanguageClassifier(
PredefinedEmbeddedLanguageClassifierNames.Json, LanguageNames.CSharp, supportsUnannotatedAPIs: true, "Json"), Shared]
internal class CSharpJsonEmbeddedLanguageClassifier : AbstractJsonEmbeddedLanguageClassifier
PredefinedEmbeddedLanguageNames.Json, LanguageNames.CSharp, supportsUnannotatedAPIs: true, "Json"), Shared]
internal class CSharpJsonClassifier : AbstractJsonClassifier
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpJsonEmbeddedLanguageClassifier()
public CSharpJsonClassifier()
: base(CSharpEmbeddedLanguagesProvider.Info)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace Microsoft.CodeAnalysis.CSharp.Features.EmbeddedLanguages
{
// Order regex classification before json classification. Json lights up on probable-json strings, but we don't
// want that to happen for APIs that are certain to be another language like Regex.
[ExtensionOrder(Before = PredefinedEmbeddedLanguageClassifierNames.Json)]
[ExtensionOrder(Before = PredefinedEmbeddedLanguageNames.Json)]
[ExportEmbeddedLanguageClassifier(
PredefinedEmbeddedLanguageClassifierNames.Regex, LanguageNames.CSharp, supportsUnannotatedAPIs: true, "Regex", "Regexp"), Shared]
internal class CSharpRegexEmbeddedLanguageClassifier : AbstractRegexEmbeddedLanguageClassifier
PredefinedEmbeddedLanguageNames.Regex, LanguageNames.CSharp, supportsUnannotatedAPIs: true, "Regex", "Regexp"), Shared]
internal class CSharpRegexClassifier : AbstractRegexClassifier
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpRegexEmbeddedLanguageClassifier()
public CSharpRegexClassifier()
: base(CSharpEmbeddedLanguagesProvider.Info)
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Composition;
using Microsoft.CodeAnalysis.CSharp.EmbeddedLanguages.LanguageServices;
using Microsoft.CodeAnalysis.DocumentHighlighting;
using Microsoft.CodeAnalysis.Features.EmbeddedLanguages.RegularExpressions.LanguageServices;
using Microsoft.CodeAnalysis.Host.Mef;

namespace Microsoft.CodeAnalysis.CSharp.Features.EmbeddedLanguages
{
[ExtensionOrder(Before = PredefinedEmbeddedLanguageNames.Json)]
[ExportEmbeddedLanguageDocumentHighlighter(
PredefinedEmbeddedLanguageNames.Regex, LanguageNames.CSharp, supportsUnannotatedAPIs: true, "Regex", "Regexp"), Shared]
internal class CSharpRegexDocumentHighlighter : AbstractRegexDocumentHighlighter
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpRegexDocumentHighlighter()
: base(CSharpEmbeddedLanguagesProvider.Info)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.EmbeddedLanguages;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Features.EmbeddedLanguages;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
Expand All @@ -21,8 +20,19 @@

namespace Microsoft.CodeAnalysis.DocumentHighlighting
{
internal abstract partial class AbstractDocumentHighlightsService : IDocumentHighlightsService
internal abstract partial class AbstractDocumentHighlightsService :
AbstractEmbeddedLanguageFeatureService<IEmbeddedLanguageDocumentHighlighter>,
IDocumentHighlightsService
{
protected AbstractDocumentHighlightsService(
string languageName,
EmbeddedLanguageInfo info,
ISyntaxKinds syntaxKinds,
IEnumerable<Lazy<IEmbeddedLanguageDocumentHighlighter, EmbeddedLanguageMetadata>> allServices)
: base(languageName, info, syntaxKinds, allServices)
{
}

public async Task<ImmutableArray<DocumentHighlights>> GetDocumentHighlightsAsync(
Document document, int position, IImmutableSet<Document> documentsToSearch, HighlightingOptions options, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -53,14 +63,13 @@ public async Task<ImmutableArray<DocumentHighlights>> GetDocumentHighlightsAsync
private async Task<ImmutableArray<DocumentHighlights>> GetDocumentHighlightsInCurrentProcessAsync(
Document document, int position, IImmutableSet<Document> documentsToSearch, HighlightingOptions options, CancellationToken cancellationToken)
{
var result = await TryGetEmbeddedLanguageHighlightsAsync(
document, position, documentsToSearch, options, cancellationToken).ConfigureAwait(false);
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var result = TryGetEmbeddedLanguageHighlights(document, semanticModel, position, options, cancellationToken);
if (!result.IsDefaultOrEmpty)
return result;

var solution = document.Project.Solution;

var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var symbol = await SymbolFinder.FindSymbolAtPositionAsync(
semanticModel, position, solution.Workspace, cancellationToken).ConfigureAwait(false);
if (symbol == null)
Expand All @@ -80,26 +89,18 @@ private async Task<ImmutableArray<DocumentHighlights>> GetDocumentHighlightsInCu
return tags;
}

private static async Task<ImmutableArray<DocumentHighlights>> TryGetEmbeddedLanguageHighlightsAsync(
Document document, int position, IImmutableSet<Document> documentsToSearch, HighlightingOptions options, CancellationToken cancellationToken)
private ImmutableArray<DocumentHighlights> TryGetEmbeddedLanguageHighlights(
Document document, SemanticModel semanticModel, int position, HighlightingOptions options, CancellationToken cancellationToken)
{
var languagesProvider = document.GetLanguageService<IEmbeddedLanguagesProvider>();
if (languagesProvider != null)
var root = semanticModel.SyntaxTree.GetRoot(cancellationToken);
var token = root.FindToken(position);
var embeddedHighlightsServices = this.GetServices(semanticModel, token, cancellationToken);
foreach (var service in embeddedHighlightsServices)
{
foreach (var language in languagesProvider.Languages)
{
var highlighter = (language as IEmbeddedLanguageFeatures)?.DocumentHighlightsService;
if (highlighter != null)
{
var highlights = await highlighter.GetDocumentHighlightsAsync(
document, position, documentsToSearch, options, cancellationToken).ConfigureAwait(false);

if (!highlights.IsDefaultOrEmpty)
{
return highlights;
}
}
}
var result = service.Value.GetDocumentHighlights(
document, semanticModel, token, position, options, cancellationToken);
if (!result.IsDefaultOrEmpty)
return result;
}

return default;
Expand All @@ -116,7 +117,7 @@ private async Task<ImmutableArray<DocumentHighlights>> GetTagsForReferencedSymbo
{
var progress = new StreamingProgressCollector();

var options = FindSymbols.FindReferencesSearchOptions.GetFeatureOptionsForStartingSymbol(symbol);
var options = FindReferencesSearchOptions.GetFeatureOptionsForStartingSymbol(symbol);
await SymbolFinder.FindReferencesAsync(
symbol, document.Project.Solution, progress,
documentsToSearch, options, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.EmbeddedLanguages;

namespace Microsoft.CodeAnalysis.DocumentHighlighting
{
/// <summary>
/// Use this attribute to export a <see cref="IEmbeddedLanguageDocumentHighlighter"/>.
/// </summary>
internal class ExportEmbeddedLanguageDocumentHighlighterAttribute : ExportEmbeddedLanguageFeatureServiceAttribute
{
public ExportEmbeddedLanguageDocumentHighlighterAttribute(
string name, string language, params string[] identifiers)
: this(name, language, supportsUnannotatedAPIs: false, identifiers)
{
}

public ExportEmbeddedLanguageDocumentHighlighterAttribute(
string name, string language, bool supportsUnannotatedAPIs, params string[] identifiers)
: base(typeof(IEmbeddedLanguageDocumentHighlighter), name, language, supportsUnannotatedAPIs, identifiers)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis.EmbeddedLanguages;

namespace Microsoft.CodeAnalysis.DocumentHighlighting
{
/// <inheritdoc cref="IDocumentHighlightsService"/>
internal interface IEmbeddedLanguageDocumentHighlighter : IEmbeddedLanguageFeatureService
{
/// <inheritdoc cref="IDocumentHighlightsService.GetDocumentHighlightsAsync"/>
ImmutableArray<DocumentHighlights> GetDocumentHighlights(
Document document,
SemanticModel semanticModel,
SyntaxToken token,
int position,
HighlightingOptions options,
CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ namespace Microsoft.CodeAnalysis.Features.EmbeddedLanguages
/// </summary>
internal interface IEmbeddedLanguageFeatures : IEmbeddedLanguage
{
/// <summary>
/// A optional highlighter that can highlight spans for an embedded language string.
/// </summary>
IDocumentHighlightsService? DocumentHighlightsService { get; }

/// <summary>
/// Completion provider that can provide completion items for this
/// specific embedded language.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ namespace Microsoft.CodeAnalysis.Features.EmbeddedLanguages.Json.LanguageService
/// <summary>
/// Classifier impl for embedded json strings.
/// </summary>
internal abstract class AbstractJsonEmbeddedLanguageClassifier : IEmbeddedLanguageClassifier
internal abstract class AbstractJsonClassifier : IEmbeddedLanguageClassifier
{
private static readonly ObjectPool<Visitor> s_visitorPool = new(() => new Visitor());
private readonly EmbeddedLanguageInfo _info;

public AbstractJsonEmbeddedLanguageClassifier(EmbeddedLanguageInfo info)
public AbstractJsonClassifier(EmbeddedLanguageInfo info)
{
_info = info;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ namespace Microsoft.CodeAnalysis.Features.EmbeddedLanguages.Json.LanguageService
{
internal class JsonEmbeddedLanguage : IEmbeddedLanguageFeatures
{
// No document-highlights for embedded json currently.
public IDocumentHighlightsService? DocumentHighlightsService => null;

// No completion for embedded json currently.
public EmbeddedLanguageCompletionProvider? CompletionProvider => null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ namespace Microsoft.CodeAnalysis.Features.EmbeddedLanguages.RegularExpressions.L
/// <summary>
/// Classifier impl for embedded regex strings.
/// </summary>
internal abstract class AbstractRegexEmbeddedLanguageClassifier : IEmbeddedLanguageClassifier
internal abstract class AbstractRegexClassifier : IEmbeddedLanguageClassifier
{
private static readonly ObjectPool<Visitor> s_visitorPool = SharedPools.Default<Visitor>();

private readonly EmbeddedLanguageInfo _info;

protected AbstractRegexEmbeddedLanguageClassifier(EmbeddedLanguageInfo info)
protected AbstractRegexClassifier(EmbeddedLanguageInfo info)
{
_info = info;
}
Expand Down
Loading