Example #1
0
        void RenameAllReferences(List <Reference> references)
        {
            string name    = references.First().Expression;
            string newName = GetNewName(name);

            if (ShouldRenameReference(newName, name))
            {
                FindReferencesAndRenameHelper.RenameReferences(references, newName);
            }
        }
        void ConvertToAutomaticProperty(ITextEditor editor, IProperty property, IField fieldDef, Ast.PropertyDeclaration astProp)
        {
            CodeGenerator codeGen = property.DeclaringType.ProjectContent.Language.CodeGenerator;

            int fieldStartOffset = editor.Document.PositionToOffset(fieldDef.Region.BeginLine, fieldDef.Region.BeginColumn);
            int fieldEndOffset   = editor.Document.PositionToOffset(fieldDef.Region.EndLine, fieldDef.Region.EndColumn);

            int startOffset = editor.Document.PositionToOffset(property.Region.BeginLine, property.Region.BeginColumn);
            int endOffset   = editor.Document.PositionToOffset(property.BodyRegion.EndLine, property.BodyRegion.EndColumn);

            ITextAnchor startAnchor = editor.Document.CreateAnchor(startOffset);
            ITextAnchor endAnchor   = editor.Document.CreateAnchor(endOffset);

            if (astProp.HasGetRegion)
            {
                astProp.GetRegion.Block = null;
            }

            if (!astProp.HasSetRegion)
            {
                astProp.SetRegion          = new Ast.PropertySetRegion(null, null);
                astProp.SetRegion.Modifier = CodeGenerator.ConvertModifier(fieldDef.Modifiers, new ClassFinder(fieldDef))
                                             & (Ast.Modifiers.Private | Ast.Modifiers.Internal | Ast.Modifiers.Protected | Ast.Modifiers.Public);
            }

            Ast.FieldDeclaration f = ParseMember <Ast.FieldDeclaration>(Path.GetExtension(editor.FileName),
                                                                        GetMemberText(fieldDef, editor));
            astProp.Initializer = f.Fields.First().Initializer;

            if (astProp.HasSetRegion)
            {
                astProp.SetRegion.Block = null;
            }
            using (AsynchronousWaitDialog monitor = AsynchronousWaitDialog.ShowWaitDialog("${res:SharpDevelop.Refactoring.ConvertToAutomaticProperty}")) {
                var refs = RefactoringService.FindReferences(fieldDef, monitor);
                using (editor.Document.OpenUndoGroup()) {
                    FindReferencesAndRenameHelper.RenameReferences(refs, property.Name);
                    editor.Document.Remove(fieldStartOffset, fieldEndOffset - fieldStartOffset);
                    editor.Document.Replace(startAnchor.Offset, endAnchor.Offset - startAnchor.Offset, codeGen.GenerateCode(astProp, ""));
                }
            }
        }
        /// <summary>
        /// Renames all references to a resource including the definition.
        /// </summary>
        /// <param name="rrr">The resource to be renamed.</param>
        /// <param name="newKey">The new name of the resource key.</param>
        /// <param name="monitor">An object implementing <see cref="IProgressMonitor"/> to report the progress of the operation. Can be <c>null</c>.</param>
        public static void Rename(ResourceResolveResult rrr, string newKey, IProgressMonitor monitor)
        {
            // Prevent duplicate key names
            if (rrr.ResourceFileContent.ContainsKey(newKey))
            {
                if (monitor != null)
                {
                    monitor.ShowingDialog = true;
                }
                MessageService.ShowWarning("${res:Hornung.ResourceToolkit.EditStringResourceDialog.DuplicateKey}");
                if (monitor != null)
                {
                    monitor.ShowingDialog = false;
                }
                return;
            }

            List <Reference> references = FindReferences(rrr.FileName, rrr.Key, monitor);

            if (references == null)
            {
                return;
            }

            if (monitor != null)
            {
                monitor.BeginTask(null, 0, false);
            }

            try {
                // rename definition (if present)
                if (rrr.ResourceFileContent.ContainsKey(rrr.Key))
                {
                    rrr.ResourceFileContent.RenameKey(rrr.Key, newKey);
                }
                else
                {
                    if (monitor != null)
                    {
                        monitor.ShowingDialog = true;
                    }
                    MessageService.ShowWarning("${res:Hornung.ResourceToolkit.RenameKeyDefinitionNotFoundWarning}");
                    if (monitor != null)
                    {
                        monitor.ShowingDialog = false;
                    }
                }
            } catch (Exception ex) {
                if (monitor != null)
                {
                    monitor.ShowingDialog = true;
                }
                MessageService.ShowWarningFormatted("${res:Hornung.ResourceToolkit.ErrorProcessingResourceFile}" + Environment.NewLine + ex.Message, rrr.ResourceFileContent.FileName);
                if (monitor != null)
                {
                    monitor.ShowingDialog = false;
                }
                if (monitor != null)
                {
                    monitor.Done();
                }
                // Do not rename the references when renaming the definition failed.
                return;
            }

            // rename references
            // FIXME: RenameReferences does not enforce escaping rules. May be a problem if someone uses double-quotes in the new resource key name.
            FindReferencesAndRenameHelper.RenameReferences(references, newKey);

            // rename definitions in localized resource files
            foreach (KeyValuePair <string, IResourceFileContent> entry in ResourceFileContentRegistry.GetLocalizedContents(rrr.FileName))
            {
                try {
                    if (entry.Value.ContainsKey(rrr.Key))
                    {
                        entry.Value.RenameKey(rrr.Key, newKey);
                    }
                } catch (Exception ex) {
                    if (monitor != null)
                    {
                        monitor.ShowingDialog = true;
                    }
                    MessageService.ShowWarningFormatted("${res:Hornung.ResourceToolkit.ErrorProcessingResourceFile}" + Environment.NewLine + ex.Message, entry.Value.FileName);
                    if (monitor != null)
                    {
                        monitor.ShowingDialog = false;
                    }
                }
            }

            if (monitor != null)
            {
                monitor.Done();
            }
        }