/// <summary> /// Called after key was changed - adds undo unit and performs refactoring of code /// </summary> public void KeyRenamed(ResXStringGridRow row, string newKey) { string oldKey = row.Status == KEY_STATUS.ERROR ? null : row.DataSourceItem.Name; GridRenameKeyUndoUnit unit = new GridRenameKeyUndoUnit(row, editorControl, oldKey, newKey); editorControl.Editor.AddUndoUnit(unit); if (VisualLocalizerPackage.Instance.DTE.Solution.ContainsProjectItem(editorControl.Editor.ProjectItem.InternalProjectItem)) { // obtain ResX project item ResXProjectItem resxItem = editorControl.Editor.ProjectItem; resxItem.ResolveNamespaceClass(resxItem.InternalProjectItem.ContainingProject.GetResXItemsAround(false, true)); if (row.ConflictItems.Count == 0 && resxItem != null && !resxItem.IsCultureSpecific() && !string.IsNullOrEmpty(newKey)) { int errors = 0; int count = row.CodeReferences.Count; // set new key row.CodeReferences.ForEach((item) => { item.KeyAfterRename = newKey.CreateIdentifier(resxItem.DesignerLanguage); }); // run the replacer try { editorControl.ReferenceCounterThreadSuspended = true; BatchReferenceReplacer replacer = new BatchReferenceReplacer(); replacer.Inline(row.CodeReferences, true, ref errors); } finally { editorControl.ReferenceCounterThreadSuspended = false; } VLOutputWindow.VisualLocalizerPane.WriteLine("Renamed {0} key references in code, {1} errors", count, errors); } } }
/// <summary> /// Generic test method /// </summary> /// <param name="files">List of files whose references should be renamed</param> public void InternalTest(string[] files) { // backup the files var backups = CreateBackupsOf(files); try { // get the result items List <CodeReferenceResultItem> list = BatchInlineLookup(files); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchInlineCommand), false); int originalCount = list.Count; Assert.IsTrue(list.Count > 0); // rename each item by adding "XX". These resources must be present in the ResX files. list.ForEach((item) => { item.Key = item.FullReferenceText.Substring(item.FullReferenceText.LastIndexOf('.') + 1); item.KeyAfterRename = item.Key + "XX"; }); // run the replacer int errors = 0; BatchReferenceReplacer replacer = new BatchReferenceReplacer(); replacer.Inline(list, true, ref errors); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchInlineCommand), false); Assert.AreEqual(0, errors); // run the inline command again - the number of result items should be equal to the one before renaming int newCount = BatchInlineLookup(files).Count((item) => { return(item.OriginalReferenceText.EndsWith("XX")); }); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchInlineCommand), false); Assert.AreEqual(originalCount, newCount); } finally { RestoreBackups(backups); } }
private void UpdateSourceReferences(string from, string to) { if (SourceRow.CodeReferenceContainsReadonly) { throw new Exception("This operation cannot be executed, because some of the references are located in readonly files."); } if (Control.Editor.ReadOnly) { throw new Exception("Cannot perform this operation - the document is readonly."); } try { // suspend the reference lookuper thread Control.ReferenceCounterThreadSuspended = true; Control.UpdateReferencesCount(SourceRow); ChangeColumnValue(from, to); // if the rows has no errors, perform pseudo-refactoring if (SourceRow.ConflictItems.Count == 0 && !string.IsNullOrEmpty(to)) { int errors = 0; int count = SourceRow.CodeReferences.Count; SourceRow.CodeReferences.ForEach((item) => { item.KeyAfterRename = to.CreateIdentifier(Control.Editor.ProjectItem.DesignerLanguage); }); try { Control.ReferenceCounterThreadSuspended = true; BatchReferenceReplacer replacer = new BatchReferenceReplacer(); replacer.Inline(SourceRow.CodeReferences, true, ref errors); } finally { Control.ReferenceCounterThreadSuspended = false; } VLOutputWindow.VisualLocalizerPane.WriteLine("Renamed {0} key references in code", count); } } catch (Exception ex) { VLOutputWindow.VisualLocalizerPane.WriteException(ex); MessageBox.ShowException(ex); } finally { Control.ReferenceCounterThreadSuspended = false; } }
private void UpdateSourceReferences(string from, string to) { if (Item.CodeReferenceContainsReadonly) { throw new Exception("This operation cannot be executed, because some of the references are located in readonly files."); } if (Control.Editor.ReadOnly) { throw new Exception("Cannot perform this operation - the document is readonly."); } try { // suspend reference lookuper thread and update references for the item Control.ReferenceCounterThreadSuspended = true; Control.UpdateReferencesCount(Item); // change the keys Item.Text = to; Item.BeforeEditKey = from; Item.AfterEditKey = to; ListView.Validate(Item); ListView.NotifyDataChanged(); if (Item.ErrorMessages.Count > 0) { Item.Status = KEY_STATUS.ERROR; } else { Item.Status = KEY_STATUS.OK; Item.DataNode.Name = Item.Key; Item.LastValidKey = Item.Key; } VLOutputWindow.VisualLocalizerPane.WriteLine("Renamed from \"{0}\" to \"{1}\"", Item.BeforeEditKey, Item.AfterEditKey); if (Item.AbstractListView != null) { Item.AbstractListView.SetContainingTabPageSelected(); } // if item has no errors, perform pseudo-refactoring if (Item.ConflictItems.Count == 0 && !string.IsNullOrEmpty(to)) { int errors = 0; int count = Item.CodeReferences.Count; Item.CodeReferences.ForEach((i) => { i.KeyAfterRename = to.CreateIdentifier(Control.Editor.ProjectItem.DesignerLanguage); }); try { Control.ReferenceCounterThreadSuspended = true; BatchReferenceReplacer replacer = new BatchReferenceReplacer(); replacer.Inline(Item.CodeReferences, true, ref errors); } finally { Control.ReferenceCounterThreadSuspended = false; } VLOutputWindow.VisualLocalizerPane.WriteLine("Renamed {0} key references in code", count); } } catch (Exception ex) { VLOutputWindow.VisualLocalizerPane.WriteException(ex); VisualLocalizer.Library.Components.MessageBox.ShowException(ex); } finally { Control.ReferenceCounterThreadSuspended = false; } }