private IDisposable EditTextBuffer() { if (_readOnlyRegion != null) { using (var edit = _historyTextBuffer.CreateReadOnlyRegionEdit()) { edit.RemoveReadOnlyRegion(_readOnlyRegion); _readOnlyRegion = null; edit.Apply(); } } HistoryChanging?.Invoke(this, new EventArgs()); return(_textBufferIsEditable.Increment()); }
/// <summary> /// Expands the read only region to the end of the current buffer /// </summary> private void ExtendReadOnlyRegion() { if (!textBuffer.EditInProgress) { if (readOnlyRegion != null) { var readOnlyRemove = textBuffer.CreateReadOnlyRegionEdit(); readOnlyRemove.RemoveReadOnlyRegion(readOnlyRegion); readOnlyRemove.Apply(); } var readOnlyEdit = textBuffer.CreateReadOnlyRegionEdit(); readOnlyRegion = readOnlyEdit.CreateReadOnlyRegion(new Span(0, textBuffer.CurrentSnapshot.Length)); readOnlyEdit.Apply(); } }
public OpenSourceGeneratedFile(SourceGeneratedFileManager fileManager, ITextBuffer textBuffer, Workspace workspace, ProjectId projectId, string generatorTypeName, string generatorAssemblyName, string generatedSourceHintName, IThreadingContext threadingContext) : base(threadingContext, assertIsForeground: true) { _fileManager = fileManager; _textBuffer = textBuffer; _workspace = workspace; _projectId = projectId; _generatorTypeName = generatorTypeName; _generatorAssemblyName = generatorAssemblyName; _generatedSourceHintName = generatedSourceHintName; // We'll create a read-only region for the file, but it'll be a dynamic region we can temporarily suspend // while we're doing edits. using (var readOnlyRegionEdit = _textBuffer.CreateReadOnlyRegionEdit()) { _readOnlyRegion = readOnlyRegionEdit.CreateDynamicReadOnlyRegion( _textBuffer.CurrentSnapshot.GetFullSpan(), SpanTrackingMode.EdgeInclusive, EdgeInsertionMode.Deny, callback: _ => !_updatingBuffer); readOnlyRegionEdit.Apply(); } _workspace.WorkspaceChanged += OnWorkspaceChanged; _batchingWorkQueue = new AsyncBatchingDelay( TimeSpan.FromSeconds(1), UpdateBufferContentsAsync, asyncListener: _fileManager._listener, _cancellationTokenSource.Token); }
private void SetReadOnlyRegionType(ReadOnlyRegionType value) { ITextBuffer buffer = WpfTextView.TextBuffer; ITextSnapshot snapshot = buffer.CurrentSnapshot; using (IReadOnlyRegionEdit edit = buffer.CreateReadOnlyRegionEdit()) { edit.ClearReadOnlyRegion(ref _readOnlyRegionBegin); edit.ClearReadOnlyRegion(ref _readOnlyRegionBody); switch (value) { case ReadOnlyRegionType.BeginAndBody: if (snapshot.Length > 0) { _readOnlyRegionBegin = edit.CreateReadOnlyRegion(new Span(0, 0), SpanTrackingMode.EdgeExclusive, EdgeInsertionMode.Deny); _readOnlyRegionBody = edit.CreateReadOnlyRegion(new Span(0, snapshot.Length)); } break; case ReadOnlyRegionType.All: _readOnlyRegionBody = edit.CreateReadOnlyRegion(new Span(0, snapshot.Length), SpanTrackingMode.EdgeExclusive, EdgeInsertionMode.Deny); break; } edit.Apply(); } }
public void Dispose() { using (var readOnlyRegionEdit = _textBuffer.CreateReadOnlyRegionEdit()) { readOnlyRegionEdit.RemoveReadOnlyRegion(_readOnlyRegion); readOnlyRegionEdit.Apply(); } _workspace.WorkspaceChanged -= OnWorkspaceChanged; // Cancel any remaining asynchronous work we may have had to update this file _cancellationTokenSource.Cancel(); }
/// <summary> /// Removes read-only region from buffer. /// </summary> public void RemoveProtection(ITextBuffer buffer, IReadOnlyRegion[] regions) { if (regions[0] != null) { Debug.Assert(regions[1] != null); foreach (var region in regions) { using (var readonlyEdit = buffer.CreateReadOnlyRegionEdit()) { readonlyEdit.RemoveReadOnlyRegion(region); readonlyEdit.Apply(); } } } }
/// <summary> /// Marks the entire buffer as read-only. /// </summary> public void ApplyProtection(ITextBuffer buffer, IReadOnlyRegion[] regions, bool allowAppend = false) { using (var readonlyEdit = buffer.CreateReadOnlyRegionEdit()) { int end = buffer.CurrentSnapshot.Length; Span span = new Span(0, end); var region0 = allowAppend ? readonlyEdit.CreateReadOnlyRegion(span, SpanTrackingMode.EdgeExclusive, EdgeInsertionMode.Allow) : readonlyEdit.CreateReadOnlyRegion(span, SpanTrackingMode.EdgeExclusive, EdgeInsertionMode.Deny); // Create a second read-only region to prevent insert at start of buffer. var region1 = (end > 0) ? readonlyEdit.CreateReadOnlyRegion(new Span(0, 0), SpanTrackingMode.EdgeExclusive, EdgeInsertionMode.Deny) : null; readonlyEdit.Apply(); if (regions != null) { regions[0] = region0; regions[1] = region1; } } }