/// <summary> /// Handles "Batch move on selection" command from code context menu. /// </summary> private void BatchMoveSelectionCodeClick(object sender, EventArgs args) { try { if (OperationInProgress) { throw new Exception("Cannot start operation 'Batch move to resources', because another operation is in progress."); } OperationInProgress = true; batchMoveCommand.ProcessSelection(true); BatchMoveToResourcesToolWindow win = ShowToolWindow <BatchMoveToResourcesToolWindow>(); if (win != null) { win.SetData(batchMoveCommand.Results); } else { throw new Exception("Unable to display tool window."); } batchMoveCommand.Results.Clear(); } catch (Exception ex) { if (OperationInProgress) { VLDocumentViewsManager.ReleaseLocks(); } VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchMoveCommand), false); VLOutputWindow.VisualLocalizerPane.WriteException(ex); MessageBox.ShowException(ex); OperationInProgress = false; } }
/// <summary> /// Handles "Batch inline" command from Solution Explorer's context menu. /// </summary> private void BatchInlineSolExpClick(object sender, EventArgs args) { try { if (OperationInProgress) { throw new Exception("Cannot start operation 'Batch inline', because another operation is in progress."); } OperationInProgress = true; batchInlineCommand.Process((Array)VisualLocalizerPackage.Instance.UIHierarchy.SelectedItems, true); BatchInlineToolWindow win = ShowToolWindow <BatchInlineToolWindow>(); if (win != null) { win.SetData(batchInlineCommand.Results); } else { throw new Exception("Unable to display tool window."); } batchInlineCommand.Results.Clear(); } catch (Exception ex) { if (OperationInProgress) { VLDocumentViewsManager.ReleaseLocks(); } VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchInlineCommand), false); VLOutputWindow.VisualLocalizerPane.WriteException(ex); MessageBox.ShowException(ex); OperationInProgress = false; } }
/// <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); } }
/// <summary> /// When window is closed /// </summary> protected override void OnWindowHidden(object sender, EventArgs e) { if (panel.SetDataFinished) { VLDocumentViewsManager.ReleaseLocks(); // unlock all previously locked documents MenuManager.OperationInProgress = false; // enable other operations to run VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchInlineCommand), false); } panel.Clear(); }
/// <summary> /// When the toolwindow is closed /// </summary> protected override void OnWindowHidden(object sender, EventArgs e) { try { if (panel.ToolGrid.SetDataFinished) { VLDocumentViewsManager.ReleaseLocks(); // unlocks all locked files panel.ToolGrid.UnloadResXItems(); // release all ResX files loaded in the grid panel.ToolGrid.ResetConflictResolver(); MenuManager.OperationInProgress = false; // permits other operations VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchMoveCommand), false); } panel.ToolGrid.Clear(); } catch (Exception ex) { VLOutputWindow.VisualLocalizerPane.WriteException(ex); VisualLocalizer.Library.Components.MessageBox.ShowException(ex); } }
/// <summary> /// Generic test method /// </summary> /// <param name="openFiles">True if the files should be opened first</param> /// <param name="fullName">True if the "Use full names" policy should be applied</param> /// <param name="mark">True if the "Mark with VL_NO_LOC policy should be applied"</param> /// <param name="testFiles">Files to test</param> /// <param name="targetFiles">ResX files where resources can be moved</param> private void InternalFileMoveTest(bool openFiles, bool fullName, bool mark, string[] testFiles, string[] targetFiles) { // backup the files Dictionary <string, string> backups = CreateBackupsOf(testFiles); // run the "batch move" command to get result items List <CodeStringResultItem> items = BatchMoveLookup(testFiles); // open the necessary files SetFilesOpened(testFiles, openFiles); // initialize ResX destination files Dictionary <string, ResXProjectItem> resxItems = InitResxItems(targetFiles, Agent.GetDTE().Solution.FindProjectItem(testFiles[0]).ContainingProject); // initialize the "batch move" tool window and grid Dictionary <ResXProjectItem, int> resxCounts; Dictionary <ProjectItem, int> sourceItemCounts; int expectedToBeMarked; BatchMoveToResourcesToolWindow_Accessor window = InitBatchToolWindow(resxItems.Values.ToList(), items, fullName, mark, out resxCounts, out sourceItemCounts, out expectedToBeMarked); try { // execute window.RunClick(null, null); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchMoveCommand), false); // verify every ResX file contains the correct number of resources foreach (ResXProjectItem target in resxItems.Values) { Assert.AreEqual(resxCounts.ContainsKey(target) ? resxCounts[target] : 0, GetResourcesCountIn(target)); } // run the "inline" command to verify all references are valid int checkedCount = resxCounts.Sum((pair) => { return(pair.Value); }); List <CodeReferenceResultItem> inlineResults = BatchInlineLookup(testFiles); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchInlineCommand), false); Assert.AreEqual(checkedCount, inlineResults.Count); // test if every unchecked string result item was marked if (mark) { int markedAfter = BatchMoveLookup(testFiles).Count((item) => { return(item.IsMarkedWithUnlocalizableComment); }); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchMoveCommand), false); Assert.AreEqual(expectedToBeMarked, markedAfter, "mark"); } // test if qualified name was really used if (fullName) { foreach (var result in inlineResults) { AspNetCodeReferenceResultItem citem = result as AspNetCodeReferenceResultItem; if (citem == null || !citem.ComesFromWebSiteResourceReference) { Assert.AreEqual(result.OriginalReferenceText, result.FullReferenceText); } } } // check if no import statement has been added twice foreach (string path in testFiles) { CheckForDuplicateUsings(path); } if (openFiles) { // use undo manager to revert the changes foreach (string file in testFiles) { var win = VsShellUtilities.GetWindowObject(VLDocumentViewsManager.GetWindowFrameForFile(file, false)); ProjectItem pitem = Agent.GetDTE().Solution.FindProjectItem(file); int count = sourceItemCounts.ContainsKey(pitem) ? sourceItemCounts[pitem] : 0; IOleUndoManager manager; VLDocumentViewsManager.GetTextLinesForFile(file, false).GetUndoManager(out manager); List <IOleUndoUnit> list = manager.RemoveTopFromUndoStack(count); foreach (AbstractUndoUnit unit in list) { unit.Undo(); } Assert.AreEqual(File.ReadAllText(backups[file]), File.ReadAllText(file)); } // check that all changes were fully reverted int sum = resxItems.Values.Sum((item) => { return(GetResourcesCountIn(item)); }); Assert.AreEqual(0, sum); } } finally { // close the files SetFilesOpened(testFiles, false); // restore the backups RestoreBackups(backups); // clear ResX files foreach (ResXProjectItem target in resxItems.Values) { ClearFile(target); } VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchMoveCommand), false); } }
/// <summary> /// Generic testing method /// </summary> /// <param name="fileOpened">True if files should be opened</param> /// <param name="referenceFiles">Files to test</param> /// <param name="correction">Number of string literals reported by the "batch move" command in the files</param> private void InternalFileTest(bool fileOpened, string[] referenceFiles, int correction) { // backup the files Dictionary <string, string> backups = CreateBackupsOf(referenceFiles); // open/close the files SetFilesOpened(referenceFiles, fileOpened); // run inline command in order to obtain result items List <CodeReferenceResultItem> inlineList = BatchInlineLookup(referenceFiles); // init the tool window and the grid int checkedCount; Dictionary <ProjectItem, int> sourceItemsCounts; BatchInlineToolWindow_Accessor window = InitBatchWindow(inlineList, out sourceItemsCounts, out checkedCount); try { // run the command window.RunClick(null, null); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchInlineCommand), false); // run "batch move" command List <CodeStringResultItem> moveList = BatchMoveLookup(referenceFiles); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchMoveCommand), false); // the number of string literals found by the "batch move" command, minus the string literals that were // already there should be equal to the number of inlined result items Assert.AreEqual(checkedCount, moveList.Count - correction); // check correct value was inlined int i = 0, j = 0; for (; i < checkedCount;) { while (!inlineList[i].MoveThisItem) { i++; } while (moveList[j].Value != inlineList[i].Value) { j++; } Assert.AreEqual(inlineList[i].Value, moveList[j].Value); i++; j++; } // use the undo manager to revert the changes if (fileOpened) { foreach (string file in referenceFiles) { IOleUndoManager undoManager; VLDocumentViewsManager.GetTextLinesForFile(file, false).GetUndoManager(out undoManager); foreach (AbstractUndoUnit unit in undoManager.RemoveTopFromUndoStack(sourceItemsCounts[Agent.GetDTE().Solution.FindProjectItem(file)])) { unit.Undo(); } Assert.AreEqual(File.ReadAllText(backups[file]), File.ReadAllText(file)); } } } finally { // close the files SetFilesOpened(referenceFiles, false); VLDocumentViewsManager.CloseInvisibleWindows(typeof(BatchInlineCommand), false); // restore backups RestoreBackups(backups); } }