/// <summary> /// Map a CodeAnalysis document, used for files in the current solution and workspace /// </summary> /// <param name="document">a CodeAnalysis document</param> /// <returns>List of found code items</returns> public static async Task <List <CodeItem> > MapDocumentAsync(Document document) { if (document == null) { return(null); } if (Path.GetExtension(document.FilePath).Equals(".js")) { return(SyntaxMapperJS.Map(document, _control)); } _tree = await document.GetSyntaxTreeAsync(); if (_tree == null) { return(null); } _semanticModel = await document.GetSemanticModelAsync(); var root = await _tree.GetRootAsync(); switch (LanguageHelper.GetLanguage(root.Language)) { case LanguageEnum.CSharp: return((root as CompilationUnitSyntax).Members.Select(MapMember).ToList()); case LanguageEnum.VisualBasic: return((root as VisualBasicSyntax.CompilationUnitSyntax).Members.Select(MapMember).ToList()); default: return(null); } }
public void Init() { document = SyntaxMapperJS.Map(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\Files\\JavaScript\\TestFunction.js"), null); Assert.IsTrue(document.Any()); Assert.AreEqual(CodeItemKindEnum.Namespace, document.First().Kind); root = (document.First() as CodeNamespaceItem).Members.First() as CodeClassItem; }
/// <summary> /// Map an EnvDTE.Document, used for files outside of the current solution eg. [from metadata] /// </summary> /// <param name="document">An EnvDTE.Document</param> /// <returns>List of found code items</returns> public static List <CodeItem> MapDocument(EnvDTE.Document document) { System.Windows.Threading.Dispatcher.CurrentDispatcher.VerifyAccess(); var text = DocumentHelper.GetText(document); if (string.IsNullOrEmpty(text)) { return(new List <CodeItem>()); } if (Path.GetExtension(document.FullName).Equals(".js")) { return(SyntaxMapperJS.Map(document, _control)); } switch (LanguageHelper.GetLanguage(document.Language)) { case LanguageEnum.CSharp: _tree = CSharpSyntaxTree.ParseText(text); try { var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); var compilation = CSharpCompilation.Create("CodeNavCompilation", new[] { _tree }, new[] { mscorlib }); _semanticModel = compilation.GetSemanticModel(_tree); } catch (ArgumentException) // SyntaxTree not found to remove { return(new List <CodeItem>()); } var root = (CompilationUnitSyntax)_tree.GetRoot(); return(root.Members.Select(MapMember).ToList()); case LanguageEnum.VisualBasic: _tree = VisualBasic.VisualBasicSyntaxTree.ParseText(text); try { var mscorlibVB = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); var compilationVB = VisualBasic.VisualBasicCompilation.Create("CodeNavCompilation", new[] { _tree }, new[] { mscorlibVB }); _semanticModel = compilationVB.GetSemanticModel(_tree); } catch (ArgumentException) // SyntaxTree not found to remove { return(new List <CodeItem>()); } var rootVB = (VisualBasicSyntax.CompilationUnitSyntax)_tree.GetRoot(); return(rootVB.Members.Select(MapMember).ToList()); default: return(new List <CodeItem>()); } }