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(); }
/// <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)); } }