/// <summary> /// Get's called to rename the eventually running document this hierarchyitem points to /// </summary> /// returns FALSE if the doc can not be renamed internal bool RenameDocument(string oldName, string newName) { var pRDT = GetService(typeof (IVsRunningDocumentTable)) as IVsRunningDocumentTable; if (pRDT == null) return false; var docData = IntPtr.Zero; IVsHierarchy pIVsHierarchy; uint itemId; uint uiVsDocCookie; var sfc = new SuspendFileChanges(ProjectMgr.Site, oldName); sfc.Suspend(); try { // Suspend ms build since during a rename operation no msbuild re-evaluation should be performed until we have finished. // Scenario that could fail if we do not suspend. // We have a project system relying on MPF that triggers a Compile target build (re-evaluates itself) whenever the project changes. (example: a file is added, property changed.) // 1. User renames a file in the above project sytem relying on MPF // 2. Our rename funstionality implemented in this method removes and readds the file and as a post step copies all msbuild entries from the removed file to the added file. // 3. The project system mentioned will trigger an msbuild re-evaluate with the new item, because it was listening to OnItemAdded. // The problem is that the item at the "add" time is only partly added to the project, since the msbuild part has not yet been copied over as mentioned in part 2 of the last step of the rename process. // The result is that the project re-evaluates itself wrongly. var renameflag = VSRENAMEFILEFLAGS.VSRENAMEFILEFLAGS_NoFlags; try { ProjectMgr.SuspendMSBuild(); ErrorHandler.ThrowOnFailure(pRDT.FindAndLockDocument((uint) _VSRDTFLAGS.RDT_NoLock, oldName, out pIVsHierarchy, out itemId, out docData, out uiVsDocCookie)); if (pIVsHierarchy != null && !Utilities.IsSameComObject(pIVsHierarchy, ProjectMgr.InteropSafeIVsHierarchy)) { // Don't rename it if it wasn't opened by us. return false; } // ask other potentially running packages if (!ProjectMgr.Tracker.CanRenameItem(oldName, newName, renameflag)) { return false; } // Allow the user to "fix" the project by renaming the item in the hierarchy // to the real name of the file on disk. if (IsFileOnDisk(oldName) || !IsFileOnDisk(newName)) { RenameInStorage(oldName, newName); } var newFileName = Path.GetFileName(newName); DocumentManager.UpdateCaption(ProjectMgr.Site, newFileName, docData); var caseOnlyChange = NativeMethods.IsSamePath(oldName, newName); if (!caseOnlyChange) { // Check out the project file if necessary. if (!ProjectMgr.QueryEditProjectFile(false)) { throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED); } RenameFileNode(oldName, newName); } else { RenameCaseOnlyChange(newFileName); } } finally { ProjectMgr.ResumeMSBuild(ProjectMgr.ReEvaluateProjectFileTargetName); } ProjectMgr.Tracker.OnItemRenamed(oldName, newName, renameflag); } finally { sfc.Resume(); if (docData != IntPtr.Zero) { Marshal.Release(docData); } } return true; }
/// <summary> /// Performs a SaveAs operation of an open document. Called from SaveItem after the running document table has been /// updated with the new doc data. /// </summary> /// <param name="docData">A pointer to the document in the rdt</param> /// <param name="newFilePath">The new file path to the document</param> /// <returns></returns> protected override int AfterSaveItemAs(IntPtr docData, string newFilePath) { if (string.IsNullOrEmpty(newFilePath)) { throw new ArgumentException( SR.GetString(SR.ParameterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "newFilePath"); } var returnCode = VSConstants.S_OK; newFilePath = newFilePath.Trim(); //Identify if Path or FileName are the same for old and new file var newDirectoryName = Path.GetDirectoryName(newFilePath); var newDirectoryUri = new Uri(newDirectoryName); var newCanonicalDirectoryName = newDirectoryUri.LocalPath; newCanonicalDirectoryName = newCanonicalDirectoryName.TrimEnd(Path.DirectorySeparatorChar); var oldCanonicalDirectoryName = new Uri(Path.GetDirectoryName(GetMkDocument())).LocalPath; oldCanonicalDirectoryName = oldCanonicalDirectoryName.TrimEnd(Path.DirectorySeparatorChar); var errorMessage = string.Empty; var isSamePath = NativeMethods.IsSamePath(newCanonicalDirectoryName, oldCanonicalDirectoryName); var isSameFile = NativeMethods.IsSamePath(newFilePath, Url); // Currently we do not support if the new directory is located outside the project cone var projectCannonicalDirecoryName = new Uri(ProjectMgr.ProjectFolder).LocalPath; projectCannonicalDirecoryName = projectCannonicalDirecoryName.TrimEnd(Path.DirectorySeparatorChar); if (!isSamePath && newCanonicalDirectoryName.IndexOf(projectCannonicalDirecoryName, StringComparison.OrdinalIgnoreCase) == -1) { errorMessage = string.Format(CultureInfo.CurrentCulture, SR.GetString(SR.LinkedItemsAreNotSupported, CultureInfo.CurrentUICulture), Path.GetFileNameWithoutExtension(newFilePath)); throw new InvalidOperationException(errorMessage); } //Get target container HierarchyNode targetContainer = null; if (isSamePath) { targetContainer = Parent; } else if (NativeMethods.IsSamePath(newCanonicalDirectoryName, projectCannonicalDirecoryName)) { //the projectnode is the target container targetContainer = ProjectMgr; } else { //search for the target container among existing child nodes targetContainer = ProjectMgr.FindChild(newDirectoryName); if (targetContainer != null && targetContainer is FileNode) { // We already have a file node with this name in the hierarchy. errorMessage = string.Format(CultureInfo.CurrentCulture, SR.GetString(SR.FileAlreadyExistsAndCannotBeRenamed, CultureInfo.CurrentUICulture), Path.GetFileNameWithoutExtension(newFilePath)); throw new InvalidOperationException(errorMessage); } } if (targetContainer == null) { // Add a chain of subdirectories to the project. var relativeUri = PackageUtilities.GetPathDistance(ProjectMgr.BaseURI.Uri, newDirectoryUri); Debug.Assert(!string.IsNullOrEmpty(relativeUri) && relativeUri != newDirectoryUri.LocalPath, "Could not make pat distance of " + ProjectMgr.BaseURI.Uri.LocalPath + " and " + newDirectoryUri); targetContainer = ProjectMgr.CreateFolderNodes(relativeUri); } Debug.Assert(targetContainer != null, "We should have found a target node by now"); //Suspend file changes while we rename the document var oldrelPath = ItemNode.GetMetadata(ProjectFileConstants.Include); var oldName = Path.Combine(ProjectMgr.ProjectFolder, oldrelPath); var sfc = new SuspendFileChanges(ProjectMgr.Site, oldName); sfc.Suspend(); try { // Rename the node. DocumentManager.UpdateCaption(ProjectMgr.Site, Path.GetFileName(newFilePath), docData); // Check if the file name was actually changed. // In same cases (e.g. if the item is a file and the user has changed its encoding) this function // is called even if there is no real rename. if (!isSameFile || (Parent.ID != targetContainer.ID)) { // The path of the file is changed or its parent is changed; in both cases we have // to rename the item. RenameFileNode(oldName, newFilePath, targetContainer.ID); OnInvalidateItems(Parent); } } catch (Exception e) { Trace.WriteLine("Exception : " + e.Message); RecoverFromRenameFailure(newFilePath, oldrelPath); throw; } finally { sfc.Resume(); } return returnCode; }
/// <summary> /// Saves the project file on a new name. /// </summary> /// <param name="newFileName">The new name of the project file.</param> /// <returns>Success value or an error code.</returns> protected virtual int SaveAs(string newFileName) { Debug.Assert(!string.IsNullOrEmpty(newFileName), "Cannot save project file for an empty or null file name"); if (string.IsNullOrEmpty(newFileName)) { throw new ArgumentNullException("newFileName"); } newFileName = newFileName.Trim(); var errorMessage = string.Empty; if (newFileName.Length > NativeMethods.MAX_PATH) { errorMessage = string.Format(CultureInfo.CurrentCulture, SR.GetString(SR.PathTooLong, CultureInfo.CurrentUICulture), newFileName); } else { var fileName = string.Empty; try { fileName = Path.GetFileNameWithoutExtension(newFileName); } // We want to be consistent in the error message and exception we throw. fileName could be for example #�&%"�&"% and that would trigger an ArgumentException on Path.IsRooted. catch (ArgumentException) { errorMessage = SR.GetString(SR.ErrorInvalidFileName, CultureInfo.CurrentUICulture); } if (errorMessage.Length == 0) { // If there is no filename or it starts with a leading dot issue an error message and quit. // For some reason the save as dialog box allows to save files like "......ext" if (string.IsNullOrEmpty(fileName) || fileName[0] == '.') { errorMessage = SR.GetString(SR.FileNameCannotContainALeadingPeriod, CultureInfo.CurrentUICulture); } else if (Utilities.ContainsInvalidFileNameChars(newFileName)) { errorMessage = SR.GetString(SR.ErrorInvalidFileName, CultureInfo.CurrentUICulture); } else { var url = Path.GetDirectoryName(newFileName); var oldUrl = Path.GetDirectoryName(Url); if (!NativeMethods.IsSamePath(oldUrl, url)) { errorMessage = string.Format(CultureInfo.CurrentCulture, SR.GetString(SR.SaveOfProjectFileOutsideCurrentDirectory, CultureInfo.CurrentUICulture), ProjectFolder); } } } } if (errorMessage.Length > 0) { // If it is not called from an automation method show a dialog box. if (!Utilities.IsInAutomationFunction(Site)) { string title = null; var icon = OLEMSGICON.OLEMSGICON_CRITICAL; var buttons = OLEMSGBUTTON.OLEMSGBUTTON_OK; var defaultButton = OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST; VsShellUtilities.ShowMessageBox(Site, title, errorMessage, icon, buttons, defaultButton); return VSConstants.OLE_E_PROMPTSAVECANCELLED; } throw new InvalidOperationException(errorMessage); } var oldName = FileName; var solution = Site.GetService(typeof (IVsSolution)) as IVsSolution; Debug.Assert(solution != null, "Could not retrieve the solution form the service provider"); if (solution == null) { throw new InvalidOperationException(); } var canRenameContinue = 0; ErrorHandler.ThrowOnFailure(solution.QueryRenameProject(InteropSafeIVsProject3, FileName, newFileName, 0, out canRenameContinue)); if (canRenameContinue == 0) { return VSConstants.OLE_E_PROMPTSAVECANCELLED; } var fileChanges = new SuspendFileChanges(Site, oldName); fileChanges.Suspend(); try { // Save the project file and project file related properties. SaveMSBuildProjectFileAs(newFileName); SetProjectFileDirty(false); // TODO: If source control is enabled check out the project file. //Redraw. OnPropertyChanged(this, (int) __VSHPROPID.VSHPROPID_Caption, 0); ErrorHandler.ThrowOnFailure(solution.OnAfterRenameProject(InteropSafeIVsProject3, oldName, FileName, 0)); var shell = Site.GetService(typeof (SVsUIShell)) as IVsUIShell; Debug.Assert(shell != null, "Could not get the ui shell from the project"); if (shell == null) { throw new InvalidOperationException(); } ErrorHandler.ThrowOnFailure(shell.RefreshPropertyBrowser(0)); } finally { fileChanges.Resume(); } return VSConstants.S_OK; }
public virtual int Save(string fileToBeSaved, int remember, uint formatIndex) { // The file name can be null. Then try to use the Url. var tempFileToBeSaved = fileToBeSaved; if (string.IsNullOrEmpty(tempFileToBeSaved) && !string.IsNullOrEmpty(Url)) { tempFileToBeSaved = Url; } if (string.IsNullOrEmpty(tempFileToBeSaved)) { throw new ArgumentException(SR.GetString(SR.InvalidParameter, CultureInfo.CurrentUICulture), "fileToBeSaved"); } var setProjectFileDirtyAfterSave = false; if (remember == 0) { setProjectFileDirtyAfterSave = IsProjectFileDirty; } // Update the project with the latest flavor data (if needed) PersistXMLFragments(); var result = VSConstants.S_OK; var saveAs = true; if (NativeMethods.IsSamePath(tempFileToBeSaved, FileName)) { saveAs = false; } if (!saveAs) { var fileChanges = new SuspendFileChanges(Site, FileName); fileChanges.Suspend(); try { // Ensure the directory exist var saveFolder = Path.GetDirectoryName(tempFileToBeSaved); if (!Directory.Exists(saveFolder)) Directory.CreateDirectory(saveFolder); // Save the project buildProject.Save(tempFileToBeSaved); SetProjectFileDirty(false); } finally { fileChanges.Resume(); } } else { result = SaveAs(tempFileToBeSaved); if (result != VSConstants.OLE_E_PROMPTSAVECANCELLED) { ErrorHandler.ThrowOnFailure(result); } } if (setProjectFileDirtyAfterSave) { SetProjectFileDirty(true); } return result; }
/// <summary> /// Renames the project file /// </summary> /// <param name="newFile">The full path of the new project file.</param> protected virtual void RenameProjectFile(string newFile) { var shell = Site.GetService(typeof (SVsUIShell)) as IVsUIShell; Debug.Assert(shell != null, "Could not get the ui shell from the project"); if (shell == null) { throw new InvalidOperationException(); } // Do some name validation if (Utilities.ContainsInvalidFileNameChars(newFile)) { throw new InvalidOperationException(SR.GetString(SR.ErrorInvalidProjectName, CultureInfo.CurrentUICulture)); } // Figure out what the new full name is var oldFile = Url; var canContinue = 0; var vsSolution = (IVsSolution) GetService(typeof (SVsSolution)); if (ErrorHandler.Succeeded(vsSolution.QueryRenameProject(InteropSafeIVsProject3, oldFile, newFile, 0, out canContinue)) && canContinue != 0) { var isFileSame = NativeMethods.IsSamePath(oldFile, newFile); // If file already exist and is not the same file with different casing if (!isFileSame && File.Exists(newFile)) { // Prompt the user for replace var message = SR.GetString(SR.FileAlreadyExists, newFile); if (!Utilities.IsInAutomationFunction(Site)) { if (!VsShellUtilities.PromptYesNo(message, null, OLEMSGICON.OLEMSGICON_WARNING, shell)) { throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED); } } else { throw new InvalidOperationException(message); } // Delete the destination file after making sure it is not read only File.SetAttributes(newFile, FileAttributes.Normal); File.Delete(newFile); } var fileChanges = new SuspendFileChanges(Site, FileName); fileChanges.Suspend(); try { // Actual file rename SaveMSBuildProjectFileAs(newFile); SetProjectFileDirty(false); if (!isFileSame) { // Now that the new file name has been created delete the old one. // TODO: Handle source control issues. File.SetAttributes(oldFile, FileAttributes.Normal); File.Delete(oldFile); } OnPropertyChanged(this, (int) __VSHPROPID.VSHPROPID_Caption, 0); // Update solution ErrorHandler.ThrowOnFailure(vsSolution.OnAfterRenameProject(InteropSafeIVsProject3, oldFile, newFile, 0)); ErrorHandler.ThrowOnFailure(shell.RefreshPropertyBrowser(0)); } finally { fileChanges.Resume(); } } else { throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED); } }