private bool ValidateTargets() { var target = findAllReferencesCommand.CurrentTarget; bool isEnum = target.Type.IsEnum(); bool isClass = false; if (!isEnum) { bool isVoid = target.Type.IsVoid(); isClass = !isVoid && target.IsStatic && (target.Member == null || RefactoringHelper.CheckFlag(target.Member.Flags, FlagType.Constructor)); } bool isGlobalFunction = false; bool isGlobalNamespace = false; if (!isEnum && !isClass && (target.InClass == null || target.InClass.IsVoid())) { isGlobalFunction = RefactoringHelper.CheckFlag(target.Member.Flags, FlagType.Function); isGlobalNamespace = RefactoringHelper.CheckFlag(target.Member.Flags, FlagType.Namespace); } // Types with not their own file if (!isEnum && !isClass && !isGlobalFunction && !isGlobalNamespace) { return(true); } var member = isEnum || isClass ? target.Type : target.Member; var inFile = member.InFile; // Is this possible? should return false? I'm inclined to think so if (inFile == null) { return(true); } oldFileName = inFile.FileName; string oldName = Path.GetFileNameWithoutExtension(oldFileName); // Private classes and similars if (string.IsNullOrEmpty(oldName) || !oldName.Equals(member.Name)) { return(true); } string fullPath = Path.GetFullPath(inFile.FileName); fullPath = Path.GetDirectoryName(fullPath); newFileName = Path.Combine(fullPath, NewName + Path.GetExtension(oldFileName)); // No point in refactoring if the old and new name is the same if (string.IsNullOrEmpty(oldFileName) || oldFileName.Equals(newFileName)) { return(false); } // Check if the new file name already exists return(FileHelper.ConfirmOverwrite(newFileName)); }
/// <summary> /// Updates the state of the menu items /// </summary> private void UpdateMenuItems() { try { this.refactorContextMenu.DelegateMenuItem.Enabled = false; this.refactorMainMenu.DelegateMenuItem.Enabled = false; Boolean isValid = this.GetLanguageIsValid(); ASResult result = isValid ? GetResultFromCurrentPosition() : null; if (result != null && result.Member != null) { Boolean isVoid = result.Type.IsVoid(); Boolean isClass = RefactoringHelper.CheckFlag(result.Member.Flags, FlagType.Class); Boolean isVariable = RefactoringHelper.CheckFlag(result.Member.Flags, FlagType.Variable); Boolean isConstructor = RefactoringHelper.CheckFlag(result.Member.Flags, FlagType.Constructor); this.refactorContextMenu.RenameMenuItem.Enabled = !(isClass || isConstructor || isVoid); this.refactorMainMenu.RenameMenuItem.Enabled = !(isClass || isConstructor || isVoid); this.editorReferencesItem.Enabled = this.viewReferencesItem.Enabled = true; if (result.Type != null && result.inClass != null && result.inFile != null) { FlagType flags = result.Member.Flags; if ((flags & FlagType.Variable) > 0 && (flags & FlagType.LocalVar) == 0 && (flags & FlagType.ParameterVar) == 0) { this.refactorContextMenu.DelegateMenuItem.Enabled = true; this.refactorMainMenu.DelegateMenuItem.Enabled = true; } } } else { this.refactorMainMenu.RenameMenuItem.Enabled = false; this.refactorContextMenu.RenameMenuItem.Enabled = false; this.editorReferencesItem.Enabled = false; this.viewReferencesItem.Enabled = false; } IASContext context = ASContext.Context; if (context != null && context.CurrentModel != null) { Boolean truncate = (this.GetLanguageIsValid() && context.CurrentModel.Imports.Count > 0); Boolean organize = (this.GetLanguageIsValid() && context.CurrentModel.Imports.Count > 1); this.refactorContextMenu.OrganizeMenuItem.Enabled = organize; this.refactorContextMenu.TruncateMenuItem.Enabled = truncate && !this.LanguageIsHaxe(); this.refactorMainMenu.OrganizeMenuItem.Enabled = organize; this.refactorMainMenu.TruncateMenuItem.Enabled = truncate && !this.LanguageIsHaxe(); } this.surroundContextMenu.Enabled = false; this.refactorMainMenu.SurroundMenu.Enabled = false; this.refactorContextMenu.ExtractMethodMenuItem.Enabled = false; this.refactorContextMenu.ExtractLocalVariableMenuItem.Enabled = false; this.refactorMainMenu.ExtractMethodMenuItem.Enabled = false; this.refactorMainMenu.ExtractLocalVariableMenuItem.Enabled = false; ITabbedDocument document = PluginBase.MainForm.CurrentDocument; if (document != null && document.SciControl != null && ASContext.Context.IsFileValid && document.SciControl.SelTextSize > 1) { Int32 selEnd = document.SciControl.SelectionEnd; Int32 selStart = document.SciControl.SelectionStart; if (!document.SciControl.PositionIsOnComment(selEnd) || !document.SciControl.PositionIsOnComment(selStart)) { this.surroundContextMenu.Enabled = true; this.refactorMainMenu.SurroundMenu.Enabled = true; this.refactorContextMenu.ExtractMethodMenuItem.Enabled = true; this.refactorMainMenu.ExtractMethodMenuItem.Enabled = true; this.refactorContextMenu.ExtractLocalVariableMenuItem.Enabled = true; this.refactorMainMenu.ExtractLocalVariableMenuItem.Enabled = true; // Generate context menu items foreach (ToolStripMenuItem item in this.surroundContextMenu.DropDownItems) { item.Click -= this.SurroundWithClicked; } this.surroundContextMenu.GenerateSnippets(document.SciControl); foreach (ToolStripMenuItem item in this.surroundContextMenu.DropDownItems) { item.Click += this.SurroundWithClicked; } // Generate main menu items foreach (ToolStripMenuItem item in this.refactorMainMenu.SurroundMenu.DropDownItems) { item.Click -= this.SurroundWithClicked; } this.refactorMainMenu.SurroundMenu.GenerateSnippets(document.SciControl); foreach (ToolStripMenuItem item in this.refactorMainMenu.SurroundMenu.DropDownItems) { item.Click += this.SurroundWithClicked; } } } } catch {} }
/// <summary> /// Updates the state of the menu items /// </summary> private void UpdateMenuItems() { try { ResolvedContext resolved = ASComplete.CurrentResolvedContext; Boolean isValid = this.GetLanguageIsValid() && resolved != null && resolved.Position >= 0; this.refactorMainMenu.DelegateMenuItem.Enabled = false; this.refactorContextMenu.DelegateMenuItem.Enabled = false; ASResult result = isValid ? resolved.Result : null; if (result != null && !result.IsNull()) { Boolean isVoid = result.Type.IsVoid(); Boolean isClass = !isVoid && result.IsStatic && result.Member == null; Boolean isVariable = !isVoid && !isClass && RefactoringHelper.CheckFlag(result.Member.Flags, FlagType.Variable); Boolean isConstructor = !isVoid && !isClass && RefactoringHelper.CheckFlag(result.Member.Flags, FlagType.Constructor); this.refactorContextMenu.RenameMenuItem.Enabled = !(isClass || isConstructor || isVoid); this.refactorMainMenu.RenameMenuItem.Enabled = !(isClass || isConstructor || isVoid); this.editorReferencesItem.Enabled = this.viewReferencesItem.Enabled = true; if (result.Member != null && result.Type != null && result.InClass != null && result.InFile != null) { FlagType flags = result.Member.Flags; if ((flags & FlagType.Variable) > 0 && (flags & FlagType.LocalVar) == 0 && (flags & FlagType.ParameterVar) == 0) { this.refactorContextMenu.DelegateMenuItem.Enabled = true; this.refactorMainMenu.DelegateMenuItem.Enabled = true; } } } else { this.refactorMainMenu.RenameMenuItem.Enabled = false; this.refactorContextMenu.RenameMenuItem.Enabled = false; this.editorReferencesItem.Enabled = false; this.viewReferencesItem.Enabled = false; } IASContext context = ASContext.Context; if (context != null && context.CurrentModel != null) { Boolean truncate = (this.GetLanguageIsValid() && context.CurrentModel.Imports.Count > 0); Boolean organize = (this.GetLanguageIsValid() && context.CurrentModel.Imports.Count > 1); this.refactorContextMenu.OrganizeMenuItem.Enabled = organize; this.refactorContextMenu.TruncateMenuItem.Enabled = truncate && !this.LanguageIsHaxe(); this.refactorMainMenu.OrganizeMenuItem.Enabled = organize; this.refactorMainMenu.TruncateMenuItem.Enabled = truncate && !this.LanguageIsHaxe(); } this.surroundContextMenu.Enabled = false; this.refactorMainMenu.SurroundMenu.Enabled = false; this.refactorContextMenu.ExtractMethodMenuItem.Enabled = false; this.refactorContextMenu.ExtractLocalVariableMenuItem.Enabled = false; this.refactorMainMenu.ExtractMethodMenuItem.Enabled = false; this.refactorMainMenu.ExtractLocalVariableMenuItem.Enabled = false; ITabbedDocument document = PluginBase.MainForm.CurrentDocument; if (document != null && document.IsEditable && this.GetLanguageIsValid() && document.SciControl.SelTextSize > 1) { Int32 selEnd = document.SciControl.SelectionEnd; Int32 selStart = document.SciControl.SelectionStart; if (!document.SciControl.PositionIsOnComment(selEnd) || !document.SciControl.PositionIsOnComment(selStart)) { this.surroundContextMenu.Enabled = true; this.refactorMainMenu.SurroundMenu.Enabled = true; this.refactorContextMenu.ExtractMethodMenuItem.Enabled = true; this.refactorMainMenu.ExtractMethodMenuItem.Enabled = true; this.refactorContextMenu.ExtractLocalVariableMenuItem.Enabled = true; this.refactorMainMenu.ExtractLocalVariableMenuItem.Enabled = true; } } this.refactorContextMenu.CodeGeneratorMenuItem.Enabled = isValid; this.refactorMainMenu.CodeGeneratorMenuItem.Enabled = isValid; } catch {} }
private void RenameFile() { ASResult target = findAllReferencesCommand.CurrentTarget; Boolean isEnum = target.Type.IsEnum(); Boolean isClass = false; Boolean isConstructor = false; if (!isEnum) { Boolean isVoid = target.Type.IsVoid(); isClass = !isVoid && target.IsStatic && target.Member == null; isConstructor = !isVoid && !isClass && RefactoringHelper.CheckFlag(target.Member.Flags, FlagType.Constructor); } Boolean isGlobalFunction = false; Boolean isGlobalNamespace = false; if (!isEnum && !isClass && !isConstructor && (target.InClass == null || target.InClass.IsVoid())) { isGlobalFunction = RefactoringHelper.CheckFlag(target.Member.Flags, FlagType.Function); isGlobalNamespace = RefactoringHelper.CheckFlag(target.Member.Flags, FlagType.Namespace); } if (!isEnum && !isClass && !isConstructor && !isGlobalFunction && !isGlobalNamespace) { return; } FileModel inFile = null; String originName = null; if (isEnum || isClass) { inFile = target.Type.InFile; originName = target.Type.Name; } else { inFile = target.Member.InFile; originName = target.Member.Name; } if (inFile == null) { return; } String oldFileName = inFile.FileName; String oldName = Path.GetFileNameWithoutExtension(oldFileName); if (!string.IsNullOrEmpty(oldName) && !oldName.Equals(originName)) { return; } String fullPath = Path.GetFullPath(inFile.FileName); fullPath = Path.GetDirectoryName(fullPath); String newFileName = Path.Combine(fullPath, NewName + Path.GetExtension(oldFileName)); if (string.IsNullOrEmpty(oldFileName) || oldFileName.Equals(newFileName)) { return; } foreach (ITabbedDocument doc in PluginBase.MainForm.Documents) { if (doc.FileName.Equals(oldFileName)) { doc.Save(); doc.Close(); } } File.Move(oldFileName, newFileName); PluginCore.Managers.DocumentManager.MoveDocuments(oldFileName, newFileName); PluginBase.MainForm.OpenEditableDocument(newFileName, false); }
/// <summary> /// A new Rename refactoring command. /// </summary> /// <param name="target">The target declaration to find references to.</param> /// <param name="outputResults">If true, will send the found results to the trace log and results panel</param> /// <param name="newName">If provided, will not query the user for a new name.</param> /// <param name="ignoreDeclarationSource">If true, will not rename the original declaration source. Useful for Encapsulation refactoring.</param> public Rename(ASResult target, Boolean outputResults, String newName, Boolean ignoreDeclarationSource) { if (target == null) { TraceManager.Add("refactor target is null"); return; } this.outputResults = outputResults; if (target.IsPackage) { string package = target.Path.Replace('.', Path.DirectorySeparatorChar); foreach (PathModel aPath in ASContext.Context.Classpath) { if (aPath.IsValid && !aPath.Updating) { string path = Path.Combine(aPath.Path, package); if (aPath.IsValid && Directory.Exists(path)) { this.newName = string.IsNullOrEmpty(newName) ? GetNewName(Path.GetFileName(path)) : newName; if (string.IsNullOrEmpty(this.newName)) { return; } renamePackage = new Move(new Dictionary <string, string> { { path, this.newName } }, true, true); return; } } } return; } Boolean isEnum = target.Type.IsEnum(); Boolean isVoid = target.Type.IsVoid(); Boolean isClass = !isVoid && target.IsStatic && (target.Member == null || RefactoringHelper.CheckFlag(target.Member.Flags, FlagType.Constructor)); if (!string.IsNullOrEmpty(newName)) { this.newName = newName; } else if (isEnum || isClass) { this.newName = GetNewName(target.Type.Name); } else { this.newName = GetNewName(target.Member.Name); } if (string.IsNullOrEmpty(this.newName)) { return; } // create a FindAllReferences refactor to get all the changes we need to make // we'll also let it output the results, at least until we implement a way of outputting the renamed results later this.findAllReferencesCommand = new FindAllReferences(target, false, ignoreDeclarationSource) { OnlySourceFiles = true }; // register a completion listener to the FindAllReferences so we can rename the entries this.findAllReferencesCommand.OnRefactorComplete += OnFindAllReferencesCompleted; }