Пример #1
0
        internal static void Save(IVsPersistDocData docData)
        {
            int    canceled;
            string newDocumentStateScope;

            if (docData.SaveDocData(VSSAVEFLAGS.VSSAVE_Save, out newDocumentStateScope, out canceled) != VSConstants.S_OK)
            {
                LogDebug("error while saving C# script document");
            }
        }
Пример #2
0
 /// <summary>
 /// Silently saves an open document
 /// </summary>
 /// <param name="saveIfDirty">Save the open document only if it is dirty</param>
 /// <remarks>The call to SaveDocData may return Microsoft.VisualStudio.Shell.Interop.PFF_RESULTS.STG_S_DATALOSS to indicate some characters could not be represented in the current codepage</remarks>
 public virtual void Save(bool saveIfDirty)
 {
     if (saveIfDirty && IsDirty)
     {
         IVsPersistDocData persistDocData = DocData;
         if (persistDocData != null)
         {
             string name;
             int    cancelled;
             ErrorHandler.ThrowOnFailure(persistDocData.SaveDocData(VSSAVEFLAGS.VSSAVE_SilentSave, out name, out cancelled));
         }
     }
 }
Пример #3
0
        /// <summary>
        /// Saves the document if it is dirty.
        /// </summary>
        /// <param name="filePath">The absolute path to the file.</param>
        public void SaveIfDirty(string filePath)
        {
            DocumentInfo docInfo = this.FindByPath(filePath);

            if (docInfo != null && docInfo.IsDirty && docInfo.SupportsInterface(typeof(IVsPersistDocData)))
            {
                string            newPath;
                int               saveCanceled;
                IVsPersistDocData persistDocData = docInfo.DocumentData as IVsPersistDocData;
                Tracer.Assert(persistDocData != null, "DocumentInfo.SupportsInterface returned true when it shouldn't have.");
                if (persistDocData != null)
                {
                    int hr = persistDocData.SaveDocData(VSSAVEFLAGS.VSSAVE_SilentSave, out newPath, out saveCanceled);
                    Tracer.WriteLineIf(classType, "SaveIfDirty", Tracer.Level.Information, NativeMethods.Succeeded(hr), "Successfully saved '{0}'.", filePath);
                    NativeMethods.ThrowOnFailure(hr);
                }
            }
        }
Пример #4
0
        public async Task ResetBufferAsync()
        {
            var projectXml = await _projectXmlAccessor.GetProjectXmlAsync().ConfigureAwait(false);

            var existingText = await ReadBufferXmlAsync().ConfigureAwait(false);

            var isLastSavedEqual = false;

            lock (_savedXmlLock)
            {
                // If the project xml is the same as the last thing we saved, then any changes in the editor are new changes
                // on top of the existing changes, and we should not attempt to overwrite them.
                isLastSavedEqual = projectXml.Equals(_lastSavedXml, StringComparison.Ordinal);
            }

            if (!isLastSavedEqual && !existingText.Equals(projectXml, StringComparison.Ordinal))
            {
                await _threadingService.SwitchToUIThread();

                // If the docdata is not dirty, we just update the buffer to avoid the file reload pop-up. Otherwise,
                // we write to disk, to force the pop-up.
                Verify.HResult(_docData.IsDocDataDirty(out int isDirty));
                if (Convert.ToBoolean(isDirty))
                {
                    _fileSystem.WriteAllText(FilePath, projectXml, await _unconfiguredProject.GetFileEncodingAsync().ConfigureAwait(true));
                }
                else
                {
                    var textSpan = new Span(0, _textDocument.TextBuffer.CurrentSnapshot.Length);

                    // When the buffer is being reset, it's often been set to ReadOnly. We can't update the buffer when it's readonly, so save
                    // the currently flags and turn off ReadOnly, restoring after we're done. We're on the UI thread, so this is invisible to
                    // the user.
                    Verify.HResult(_textBuffer.GetStateFlags(out uint oldFlags));
                    _textBuffer.SetStateFlags(oldFlags & ~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY);

                    // Make sure that the buffer is reset back to the old flags, regardless of TextBuffer.Replace throwing any kind of exception
                    try
                    {
                        _textDocument.TextBuffer.Replace(textSpan, projectXml);
                        // We save the DocData here so that if the user decides to Undo the change from the file itself, the editor will be considered
                        // dirty and will prompt about unsaved changes if closed before saving again.
                        _docData.SaveDocData(VSSAVEFLAGS.VSSAVE_Save, out string unused, out int cancelled);
                    }
                    finally
                    {
                        var isReadonly = (oldFlags & (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY) == (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY;
                        if (isReadonly)
                        {
                            Verify.HResult(_textBuffer.GetStateFlags(out uint newFlags));
                            _textBuffer.SetStateFlags(newFlags | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY);
                        }
                    }
                }
            }

            // Since we're writing to the file on disk in all scenarios, we can potentially race with the project reload mechanism during project
            // close if the file on disk has divergent changes from the temp file. This causes a series of confusing popups for the user that make
            // it very likely they will accidentally discard changes. We save here to make sure we don't run into that race.
            await _unconfiguredProject.SaveAsync().ConfigureAwait(false);
        }
Пример #5
0
 internal static void Save(IVsPersistDocData docData)
 {
     int canceled;
     string newDocumentStateScope;
     if (docData.SaveDocData(VSSAVEFLAGS.VSSAVE_Save, out newDocumentStateScope, out canceled) != VSConstants.S_OK)
     {
         LogDebug("error while saving C# script document");
     }
 }