提交 0d031f0c 编写于 作者: C Cyrus Najmabadi

Rename type for clarity

上级 cd664bc5
......@@ -152,14 +152,14 @@ private void ProduceTags(TaggerContext<TTag> context, DocumentSnapshotSpan spanT
var suppressedDiagnosticsSpans = (NormalizedSnapshotSpanCollection?)null;
buffer?.Properties.TryGetProperty(PredefinedPreviewTaggerKeys.SuppressDiagnosticsSpansKey, out suppressedDiagnosticsSpans);
var eventArgs = _diagnosticService.GetDiagnosticsUpdatedEventArgs(
var buckets = _diagnosticService.GetDiagnosticBuckets(
workspace, document.Project.Id, document.Id, context.CancellationToken);
foreach (var updateArg in eventArgs)
foreach (var bucket in buckets)
{
ProduceTags(
context, spanToTag, workspace, document,
suppressedDiagnosticsSpans, updateArg, cancellationToken);
suppressedDiagnosticsSpans, bucket, cancellationToken);
}
}
......@@ -167,13 +167,13 @@ private void ProduceTags(TaggerContext<TTag> context, DocumentSnapshotSpan spanT
TaggerContext<TTag> context, DocumentSnapshotSpan spanToTag,
Workspace workspace, Document document,
NormalizedSnapshotSpanCollection? suppressedDiagnosticsSpans,
UpdatedEventArgs updateArgs, CancellationToken cancellationToken)
DiagnosticBucket bucket, CancellationToken cancellationToken)
{
try
{
var id = updateArgs.Id;
var id = bucket.Id;
var diagnostics = _diagnosticService.GetDiagnostics(
workspace, document.Project.Id, document.Id, id, false, cancellationToken);
workspace, document.Project.Id, document.Id, id, includeSuppressedDiagnostics: false, cancellationToken);
var isLiveUpdate = id is ISupportLiveUpdate;
......
......@@ -34,7 +34,7 @@ public MockDiagnosticService()
{
}
public ImmutableArray<DiagnosticData> GetDiagnostics(Workspace workspace, ProjectId projectId, DocumentId documentId, object id, bool includeSuppressedDiagnostics, CancellationToken cancellationToken)
public ImmutableArray<DiagnosticData> GetDiagnostics(Workspace workspace, ProjectId? projectId, DocumentId? documentId, object? id, bool includeSuppressedDiagnostics, CancellationToken cancellationToken)
{
Assert.Equal(projectId, GetProjectId(workspace));
Assert.Equal(documentId, GetDocumentId(workspace));
......@@ -42,14 +42,14 @@ public ImmutableArray<DiagnosticData> GetDiagnostics(Workspace workspace, Projec
return _diagnostic == null ? ImmutableArray<DiagnosticData>.Empty : ImmutableArray.Create(_diagnostic);
}
public ImmutableArray<UpdatedEventArgs> GetDiagnosticsUpdatedEventArgs(Workspace workspace, ProjectId projectId, DocumentId documentId, CancellationToken cancellationToken)
public ImmutableArray<DiagnosticBucket> GetDiagnosticBuckets(Workspace workspace, ProjectId? projectId, DocumentId? documentId, CancellationToken cancellationToken)
{
Assert.Equal(projectId, GetProjectId(workspace));
Assert.Equal(documentId, GetDocumentId(workspace));
return _diagnostic == null
? ImmutableArray<UpdatedEventArgs>.Empty
: ImmutableArray.Create(new UpdatedEventArgs(this, workspace, GetProjectId(workspace), GetDocumentId(workspace)));
? ImmutableArray<DiagnosticBucket>.Empty
: ImmutableArray.Create(new DiagnosticBucket(this, workspace, GetProjectId(workspace), GetDocumentId(workspace)));
}
internal void CreateDiagnosticAndFireEvents(Workspace workspace, Location location)
......
......@@ -171,7 +171,7 @@ protected static string ApplyTextEdits(LSP.TextEdit[] edits, SourceText original
return text.ToString();
}
protected static LSP.SymbolInformation CreateSymbolInformation(LSP.SymbolKind kind, string name, LSP.Location location, string? containerName = null)
protected static LSP.SymbolInformation CreateSymbolInformation(LSP.SymbolKind kind, string name, LSP.Location location, string containerName = "")
=> new LSP.SymbolInformation()
{
Kind = kind,
......@@ -223,7 +223,8 @@ protected static LSP.CompletionParams CreateCompletionParams(LSP.Location caret,
string text, LSP.CompletionItemKind kind, string[] tags,
LSP.CompletionParams requestParameters, bool preselect = false,
string[]? commitCharacters = null)
=> new LSP.VSCompletionItem()
{
var item = new LSP.VSCompletionItem()
{
FilterText = text,
InsertText = text,
......@@ -237,11 +238,18 @@ protected static LSP.CompletionParams CreateCompletionParams(LSP.Location caret,
TextDocument = requestParameters.TextDocument,
Position = requestParameters.Position
},
Icon = tags != null ? new ImageElement(tags.ToImmutableArray().GetFirstGlyph().GetImageId()) : null,
Preselect = preselect,
CommitCharacters = commitCharacters
};
if (tags != null)
item.Icon = new ImageElement(tags.ToImmutableArray().GetFirstGlyph().GetImageId());
if (commitCharacters != null)
item.CommitCharacters = commitCharacters;
return item;
}
private protected static CodeActionResolveData CreateCodeActionResolveData(string uniqueIdentifier, LSP.Location location)
=> new CodeActionResolveData(uniqueIdentifier, location.Range, CreateTextDocumentIdentifier(location.Uri));
......
// 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.
#nullable enable
namespace Microsoft.CodeAnalysis.Diagnostics
{
internal readonly struct DiagnosticBucket
{
/// <summary>
/// The identity of update group.
/// </summary>
public readonly object Id;
/// <summary>
/// <see cref="Workspace"/> this update is associated with.
/// </summary>
public readonly Workspace Workspace;
/// <summary>
/// <see cref="ProjectId"/> this update is associated with, or <see langword="null"/>.
/// </summary>
public readonly ProjectId? ProjectId;
/// <summary>
/// <see cref="DocumentId"/> this update is associated with, or <see langword="null"/>.
/// </summary>
public readonly DocumentId? DocumentId;
public DiagnosticBucket(object id, Workspace workspace, ProjectId? projectId, DocumentId? documentId)
{
Id = id;
Workspace = workspace;
ProjectId = projectId;
DocumentId = documentId;
}
}
}
......@@ -262,11 +262,11 @@ private ImmutableArray<DiagnosticData> GetSpecificDiagnostics(Workspace workspac
buffer.Clear();
if (source.SupportGetDiagnostics)
{
result.AddRange(source.GetDiagnostics(workspace, projectId, documentId, null, includeSuppressedDiagnostics, cancellationToken));
result.AddRange(source.GetDiagnostics(workspace, projectId, documentId, id: null, includeSuppressedDiagnostics, cancellationToken));
}
else
{
AppendMatchingData(source, workspace, projectId, documentId, null, buffer);
AppendMatchingData(source, workspace, projectId, documentId, id: null, buffer);
foreach (var data in buffer)
{
......@@ -283,10 +283,10 @@ private ImmutableArray<DiagnosticData> GetSpecificDiagnostics(Workspace workspac
return result.ToImmutable();
}
public ImmutableArray<UpdatedEventArgs> GetDiagnosticsUpdatedEventArgs(
public ImmutableArray<DiagnosticBucket> GetDiagnosticBuckets(
Workspace workspace, ProjectId projectId, DocumentId documentId, CancellationToken cancellationToken)
{
using var _1 = ArrayBuilder<UpdatedEventArgs>.GetInstance(out var result);
using var _1 = ArrayBuilder<DiagnosticBucket>.GetInstance(out var result);
using var _2 = ArrayBuilder<Data>.GetInstance(out var buffer);
foreach (var source in _updateSources)
......@@ -296,7 +296,7 @@ private ImmutableArray<DiagnosticData> GetSpecificDiagnostics(Workspace workspac
AppendMatchingData(source, workspace, projectId, documentId, id: null, buffer);
foreach (var data in buffer)
result.AddRange(new UpdatedEventArgs(data.Id, data.Workspace, data.ProjectId, data.DocumentId));
result.AddRange(new DiagnosticBucket(data.Id, data.Workspace, data.ProjectId, data.DocumentId));
}
return result.ToImmutable();
......
......@@ -2,11 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis.Common;
namespace Microsoft.CodeAnalysis.Diagnostics
{
......@@ -26,11 +26,12 @@ internal interface IDiagnosticService
/// <summary>
/// Get current diagnostics stored in IDiagnosticUpdateSource
/// </summary>
ImmutableArray<DiagnosticData> GetDiagnostics(Workspace workspace, ProjectId projectId, DocumentId documentId, object id, bool includeSuppressedDiagnostics, CancellationToken cancellationToken);
ImmutableArray<DiagnosticData> GetDiagnostics(Workspace workspace, ProjectId? projectId, DocumentId? documentId, object? id, bool includeSuppressedDiagnostics, CancellationToken cancellationToken);
/// <summary>
/// Get current UpdatedEventArgs stored in IDiagnosticUpdateSource
/// Get current buckets stored our diagnostics are grouped into. Specific buckets can be retrieved by calling
/// <see cref="IDiagnosticServiceExtensions.GetDiagnostics(IDiagnosticService, DiagnosticBucket, bool, CancellationToken)"/>.
/// </summary>
ImmutableArray<UpdatedEventArgs> GetDiagnosticsUpdatedEventArgs(Workspace workspace, ProjectId projectId, DocumentId documentId, CancellationToken cancellationToken);
ImmutableArray<DiagnosticBucket> GetDiagnosticBuckets(Workspace workspace, ProjectId? projectId, DocumentId? documentId, CancellationToken cancellationToken);
}
}
// 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.PooledObjects;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Diagnostics
{
internal static class IDiagnosticServiceExtensions
{
public static ImmutableArray<DiagnosticData> GetDiagnostics(this IDiagnosticService service, DiagnosticBucket bucket, bool includeSuppressedDiagnostics, CancellationToken cancellationToken)
=> service.GetDiagnostics(bucket.Workspace, bucket.ProjectId, bucket.DocumentId, bucket.Id, includeSuppressedDiagnostics, cancellationToken);
public static ImmutableArray<DiagnosticData> GetDiagnostics(this IDiagnosticService service, Document document, bool includeSuppressedDiagnostics, CancellationToken cancellationToken)
{
var project = document.Project;
var workspace = project.Solution.Workspace;
using var _ = ArrayBuilder<DiagnosticData>.GetInstance(out var result);
foreach (var bucket in service.GetDiagnosticBuckets(workspace, project.Id, document.Id, cancellationToken))
{
Contract.ThrowIfFalse(workspace.Equals(bucket.Workspace));
Contract.ThrowIfFalse(document.Id.Equals(bucket.DocumentId));
var diagnostics = service.GetDiagnostics(bucket, includeSuppressedDiagnostics, cancellationToken);
result.AddRange(diagnostics);
}
return result.ToImmutable();
}
}
}
......@@ -177,17 +177,17 @@ public override async ValueTask OnReferenceFoundAsync(SourceReferenceItem refere
var result = new LSP.VSReferenceItem
{
ContainingMember = properties.TryGetValue(
AbstractReferenceFinder.ContainingMemberInfoPropertyName, out var referenceContainingMember) ? referenceContainingMember : null,
AbstractReferenceFinder.ContainingMemberInfoPropertyName, out var referenceContainingMember) ? referenceContainingMember : "",
ContainingType = properties.TryGetValue(
AbstractReferenceFinder.ContainingTypeInfoPropertyName, out var referenceContainingType) ? referenceContainingType : null,
AbstractReferenceFinder.ContainingTypeInfoPropertyName, out var referenceContainingType) ? referenceContainingType : "",
DefinitionId = definitionId,
DefinitionText = definitionText, // Only definitions should have a non-null DefinitionText
DisplayPath = location.Uri.LocalPath,
DocumentName = documentSpan == default ? null : documentSpan.Document.Name,
DocumentName = documentSpan == default ? "" : documentSpan.Document.Name,
Id = id,
Kind = symbolUsageInfo.HasValue ? ProtocolConversions.SymbolUsageInfoToReferenceKinds(symbolUsageInfo.Value) : Array.Empty<ReferenceKind>(),
Location = location,
ProjectName = documentSpan == default ? null : documentSpan.Document.Project.Name,
ProjectName = documentSpan == default ? "" : documentSpan.Document.Project.Name,
ResolutionStatus = ResolutionStatusKind.ConfirmedAsReference,
Text = text,
};
......
......@@ -105,7 +105,7 @@ public static LSP.TextEdit TextChangeToTextEdit(TextChange textChange, SourceTex
{
return new LSP.TextEdit
{
NewText = textChange.NewText,
NewText = textChange.NewText ?? "",
Range = TextSpanToRange(textChange.Span, text)
};
}
......
......@@ -59,6 +59,7 @@ public Task<LSP.InitializeResult> HandleRequestAsync(LSP.InitializeParams reques
ReferencesProvider = true,
ProjectContextProvider = true,
ExecuteCommandProvider = new LSP.ExecuteCommandOptions(),
SupportsDiagnosticRequests = true,
TextDocumentSync = new LSP.TextDocumentSyncOptions
{
Change = LSP.TextDocumentSyncKind.None
......
......@@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.NavigateTo;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.LanguageServer.Handler
{
......@@ -30,7 +31,8 @@ public override async Task<SymbolInformation[]> HandleRequestAsync(WorkspaceSymb
{
var solution = SolutionProvider.GetCurrentSolutionForMainWorkspace();
var searchTasks = Task.WhenAll(solution.Projects.Select(project => SearchProjectAsync(project, request, cancellationToken)));
return (await searchTasks.ConfigureAwait(false)).SelectMany(s => s).ToArray();
var result = await searchTasks.ConfigureAwait(false);
return result.SelectMany(a => a).ToArray();
// local functions
static async Task<ImmutableArray<SymbolInformation>> SearchProjectAsync(Project project, WorkspaceSymbolParams request, CancellationToken cancellationToken)
......@@ -46,19 +48,25 @@ static async Task<ImmutableArray<SymbolInformation>> SearchProjectAsync(Project
request.Query,
searchService.KindsProvided,
cancellationToken).ConfigureAwait(false);
var projectSymbolsTasks = Task.WhenAll(items.Select(item => CreateSymbolInformation(item, cancellationToken)));
return (await projectSymbolsTasks.ConfigureAwait(false)).ToImmutableArray();
var result = await projectSymbolsTasks.ConfigureAwait(false);
return result.WhereNotNull().ToImmutableArray();
}
return ImmutableArray.Create<SymbolInformation>();
static async Task<SymbolInformation> CreateSymbolInformation(INavigateToSearchResult result, CancellationToken cancellationToken)
static async Task<SymbolInformation?> CreateSymbolInformation(INavigateToSearchResult result, CancellationToken cancellationToken)
{
var location = await ProtocolConversions.TextSpanToLocationAsync(result.NavigableItem.Document, result.NavigableItem.SourceSpan, cancellationToken).ConfigureAwait(false);
if (location == null)
return null;
return new SymbolInformation
{
Name = result.Name,
Kind = ProtocolConversions.NavigateToKindToSymbolKind(result.Kind),
Location = await ProtocolConversions.TextSpanToLocationAsync(result.NavigableItem.Document, result.NavigableItem.SourceSpan, cancellationToken).ConfigureAwait(false),
Location = location,
};
}
}
......
......@@ -426,7 +426,7 @@ static LanguageServer.Protocol.Diagnostic ConvertToLspDiagnostic(DiagnosticData
private bool IncludeDiagnostic(DiagnosticData diagnostic) =>
diagnostic.Properties.GetOrDefault(nameof(DocumentPropertiesService.DiagnosticsLspClientName)) == _clientName;
private static LanguageServer.Protocol.Range? GetDiagnosticRange(DiagnosticDataLocation? diagnosticDataLocation, SourceText text)
private static LanguageServer.Protocol.Range GetDiagnosticRange(DiagnosticDataLocation? diagnosticDataLocation, SourceText text)
{
var linePositionSpan = DiagnosticData.GetLinePositionSpan(diagnosticDataLocation, text, useMapped: true);
return ProtocolConversions.LinePositionToRange(linePositionSpan);
......
......@@ -166,9 +166,9 @@ private object CreateAggregationKey(object data)
private void PopulateInitialData(Workspace workspace, IDiagnosticService diagnosticService)
{
foreach (var args in diagnosticService.GetDiagnosticsUpdatedEventArgs(workspace, projectId: null, documentId: null, cancellationToken: CancellationToken.None))
foreach (var bucket in diagnosticService.GetDiagnosticBuckets(workspace, projectId: null, documentId: null, cancellationToken: CancellationToken.None))
{
OnDataAddedOrChanged(args);
OnDataAddedOrChanged(bucket);
}
}
......
......@@ -841,16 +841,16 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Diagnostics
Return diagnostics
End Function
Public Function GetDiagnosticsArgs(workspace As Workspace, projectId As ProjectId, documentId As DocumentId, cancellationToken As CancellationToken) As ImmutableArray(Of UpdatedEventArgs) Implements IDiagnosticService.GetDiagnosticsUpdatedEventArgs
Public Function GetDiagnosticsArgs(workspace As Workspace, projectId As ProjectId, documentId As DocumentId, cancellationToken As CancellationToken) As ImmutableArray(Of DiagnosticBucket) Implements IDiagnosticService.GetDiagnosticBuckets
Assert.NotNull(workspace)
Dim diagnosticsArgs As ImmutableArray(Of UpdatedEventArgs)
Dim diagnosticsArgs As ImmutableArray(Of DiagnosticBucket)
If documentId IsNot Nothing Then
diagnosticsArgs = Items.Where(Function(t) t.DocumentId Is documentId) _
.Select(
Function(t)
Return New UpdatedEventArgs(
Return New DiagnosticBucket(
New ErrorId(Me, If(CObj(t.DocumentId), t.ProjectId)),
workspace, t.ProjectId, t.DocumentId)
End Function).ToImmutableArrayOrEmpty()
......@@ -858,14 +858,14 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.Diagnostics
diagnosticsArgs = Items.Where(Function(t) t.ProjectId Is projectId) _
.Select(
Function(t)
Return New UpdatedEventArgs(
Return New DiagnosticBucket(
New ErrorId(Me, If(CObj(t.DocumentId), t.ProjectId)),
workspace, t.ProjectId, t.DocumentId)
End Function).ToImmutableArrayOrEmpty()
Else
diagnosticsArgs = Items.Select(
Function(t)
Return New UpdatedEventArgs(
Return New DiagnosticBucket(
New ErrorId(Me, If(CObj(t.DocumentId), t.ProjectId)),
workspace, t.ProjectId, t.DocumentId)
End Function).ToImmutableArrayOrEmpty()
......
......@@ -56,24 +56,12 @@ public override async Task<CompletionItem[]> HandleRequestAsync(CompletionParams
private static CompletionItem CreateCompletionItem(XamlCompletionItem xamlCompletion, DocumentId documentId, SourceText text, Position position)
{
TextEdit? textEdit = null;
if (xamlCompletion.Span.HasValue)
{
textEdit = new TextEdit
{
NewText = xamlCompletion.InsertText,
Range = ProtocolConversions.LinePositionToRange(text.Lines.GetLinePositionSpan(xamlCompletion.Span.Value))
};
}
return new VSCompletionItem
var item = new VSCompletionItem
{
Label = xamlCompletion.DisplayText,
CommitCharacters = xamlCompletion.CommitCharacters,
Detail = xamlCompletion.Detail,
InsertText = xamlCompletion.InsertText,
TextEdit = textEdit,
Preselect = xamlCompletion.Preselect,
SortText = xamlCompletion.SortText,
FilterText = xamlCompletion.FilterText,
......@@ -82,9 +70,20 @@ private static CompletionItem CreateCompletionItem(XamlCompletionItem xamlComple
Icon = xamlCompletion.Icon,
Data = new CompletionResolveData { ProjectGuid = documentId.ProjectId.Id, DocumentGuid = documentId.Id, Position = position, DisplayText = xamlCompletion.DisplayText }
};
if (xamlCompletion.Span.HasValue)
{
item.TextEdit = new TextEdit
{
NewText = xamlCompletion.InsertText,
Range = ProtocolConversions.LinePositionToRange(text.Lines.GetLinePositionSpan(xamlCompletion.Span.Value))
};
}
return item;
}
private static CompletionItem[] CreateErrorItem(string message, string? details = null)
private static CompletionItem[] CreateErrorItem(string message, string details = "")
{
var item = new CompletionItem
{
......
......@@ -53,12 +53,12 @@ public override async Task<LSP.DocumentOnAutoInsertResponseItem[]> HandleRequest
return response.ToArray();
}
var insertText = result.TextChange.NewText;
var insertText = result.TextChange.NewText ?? "";
var insertFormat = LSP.InsertTextFormat.Plaintext;
if (result.CaretOffset.HasValue)
{
insertFormat = LSP.InsertTextFormat.Snippet;
insertText = insertText?.Insert(result.CaretOffset.Value, "$0");
insertText = insertText.Insert(result.CaretOffset.Value, "$0");
}
response.Add(new LSP.DocumentOnAutoInsertResponseItem
......
......@@ -372,8 +372,10 @@ public override int WriteTimeout
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) => _stream.CopyToAsync(destination, bufferSize, cancellationToken);
#pragma warning disable CS0672 // Member overrides obsolete member
public override object InitializeLifetimeService()
=> throw new NotSupportedException();
#pragma warning restore CS0672 // Member overrides obsolete member
#if !NETCOREAPP
public override ObjRef CreateObjRef(Type requestedType)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册