private static async Task <LSP.Location?> GetSourceDefinitionLocationAsync(XamlSourceDefinition sourceDefinition, RequestContext context, CancellationToken cancellationToken) { Contract.ThrowIfNull(sourceDefinition.FilePath); if (sourceDefinition.Span != null) { // If the Span is not null, use the span. var document = context.Solution?.GetDocuments(ProtocolConversions.GetUriFromFilePath(sourceDefinition.FilePath)).FirstOrDefault(); if (document != null) { return(await ProtocolConversions.TextSpanToLocationAsync( document, sourceDefinition.Span.Value, isStale : false, cancellationToken).ConfigureAwait(false)); } else { // Cannot find the file in solution. This is probably a file lives outside of the solution like generic.xaml // which lives in the Windows SDK folder. Try getting the SourceText from the file path. using var fileStream = new FileStream(sourceDefinition.FilePath, FileMode.Open, FileAccess.Read); var sourceText = SourceText.From(fileStream); return(new LSP.Location { Uri = new Uri(sourceDefinition.FilePath), Range = ProtocolConversions.TextSpanToRange(sourceDefinition.Span.Value, sourceText) }); } } else { // We should have the line and column, so use them to build the LSP Range. var position = new Position(sourceDefinition.Line, sourceDefinition.Column); return(new LSP.Location { Uri = new Uri(sourceDefinition.FilePath), Range = new LSP.Range() { Start = position, End = position } }); } }
private static async Task <LSP.Location?> GetSourceDefinitionLocationAsync(XamlSourceDefinition sourceDefinition, RequestContext context, CancellationToken cancellationToken) { Contract.ThrowIfNull(sourceDefinition.FilePath); var document = context.Solution?.GetDocuments(ProtocolConversions.GetUriFromFilePath(sourceDefinition.FilePath)).FirstOrDefault(); if (document != null) { var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); var span = sourceDefinition.GetTextSpan(sourceText); if (span != null) { return(await ProtocolConversions.TextSpanToLocationAsync( document, span.Value, isStale : false, cancellationToken).ConfigureAwait(false)); } } else { // Cannot find the file in solution. This is probably a file lives outside of the solution like generic.xaml // which lives in the Windows SDK folder. Try getting the SourceText from the file path. using var fileStream = new FileStream(sourceDefinition.FilePath, FileMode.Open, FileAccess.Read); var sourceText = SourceText.From(fileStream); var span = sourceDefinition.GetTextSpan(sourceText); if (span != null) { return(new LSP.Location { Uri = new Uri(sourceDefinition.FilePath), Range = ProtocolConversions.TextSpanToRange(span.Value, sourceText), }); } } return(null); }