Example #1
0
        public async Task RenameSymbol(Workspace workspace, DocumentId documentId, RoslynSourceTextContainerAdapter sourceText, int caretPosition, object topLevelWindow, Action FocusOnEditor)
        {
            var document = workspace.CurrentSolution.GetDocument(documentId);
            var symbol   = await RenameHelper.GetRenameSymbol(document, caretPosition).ConfigureAwait(true); // we need Gui context

            if (symbol == null)
            {
                return;
            }

            // var dialog = host.GetService<IRenameSymbolDialog>();
            var dialog = new Gui.CodeEditing.Renaming.RenameSymbolDialog();

            dialog.Initialize(symbol.Name);
            dialog.Show(topLevelWindow);
            if (dialog.ShouldRename && dialog.SymbolName != symbol.Name)
            {
                var newSymbolName = dialog.SymbolName;

                var renameLocations = await Renamer.GetRenameLocationsAsync(document.Project.Solution, new Microsoft.CodeAnalysis.FindSymbols.SymbolAndProjectId(symbol, document.Project.Id), null, CancellationToken.None).ConfigureAwait(true); // we need Gui context afterwards

                var textChanges = renameLocations.Locations.Select(loc => new TextChange(loc.Location.SourceSpan, newSymbolName));
                sourceText.ApplyTextChanges(textChanges, (modifiedSourceText) => workspace.TryApplyChanges(document.WithText(modifiedSourceText).Project.Solution));

                /*
                 *
                 *                      var newSolution = await Renamer.RenameSymbolAsync(document.Project.Solution, symbol, dialog.SymbolName, null).ConfigureAwait(true);
                 *                      var newDocument = newSolution.GetDocument(documentId);
                 *                      // TODO: possibly update entire solution
                 *                      host.UpdateDocument(newDocument);
                 */
            }
            FocusOnEditor?.Invoke();
        }
Example #2
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);
        }
        /// <summary>
        /// Formats the document after entering a trigger character. Trigger chars are e.g. closing curly brace (then format whole paragraph)
        /// or semicolon (then format line).
        /// </summary>
        /// <param name="caretPosition">The caret position after (!) the trigger char.</param>
        /// <returns></returns>
        public async Task FormatDocumentAfterEnteringTriggerChar(Workspace workspace, DocumentId documentId, RoslynSourceTextContainerAdapter sourceText, int caretPosition, char triggerChar)
        {
            var document   = workspace.CurrentSolution.GetDocument(documentId);
            var syntaxTree = await document.GetSyntaxRootAsync();

            TextSpan?textSpanToFormat = null;

            if (triggerChar == '}')
            {
                var visitor = new WalkerForCurlyBrace(caretPosition);
                visitor.Visit(syntaxTree);
                textSpanToFormat = visitor.TextSpanToFormat;
            }
            else if (triggerChar == ';')
            {
                var visitor = new WalkerForSemicolon(caretPosition);
                visitor.Visit(syntaxTree);
                textSpanToFormat = visitor.TextSpanToFormat;
            }

            if (textSpanToFormat.HasValue)
            {
                var textChanges = await Formatter.GetFormattedTextChangesAsync(syntaxTree, textSpanToFormat.Value, workspace);

                sourceText.ApplyTextChanges(textChanges, (modifiedSourceText) => workspace.TryApplyChanges(document.WithText(modifiedSourceText).Project.Solution));
            }
        }