Skip to content

Commit

Permalink
Merge pull request #53500 from CyrusNajmabadi/backport53495
Browse files Browse the repository at this point in the history
Port 'Fix out of bound crash in lsp navto' to dev 16.11
  • Loading branch information
CyrusNajmabadi authored May 19, 2021
2 parents 694b43b + e90d26e commit bfbaf81
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,13 @@ public static LSP.Range TextSpanToRange(TextSpan textSpan, SourceText text)
}

public static Task<LSP.Location?> DocumentSpanToLocationAsync(DocumentSpan documentSpan, CancellationToken cancellationToken)
=> TextSpanToLocationAsync(documentSpan.Document, documentSpan.SourceSpan, cancellationToken);
=> TextSpanToLocationAsync(documentSpan.Document, documentSpan.SourceSpan, isStale: false, cancellationToken);

public static async Task<LSP.LocationWithText?> DocumentSpanToLocationWithTextAsync(DocumentSpan documentSpan, ClassifiedTextElement text, CancellationToken cancellationToken)
public static async Task<LSP.LocationWithText?> DocumentSpanToLocationWithTextAsync(
DocumentSpan documentSpan, ClassifiedTextElement text, CancellationToken cancellationToken)
{
var location = await TextSpanToLocationAsync(documentSpan.Document, documentSpan.SourceSpan, cancellationToken).ConfigureAwait(false);
var location = await TextSpanToLocationAsync(
documentSpan.Document, documentSpan.SourceSpan, isStale: false, cancellationToken).ConfigureAwait(false);

return location == null ? null : new LSP.LocationWithText
{
Expand Down Expand Up @@ -280,18 +282,22 @@ public static LSP.Range TextSpanToRange(TextSpan textSpan, SourceText text)
return documentEdits;
}

public static async Task<LSP.Location?> TextSpanToLocationAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken)
public static async Task<LSP.Location?> TextSpanToLocationAsync(
Document document,
TextSpan textSpan,
bool isStale,
CancellationToken cancellationToken)
{
var result = await GetMappedSpanResultAsync(document, ImmutableArray.Create(textSpan), cancellationToken).ConfigureAwait(false);
if (result == null)
{
return await ConvertTextSpanToLocation(document, textSpan, cancellationToken).ConfigureAwait(false);
return await ConvertTextSpanToLocation(document, textSpan, isStale, cancellationToken).ConfigureAwait(false);
}

var mappedSpan = result.Value.Single();
if (mappedSpan.IsDefault)
{
return await ConvertTextSpanToLocation(document, textSpan, cancellationToken).ConfigureAwait(false);
return await ConvertTextSpanToLocation(document, textSpan, isStale, cancellationToken).ConfigureAwait(false);
}

return new LSP.Location
Expand All @@ -300,10 +306,24 @@ public static LSP.Range TextSpanToRange(TextSpan textSpan, SourceText text)
Range = MappedSpanResultToRange(mappedSpan)
};

static async Task<LSP.Location> ConvertTextSpanToLocation(Document document, TextSpan span, CancellationToken cancellationToken)
static async Task<LSP.Location> ConvertTextSpanToLocation(
Document document,
TextSpan span,
bool isStale,
CancellationToken cancellationToken)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

if (isStale)
{
// in the case of a stale item, the span may be out of bounds of the document. Cap
// us to the end of the document as that's where we're going to navigate the user
// to.
span = TextSpan.FromBounds(
Math.Min(text.Length, span.Start),
Math.Min(text.Length, span.End));
}

return ConvertTextSpanWithTextToLocation(span, text, document.GetURI());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public AbstractGoToDefinitionHandler(IMetadataAsSourceFileService metadataAsSour
continue;
}

var location = await ProtocolConversions.TextSpanToLocationAsync(definition.Document, definition.SourceSpan, cancellationToken).ConfigureAwait(false);
var location = await ProtocolConversions.TextSpanToLocationAsync(
definition.Document, definition.SourceSpan, definition.IsStale, cancellationToken).ConfigureAwait(false);
locations.AddIfNotNull(location);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public void ReportProgress(int current, int maximum)

private async Task ReportSymbolInformationAsync(INavigateToSearchResult result, CancellationToken cancellationToken)
{
var location = await ProtocolConversions.TextSpanToLocationAsync(result.NavigableItem.Document, result.NavigableItem.SourceSpan, cancellationToken).ConfigureAwait(false);
var location = await ProtocolConversions.TextSpanToLocationAsync(
result.NavigableItem.Document, result.NavigableItem.SourceSpan, result.NavigableItem.IsStale, cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(location);
_progress.Report(new VSSymbolInformation
{
Expand Down

0 comments on commit bfbaf81

Please sign in to comment.