DocumentSpan.cs 2.9 KB
Newer Older
1 2 3
// 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;
4 5 6
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.GeneratedCodeRecognition;
7
using Microsoft.CodeAnalysis.Navigation;
8
using Microsoft.CodeAnalysis.Shared.Extensions;
9 10 11
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

C
CyrusNajmabadi 已提交
12
namespace Microsoft.CodeAnalysis
13 14 15 16
{
    /// <summary>
    /// Represents a <see cref="TextSpan"/> location in a <see cref="Document"/>.
    /// </summary>
C
CyrusNajmabadi 已提交
17
    internal struct DocumentSpan : IEquatable<DocumentSpan>
18 19 20 21
    {
        public Document Document { get; }
        public TextSpan SourceSpan { get; }

C
CyrusNajmabadi 已提交
22
        public DocumentSpan(Document document, TextSpan sourceSpan)
23 24 25 26 27 28
        {
            Document = document;
            SourceSpan = sourceSpan;
        }

        public override bool Equals(object obj)
29
            => Equals((DocumentSpan)obj);
30

C
CyrusNajmabadi 已提交
31
        public bool Equals(DocumentSpan obj)
32
            => this.Document == obj.Document && this.SourceSpan == obj.SourceSpan;
33

C
CyrusNajmabadi 已提交
34
        public static bool operator ==(DocumentSpan d1, DocumentSpan d2)
35
            => d1.Equals(d2);
36

C
CyrusNajmabadi 已提交
37
        public static bool operator !=(DocumentSpan d1, DocumentSpan d2)
38
            => !(d1 == d2);
39 40

        public override int GetHashCode()
41
            => Hash.Combine(
42
                this.Document,
C
CyrusNajmabadi 已提交
43
                this.SourceSpan.GetHashCode());
44
    }
45

46 47 48
    internal static class DocumentSpanExtensions
    {
        public static bool CanNavigateTo(this DocumentSpan documentSpan)
49
        {
50
            var workspace = documentSpan.Document.Project.Solution.Workspace;
51
            var service = workspace.Services.GetService<IDocumentNavigationService>();
52
            return service.CanNavigateToSpan(workspace, documentSpan.Document.Id, documentSpan.SourceSpan);
53 54
        }

55
        public static bool TryNavigateTo(this DocumentSpan documentSpan)
56
        {
57
            var solution = documentSpan.Document.Project.Solution;
58
            var workspace = solution.Workspace;
59
            var service = workspace.Services.GetService<IDocumentNavigationService>();
60
            return service.TryNavigateToSpan(workspace, documentSpan.Document.Id, documentSpan.SourceSpan,
61
                options: solution.Options.WithChangedOption(NavigationOptions.PreferProvisionalTab, true));
62
        }
63 64 65 66 67 68 69 70 71 72 73 74 75

        public static async Task<bool> IsHiddenAsync(
            this DocumentSpan documentSpan, CancellationToken cancellationToken)
        {
            var document = documentSpan.Document;
            if (document.SupportsSyntaxTree)
            {
                var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
                return tree.IsHiddenPosition(documentSpan.SourceSpan.Start, cancellationToken);
            }

            return false;
        }
76
    }
77
}