Ejemplo n.º 1
0
        public IListDataProvider <string> GetCollections()
        {
            var result          = new List <CodeTemplateVariableValue> ();
            var ext             = CurrentContext.DocumentContext.GetContent <CompletionTextEditorExtension> ();
            var analysisProject = TypeSystemService.GetCodeAnalysisProject(CurrentContext.DocumentContext.Project);
            var compilation     = analysisProject != null?analysisProject.GetCompilationAsync().Result : null;

            if (ext != null)
            {
                if (list == null)
                {
                    list = ext.CodeCompletionCommand(
                        CurrentContext.DocumentContext.GetContent <MonoDevelop.Ide.CodeCompletion.ICompletionWidget> ().CurrentCodeCompletionContext).Result;
                }

                foreach (var data in list.OfType <ISymbolCompletionData> ())
                {
                    if (GetElementType(compilation, data.Symbol.GetReturnType()).TypeKind != TypeKind.Error)
                    {
                        var method = data as IMethodSymbol;
                        if (method != null)
                        {
                            if (method.Parameters.Length == 0)
                            {
                                result.Add(new CodeTemplateVariableValue(data.Symbol.Name + " ()", ((CompletionData)data).Icon));
                            }
                            continue;
                        }

                        result.Add(new CodeTemplateVariableValue(data.Symbol.Name, ((CompletionData)data).Icon));
                    }
                }

                foreach (var data in list.OfType <ISymbolCompletionData> ())
                {
                    var m = data.Symbol as IParameterSymbol;
                    if (m != null)
                    {
                        if (GetElementType(compilation, m.Type).TypeKind != TypeKind.Error)
                        {
                            result.Add(new CodeTemplateVariableValue(m.Name, ((CompletionData)data).Icon));
                        }
                    }
                }

                foreach (var sym in list.OfType <ISymbolCompletionData> ())
                {
                    var m = sym.Symbol as ILocalSymbol;
                    if (m == null)
                    {
                        continue;
                    }
                    if (GetElementType(compilation, m.Type).TypeKind != TypeKind.Error)
                    {
                        result.Add(new CodeTemplateVariableValue(m.Name, ((CompletionData)m).Icon));
                    }
                }
            }
            return(new CodeTemplateListDataProvider(result));
        }
Ejemplo n.º 2
0
        protected override void Update(CommandInfo info)
        {
            var currentProject = IdeApp.ProjectOperations.CurrentSelectedProject;

            if (currentProject == null)
            {
                info.Enabled = false;
                return;
            }
            var analysisProject = TypeSystemService.GetCodeAnalysisProject(currentProject);

            if (analysisProject == null)
            {
                info.Enabled = false;
                return;
            }
            var pad           = IdeApp.Workbench.GetPad <ProjectSolutionPad> ().Content as ProjectSolutionPad;
            var selectedNodes = pad.TreeView.GetSelectedNodes();

            if (selectedNodes == null || selectedNodes.Length != 1)
            {
                info.Enabled = false;
                return;
            }
            info.Enabled = true;
        }
Ejemplo n.º 3
0
        static void CreateCSharpParsedDocument(RazorCSharpParserContext context)
        {
            if (context.Project == null)
            {
                return;
            }

            context.CSharpCode       = CreateCodeFile(context);
            context.ParsedSyntaxTree = CSharpSyntaxTree.ParseText(Microsoft.CodeAnalysis.Text.SourceText.From(context.CSharpCode));

            var originalProject = TypeSystemService.GetCodeAnalysisProject(context.Project);

            if (originalProject != null)
            {
                string fileName   = context.FileName + ".g.cs";
                var    documentId = TypeSystemService.GetDocumentId(originalProject.Id, fileName);
                if (documentId == null)
                {
                    context.AnalysisDocument = originalProject.AddDocument(
                        fileName,
                        context.ParsedSyntaxTree?.GetRoot());
                }
                else
                {
                    context.AnalysisDocument = TypeSystemService.GetCodeAnalysisDocument(documentId);
                }
            }
        }
Ejemplo n.º 4
0
        public Task <Microsoft.CodeAnalysis.Compilation> GetCompilationAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var project = TypeSystemService.GetCodeAnalysisProject(adhocProject ?? Project);

            if (project == null)
            {
                return(new Task <Microsoft.CodeAnalysis.Compilation> (() => null));
            }
            return(project.GetCompilationAsync(cancellationToken));
        }
Ejemplo n.º 5
0
        public IListDataProvider <string> GetCollections()
        {
            var result          = new List <CodeTemplateVariableValue> ();
            var ext             = CurrentContext.DocumentContext.GetContent <CompletionTextEditorExtension> ();
            var analysisProject = TypeSystemService.GetCodeAnalysisProject(CurrentContext.DocumentContext.Project);
            var compilation     = analysisProject != null?analysisProject.GetCompilationAsync().Result : null;

            if (ext != null)
            {
                if (list == null)
                {
                    list = ext.HandleCodeCompletionAsync(
                        CurrentContext.DocumentContext.GetContent <MonoDevelop.Ide.CodeCompletion.ICompletionWidget> ().CurrentCodeCompletionContext,
                        CompletionTriggerInfo.CodeCompletionCommand).Result;
                }

                //foreach (var data in list.OfType<ISymbolCompletionData> ()) {
                //	if (data.Symbol == null)
                //		continue;
                //	var type = data.Symbol.GetReturnType ();
                //	if (type == null)
                //		continue;
                //	if (GetElementType (compilation, type) != null) {
                //		var method = data as IMethodSymbol;
                //		if (method != null) {
                //			if (method.Parameters.Length == 0)
                //				result.Add (new CodeTemplateVariableValue (data.Symbol.Name + " ()", ((CompletionData)data).Icon));
                //			continue;
                //		}
                //		if (!result.Any (r => r.Text == data.Symbol.Name))
                //			result.Add (new CodeTemplateVariableValue (data.Symbol.Name, ((CompletionData)data).Icon));
                //	}
                //}

                //foreach (var data in list.OfType<ISymbolCompletionData> ()) {
                //	var m = data.Symbol as IParameterSymbol;
                //	if (m != null) {
                //		if (GetElementType (compilation, m.Type) != null && !result.Any (r => r.Text == m.Name))
                //			result.Add (new CodeTemplateVariableValue (m.Name, ((CompletionData)data).Icon));
                //	}
                //}

                //foreach (var sym in list.OfType<ISymbolCompletionData> ()) {
                //	var m = sym.Symbol as ILocalSymbol;
                //	if (m == null)
                //		continue;
                //	if (GetElementType (compilation, m.Type) != null && !result.Any (r => r.Text == m.Name))
                //		result.Add (new CodeTemplateVariableValue (m.Name, ((CompletionData)sym).Icon));
                //}
            }
            return(new CodeTemplateListDataProvider(result));
        }
Ejemplo n.º 6
0
        protected override void Update(CommandInfo info)
        {
            var project = IdeApp.ProjectOperations.CurrentSelectedProject;

            if (project == null || TypeSystemService.GetCodeAnalysisProject(project) == null)
            {
                info.Text    = GettextCatalog.GetString("Current Project");
                info.Enabled = false;
                return;
            }
            info.Text    = GettextCatalog.GetString("Project '{0}'", project.Name);
            info.Enabled = true;
        }
        internal static async Task <LookupResult> TryLookupSymbol(string documentationCommentId, MonoDevelop.Projects.Project hintProject, CancellationToken token)
        {
            Microsoft.CodeAnalysis.Project codeAnalysisHintProject = null;
            LookupResult result = LookupResult.Failure;

            if (hintProject != null)
            {
                codeAnalysisHintProject = TypeSystemService.GetCodeAnalysisProject(hintProject);
                if (codeAnalysisHintProject != null)
                {
                    var curResult = await TryLookupSymbolInProject(codeAnalysisHintProject, documentationCommentId, token);

                    if (curResult.Success)
                    {
                        curResult.MonoDevelopProject = hintProject;
                        result = curResult;
                    }
                }
            }
            if (result.Success && result.Symbol.IsDefinedInSource())
            {
                return(result);
            }
            foreach (var ws in TypeSystemService.AllWorkspaces)
            {
                foreach (var prj in ws.CurrentSolution.Projects)
                {
                    if (prj == codeAnalysisHintProject)
                    {
                        continue;
                    }
                    var curResult = await TryLookupSymbolInProject(prj, documentationCommentId, token);

                    if (curResult.Success)
                    {
                        curResult.MonoDevelopProject = TypeSystemService.GetMonoProject(prj);
                        if (curResult.Symbol.IsDefinedInSource())
                        {
                            return(curResult);
                        }
                        result = curResult;
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 8
0
        internal static async void Execute()
        {
            var project = IdeApp.ProjectOperations.CurrentSelectedProject;

            if (project == null)
            {
                return;
            }
            var analysisProject = TypeSystemService.GetCodeAnalysisProject(project);

            if (analysisProject == null)
            {
                return;
            }
            try {
                using (var monitor = IdeApp.Workbench.ProgressMonitors.GetStatusProgressMonitor(GettextCatalog.GetString("Analyzing project"), null, false, true, false, null, true)) {
                    CancellationToken token = monitor.CancellationToken;
                    var allDiagnostics      = await Task.Run(async delegate {
                        var diagnosticList = new List <Diagnostic> ();
                        monitor.BeginTask(GettextCatalog.GetString("Analyzing {0}", project.Name), 1);
                        var providers = await AnalyzeWholeSolutionHandler.GetProviders(analysisProject);
                        diagnosticList.AddRange(await AnalyzeWholeSolutionHandler.GetDiagnostics(analysisProject, providers, token));
                        monitor.EndTask();
                        return(diagnosticList);
                    }).ConfigureAwait(false);

                    await Runtime.RunInMainThread(delegate {
                        AnalyzeWholeSolutionHandler.Report(monitor, allDiagnostics);
                    }).ConfigureAwait(false);
                }
            } catch (OperationCanceledException) {
            } catch (AggregateException ae) {
                ae.Flatten().Handle(ix => ix is OperationCanceledException);
            } catch (Exception e) {
                LoggingService.LogError("Error while running diagnostics.", e);
            }
        }
Ejemplo n.º 9
0
        public override async Task FindReferences(ProjectReference projectReference, SearchProgressMonitor monitor)
        {
            await Task.Run(delegate {
                var currentProject = IdeApp.ProjectOperations.CurrentSelectedProject;
                if (currentProject == null)
                {
                    return;
                }
                var analysisProject = TypeSystemService.GetCodeAnalysisProject(currentProject);
                if (analysisProject == null)
                {
                    return;
                }

                monitor.BeginTask(GettextCatalog.GetString("Analyzing project"), analysisProject.Documents.Count());
                Parallel.ForEach(analysisProject.Documents, async document => {
                    try {
                        var model = await document.GetSemanticModelAsync(monitor.CancellationToken).ConfigureAwait(false);
                        if (monitor.CancellationToken.IsCancellationRequested)
                        {
                            return;
                        }

                        var root = await model.SyntaxTree.GetRootAsync(monitor.CancellationToken).ConfigureAwait(false);
                        if (monitor.CancellationToken.IsCancellationRequested)
                        {
                            return;
                        }

                        root.DescendantNodes(node => {
                            if (monitor.CancellationToken.IsCancellationRequested)
                            {
                                return(false);
                            }

                            if (node is ExpressionSyntax expr)
                            {
                                var info = model.GetSymbolInfo(expr);
                                if (info.Symbol == null || info.Symbol.ContainingAssembly == null)
                                {
                                    return(true);
                                }
                                if (projectReference.Reference.IndexOf(',') >= 0)
                                {
                                    if (!string.Equals(info.Symbol.ContainingAssembly.ToString(), projectReference.Reference, StringComparison.OrdinalIgnoreCase))
                                    {
                                        return(true);
                                    }
                                }
                                else
                                {
                                    if (!info.Symbol.ContainingAssembly.ToString().StartsWith(projectReference.Reference, StringComparison.OrdinalIgnoreCase))
                                    {
                                        return(true);
                                    }
                                }
                                monitor.ReportResult(new MemberReference(info.Symbol, document.FilePath, node.Span.Start, node.Span.Length));
                                return(false);
                            }
                            return(true);
                        }).Count();
                    } finally {
                        monitor.Step();
                    }
                });
                monitor.EndTask();
            });
Ejemplo n.º 10
0
 public IEnumerable <Project> GetProjectsWithInstalledPackage(Solution solution, string packageName, string version)
 {
     return(PackageServices.GetProjectsWithInstalledPackage(IdeApp.ProjectOperations.CurrentSelectedSolution, packageName, version).Select(p => TypeSystemService.GetCodeAnalysisProject(p)));
 }
Ejemplo n.º 11
0
        void DocumentContext_DocumentParsed(object sender, EventArgs e)
        {
            SubscribeCaretPositionChange();

            // Fixes a potential memory leak see: https://bugzilla.xamarin.com/show_bug.cgi?id=38041
            if (ownerProjects.Count > 1)
            {
                var currentOwners = ownerProjects.Where(p => p != DocumentContext.Project).Select(p => TypeSystemService.GetCodeAnalysisProject(p)).ToList();
                CancelDocumentParsedUpdate();
                var token = documentParsedCancellationTokenSource.Token;
                Task.Run(async delegate {
                    foreach (var otherProject in currentOwners)
                    {
                        if (otherProject == null)
                        {
                            continue;
                        }
                        await otherProject.GetCompilationAsync(token).ConfigureAwait(false);
                    }
                });
            }
        }
Ejemplo n.º 12
0
        public override async System.Threading.Tasks.Task <ParsedDocument> Parse(MonoDevelop.Ide.TypeSystem.ParseOptions options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            var fileName = options.FileName;
            var project  = options.Project;
            var result   = new CSharpParsedDocument(fileName);

            if (project != null)
            {
                var projectFile = project.Files.GetFile(fileName);
                if (projectFile != null && !TypeSystemParserNode.IsCompileBuildAction(projectFile.BuildAction))
                {
                    result.Flags |= ParsedDocumentFlags.NonSerializable;
                }
            }

            var        compilerArguments = GetCompilerArguments(project);
            SyntaxTree unit = null;

            if (project != null)
            {
                var curDoc = options.RoslynDocument;
                if (curDoc == null)
                {
                    var curProject = TypeSystemService.GetCodeAnalysisProject(project);
                    if (curProject != null)
                    {
                        var documentId = TypeSystemService.GetDocumentId(project, fileName);
                        if (documentId != null)
                        {
                            curDoc = curProject.GetDocument(documentId);
                        }
                    }
                }
                if (curDoc != null)
                {
                    try {
                        var model = await curDoc.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                        unit       = model.SyntaxTree;
                        result.Ast = model;
                    } catch (AggregateException ae) {
                        ae.Flatten().Handle(x => x is OperationCanceledException);
                        return(result);
                    } catch (OperationCanceledException) {
                        return(result);
                    } catch (Exception e) {
                        LoggingService.LogError("Error while getting the semantic model for " + fileName, e);
                    }
                }
            }

            if (unit == null)
            {
                unit = CSharpSyntaxTree.ParseText(SourceText.From(options.Content.Text), compilerArguments, fileName);
            }

            result.Unit = unit;

            DateTime time;

            try {
                time = System.IO.File.GetLastWriteTimeUtc(fileName);
            } catch (Exception) {
                time = DateTime.UtcNow;
            }
            result.LastWriteTimeUtc = time;
            return(result);
        }
Ejemplo n.º 13
0
        protected async override void Run()
        {
            var currentProject = IdeApp.ProjectOperations.CurrentSelectedProject;

            if (currentProject == null)
            {
                return;
            }
            var analysisProject = TypeSystemService.GetCodeAnalysisProject(currentProject);

            if (analysisProject == null)
            {
                return;
            }
            var pad           = IdeApp.Workbench.GetPad <ProjectSolutionPad> ().Content as ProjectSolutionPad;
            var selectedNodes = pad.TreeView.GetSelectedNodes();

            if (selectedNodes == null || selectedNodes.Length != 1)
            {
                return;
            }
            var dataItem = selectedNodes[0].DataItem;

            var projectRef = dataItem as ProjectReference;

            if (projectRef != null)
            {
                await Task.Run(delegate {
                    using (var monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor(true, true)) {
                        monitor.BeginTask(GettextCatalog.GetString("Analyzing project"), analysisProject.Documents.Count());
                        Parallel.ForEach(analysisProject.Documents, async document => {
                            try {
                                var model = await document.GetSemanticModelAsync(monitor.CancellationToken).ConfigureAwait(false);
                                if (monitor.CancellationToken.IsCancellationRequested)
                                {
                                    return;
                                }

                                var root = await model.SyntaxTree.GetRootAsync(monitor.CancellationToken).ConfigureAwait(false);
                                if (monitor.CancellationToken.IsCancellationRequested)
                                {
                                    return;
                                }

                                root.DescendantNodes(node => {
                                    if (monitor.CancellationToken.IsCancellationRequested)
                                    {
                                        return(false);
                                    }

                                    var expr = node as ExpressionSyntax;
                                    if (expr != null)
                                    {
                                        var info = model.GetSymbolInfo(expr);
                                        if (info.Symbol == null || info.Symbol.ContainingAssembly == null)
                                        {
                                            return(true);
                                        }
                                        if (projectRef.Reference.IndexOf(',') >= 0)
                                        {
                                            if (!string.Equals(info.Symbol.ContainingAssembly.ToString(), projectRef.Reference, StringComparison.OrdinalIgnoreCase))
                                            {
                                                return(true);
                                            }
                                        }
                                        else
                                        {
                                            if (!info.Symbol.ContainingAssembly.ToString().StartsWith(projectRef.Reference, StringComparison.OrdinalIgnoreCase))
                                            {
                                                return(true);
                                            }
                                        }
                                        monitor.ReportResult(new MemberReference(info.Symbol, document.FilePath, node.Span.Start, node.Span.Length));
                                        return(false);
                                    }
                                    return(true);
                                }).Count();
                            } finally {
                                monitor.Step();
                            }
                        });
                        monitor.EndTask();
                    }
                }).ConfigureAwait(false);
            }
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Gets a CodeAnalysis project from a MonoDevelop project
 /// </summary>
 public static Microsoft.CodeAnalysis.Project GetCodeAnalysisProject(this MonoDevelop.Projects.Project monoDevelopProject)
 {
     return(TypeSystemService.GetCodeAnalysisProject(monoDevelopProject));
 }