Esempio n. 1
0
        internal static async Task <SyntaxNode> GetSelectedSyntaxNode(IVsTextManager textManager)
        {
            int    startPosition, endPosition;
            string filePath;

            if (textManager.TryFindDocumentAndPosition(out filePath, out startPosition, out endPosition))
            {
                Document document;
                try
                {
                    document = WorkspaceHelpers.GetDocument(filePath);
                }
                catch (NullReferenceException ex)
                {
                    StatusBar.ShowStatus($"Error accessing the document. Try building the solution.");
                    return(null);
                }
                var root = await document.GetSyntaxRootAsync();

                var element = root.FindNode(new Microsoft.CodeAnalysis.Text.TextSpan(startPosition, endPosition - startPosition));
                return(element);
            }
            else
            {
                StatusBar.ShowStatus("To use generator preview, please navigate to C# code.");
                return(null);
            }
        }