コード例 #1
0
        private NativeMethods.FindSourceDefinitionsAndDetermineSymbolResult GetNode(SnapshotPoint point, Project project, string fileName)
        {
            Compiler compiler;

            try {
                compiler = compilerHost.Value.CreateCompiler(project);
            } catch (COMException) { return(null); }            // Don't choke on metadata as source

            var sourceFile = compiler.SourceFiles[new FileName(fileName)];

            var node = sourceFile.GetParseTree().FindLeafNode(CSharpLanguageUtilities.ToPosition(point));

            if (node == null)
            {
                return(null);
            }

            var rNode = ParseTreeMatch.GetReferencedNode(node);

            if (rNode == null)
            {
                return(null);
            }
            return(NativeMethods.FindSourceDefinitionsAndDetermineSymbolFromParseTree((IDECompilation)compiler.GetCompilation(), null, rNode));
        }
コード例 #2
0
        // Stolen from Microsoft.VisualStudio.CSharp.Services.Language.Features.Peek.PeekableItemSource

        static IEnumerable <GoToDefLocation> GetGoToDefLocations(string sourceFileName, SnapshotPoint point)
        {
            // I cannot use Position, because it will be compiled into a
            // a field in the iterator type, which will throw a TypeLoad
            // exception before I add my AssemblyResolve handler.
            var position = CSharpLanguageUtilities.ToPositionTuple(point, null);

            string[] fileNames, rqNames, assemblyBinaryNames;
            int[]    lines, columns;
            bool[]   isMetaDataFlags;
            try {
                NativeMethods.GoToDefinition_GetLocations(position.Item1, position.Item2, sourceFileName, out fileNames, out lines, out columns, out rqNames, out assemblyBinaryNames, out isMetaDataFlags);
            } catch (InvalidOperationException) {
                yield break;
            }
            if (fileNames == null || lines == null || columns == null || rqNames == null || assemblyBinaryNames == null)
            {
                yield break;
            }

            if (fileNames.Length != lines.Length || lines.Length != columns.Length || columns.Length != rqNames.Length || rqNames.Length != assemblyBinaryNames.Length)
            {
                throw new InvalidOperationException("GoToDefinition_GetLocations returned inconsistent arrays");
            }

            for (int i = 0; i < fileNames.Length; i++)
            {
                yield return(new GoToDefLocation(fileNames[i], rqNames[i], assemblyBinaryNames[i], isMetaDataFlags[i]));
            }
        }