private static IVsProfileSettingsFileInfo GetProfileSettingsFileInfo(string importFilePath) { IVsProfileDataManager vsProfileDataManager = (IVsProfileDataManager)Package.GetGlobalService(typeof(SVsProfileDataManager)); vsProfileDataManager.GetSettingsFiles(uint.MaxValue, out IVsProfileSettingsFileCollection vsProfileSettingsFileCollection); vsProfileSettingsFileCollection.AddBrowseFile(importFilePath, out IVsProfileSettingsFileInfo profileSettingsFileInfo); return(profileSettingsFileInfo); }
private IVsProfileSettingsFileInfo GetDefaultProfileSettingsFileInfo(IVsProfileDataManager manager) { const string DEFAULT_SHORTCUTS_FILENAME = "DefaultShortcuts.vssettings"; string extensionDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string resetFilePath = Path.Combine(extensionDir, "Resources", DEFAULT_SHORTCUTS_FILENAME); return(GetProfileSettingsFileInfo(resetFilePath)); }
public SettingsController(string extPath, IVsProfileDataManager manager, IXmlRepository xmlRepository) { _fullPath = Path.Combine(extPath, FileName); _manager = manager; _xmlRepository = xmlRepository; }
public void Setup() { _manager = Mock.Create <IVsProfileDataManager>(); _errorInformation = Mock.Create <IVsSettingsErrorInformation>(); _files = Mock.Create <IVsProfileSettingsFileCollection>(); _xmlRepository = Mock.Create <IXmlRepository>(); _settingsFileInfo = Mock.Create <IVsProfileSettingsFileInfo>(); _sets = Mock.Create <IVsProfileSettingsTree>(); }
private static bool ImportSettingsFromSettingsTree(IVsProfileSettingsTree profileSettingsTree) { //EnableKeyboardOnlyInProfileSettingsTree(profileSettingsTree); IVsProfileDataManager vsProfileDataManager = (IVsProfileDataManager)Package.GetGlobalService(typeof(SVsProfileDataManager)); int result = vsProfileDataManager.ImportSettings(profileSettingsTree, out IVsSettingsErrorInformation errorInfo); if (ErrorHandler.Failed(result)) { // Something went wrong. TODO: Handle error. MessageBox.Show("Error occurred attempting to import settings."); return(false); } return(true); }
private string UpdateAndSaveNewSettingsFile(XDocument vsSettingsFile, List <VSShortcut> removeList) { // Gather shortcuts to be removed var userShortcuts = vsSettingsFile.Descendants("UserShortcuts"); var shortcutsToDelete = new List <XElement>(); foreach (var userShortcut in userShortcuts) { foreach (var shortcut in userShortcut.Descendants("Shortcut")) { if (removeList.Contains(new VSShortcut { Command = shortcut.Attribute("Command").Value, Scope = shortcut.Attribute("Scope").Value, Shortcut = shortcut.Value })) { shortcutsToDelete.Add(shortcut); } } } // remove shortcuts foreach (var shortcut in shortcutsToDelete) { shortcut.Remove(); } // save vs settings IVsProfileDataManager vsProfileDataManager = (IVsProfileDataManager)ServiceProvider.GetService(typeof(SVsProfileDataManager)); // Generate default path for saving shortcuts // e.g. c:\users\justcla\appdata\local\microsoft\visualstudio\15.0_cf83efb8exp\Settings\Exp\CurrentSettings-2017-12-27-1.vssettings string uniqueExportPath = GetExportFilePath(vsProfileDataManager); string userExportPath = Path.GetDirectoryName(uniqueExportPath); string uniqueFilename = Path.GetFileNameWithoutExtension(uniqueExportPath); string fileExtension = Path.GetExtension(uniqueExportPath); string saveFilePath = Path.Combine(userExportPath, $"{uniqueFilename}{fileExtension}"); vsSettingsFile.Save(saveFilePath); return(saveFilePath); }
/// <summary> /// Creates a new .vssettings file that matches the input vsSettingsXDoc without the items in the removeList /// </summary> /// <returns>string name of the file that was created</returns> private string CreateSettingsFile(XDocument vsSettingsXDoc, List <VSShortcut> removeList) { // Gather shortcuts to be removed IEnumerable <XElement> userShortcuts = vsSettingsXDoc.Descendants("UserShortcuts"); List <XElement> shortcutsToDelete = new List <XElement>(); foreach (XElement userShortcut in userShortcuts) { foreach (XElement shortcut in userShortcut.Descendants("Shortcut")) { if (removeList.Contains(new VSShortcut { Command = shortcut.Attribute("Command").Value, Scope = shortcut.Attribute("Scope").Value, Shortcut = shortcut.Value })) { shortcutsToDelete.Add(shortcut); } } } // remove shortcuts foreach (XElement shortcut in shortcutsToDelete) { shortcut.Remove(); } // Create a new VSSettings file without the removed shortcuts //string saveFilePath = WriteToUniqueFilePath(vsSettingsXDoc); // save vs settings IVsProfileDataManager vsProfileDataManager = (IVsProfileDataManager)ServiceProvider.GetService(typeof(SVsProfileDataManager)); string saveFilePath = GetExportFilePath(vsProfileDataManager); // Write the XmlDoc to file using the new unique filename vsSettingsXDoc.Save(saveFilePath); return(saveFilePath); }
//-------- Reset Shortcuts -------- private bool ResetShortcutsViaProfileManager() { IVsProfileDataManager vsProfileDataManager = (IVsProfileDataManager)ServiceProvider.GetService(typeof(SVsProfileDataManager)); IVsProfileSettingsFileInfo profileSettingsFileInfo = GetDefaultProfileSettingsFileInfo(vsProfileDataManager); if (profileSettingsFileInfo == null) { MessageBox.Show("Unable to find Default Shortcuts file."); return(false); } // Apply the Reset int result = vsProfileDataManager.ResetSettings(profileSettingsFileInfo, out IVsSettingsErrorInformation errorInfo); if (ErrorHandler.Failed(result)) { // Something went wrong. TODO: Handle error. MessageBox.Show("Error occurred attempting to reset keyboard shortcuts."); return(false); } MessageBox.Show("Success! Keyboard shortcuts have been reset.", MSG_CAPTION_RESET, MessageBoxButtons.OK); return(true); }
private static IVsProfileSettingsTree GetShortcutsSettingsTreeForExport(IVsProfileDataManager vsProfileDataManager) { vsProfileDataManager.GetSettingsForExport(out IVsProfileSettingsTree profileSettingsTree); EnableKeyboardOnlyInProfileSettingsTree(profileSettingsTree); return(profileSettingsTree); }
private static string GetExportFilePath(IVsProfileDataManager vsProfileDataManager) { vsProfileDataManager.GetUniqueExportFileName((uint)__VSPROFILEGETFILENAME.PGFN_SAVECURRENT, out string exportFilePath); return(exportFilePath); }
//-------- Backup Shortcuts -------- public void ExecuteSaveShortcuts() { // Confirm Save operation //if (MessageBox.Show("Save current keyboard shortcuts?", MSG_CAPTION_BACKUP, MessageBoxButtons.OKCancel) != DialogResult.OK) //{ // return; //} IVsProfileDataManager vsProfileDataManager = (IVsProfileDataManager)ServiceProvider.GetService(typeof(SVsProfileDataManager)); // Generate default path for saving shortcuts // e.g. c:\users\justcla\appdata\local\microsoft\visualstudio\15.0_cf83efb8exp\Settings\Exp\CurrentSettings-2017-12-27-1.vssettings string uniqueExportPath = GetExportFilePath(vsProfileDataManager); string userExportPath = Path.GetDirectoryName(uniqueExportPath); string uniqueFilename = Path.GetFileNameWithoutExtension(uniqueExportPath); string fileExtension = Path.GetExtension(uniqueExportPath); // Open UI to let user name the file string shortcutsName = uniqueFilename; if (!SaveShortcuts.GetShortcutsName(ref shortcutsName)) { // Cancel or ESC pressed return; } // Construct the filename where the vssettings file will be saved string saveFilePath = Path.Combine(userExportPath, $"{shortcutsName}{fileExtension}"); // Check if file already exists if (File.Exists(saveFilePath)) { // Prompt to overwrite if (MessageBox.Show($"The settings file already exists {saveFilePath}\n\nDo you want to replace this file?", MSG_CAPTION_SAVE_SHORTCUTS, MessageBoxButtons.YesNo) != DialogResult.Yes) { // Duplicate file. User does not want to overwrite. Exit out. // TODO: Consider returning the user to the file name dialog. return; } } // Check if name already used if (userShortcutsManager.HasUserShortcuts(shortcutsName)) { // Prompt to overwrite if (MessageBox.Show($"Settings already exist with the name: {saveFilePath}\n\nDo you want to replace these settings?", MSG_CAPTION_SAVE_SHORTCUTS, MessageBoxButtons.YesNo) != DialogResult.Yes) { // Duplicate file. User does not want to overwrite. Exit out. // TODO: Consider returning the user to the file name dialog. return; } } // Do the export IVsProfileSettingsTree keyboardOnlyExportSettings = GetShortcutsSettingsTreeForExport(vsProfileDataManager); int result = vsProfileDataManager.ExportSettings(saveFilePath, keyboardOnlyExportSettings, out IVsSettingsErrorInformation errorInfo); if (result != VSConstants.S_OK) { // Something went wrong. TODO: Handle error. MessageBox.Show($"Oops.... Something went wrong trying to export settings to the following file:\n\n{saveFilePath}", MSG_CAPTION_SAVE_SHORTCUTS); return; } // Save Backup file path to SettingsManager and to UserShortcutsRegistry SaveBackupFilePath(saveFilePath); AddUserShortcutsFileToRegistry(saveFilePath); // Report success string Text = $"Your keyboard shortcuts have been saved to the following file:\n\n{saveFilePath}"; MessageBox.Show(Text, MSG_CAPTION_SAVE_SHORTCUTS, MessageBoxButtons.OK); }