Пример #1
0
        /// <summary>
        /// Gets a new instance of the code editor with diagnostics. This is the highest level code editor control (code editor, splittable, with quick class browser and error message window).
        /// </summary>
        /// <param name="workspace">The workspace (contains solution, project and referenced assemblies).</param>
        /// <param name="initialText">The initial code text.</param>
        /// <param name="fontFamily">The font family used for the code editor.</param>
        /// <param name="fontSize">Size of the font used for the code editor.</param>
        /// <returns>New instance of the code editor with diagnostics.</returns>
        /// <exception cref="ArgumentNullException">workspace</exception>
        public CodeEditorWithDiagnostics NewCodeEditorWithDiagnostics(AltaxoWorkspaceBase workspace, string initialText, FontFamily fontFamily = null, double?fontSize = null)
        {
            if (null == workspace)
            {
                throw new ArgumentNullException(nameof(workspace));
            }

            var codeEditor = new CodeEditorWithDiagnostics()
            {
                DocumentText = initialText
            };

            var editor = codeEditor.primaryTextEditor;

            editor.FontFamily = fontFamily ?? _defaultFont;
            editor.FontSize   = fontSize ?? _defaultFontSize;

            // create the source text container that is connected with this editor
            var sourceTextContainer = new RoslynSourceTextContainerAdapter(codeEditor.Document, codeEditor);
            var document            = workspace.CreateAndOpenDocument(sourceTextContainer, sourceTextContainer.UpdateText);

            codeEditor.Adapter = new CodeEditorViewAdapterCSharp(workspace, document.Id, sourceTextContainer);

            editor.Document.UndoStack.ClearAll();

            return(codeEditor);
        }
Пример #2
0
        /// <summary>
        /// Uninitializes the specified code editor with diagnostics and (if there is no other document open) removes the corresponding workspace from Roslyn.
        /// </summary>
        /// <param name="codeEditor">The code editor.</param>
        public void Uninitialize(CodeEditorWithDiagnostics codeEditor)
        {
            var adapter = codeEditor.Adapter;

            codeEditor.Adapter = null;
            if (null != adapter)
            {
                adapter.Workspace.CloseDocument(adapter.DocumentId);
                if (null == adapter.Workspace.GetOpenDocumentIds().FirstOrDefault()) // dispose workspace if it has no open documents
                {
                    adapter.Workspace.Dispose();
                }
            }
        }