public void Refresh(bool reloadShortcut = true) { // If the shortcuts have been modified, ask to save them if (this.shortcutsDirty && reloadShortcut) { VSConstants.MessageBoxResult result = this.ShowMessageBox(Resources.ShortcutsChanged, OLEMSGBUTTON.OLEMSGBUTTON_YESNOCANCEL); switch (result) { case VSConstants.MessageBoxResult.IDCANCEL: return; case VSConstants.MessageBoxResult.IDYES: this.SaveShortcuts(); break; } } // Recreate file system to ensure that the required files exist CreateFileSystem(); MacroFSNode.RefreshTree(); this.LoadShortcuts(); }
public void StopRecording() { string current = Manager.CurrentMacroPath; bool currentWasOpen = false; // Close current macro if open try { this.dte.Documents.Item(Manager.CurrentMacroPath).Close(EnvDTE.vsSaveChanges.vsSaveChangesNo); currentWasOpen = true; } catch (Exception e) { if (ErrorHandler.IsCriticalException(e)) { throw; } } this.recorder.StopRecording(current); this.IsRecording = false; MacroFSNode.SelectNode(CurrentMacroPath); // Reopen current macro if (currentWasOpen) { VsShellUtilities.OpenDocument(VSMacrosPackage.Current, Manager.CurrentMacroPath); this.PreviousWindow.Show(); } }
public void NewMacro() { MacroFSNode macro = this.SelectedMacro; macro.IsExpanded = true; string basePath = Path.Combine(macro.FullPath, "New Macro"); string extension = ".js"; string path = basePath + extension; // Increase count until filename is available (e.g. 'New Macro (2).js') int count = 2; while (File.Exists(path)) { path = basePath + " (" + count++ + ")" + extension; } // Create the file File.WriteAllText(path, "/// <reference path=\"" + Manager.IntellisensePath + "\" />"); // Refresh the tree MacroFSNode.RefreshTree(); // Select new node MacroFSNode node = MacroFSNode.SelectNode(path); node.IsExpanded = true; node.IsEditable = true; }
protected override void OnStartSearch() { // Enable search on the MacroFSNodes so that only the matched nodes will be shown MacroFSNode.EnableSearch(); // Get the search option. bool matchCase = this.toolWindow.MatchCaseOption.Value; bool withinFileContents = this.toolWindow.WithinFileOption.Value; try { string searchString = this.SearchQuery.SearchString; StringComparison comp = matchCase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; this.TraverseAndMark(MacroFSNode.RootNode, searchString, comp, withinFileContents); } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); this.ErrorCode = VSConstants.E_FAIL; } // Call the implementation of this method in the base class. // This sets the task status to complete and reports task completion. base.OnStartSearch(); }
public MacrosToolWindow() : base(null) { this.owningPackage = VSMacrosPackage.Current; this.Caption = Resources.ToolWindowTitle; this.BitmapResourceID = 301; this.BitmapIndex = 1; // Instantiate Tool Window Toolbar this.ToolBar = new CommandID(GuidList.GuidVSMacrosCmdSet, PkgCmdIDList.MacrosToolWindowToolbar); Manager.CreateFileSystem(); string macroDirectory = Manager.MacrosPath; // Create tree view root MacroFSNode root = new MacroFSNode(macroDirectory); // Make sure it is opened and selected by default root.IsExpanded = true; // Initialize Macros Control var macroControl = new MacrosControl(root); macroControl.Loaded += this.OnLoaded; this.Content = macroControl; Manager.Instance.LoadFolderExpansion(); }
public void CreateFileSystemNodes() { // Create a file MacroFSNode string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Test.js"); if (!File.Exists(path)) { File.Create(path).Close(); } this.fileNode = new MacroFSNode(path); // Create a directory MacroFSNode path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Macros"); Directory.CreateDirectory(path); this.directoryNode = new MacroFSNode(path); // Add 5 files to the folder for (int i = 0; i < 5; i++) { path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Macros", i + ".js"); File.Create(path).Close(); } }
private void TraverseAndMark(MacroFSNode root, string searchString, StringComparison comp, bool withinFileContents) { if (this.Contains(root.Name, searchString, comp)) { root.IsMatch = true; } else if (withinFileContents && !root.IsDirectory) { System.Threading.Tasks.Task.Run(() => { string allText = File.ReadAllText(root.FullPath); if (this.Contains(allText, searchString, comp)) { root.IsMatch = true; } }); } else { root.IsMatch = false; } if (root.Children != null) { foreach (var child in root.Children) { this.TraverseAndMark(child, searchString, comp, withinFileContents); } } }
public void Edit() { // TODO detect when a macro is dragged and it's opened -> use the overload to get the itemID MacroFSNode macro = this.SelectedMacro; string path = macro.FullPath; VsShellUtilities.OpenDocument(VSMacrosPackage.Current, path); }
public void Rename() { MacroFSNode macro = this.SelectedMacro; if (macro.FullPath != Manager.CurrentMacroPath) { macro.EnableEdit(); } }
private void TreeViewItem_MouseDoubleClick(object sender, RoutedEventArgs e) { MacroFSNode node = ((TreeViewItem)sender).Header as MacroFSNode; if (!node.IsDirectory) { Action playback = () => Manager.Instance.Playback(node.FullPath); this.Dispatcher.BeginInvoke(playback); } }
private void MacroTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e) { // Disable edit for previously selected node MacroFSNode oldNode = e.OldValue as MacroFSNode; if (oldNode != null) { oldNode.DisableEdit(); } }
public MacrosControl(MacroFSNode rootNode) { Current = this; MacroFSNode.RootNode = rootNode; // Let the UI bind to the view-model this.DataContext = new MacroFSNode[] { rootNode }; this.InitializeComponent(); }
/// <summary> /// Notifies clients of changes made to a directory. /// </summary> /// <param name="dir"> /// Name of the directory that had a change. /// </param> public int DirectoryChanged(string dir) { MacroFSNode node = MacroFSNode.FindNodeFromFullPath(dir); if (node != null) { // Refresh tree rooted at the changed directory MacroFSNode.RefreshTree(node); } return(VSConstants.S_OK); }
public void Delete() { MacroFSNode macro = this.SelectedMacro; // Don't delete if macro is being edited if (macro.IsEditable) { return; } string path = macro.FullPath; FileSystemInfo file; string fileName = Path.GetFileNameWithoutExtension(path); string message; if (macro.IsDirectory) { file = new DirectoryInfo(path); message = string.Format(VSMacros.Resources.DeleteFolder, fileName); } else { file = new FileInfo(path); message = string.Format(VSMacros.Resources.DeleteMacro, fileName); } if (file.Exists) { VSConstants.MessageBoxResult result; result = this.ShowMessageBox(message, OLEMSGBUTTON.OLEMSGBUTTON_OKCANCEL); if (result == VSConstants.MessageBoxResult.IDOK) { try { // Delete file or directory from disk Manager.DeleteFileOrFolder(path); // Delete file from collection macro.Delete(); } catch (Exception e) { this.ShowMessageBox(e.Message); } } } else { macro.Delete(); } }
private bool InSamples(MacroFSNode node) { do { if (node.FullPath == Manager.SamplesFolderPath) { return(true); } node = node.Parent; } while (node != MacroFSNode.RootNode); return(false); }
private string GetExecutingMacroNameForPossibleErrorDisplay(MacroFSNode node, string path) { if (node != null) { return(node.Name); } if (path == null) { throw new ArgumentNullException("path"); } int lastBackslash = path.LastIndexOf('\\'); string fileName = Path.GetFileNameWithoutExtension(path.Substring(lastBackslash != -1 ? lastBackslash + 1 : 0)); return(fileName); }
public void SaveCurrent() { SaveCurrentDialog dlg = new SaveCurrentDialog(); dlg.ShowDialog(); if (dlg.DialogResult == true) { try { string pathToNew = Path.Combine(Manager.MacrosPath, dlg.MacroName.Text + ".js"); string pathToCurrent = Manager.CurrentMacroPath; int newShortcutNumber = dlg.SelectedShortcutNumber; // Move Current to new file and create a new Current File.Move(pathToCurrent, pathToNew); CreateCurrentMacro(); MacroFSNode macro = new MacroFSNode(pathToNew, MacroFSNode.RootNode); if (newShortcutNumber != MacroFSNode.None) { // Update dictionary Manager.Shortcuts[newShortcutNumber] = macro.FullPath; } this.SaveShortcuts(true); this.Refresh(); // Select new node MacroFSNode.SelectNode(pathToNew); } catch (Exception e) { if (ErrorHandler.IsCriticalException(e)) { throw; } this.ShowMessageBox(e.Message); } } }
/// <summary> /// Notifies clients of changes made to one or more files. /// </summary> /// <param name="numberOfFilesChanged"> /// Number of files changed. /// </param> /// <param name="files"> /// Array of file names. /// </param> /// <param name="typesOfChange"> /// Array of flags indicating the type of changes. <see cref="_VSFILECHANGEFLAGS" />. public int FilesChanged(uint numberOfFilesChanged, string[] files, uint[] changeTypes) { // Go over each file and treat the change appropriately for (int i = 0; i < files.Length; i++) { string path = files[i]; _VSFILECHANGEFLAGS change = (_VSFILECHANGEFLAGS)changeTypes[i]; // _VSFILECHANGEFLAGS.VSFILECHG_Add is handled by DirectoryChanged // Only handle _VSFILECHANGEFLAGS.VSFILECHG_Del here if (change == _VSFILECHANGEFLAGS.VSFILECHG_Del) { MacroFSNode node = MacroFSNode.FindNodeFromFullPath(path); if (node != null) { node.Delete(); } } } return(VSConstants.S_OK); }
public void AssignShortcut() { AssignShortcutDialog dlg = new AssignShortcutDialog(); dlg.ShowDialog(); if (dlg.DialogResult == true) { MacroFSNode macro = this.SelectedMacro; // Remove old shortcut if it exists if (macro.Shortcut != MacroFSNode.None) { Manager.Shortcuts[macro.Shortcut] = string.Empty; } int newShortcutNumber = dlg.SelectedShortcutNumber; // At this point, the shortcut has been removed // Assign a new one only if the user selected a key binding if (newShortcutNumber != MacroFSNode.None) { // Get the node that previously owned that shortcut MacroFSNode previousNode = MacroFSNode.FindNodeFromFullPath(Manager.Shortcuts[newShortcutNumber]); // Update dictionary Manager.Shortcuts[newShortcutNumber] = macro.FullPath; // Update the UI binding for the old node previousNode.Shortcut = MacroFSNode.ToFetch; } // Update UI with new macro's shortcut macro.Shortcut = MacroFSNode.ToFetch; // Mark the shortcuts in memory as dirty this.shortcutsDirty = true; } }
private void MacroTreeView_DragOver(object sender, DragEventArgs e) { Point currentPosition = e.GetPosition(this.MacroTreeView); if ((Math.Abs(currentPosition.X - this.startPos.X) > SystemParameters.MinimumHorizontalDragDistance) || (Math.Abs(currentPosition.Y - this.startPos.Y) > SystemParameters.MinimumVerticalDragDistance) && this.isDragging && e.Data.GetDataPresent(typeof(MacroFSNode)) || e.Data.GetDataPresent(DataFormats.FileDrop)) { // Verify that this is a valid drop TreeViewItem item = this.GetNearestContainer(e.OriginalSource as UIElement); MacroFSNode node = item.Header as MacroFSNode; if (node != null && !this.InSamples(node)) { e.Effects = DragDropEffects.Move; } else { e.Effects = DragDropEffects.None; } } e.Handled = true; }
public void NewFolder() { MacroFSNode macro = this.SelectedMacro; macro.IsExpanded = true; string basePath = Path.Combine(macro.FullPath, "New Folder"); string path = basePath; int count = 2; while (Directory.Exists(path)) { path = basePath + " (" + count++ + ")"; } Directory.CreateDirectory(path); MacroFSNode.RefreshTree(); MacroFSNode node = MacroFSNode.SelectNode(path); node.IsEditable = true; }
private void TreeViewItem_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { // Has the mouse moved enough? var mousePos = e.GetPosition(this.MacroTreeView); var diff = mousePos - this.startPos; if (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance && this.isDragging) { // Get the dragged MacroFSNode MacroFSNode draggedNode = this.MacroTreeView.SelectedItem as MacroFSNode; // The root node is not draggable if (draggedNode != null && draggedNode != MacroFSNode.RootNode) { // Initialize the drag & drop operation DragDrop.DoDragDrop(this.MacroTreeView, draggedNode, DragDropEffects.Move); } } } }
public override void ClearSearch() { MacroFSNode.DisableSearch(); }
private void CollapseAll(object sender, EventArgs arguments) { MacroFSNode.CollapseAllNodes(MacroFSNode.RootNode); }
private bool ValidDropTarget(MacroFSNode sourceItem, MacroFSNode targetItem) { // Check whether the target item is meeting your condition return(!sourceItem.Equals(targetItem)); }
public void MoveItem(MacroFSNode sourceItem, MacroFSNode targetItem) { string sourcePath = sourceItem.FullPath; string targetPath = Path.Combine(targetItem.FullPath, sourceItem.Name); string extension = ".js"; MacroFSNode selected; // We want to expand the node and all its parents if it was expanded before OR if it is a file bool wasExpanded = sourceItem.IsExpanded; try { // Move on disk if (sourceItem.IsDirectory) { System.IO.Directory.Move(sourcePath, targetPath); } else { targetPath = targetPath + extension; System.IO.File.Move(sourcePath, targetPath); // Close in the editor this.Reopen(sourcePath, targetPath); } // Move shortcut as well if (sourceItem.Shortcut != MacroFSNode.None) { int shortcutNumber = sourceItem.Shortcut; Manager.Shortcuts[shortcutNumber] = targetPath; } } catch (Exception e) { if (ErrorHandler.IsCriticalException(e)) { throw; } targetPath = sourceItem.FullPath; Manager.Instance.ShowMessageBox(e.Message); } CreateCurrentMacro(); // Refresh tree MacroFSNode.RefreshTree(); // Restore previously selected node selected = MacroFSNode.SelectNode(targetPath); selected.IsExpanded = wasExpanded; selected.Parent.IsExpanded = true; // Notify change in shortcut selected.Shortcut = MacroFSNode.ToFetch; // Make editable if the macro is the current macro if (sourceItem.FullPath == Manager.CurrentMacroPath) { selected.IsEditable = true; } }
private void TreeViewItem_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { // Make sure dragging is not initiated this.isDragging = false; // Make sure that the clicks has selected the item TreeViewItem treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject); if (treeViewItem != null) { treeViewItem.Focus(); e.Handled = true; } /* Show Context Menu */ IVsUIShell uiShell = (IVsUIShell)((IServiceProvider)VSMacrosPackage.Current).GetService(typeof(SVsUIShell)); if (uiShell != null) { // Get context menu id int menuID; MacroFSNode selectedNode = this.MacroTreeView.SelectedItem as MacroFSNode; if (selectedNode.IsDirectory) { if (selectedNode == MacroFSNode.RootNode) { menuID = PkgCmdIDList.BrowserContextMenu; } else if (this.InSamples(selectedNode)) { menuID = PkgCmdIDList.SampleFolderContextMenu; } else { menuID = PkgCmdIDList.FolderContextMenu; } } else { if (selectedNode.FullPath == Manager.CurrentMacroPath) { menuID = PkgCmdIDList.CurrentContextMenu; } else if (this.InSamples(selectedNode)) { menuID = PkgCmdIDList.SampleMacroContextMenu; } else { menuID = PkgCmdIDList.MacroContextMenu; } } // Show right context menu System.Drawing.Point pt = System.Windows.Forms.Cursor.Position; POINTS[] pnts = new POINTS[1]; pnts[0].x = (short)pt.X; pnts[0].y = (short)pt.Y; uiShell.ShowContextMenu(0, GuidList.GuidVSMacrosCmdSet, menuID, pnts, null); } }
protected override void OnStopSearch() { // Disable the search (will notify all nodes of the change) MacroFSNode.DisableSearch(); }
private void TreeViewItem_Drop(object sender, DragEventArgs e) { e.Effects = DragDropEffects.None; e.Handled = true; // Get target node TreeViewItem targetItem = this.GetNearestContainer(e.OriginalSource as UIElement); MacroFSNode target = targetItem.Header as MacroFSNode; if (e.Data.GetDataPresent(typeof(MacroFSNode)) && this.isDragging) { // Get dragged node MacroFSNode dragged = e.Data.GetData(typeof(MacroFSNode)) as MacroFSNode; if (target != null && dragged != null && target != dragged) { if (!target.IsDirectory) { target = target.Parent; } Manager.Instance.MoveItem(dragged, target); } } else if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] droppedFiles = e.Data.GetData(DataFormats.FileDrop, true) as string[]; if (!target.IsDirectory) { target = target.Parent; } foreach (string s in droppedFiles) { string filename = Path.GetFileNameWithoutExtension(s); // If the file is a macro file if (Path.GetExtension(s) == ".js") { string destPath = Path.Combine(target.FullPath, filename + ".js"); VSConstants.MessageBoxResult result = VSConstants.MessageBoxResult.IDYES; if (File.Exists(destPath)) { string message = string.Format(VSMacros.Resources.DragDropFileExists, filename); result = Manager.Instance.ShowMessageBox(message, OLEMSGBUTTON.OLEMSGBUTTON_YESNO); } if (result == VSConstants.MessageBoxResult.IDYES) { File.Copy(s, destPath, true); } } else if (Path.GetExtension(s) == "") { string destPath = Path.Combine(target.FullPath, filename); VSConstants.MessageBoxResult result = VSConstants.MessageBoxResult.IDYES; if (Directory.Exists(destPath)) { string message = string.Format(VSMacros.Resources.DragDropFileExists, filename); result = Manager.Instance.ShowMessageBox(message, OLEMSGBUTTON.OLEMSGBUTTON_YESNO); } if (result == VSConstants.MessageBoxResult.IDYES) { Manager.DirectoryCopy(s, destPath, true); } } } // Unset IsTreeViewItemDropOver for target MacrosControl.SetIsTreeViewItemDropOver(targetItem, false); MacroFSNode.RefreshTree(target); } this.isDragging = false; }
private void MacroTreeView_Loaded(object sender, RoutedEventArgs e) { // Select Current macro MacroFSNode.FindNodeFromFullPath(Manager.CurrentMacroPath).IsSelected = true; }