public static void MakeEditorReadOnly(IVsTextBuffer buffer, bool readOnly) { uint flags; if (VSConstants.S_OK == buffer.GetStateFlags(out flags)) { flags = readOnly? flags | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY : flags & ~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY; buffer.SetStateFlags(flags); } }
// ---------------------------------------------------------------------------------- /// <summary> /// Turns on or off the read-only mode of the editor. /// </summary> // ---------------------------------------------------------------------------------- public void SetReadOnly(bool isReadOnly) { uint flags; _BufferAdapter.GetStateFlags(out flags); var newFlags = isReadOnly ? flags | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY : flags & ~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY; _BufferAdapter.SetStateFlags(newFlags); }
public async Task ResetBufferAsync() { var projectXml = await _projectXmlAccessor.GetProjectXmlAsync().ConfigureAwait(false); var existingText = await ReadBufferXmlAsync().ConfigureAwait(false); if (!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); } finally { _textBuffer.SetStateFlags(oldFlags); } } } }
private void SetReadOnlyFlag(IVsTextBuffer buffer, bool value) { uint newFlags; buffer.GetStateFlags(out var oldFlags); if (value) { newFlags = oldFlags | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY; } else { newFlags = oldFlags & ~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY; } if (oldFlags != newFlags) { buffer.SetStateFlags(newFlags); } }
private void SetReadOnlyFlag(IVsTextBuffer buffer, bool value) { uint oldFlags; uint newFlags; buffer.GetStateFlags(out oldFlags); if (value) { newFlags = oldFlags | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY; } else { newFlags = oldFlags & ~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY; } if (oldFlags != newFlags) { buffer.SetStateFlags(newFlags); } }
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); }
public void SetReadOnly(bool isReadOnly) { _vsTextBuffer.SetStateFlags(isReadOnly?(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY:0); }