Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
		public List<Change> PerformChanges (ISymbol symbol, RenameProperties properties)
		{
			var solution = IdeApp.ProjectOperations.CurrentSelectedSolution;
			var ws = TypeSystemService.GetWorkspace (solution);

			var newSolution = Renamer.RenameSymbolAsync (ws.CurrentSolution, symbol, properties.NewName, ws.Options).Result;
			var result = new List<Change> ();

			foreach (var change in ws.CurrentSolution.GetChanges (newSolution).GetProjectChanges ()) {
				foreach (var changedDocument in change.GetChangedDocuments ()) {
					var oldDoc = change.OldProject.GetDocument (changedDocument);
					var newDoc = change.NewProject.GetDocument (changedDocument);

					foreach (var textChange in oldDoc.GetTextChangesAsync (newDoc).Result.OrderByDescending(ts => ts.Span.Start)) {
						var trChange = new TextReplaceChange ();
						trChange.FileName = oldDoc.FilePath;
						trChange.Offset = textChange.Span.Start;
						trChange.RemovedChars = textChange.Span.Length;
						trChange.InsertedText = textChange.NewText;
						trChange.Description = string.Format (GettextCatalog.GetString ("Replace '{0}' with '{1}'"), symbol.Name, properties.NewName);
						result.Add (trChange);
					}
				}
			}

			if (properties.RenameFile && symbol.Kind == SymbolKind.NamedType) {
				int currentPart = 1;
				var alreadyRenamed = new HashSet<string> ();
				foreach (var part in symbol.Locations) {
					var filePath = part.SourceTree.FilePath;
					if (alreadyRenamed.Contains (filePath))
						continue;
					alreadyRenamed.Add (filePath);

					string oldFileName = System.IO.Path.GetFileNameWithoutExtension (filePath);
					string newFileName;
					if (oldFileName.ToUpper () == properties.NewName.ToUpper () || oldFileName.ToUpper ().EndsWith ("." + properties.NewName.ToUpper (), StringComparison.Ordinal))
						continue;
					int idx = oldFileName.IndexOf (symbol.Name, StringComparison.Ordinal);
					if (idx >= 0) {
						newFileName = oldFileName.Substring (0, idx) + properties.NewName + oldFileName.Substring (idx + symbol.Name.Length);
					} else {
						newFileName = currentPart != 1 ? properties.NewName + currentPart : properties.NewName;
						currentPart++;
					}

					int t = 0;
					while (System.IO.File.Exists (GetFullFileName (newFileName, filePath, t))) {
						t++;
					}
					result.Add (new RenameFileChange (filePath, GetFullFileName (newFileName, filePath, t)));
				}
			}

			return result;
		}
Ejemplo n.º 3
0
        public async Task <List <Change> > PerformChangesAsync(ISymbol symbol, RenameProperties properties)
        {
            var ws          = IdeApp.Workbench.ActiveDocument.DocumentContext.RoslynWorkspace;
            var newSolution = await Renamer.RenameSymbolAsync(ws.CurrentSolution, symbol, properties.NewName, ws.Options);

            var documents = new List <Microsoft.CodeAnalysis.Document> ();

            foreach (var projectChange in newSolution.GetChanges(ws.CurrentSolution).GetProjectChanges())
            {
                documents.AddRange(projectChange.GetChangedDocuments().Select(d => newSolution.GetDocument(d)));
            }
            FilterDuplicateLinkedDocs((MonoDevelopWorkspace)ws, newSolution, documents);
            var changes = new List <Change> ();

            foreach (var newDoc in documents)
            {
                foreach (var textChange in await newDoc.GetTextChangesAsync(ws.CurrentSolution.GetDocument(newDoc.Id)))
                {
                    changes.Add(new TextReplaceChange()
                    {
                        FileName     = newDoc.FilePath,
                        Offset       = textChange.Span.Start,
                        RemovedChars = textChange.Span.Length,
                        InsertedText = textChange.NewText
                    });
                }
            }

            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 (IsCompatibleForRenaming(oldFileName, newName))
                    {
                        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)));
                }
            }
            return(changes);
        }
Ejemplo n.º 4
0
        public List <Change> PerformChanges(ISymbol symbol, RenameProperties properties)
        {
            var solution = IdeApp.ProjectOperations.CurrentSelectedSolution;
            var ws       = TypeSystemService.GetWorkspace(solution);

            var newSolution = Renamer.RenameSymbolAsync(ws.CurrentSolution, symbol, properties.NewName, ws.Options).Result;
            var result      = new List <Change> ();

            foreach (var change in ws.CurrentSolution.GetChanges(newSolution).GetProjectChanges())
            {
                foreach (var changedDocument in change.GetChangedDocuments())
                {
                    var oldDoc = change.OldProject.GetDocument(changedDocument);
                    var newDoc = change.NewProject.GetDocument(changedDocument);

                    foreach (var textChange in oldDoc.GetTextChangesAsync(newDoc).Result.OrderByDescending(ts => ts.Span.Start))
                    {
                        var trChange = new TextReplaceChange();
                        trChange.FileName     = oldDoc.FilePath;
                        trChange.Offset       = textChange.Span.Start;
                        trChange.RemovedChars = textChange.Span.Length;
                        trChange.InsertedText = textChange.NewText;
                        trChange.Description  = string.Format(GettextCatalog.GetString("Replace '{0}' with '{1}'"), symbol.Name, properties.NewName);
                        result.Add(trChange);
                    }
                }
            }

            if (properties.RenameFile && symbol.Kind == SymbolKind.NamedType)
            {
                int currentPart    = 1;
                var alreadyRenamed = new HashSet <string> ();
                foreach (var part in symbol.Locations)
                {
                    var filePath = part.SourceTree.FilePath;
                    if (alreadyRenamed.Contains(filePath))
                    {
                        continue;
                    }
                    alreadyRenamed.Add(filePath);

                    string oldFileName = System.IO.Path.GetFileNameWithoutExtension(filePath);
                    string newFileName;
                    if (oldFileName.ToUpper() == properties.NewName.ToUpper() || oldFileName.ToUpper().EndsWith("." + properties.NewName.ToUpper(), StringComparison.Ordinal))
                    {
                        continue;
                    }
                    int idx = oldFileName.IndexOf(symbol.Name, StringComparison.Ordinal);
                    if (idx >= 0)
                    {
                        newFileName = oldFileName.Substring(0, idx) + properties.NewName + oldFileName.Substring(idx + symbol.Name.Length);
                    }
                    else
                    {
                        newFileName = currentPart != 1 ? properties.NewName + currentPart : properties.NewName;
                        currentPart++;
                    }

                    int t = 0;
                    while (System.IO.File.Exists(GetFullFileName(newFileName, filePath, t)))
                    {
                        t++;
                    }
                    result.Add(new RenameFileChange(filePath, GetFullFileName(newFileName, filePath, t)));
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        public override List <Change> PerformChanges(RefactoringOptions options, object prop)
        {
            RenameProperties properties       = (RenameProperties)prop;
            List <Change>    result           = new List <Change> ();
            IEnumerable <MemberReference> col = null;

            using (var monitor = new MessageDialogProgressMonitor(true, false, false, true)) {
                col = ReferenceFinder.FindReferences(options.SelectedItem, properties.IncludeOverloads, monitor);
                if (col == null)
                {
                    return(result);
                }

                if (properties.RenameFile && options.SelectedItem is IType)
                {
                    var cls            = ((IType)options.SelectedItem).GetDefinition();
                    int currentPart    = 1;
                    var alreadyRenamed = new HashSet <string> ();
                    foreach (var part in cls.Parts)
                    {
                        if (alreadyRenamed.Contains(part.Region.FileName))
                        {
                            continue;
                        }
                        alreadyRenamed.Add(part.Region.FileName);

                        string oldFileName = System.IO.Path.GetFileNameWithoutExtension(part.Region.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(cls.Name, StringComparison.Ordinal);
                        if (idx >= 0)
                        {
                            newFileName = oldFileName.Substring(0, idx) + newName + oldFileName.Substring(idx + cls.Name.Length);
                        }
                        else
                        {
                            newFileName = currentPart != 1 ? newName + currentPart : newName;
                            currentPart++;
                        }

                        int t = 0;
                        while (System.IO.File.Exists(GetFullFileName(newFileName, part.Region.FileName, t)))
                        {
                            t++;
                        }
                        result.Add(new RenameFileChange(part.Region.FileName, GetFullFileName(newFileName, part.Region.FileName, t)));
                    }
                }

                foreach (var memberRef in col)
                {
                    TextReplaceChange change = new TextReplaceChange();
                    change.FileName     = memberRef.FileName;
                    change.Offset       = memberRef.Offset;
                    change.RemovedChars = memberRef.Length;
                    change.InsertedText = properties.NewName;
                    change.Description  = string.Format(GettextCatalog.GetString("Replace '{0}' with '{1}'"), memberRef.GetName(), properties.NewName);
                    result.Add(change);
                }
            }
            return(result);
        }
Ejemplo n.º 6
0
        public async Task <List <Change> > 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);

            var changes = new List <Change> ();

            foreach (var projectChange in newSolution.GetChanges(ws.CurrentSolution).GetProjectChanges())
            {
                foreach (var changedDoc in projectChange.GetChangedDocuments())
                {
                    var newDoc = newSolution.GetDocument(changedDoc);
                    foreach (var textChange in await newDoc.GetTextChangesAsync(ws.CurrentSolution.GetDocument(changedDoc)))
                    {
                        changes.Add(new TextReplaceChange()
                        {
                            FileName     = newDoc.FilePath,
                            Offset       = textChange.Span.Start,
                            RemovedChars = textChange.Span.Length,
                            InsertedText = textChange.NewText
                        });
                    }
                }
            }

            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)));
                }
            }
            return(changes);
        }
        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 void PerformChanges (ISymbol symbol, RenameProperties properties)
		{
			var solution = IdeApp.ProjectOperations.CurrentSelectedSolution;
			var ws = TypeSystemService.GetWorkspace (solution);

			var newSolution = Renamer.RenameSymbolAsync (ws.CurrentSolution, symbol, properties.NewName, ws.Options).Result;

			ws.TryApplyChanges (newSolution);
		}
Ejemplo n.º 9
0
        public override List <Change> PerformChanges(RefactoringOptions options, object prop)
        {
            RenameProperties properties = (RenameProperties)prop;
            List <Change>    result     = new List <Change> ();

            MemberReferenceCollection col = GetReferences(options);

            if (col == null)
            {
                return(result);
            }

            if (properties.RenameFile && options.SelectedItem is IType)
            {
                IType            cls            = (IType)options.SelectedItem;
                int              currentPart    = 1;
                HashSet <string> alreadyRenamed = new HashSet <string> ();
                foreach (IType part in cls.Parts)
                {
                    if (part.CompilationUnit.FileName != options.Document.FileName && System.IO.Path.GetFileNameWithoutExtension(part.CompilationUnit.FileName) != System.IO.Path.GetFileNameWithoutExtension(options.Document.FileName))
                    {
                        continue;
                    }
                    if (alreadyRenamed.Contains(part.CompilationUnit.FileName))
                    {
                        continue;
                    }
                    alreadyRenamed.Add(part.CompilationUnit.FileName);

                    string oldFileName = System.IO.Path.GetFileNameWithoutExtension(part.CompilationUnit.FileName);
                    string newFileName;

                    int idx = oldFileName.IndexOf(cls.Name);
                    if (idx >= 0)
                    {
                        newFileName = oldFileName.Substring(0, idx) + properties.NewName + oldFileName.Substring(idx + cls.Name.Length);
                    }
                    else
                    {
                        newFileName = currentPart != 1 ? properties.NewName + currentPart : properties.NewName;
                        currentPart++;
                    }

                    int t = 0;
                    while (System.IO.File.Exists(GetFullFileName(newFileName, part.CompilationUnit.FileName, t)))
                    {
                        t++;
                    }
                    result.Add(new RenameFileChange(part.CompilationUnit.FileName, GetFullFileName(newFileName, part.CompilationUnit.FileName, t)));
                }
            }

            foreach (MemberReference memberRef in col)
            {
                TextReplaceChange change = new TextReplaceChange();
                change.FileName     = memberRef.FileName;
                change.Offset       = memberRef.Position;
                change.RemovedChars = memberRef.Name.Length;
                change.InsertedText = properties.NewName;
                change.Description  = string.Format(GettextCatalog.GetString("Replace '{0}' with '{1}'"), memberRef.Name, properties.NewName);
                result.Add(change);
            }
            return(result);
        }
Ejemplo n.º 10
0
		public async Task<List<Change>> 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);
			var changes = new List<Change> ();
			var documents = new List<DocumentId> ();
			foreach (var projectChange in newSolution.GetChanges (ws.CurrentSolution).GetProjectChanges ()) {
				documents.AddRange (projectChange.GetChangedDocuments ());
			}
			FilterDuplicateLinkedDocs (newSolution, documents);
			foreach (var changedDoc in documents) {
				var newDoc = newSolution.GetDocument (changedDoc);
				foreach (var textChange in await newDoc.GetTextChangesAsync (ws.CurrentSolution.GetDocument (changedDoc))) {
					changes.Add (new TextReplaceChange () {
						FileName = newDoc.FilePath,
						Offset = textChange.Span.Start,
						RemovedChars = textChange.Span.Length,
						InsertedText = textChange.NewText
					});
				}
			}

			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)));
				}
			}
			return changes;
		}
		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);
		}