Example #1
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);
                }
            }
        }
        internal static GuiBuilderWindow GetWindow(string file, Project project)
        {
            if (!IdeApp.Workspace.IsOpen)
            {
                return(null);
            }
            if (!GtkDesignInfo.HasDesignedObjects(project))
            {
                return(null);
            }
            GtkDesignInfo info = GtkDesignInfo.FromProject(project);

            if (file.StartsWith(info.GtkGuiFolder))
            {
                return(null);
            }
            var docId = TypeSystemService.GetDocumentId(project, file);

            if (docId == null)
            {
                return(null);
            }
            var doc = TypeSystemService.GetCodeAnalysisDocument(docId);

            if (doc == null)
            {
                return(null);
            }
            Microsoft.CodeAnalysis.SemanticModel semanticModel;
            try {
                semanticModel = doc.GetSemanticModelAsync().Result;
            } catch {
                return(null);
            }
            if (semanticModel == null)
            {
                return(null);
            }
            var root = semanticModel.SyntaxTree.GetRoot();

            foreach (var classDeclaration in root.DescendantNodesAndSelf(child => !(child is BaseTypeDeclarationSyntax)).OfType <ClassDeclarationSyntax> ())
            {
                var c = semanticModel.GetDeclaredSymbol(classDeclaration);
                GuiBuilderWindow win = info.GuiBuilderProject.GetWindowForClass(c.ToDisplayString(Microsoft.CodeAnalysis.SymbolDisplayFormat.CSharpErrorMessageFormat));
                if (win != null)
                {
                    return(win);
                }
            }
            return(null);
        }
        Task EnsureAnalysisDocumentIsOpen()
        {
            if (analysisDocument != null)
            {
                Microsoft.CodeAnalysis.Document doc;
                try {
                    doc = RoslynWorkspace.CurrentSolution.GetDocument(analysisDocument);
                    if (doc == null && RoslynWorkspace.CurrentSolution.ContainsAdditionalDocument(analysisDocument))
                    {
                        return(Task.CompletedTask);
                    }
                } catch (Exception) {
                    doc = null;
                }
                if (doc != null)
                {
                    return(Task.CompletedTask);
                }
            }

            if (Editor == null)
            {
                UnsubscribeAnalysisDocument();
                UpdateTextBufferRegistration();
                return(Task.CompletedTask);
            }

            lock (analysisDocumentLock) {
                UnsubscribeRoslynWorkspace();
                RoslynWorkspace = typeSystemService.GetWorkspaceInternal(Project?.ParentSolution);
                SubscribeRoslynWorkspace();
                var newAnalysisDocument = FileName != null?typeSystemService.GetDocumentId(Project, FileName) : null;

                var changedAnalysisDocument = newAnalysisDocument != analysisDocument;
                analysisDocument = newAnalysisDocument;
                if (changedAnalysisDocument)
                {
                    OnAnalysisDocumentChanged(EventArgs.Empty);
                }
            }
            return(Task.CompletedTask);
        }
Example #4
0
        void OnFileAdded(object sender, ProjectFileEventArgs e)
        {
            foreach (ProjectFileEventInfo args in e)
            {
                var docId = TypeSystemService.GetDocumentId(args.Project, args.ProjectFile.Name);
                if (docId == null)
                {
                    continue;
                }
                var doc = TypeSystemService.GetCodeAnalysisDocument(docId);
                if (doc == null)
                {
                    continue;
                }

                string dir = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "stetic"), "deleted-designs");
                if (!Directory.Exists(dir) || Directory.GetFiles(dir).Length == 0)
                {
                    continue;
                }
                var semanticModel = doc.GetSemanticModelAsync().Result;
                if (semanticModel == null)
                {
                    continue;
                }

                foreach (var classDeclaration in semanticModel.SyntaxTree.GetRoot().DescendantNodesAndSelf(child => !(child is BaseTypeDeclarationSyntax)).OfType <ClassDeclarationSyntax> ())
                {
                    var    c    = semanticModel.GetDeclaredSymbol(classDeclaration);
                    string path = Path.Combine(dir, c.ToDisplayString(Microsoft.CodeAnalysis.SymbolDisplayFormat.CSharpErrorMessageFormat) + ".xml");
                    if (!System.IO.File.Exists(path))
                    {
                        continue;
                    }
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(path);
                    AddNewComponent(xmldoc.DocumentElement);
                    System.IO.File.Delete(path);
                }
            }
        }
Example #5
0
        void OnFileRemoved(object sender, ProjectFileEventArgs e)
        {
            ArrayList toDelete = new ArrayList();

            foreach (ProjectFileEventInfo args in e)
            {
                var docId = TypeSystemService.GetDocumentId(args.Project, args.ProjectFile.Name);
                if (docId == null)
                {
                    continue;
                }
                var doc = TypeSystemService.GetCodeAnalysisDocument(docId);
                if (doc == null)
                {
                    continue;
                }
                var semanticModel = doc.GetSemanticModelAsync().Result;
                if (semanticModel == null)
                {
                    continue;
                }


                foreach (var classDeclaration in semanticModel.SyntaxTree.GetRoot().DescendantNodesAndSelf(child => !(child is BaseTypeDeclarationSyntax)).OfType <ClassDeclarationSyntax> ())
                {
                    var c = semanticModel.GetDeclaredSymbol(classDeclaration);
                    GuiBuilderWindow win = GetWindowForClass(c.ToDisplayString(Microsoft.CodeAnalysis.SymbolDisplayFormat.MinimallyQualifiedFormat));
                    if (win != null)
                    {
                        toDelete.Add(win);
                    }
                }
            }

            foreach (GuiBuilderWindow win in toDelete)
            {
                Remove(win);
            }
        }
Example #6
0
        Task EnsureAnalysisDocumentIsOpen()
        {
            if (analysisDocument != null)
            {
                Microsoft.CodeAnalysis.Document doc;
                try {
                    doc = RoslynWorkspace.CurrentSolution.GetDocument(analysisDocument);
                    if (doc == null && RoslynWorkspace.CurrentSolution.ContainsAdditionalDocument(analysisDocument))
                    {
                        return(Task.CompletedTask);
                    }
                } catch (Exception) {
                    doc = null;
                }
                if (doc != null)
                {
                    return(Task.CompletedTask);
                }
            }
            if (Editor == null)
            {
                UnsubscribeAnalysisDocument();
                return(Task.CompletedTask);
            }
            if (Project != null && !IsUnreferencedSharedProject(Project))
            {
                lock (analysisDocumentLock) {
                    UnsubscribeRoslynWorkspace();
                    RoslynWorkspace = TypeSystemService.GetWorkspace(this.Project.ParentSolution);
                    if (RoslynWorkspace == null)                     // Solution not loaded yet
                    {
                        return(Task.CompletedTask);
                    }
                    SubscribeRoslynWorkspace();
                    analysisDocument = FileName != null?TypeSystemService.GetDocumentId(this.Project, this.FileName) : null;

                    if (analysisDocument != null && !RoslynWorkspace.CurrentSolution.ContainsAdditionalDocument(analysisDocument) && !RoslynWorkspace.IsDocumentOpen(analysisDocument))
                    {
                        TypeSystemService.InformDocumentOpen(analysisDocument, Editor, this);
                        OnAnalysisDocumentChanged(EventArgs.Empty);
                    }
                    return(Task.CompletedTask);
                }
            }
            lock (adhocProjectLock) {
                var token = analysisDocumentSrc.Token;
                if (adhocProject != null || IsInProjectSettingLoadingProcess)
                {
                    return(Task.CompletedTask);
                }

                if (Editor != null)
                {
                    var node = TypeSystemService.GetTypeSystemParserNode(Editor.MimeType, BuildAction.Compile);
                    if (Editor.MimeType == "text/x-csharp" || node?.Parser.CanGenerateAnalysisDocument(Editor.MimeType, BuildAction.Compile, new string[0]) == true)
                    {
                        var newProject = Services.ProjectService.CreateDotNetProject("C#");

                        this.adhocProject = newProject;

                        newProject.Name = "InvisibleProject";
                        newProject.References.Add(ProjectReference.CreateAssemblyReference("mscorlib"));
                        newProject.References.Add(ProjectReference.CreateAssemblyReference("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));
                        newProject.References.Add(ProjectReference.CreateAssemblyReference("System.Core"));

                        // Use a different name for each project, otherwise the msbuild builder will complain about duplicate projects.
                        newProject.FileName = "adhoc_" + (++adhocProjectCount) + ".csproj";
                        if (!Window.ViewContent.IsUntitled)
                        {
                            adHocFile = Editor.FileName;
                        }
                        else
                        {
                            adHocFile = (Platform.IsWindows ? "C:\\" : "/") + Window.ViewContent.UntitledName + ".cs";
                        }

                        newProject.Files.Add(new ProjectFile(adHocFile, BuildAction.Compile));

                        adhocSolution = new Solution();
                        adhocSolution.AddConfiguration("", true);
                        adhocSolution.DefaultSolutionFolder.AddItem(newProject);
                        return(TypeSystemService.Load(adhocSolution, new ProgressMonitor(), token, false).ContinueWith(task => {
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }
                            UnsubscribeRoslynWorkspace();
                            RoslynWorkspace = task.Result.FirstOrDefault();                              // 1 solution loaded ->1 workspace as result
                            SubscribeRoslynWorkspace();
                            analysisDocument = RoslynWorkspace.CurrentSolution.Projects.First().DocumentIds.First();
                            TypeSystemService.InformDocumentOpen(RoslynWorkspace, analysisDocument, Editor, this);
                            OnAnalysisDocumentChanged(EventArgs.Empty);
                        }));
                    }
                }
            }
            return(Task.CompletedTask);
        }
Example #7
0
        Task EnsureAnalysisDocumentIsOpen()
        {
            if (analysisDocument != null)
            {
                Microsoft.CodeAnalysis.Document doc;
                try {
                    doc = RoslynWorkspace.CurrentSolution.GetDocument(analysisDocument);
                } catch (Exception) {
                    doc = null;
                }
                if (doc != null)
                {
                    return(SpecializedTasks.EmptyTask);
                }
            }
            if (Editor == null)
            {
                UnsubscibeAnalysisdocument();
                return(SpecializedTasks.EmptyTask);
            }
            if (Project != null && Editor.MimeType == "text/x-csharp" && !IsUnreferencedSharedProject(Project))
            {
                UnsubscribeRoslynWorkspace();
                RoslynWorkspace = TypeSystemService.GetWorkspace(this.Project.ParentSolution);
                SubscribeRoslynWorkspace();
                analysisDocument = TypeSystemService.GetDocumentId(this.Project, this.FileName);
                if (analysisDocument != null)
                {
                    TypeSystemService.InformDocumentOpen(analysisDocument, Editor);
                }
            }
            else
            {
                CancelEnsureAnalysisDocumentIsOpen();
                lock (adhocProjectLock) {
                    var token = analysisDocumentSrc.Token;
                    if (adhocProject != null)
                    {
                        return(SpecializedTasks.EmptyTask);
                    }
                    if (Editor != null && Editor.MimeType == "text/x-csharp")
                    {
                        var newProject = Services.ProjectService.CreateDotNetProject("C#");
                        this.adhocProject = newProject;

                        newProject.Name = "InvisibleProject";
                        newProject.References.Add(ProjectReference.CreateAssemblyReference("mscorlib"));
                        newProject.References.Add(ProjectReference.CreateAssemblyReference("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));
                        newProject.References.Add(ProjectReference.CreateAssemblyReference("System.Core"));

                        newProject.FileName = "test.csproj";
                        if (!Window.ViewContent.IsUntitled)
                        {
                            adHocFile = Editor.FileName;
                        }
                        else
                        {
                            adHocFile = (Platform.IsWindows ? "C:\\" : "/") + Window.ViewContent.UntitledName + ".cs";
                        }

                        newProject.Files.Add(new ProjectFile(adHocFile, BuildAction.Compile));

                        adhocSolution = new Solution();
                        adhocSolution.AddConfiguration("", true);
                        adhocSolution.DefaultSolutionFolder.AddItem(newProject);
                        return(TypeSystemService.Load(adhocSolution, new ProgressMonitor(), token).ContinueWith(task => {
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }
                            UnsubscribeRoslynWorkspace();
                            RoslynWorkspace = task.Result.FirstOrDefault();                             // 1 solution loaded ->1 workspace as result
                            SubscribeRoslynWorkspace();
                            analysisDocument = TypeSystemService.GetDocumentId(RoslynWorkspace, newProject, adHocFile);
                            TypeSystemService.InformDocumentOpen(RoslynWorkspace, analysisDocument, Editor);
                        }));
                    }
                }
            }
            return(SpecializedTasks.EmptyTask);
        }
Example #8
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);
        }