VisualStudioDefinitionsAndReferencesFactory.cs 6.7 KB
Newer Older
1 2 3 4 5 6
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Completion;
7
using Microsoft.CodeAnalysis.FindReferences;
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Navigation;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TextManager.Interop;
using OLEServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.FindReferences
{
    [ExportWorkspaceService(typeof(IDefinitionsAndReferencesFactory), ServiceLayer.Desktop), Shared]
    internal class VisualStudioDefinitionsAndReferencesFactory
        : DefaultDefinitionsAndReferencesFactory
    {
        private readonly IServiceProvider _serviceProvider;

        [ImportingConstructor]
        public VisualStudioDefinitionsAndReferencesFactory(SVsServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        protected override DefinitionItem GetThirdPartyDefinitionItem(
            Solution solution, ISymbol definition)
        {
            var symbolNavigationService = solution.Workspace.Services.GetService<ISymbolNavigationService>();
            string filePath;
            int lineNumber, charOffset;
            if (!symbolNavigationService.WouldNavigateToSymbol(definition, solution, out filePath, out lineNumber, out charOffset))
            {
                return null;
            }

C
CyrusNajmabadi 已提交
42
            var displayParts = GetDisplayParts(filePath, lineNumber, charOffset);
43
            var mainLocation = new ExternalDefinitionLocation(
44 45 46 47 48
                _serviceProvider, filePath, lineNumber, charOffset);

            return new DefinitionItem(
                GlyphTags.GetTags(definition.GetGlyph()),
                displayParts,
49 50
                mainLocation,
                additionalLocations: ImmutableArray<DocumentLocation>.Empty,
51 52 53 54 55 56 57 58 59
                displayIfNoReferences: true);
        }

        private ImmutableArray<TaggedText> GetDisplayParts(
            string filePath, int lineNumber, int charOffset)
        {
            var builder = ImmutableArray.CreateBuilder<TaggedText>();

            var sourceLine = GetSourceLine(filePath, lineNumber).Trim(' ', '\t');
C
CyrusNajmabadi 已提交
60 61 62

            // Put the line in 1-based for the presentation of this item.
            var formatted = $"{filePath} - ({lineNumber + 1}, {charOffset + 1}) : {sourceLine}";
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

            return ImmutableArray.Create(new TaggedText(TextTags.Text, formatted));
        }

        private string GetSourceLine(string filePath, int lineNumber)
        {
            using (var invisibleEditor = new InvisibleEditor(
                _serviceProvider, filePath, needsSave: false, needsUndoDisabled: false))
            {
                var vsTextLines = invisibleEditor.VsTextLines;
                int lineLength;
                string lineText;

                if (vsTextLines != null &&
                    vsTextLines.GetLengthOfLine(lineNumber, out lineLength) == VSConstants.S_OK &&
                    vsTextLines.GetLineText(lineNumber, 0, lineNumber, lineLength, out lineText) == VSConstants.S_OK)
                {
                    return lineText;
                }

                return ServicesVSResources.Preview_unavailable;
            }
        }

        private class ExternalDefinitionLocation : DefinitionLocation
        {
            private readonly IServiceProvider _serviceProvider;
            private readonly string _filePath;
            private readonly int _lineNumber;
            private readonly int _charOffset;

            public ExternalDefinitionLocation(
                IServiceProvider serviceProvider,
                string filePath,
                int lineNumber,
                int charOffset)
            {
                _serviceProvider = serviceProvider;
                _filePath = filePath;
                _lineNumber = lineNumber;
                _charOffset = charOffset;
            }

106 107
            public override ImmutableArray<TaggedText> OriginationParts => ImmutableArray<TaggedText>.Empty;

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
            public override bool CanNavigateTo() => true;

            public override bool TryNavigateTo()
            {
                return TryOpenFile() && TryNavigateToPosition();
            }

            private bool TryOpenFile()
            {
                var shellOpenDocument = (IVsUIShellOpenDocument)_serviceProvider.GetService(typeof(SVsUIShellOpenDocument));
                var textViewGuid = VSConstants.LOGVIEWID.TextView_guid;

                uint itemid;
                IVsUIHierarchy hierarchy;
                OLEServiceProvider oleServiceProvider;
                IVsWindowFrame frame;
                if (shellOpenDocument.OpenDocumentViaProject(
                    _filePath, ref textViewGuid, out oleServiceProvider, out hierarchy, out itemid, out frame) == VSConstants.S_OK)
                {
                    frame.Show();
                    return true;
                }

                return false;
            }

            private bool TryNavigateToPosition()
            {
                IVsRunningDocumentTable docTable = (IVsRunningDocumentTable)_serviceProvider.GetService(typeof(SVsRunningDocumentTable));

                IntPtr bufferPtr;
                IVsHierarchy hierarchy;
                uint cookie;
                uint itemid;

                if (docTable.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, _filePath, out hierarchy, out itemid, out bufferPtr, out cookie) != VSConstants.S_OK)
                {
                    return false;
                }

                try
                {
                    var lines = Marshal.GetObjectForIUnknown(bufferPtr) as IVsTextLines;
                    if (lines == null)
                    {
                        return false;
                    }

                    var textManager = (IVsTextManager)_serviceProvider.GetService(typeof(SVsTextManager));
                    if (textManager == null)
                    {
                        return false;
                    }

                    return textManager.NavigateToLineAndColumn(
                        lines, VSConstants.LOGVIEWID.TextView_guid, 
                        _lineNumber, _charOffset, 
                        _lineNumber, _charOffset) == VSConstants.S_OK;
                }
                finally
                {
                    if (bufferPtr != IntPtr.Zero)
                    {
                        Marshal.Release(bufferPtr);
                    }
                }
            }
        }
    }
}