public PerformChangesAsync ( ISymbol symbol, RenameProperties properties ) : Task | ||
symbol | ISymbol | |
properties | RenameProperties | |
return | Task |
public async Task PerformChangesAsync (ISymbol symbol, RenameProperties properties)
{
var solution = IdeApp.ProjectOperations.CurrentSelectedSolution;
var ws = TypeSystemService.GetWorkspace (solution);
var newSolution = await Renamer.RenameSymbolAsync (ws.CurrentSolution, symbol, properties.NewName, ws.Options);
ws.TryApplyChanges (newSolution);
var changes = new List<Change> ();
if (properties.RenameFile && symbol is INamedTypeSymbol) {
var type = (INamedTypeSymbol)symbol;
int currentPart = 1;
var alreadyRenamed = new HashSet<string> ();
foreach (var part in type.Locations) {
if (!part.IsInSource)
continue;
var fileName = part?.SourceTree?.FilePath;
if (fileName == null || alreadyRenamed.Contains (fileName))
continue;
alreadyRenamed.Add (fileName);
string oldFileName = System.IO.Path.GetFileNameWithoutExtension (fileName);
string newFileName;
var newName = properties.NewName;
if (string.IsNullOrEmpty (oldFileName) || string.IsNullOrEmpty (newName))
continue;
if (oldFileName.ToUpper () == newName.ToUpper () || oldFileName.ToUpper ().EndsWith ("." + newName.ToUpper (), StringComparison.Ordinal))
continue;
int idx = oldFileName.IndexOf (type.Name, StringComparison.Ordinal);
if (idx >= 0) {
newFileName = oldFileName.Substring (0, idx) + newName + oldFileName.Substring (idx + type.Name.Length);
} else {
newFileName = currentPart != 1 ? newName + currentPart : newName;
currentPart++;
}
int t = 0;
while (System.IO.File.Exists (GetFullFileName (newFileName, fileName, t))) {
t++;
}
changes.Add (new RenameFileChange (fileName, GetFullFileName (newFileName, fileName, t)));
}
}
RefactoringService.AcceptChanges (new ProgressMonitor (), changes);
}
public RenameItemDialog (ISymbol symbol, RenameRefactoring rename) { this.Build (); string title; if (symbol is ITypeSymbol) { var t = (ITypeSymbol)symbol; if (t.TypeKind == TypeKind.TypeParameter) { title = GettextCatalog.GetString ("Rename Type Parameter"); entry.Text = t.Name; } else { var typeDefinition = t; if (typeDefinition.ContainingType == null) { // not supported for inner types this.renameFileFlag.Visible = true; this.renameFileFlag.Active = t.Locations.First ().SourceTree.FilePath.Contains (typeDefinition.Name); } else { this.renameFileFlag.Active = false; } if (typeDefinition.TypeKind == TypeKind.Interface) title = GettextCatalog.GetString ("Rename Interface"); else if (typeDefinition.TypeKind == TypeKind.Delegate) title = GettextCatalog.GetString ("Rename Delegate"); else if (typeDefinition.TypeKind == TypeKind.Enum) title = GettextCatalog.GetString ("Rename Enum"); else if (typeDefinition.TypeKind == TypeKind.Struct) title = GettextCatalog.GetString ("Rename Struct"); else title = GettextCatalog.GetString ("Rename Class"); } // this.fileName = type.GetDefinition ().Region.FileName; } else if (symbol.Kind == SymbolKind.Field) { title = GettextCatalog.GetString ("Rename Field"); } else if (symbol.Kind == SymbolKind.Property) { title = GettextCatalog.GetString ("Rename Property"); } else if (symbol.Kind == SymbolKind.Event) { title = GettextCatalog.GetString ("Rename Event"); } else if (symbol.Kind == SymbolKind.Method) { var m = (IMethodSymbol)symbol; if (m.MethodKind == MethodKind.Constructor || m.MethodKind == MethodKind.StaticConstructor || m.MethodKind == MethodKind.Destructor) { title = GettextCatalog.GetString ("Rename Class"); } else { title = GettextCatalog.GetString ("Rename Method"); includeOverloadsCheckbox.Visible = m.ContainingType.GetMembers (m.Name).Length > 1; } } else if (symbol.Kind == SymbolKind.Parameter) { title = GettextCatalog.GetString ("Rename Parameter"); } else if (symbol.Kind == SymbolKind.Local) { title = GettextCatalog.GetString ("Rename Variable"); } else if (symbol.Kind == SymbolKind.TypeParameter) { title = GettextCatalog.GetString ("Rename Type Parameter"); } else if (symbol.Kind == SymbolKind.Namespace) { title = GettextCatalog.GetString ("Rename Namespace"); } else if (symbol.Kind == SymbolKind.Label) { title = GettextCatalog.GetString ("Rename Label"); } else { title = GettextCatalog.GetString ("Rename Item"); } Init (title, symbol.Name, async prop => { await rename.PerformChangesAsync (symbol, prop); return new List<Change> (); }); }