示例#1
0
        public DocumentId AddDocument(DocumentCreationArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            if (args.SourceTextContainer == null)
            {
                throw new ArgumentNullException(nameof(args.SourceTextContainer));
            }

            if (!args.IsCSharpScript)
            {
                var workspace = GetProjectWorkspace();

                //check workspace is already created for project
                if (workspace == null)
                {
                    //create workspace
                    workspace = new WorkspaceImpl(HostServices, WorkspaceImpl.WorkspaceType.Project);
                    lock (workspaces)
                        workspaces.Add(workspace);

                    //create project
                    var project = CreateProject(workspace, args);
                    workspace.SetCurrentSolutionPublic(project.Solution);
                    workspace.project = project.Id;

                    //create project file documents
                    if (NeoAxisCoreExchange.Instance.GetProjectData(out var csFiles, out var references))
                    {
                        foreach (var csFile in csFiles)
                        {
                            OnAddCsFileToProject(csFile);
                        }
                    }
                }

                //open document editor
                {
                    if (workspace.documentByFileName.TryGetValue(args.CsFileProjectPath, out var documentId))
                    {
                        //project contains the file

                        //open document
                        workspace.CallDocumentOpenedAndContextUpdated(documentId, args.SourceTextContainer);

                        //configure editor
                        _diagnosticsUpdatedNotifiers.TryAdd(documentId, args.OnDiagnosticsUpdated);
                        workspace.ApplyingTextChange += (d, s) =>
                        {
                            if (documentId == d)
                            {
                                args.OnTextUpdated(s);
                            }
                        };

                        workspace.openedDocuments[documentId] = 1;

                        return(documentId);
                    }
                    else
                    {
                        //project not contains the file

                        //create workspace
                        workspace = new WorkspaceImpl(HostServices, WorkspaceImpl.WorkspaceType.FreeCsFile);
                        lock (workspaces)
                            workspaces.Add(workspace);

                        //create project
                        var project = CreateProject(workspace, args);
                        workspace.SetCurrentSolutionPublic(project.Solution);
                        workspace.project = project.Id;

                        //create document
                        documentId = DocumentId.CreateNewId(project.Id);
                        var solution = project.Solution.AddDocument(documentId, args.Name ?? project.Name, args.SourceTextContainer.CurrentText);
                        var document = solution.GetDocument(documentId);
                        workspace.SetCurrentSolutionPublic(document.Project.Solution);
                        workspace.documents.Add(documentId);

                        //open document
                        workspace.CallDocumentOpenedAndContextUpdated(documentId, args.SourceTextContainer);

                        //configure editor
                        _diagnosticsUpdatedNotifiers.TryAdd(documentId, args.OnDiagnosticsUpdated);
                        workspace.ApplyingTextChange += (d, s) =>
                        {
                            if (documentId == d)
                            {
                                args.OnTextUpdated(s);
                            }
                        };

                        workspace.openedDocuments[documentId] = 1;

                        return(documentId);
                    }
                }
            }
            else
            {
                //create workspace
                var workspace = new WorkspaceImpl(HostServices, WorkspaceImpl.WorkspaceType.CSharpScript);
                lock (workspaces)
                    workspaces.Add(workspace);

                //create project
                var project = CreateProject(workspace, args);
                workspace.SetCurrentSolutionPublic(project.Solution);
                workspace.project = project.Id;

                //create document
                var documentId = DocumentId.CreateNewId(project.Id);
                var solution   = project.Solution.AddDocument(documentId, args.Name ?? project.Name, args.SourceTextContainer.CurrentText);
                var document   = solution.GetDocument(documentId);
                workspace.SetCurrentSolutionPublic(document.Project.Solution);
                workspace.documents.Add(documentId);

                //open document
                workspace.CallDocumentOpenedAndContextUpdated(documentId, args.SourceTextContainer);

                //configure editor
                _diagnosticsUpdatedNotifiers.TryAdd(documentId, args.OnDiagnosticsUpdated);
                workspace.ApplyingTextChange += (d, s) =>
                {
                    if (documentId == d)
                    {
                        args.OnTextUpdated(s);
                    }
                };

                workspace.openedDocuments[documentId] = 1;

                return(documentId);
            }
        }
示例#2
0
        Project CreateProject(WorkspaceImpl workspace, DocumentCreationArgs args)
        {
            string name;

            if (workspace.workspaceType == WorkspaceImpl.WorkspaceType.Project)
            {
                name = "Project";
            }
            else
            {
                name = args.Name ?? "Program" + Interlocked.Increment(ref _documentNumber);
            }

            var id = ProjectId.CreateNewId(name);

            //create parse options
            var parseOptions = CreateDefaultParseOptions(args.IsCSharpScript);

            //create compilation options
            var compilationOptions = CreateCompilationOptions(args);

            if (args.IsCSharpScript)
            {
                compilationOptions = compilationOptions.WithScriptClassName(name);
            }


            //get references
            List <string> referenceNames;

            if (workspace.workspaceType == WorkspaceImpl.WorkspaceType.Project)
            {
                referenceNames = new List <string>();
                if (NeoAxisCoreExchange.Instance.GetProjectData(out _, out var referenceNames2))
                {
                    foreach (var referenceName in referenceNames2)
                    {
                        var fullPath = NeoAxisCoreExchange.Instance.ResolveAssemblyName(referenceName);
                        if (File.Exists(fullPath))
                        {
                            referenceNames.Add(fullPath);
                        }
                    }
                }
            }
            else
            {
                referenceNames = NeoAxisCoreExchange.Instance.CSharpScriptReferenceAssemblies;
            }

            //!!!!RoslynHostReferences class is too complex
            var references1 = RoslynHostReferences.Default.With(assemblyPathReferences: referenceNames);
            var references  = references1.GetReferences(_documentationProviderService.GetDocumentationProvider);


            var newSolution = workspace.CurrentSolution.AddProject(ProjectInfo.Create(
                                                                       id,
                                                                       VersionStamp.Create(),
                                                                       name,
                                                                       name,
                                                                       LanguageNames.CSharp,
                                                                       isSubmission: args.IsCSharpScript,
                                                                       parseOptions: parseOptions,
                                                                       compilationOptions: compilationOptions,
                                                                       metadataReferences: /*previousProject != null ? ImmutableArray<MetadataReference>.Empty : */ references,
                                                                       //projectReferences: /*previousProject != null ? new[] { new ProjectReference( previousProject.Id ) } :*/ null,
                                                                       hostObjectType: args.ContextType
                                                                       ));

            var project = newSolution.GetProject(id);

            return(project);
        }