public static bool TryGetComponentAt( this IReadOnlyBorder border, Coord coord, out BorderComponentType component, out IReadOnlyRegion componentRegion) { if (!border.OuterRegion.Overlaps(coord)) { goto fail; } for (int y = 2; 0 < y; y--) { for (int x = 2; 0 < x; x--) { BorderComponentType type = ComponentTypes[y, x]; IReadOnlyRegion region = border.GetComponent(type); if (region.Overlaps(coord)) { component = type; componentRegion = region; return(true); } } } fail: component = default; componentRegion = default !;
public Text( FormattedString content, IReadOnlyRegion constrainedTo, CharColors?backgroundFill = null) : this(content, new Region(constrainedTo), backgroundFill) { }
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(); } }
/// <summary> /// Tries to translate <paramref name="region"/> by the specified <paramref name="offset"/>, and returns a /// value indicating whether the translation was successful. If the translation was successful, /// <see langword="true"/> will be returned, and <paramref name="beforeChange"/> will be set to a region /// equivalent to <paramref name="region"/> before the translation was applied. /// </summary> /// <param name="region"> /// The region to try to translate. /// </param> /// <param name="offset"> /// The offset by which to translate this region. /// </param> /// <param name="beforeChange"> /// If the translation succeeds, a region equivalent to this region before the translation was applied. /// </param> /// <returns> /// <see langword="true"/> if the translation succeeded; otherwise, <see langword="false"/>. /// </returns> public static bool TryTranslateBy(this IMoveOnlyRegion region, Coord offset, out IReadOnlyRegion beforeChange) { if (region is null) { throw new ArgumentNullException(nameof(region)); } return(region.TryMoveTo(region.TopLeft + offset, out beforeChange)); }
public static Rectangle ToRectangle(this IReadOnlyRegion region) { if (region is null) { throw new ArgumentNullException(nameof(region)); } return(new Rectangle(region.TopLeft, region.BottomRight)); }
/// <summary> /// Initializes a new instance of the <see cref="RegionChangedEventArgs"/> class. /// </summary> /// <param name="beforeChange"> /// A region with properties equivalent to those without the change. /// </param> /// <param name="afterChange"> /// A region with properties equivalent to those with the change. /// </param> /// <param name="changeTypes"> /// The types of changes encapsulated by this change event. /// </param> internal RegionChangedEventArgs( IReadOnlyRegion beforeChange, IReadOnlyRegion afterChange, RegionChangeTypes changeTypes) { this.BeforeChange = beforeChange; this.AfterChange = afterChange; this.ChangeTypes = changeTypes; }
private void RemoveReadOnlyRegionFromBuffer(DocumentId documentId, IReadOnlyRegion region) { AssertIsForeground(); var textBuffer = GetTextBuffer(_workspace, documentId); using var readOnlyEdit = textBuffer.CreateReadOnlyRegionEdit(); readOnlyEdit.RemoveReadOnlyRegion(region); readOnlyEdit.Apply(); }
private void MakeTextBufferReadOnly() { using (var edit = _historyTextBuffer.CreateReadOnlyRegionEdit()) { var span = new Span(0, edit.Snapshot.Length); _readOnlyRegion = edit.CreateReadOnlyRegion(span, SpanTrackingMode.EdgeInclusive, EdgeInsertionMode.Deny); edit.Apply(); } _currentEntry = null; HistoryChanged?.Invoke(this, new EventArgs()); }
public Label( IReadOnlyRegion region, Alignments alignments, FormattedString content, CharColors?backgroundFill = null) { this.content = new ContentValue(content); this.alignments = alignments; this.backgroundFill = backgroundFill; this.Ctor(region); }
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()); }
public static bool Overlaps(this IReadOnlyRegion region1, IReadOnlyRegion region2) { if (region1 is null) { throw new ArgumentNullException(nameof(region1)); } if (region2 is null) { throw new ArgumentNullException(nameof(region2)); } return(region1.Overlaps(region2.ToRectangle())); }
public void Draw(ISink sink, IReadOnlyRegion region) { if (this.InnerRegion.Contains(region)) { this.DrawInternal( sink, new Rectangle( region.TopLeft - this.InnerRegion.TopLeft, region.BottomRight - this.InnerRegion.TopLeft - Coord.OneOffset)); } else if (this.border.OuterRegion.Overlaps(region)) { this.Draw(sink); } }
internal void ClearReadOnlyRegion() { var readOnlyRemove = textBuffer.CreateReadOnlyRegionEdit(); readOnlyRemove.RemoveReadOnlyRegion(readOnlyRegion); readOnlyRemove.Apply(); readOnlyRegion = null; var edit = textBuffer.CreateEdit(); edit.Delete(new Span(0, textBuffer.CurrentSnapshot.Length));//, string.Empty); edit.Insert(0, Resources.DefaultConsolePrompt); edit.Apply(); ExtendReadOnlyRegion(); }
internal void ClearReadOnlyRegion() { var readOnlyRemove = mefTextBuffer.CreateReadOnlyRegionEdit(); readOnlyRemove.RemoveReadOnlyRegion(readOnlyRegion); readOnlyRemove.Apply(); readOnlyRegion = null; var edit = mefTextBuffer.CreateEdit(); edit.Delete(new Span(0, mefTextBuffer.CurrentSnapshot.Length));//, string.Empty); edit.Insert(0, ">"); edit.Apply(); ExtendReadOnlyRegion(); }
public static void Draw(this Symbol symbol, ITerminalSink sink, IReadOnlyRegion region) { if (symbol is null) { throw new ArgumentNullException(nameof(symbol)); } if (sink is null) { throw new ArgumentNullException(nameof(sink)); } if (region is null) { throw new ArgumentNullException(nameof(region)); } symbol.Draw(sink, new Rectangle(region.TopLeft, region.BottomRight)); }
public Fill( IReadOnlyRegion region, FormattedString pattern, CharColors?backgroundFill = null) { ContentValue contentValue = new ContentValue(pattern); this.Region = new Region(region); this.Region.OnChanged += (obj, e) => { if (e.ChangeTypes.HasFlag(RegionChangeTypes.Resize)) { this.Recalculate(e.AfterChange, contentValue, backgroundFill); } }; this.Recalculate(this.Region, contentValue, backgroundFill); }
public Fill(IReadOnlyRegion region, CharColors fill) { void Fill(IReadOnlyRegion fillToSize) { this.undelayedContent = new CharInfo[fillToSize.Height, fillToSize.Width]; this.undelayedContent.Fill(new CharInfo(' ', fill)); } this.Region = new Region(region); Fill(this.Region); this.Region.OnChanged += (obj, e) => { if (e.ChangeTypes.HasFlag(RegionChangeTypes.Resize)) { Fill(e.AfterChange); } }; }
public void RemoveReadOnlyRegion(IReadOnlyRegion readOnlyRegion) { // Throw if trying to remove a region if there aren't that many regions created. if (this.baseBuffer.readOnlyRegions == null) { throw new InvalidOperationException(Strings.RemoveNoReadOnlyRegion); } // Throw if trying to remove a region from the wrong buffer if (this.readOnlyRegionsToRemove.Exists(delegate(IReadOnlyRegion match) { return(!object.ReferenceEquals(match.Span.TextBuffer, this.baseBuffer)); })) { throw new InvalidOperationException(Strings.InvalidReadOnlyRegion); } this.readOnlyRegionsToRemove.Add(readOnlyRegion); Span regionSpan = readOnlyRegion.Span.GetSpan(this.baseBuffer.CurrentSnapshot); this.aggregateStart = Math.Min(this.aggregateStart, regionSpan.Start); this.aggregateEnd = Math.Max(this.aggregateEnd, regionSpan.End); }
/// <summary> /// Initializes a new instance of the <see cref="RegionChangedEventArgs"/> class. /// </summary> /// <param name="beforeChange"> /// A region with properties equivalent to those without the change. /// </param> /// <param name="afterChange"> /// A region with properties equivalent to those with the change. /// </param> /// <exception cref="ArgumentNullException"> /// Thrown when <paramref name="beforeChange"/> or <paramref name="afterChange"/> is <see langword="null"/>. /// </exception> public RegionChangedEventArgs( IReadOnlyRegion beforeChange, IReadOnlyRegion afterChange) { this.BeforeChange = beforeChange ?? throw new ArgumentNullException(nameof(beforeChange)); this.AfterChange = afterChange ?? throw new ArgumentNullException(nameof(afterChange)); RegionChangeTypes changeTypes = default; if (beforeChange.TopLeft != afterChange.TopLeft) { changeTypes |= RegionChangeTypes.Move; } if (beforeChange.MathHeight != afterChange.MathHeight || beforeChange.MathWidth != afterChange.MathWidth) { changeTypes |= RegionChangeTypes.Resize; } this.ChangeTypes = changeTypes; }
private static bool IsEditAllowed(IReadOnlyRegion region, Span span, ITextSnapshot textSnapshot) { return(new ReadOnlySpan(textSnapshot.Version, region).IsReplaceAllowed(span, textSnapshot)); }
private void RemoveReadOnlyRegionFromBuffer(DocumentId documentId, IReadOnlyRegion region) { AssertIsForeground(); var textBuffer = GetTextBuffer(_workspace, documentId); using (var readOnlyEdit = textBuffer.CreateReadOnlyRegionEdit()) { readOnlyEdit.RemoveReadOnlyRegion(region); readOnlyEdit.Apply(); } }
public ReplSpan(ITrackingSpan span, ReplSpanKind kind, IReadOnlyRegion readOnlyRegion) { Span = span; Kind = kind; ReadOnlyRegion = readOnlyRegion; }
public Region(IReadOnlyRegion region) { this.topLeft = region.TopLeft; this.bottomRight = region.BottomRight; region.OnChanged += (obj, e) => { RegionChangeTypes changeType; if (e.AfterChange.TopLeft == this.topLeft) { if (e.AfterChange.BottomRight == this.bottomRight) { return; } else { changeType = RegionChangeTypes.Resize; } } else { Coord newSize = new Coord(e.AfterChange.Width, e.AfterChange.Height); Coord currentSize = new Coord(this.Width, this.Height); Coord delta = currentSize - newSize; if (delta.X == 0 && delta.Y == 0) { changeType = RegionChangeTypes.Move; } else { changeType = RegionChangeTypes.Move | RegionChangeTypes.Resize; } } RegionChangeEventArgs args = new RegionChangeEventArgs( this, e.AfterChange, changeType); this.OnChangeRequested?.Invoke(obj, args); if (args.Canceled) { return; } Region oldRegion = this.Clone(); this.topLeft = e.AfterChange.TopLeft; this.bottomRight = e.AfterChange.BottomRight; if (this != oldRegion) { this.OnChanged?.Invoke( this, new RegionChangedEventArgs( oldRegion, this, changeType)); } }; }
internal ReadOnlySpan(ITextVersion version, IReadOnlyRegion readOnlyRegion) : base(version, readOnlyRegion.Span.GetSpan(version), readOnlyRegion.Span.TrackingMode) { _startEdgeInsertionMode = readOnlyRegion.EdgeInsertionMode; _endEdgeInsertionMode = readOnlyRegion.EdgeInsertionMode; }
/// <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(); } }
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(); }
private static bool IsEditAllowed(IReadOnlyRegion region, int position, ITextSnapshot textSnapshot) { return(new ReadOnlySpan(textSnapshot.Version, region).IsInsertAllowed(position, textSnapshot)); }
/// <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; } } }
/// <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> /// Removes a ReadOnlyRegion and clears the reference (set to null). /// </summary> public static void ClearReadOnlyRegion(this IReadOnlyRegionEdit readOnlyRegionEdit, ref IReadOnlyRegion readOnlyRegion) { if (readOnlyRegion != null) { readOnlyRegionEdit.RemoveReadOnlyRegion(readOnlyRegion); readOnlyRegion = null; } }