/// <summary> /// Deletes a range of text /// </summary> /// <param name="Range">the range that should be deleted</param> public void DeleteRange(TextRange Range) { DeleteRange(Range, true); }
/// <summary> /// Deletes a range of text /// </summary> /// <param name="Range">Range to delete</param> /// <param name="StoreUndo">true if the actions should be pushed onto the undo stack</param> public void DeleteRange(TextRange Range, bool StoreUndo) { TextRange r = Range; Modified = true; if (StoreUndo) { string deltext = GetRange(Range); PushUndoBlock(UndoAction.DeleteRange, deltext, r.FirstColumn, r.FirstRow); } if (r.FirstRow == r.LastRow) { Row xtr = this[r.FirstRow]; int max = Math.Min(r.FirstColumn, xtr.Text.Length); string left = xtr.Text.Substring(0, max); string right = ""; if (xtr.Text.Length >= r.LastColumn) right = xtr.Text.Substring(r.LastColumn); xtr.Text = left + right; } else { if (r.LastRow > Count - 1) r.LastRow = Count - 1; Row xtr = this[r.FirstRow]; if (r.FirstColumn > xtr.Text.Length) { int diff = r.FirstColumn - xtr.Text.Length; var ws = new string(' ', diff); InsertText(ws, xtr.Text.Length, r.FirstRow, true); //return; } string row1 = xtr.Text.Substring(0, r.FirstColumn); Row xtr2 = this[r.LastRow]; int Max = Math.Min(xtr2.Text.Length, r.LastColumn); string row2 = xtr2.Text.Substring(Max); string tot = row1 + row2; //bool fold=this[r.LastRow].IsCollapsed | this[r.FirstRow].IsCollapsed ; int start = r.FirstRow; int end = r.LastRow; for (int i = end - 1; i >= start; i--) { Remove(i, false, false); } //todo: DeleteRange error //this.Insert ( tot ,r.FirstRow,false); Row row = this[start]; row.Expanded = true; row.Text = tot; row.startSpans.Clear(); row.endSpans.Clear(); row.startSpan = null; row.endSpan = null; row.Parse(); } ResetVisibleRows(); OnChange(); }
/// <summary> /// Gets a Range from a given text /// </summary> /// <param name="text"></param> /// <param name="xPos"></param> /// <param name="yPos"></param> /// <returns></returns> public TextRange GetRangeFromText(string text, int xPos, int yPos) { string t = text.Replace(Environment.NewLine, "\n"); string[] lines = t.Split("\n".ToCharArray()); var r = new TextRange { FirstColumn = xPos, FirstRow = yPos, LastRow = (lines.Length - 1 + yPos), LastColumn = lines[lines.Length - 1].Length }; if (r.FirstRow == r.LastRow) r.LastColumn += r.FirstColumn; return r; }
/// <summary> /// Remove a row at specified row index /// </summary> /// <param name="index">index of the row that should be removed</param> /// <param name="storeUndo">true if and undo action should be added to the undo stack</param> /// <param name="raiseChanged"></param> public void Remove(int index, bool storeUndo, bool raiseChanged) { Row r = this[index]; if (storeUndo) { var ra = new TextRange(); if (index != Count - 1) { ra.FirstColumn = 0; ra.FirstRow = index; ra.LastRow = index + 1; ra.LastColumn = 0; } else { ra.FirstColumn = r.PrevRow.Text.Length; ra.FirstRow = index - 1; ra.LastRow = index; ra.LastColumn = r.Text.Length; } PushUndoBlock(UndoAction.DeleteRange, GetRange(ra), ra.FirstColumn, ra.FirstRow); } rows.RemoveAt(index); if (r.InKeywordQueue) KeywordQueue.Remove(r); if (r.InQueue) ParseQueue.Remove(r); //this.ResetVisibleRows (); OnRowDeleted(r); if (raiseChanged) OnChange(); }
private void DeleteBackwards() { Caret.CropPosition(); if (Selection.IsValid) Selection.DeleteSelection(); else { Row xtr = Caret.CurrentRow; if (Caret.Position.X == 0) { if (Caret.Position.Y > 0) { Caret.Position.Y--; Caret.MoveEnd(false); DeleteForward(); //Caret.CurrentRow.Parse (); Document.ResetVisibleRows(); } } else { if (Caret.Position.X >= xtr.Text.Length) { var r = new TextRange {FirstColumn = (Caret.Position.X - 1), FirstRow = Caret.Position.Y}; r.LastRow = r.FirstRow; r.LastColumn = r.FirstColumn + 1; Document.DeleteRange(r); Document.ResetVisibleRows(); Caret.MoveEnd(false); Caret.CurrentRow.Parse(); } else { var r = new TextRange {FirstColumn = (Caret.Position.X - 1), FirstRow = Caret.Position.Y}; r.LastRow = r.FirstRow; r.LastColumn = r.FirstColumn + 1; Document.DeleteRange(r); Document.ResetVisibleRows(); Caret.MoveLeft(false); Caret.CurrentRow.Parse(); } } } }
/// <summary> /// Get a range of text /// </summary> /// <param name="Range">The range to get</param> /// <returns>string containing the text inside the given range</returns> public string GetRange(TextRange Range) { if (Range.FirstRow >= Count) Range.FirstRow = Count; if (Range.LastRow >= Count) Range.LastRow = Count; if (Range.FirstRow != Range.LastRow) { //note:error has been tracked here Row r1 = this[Range.FirstRow]; int mx = Math.Min(r1.Text.Length, Range.FirstColumn); string s1 = r1.Text.Substring(mx) + Environment.NewLine; //if (Range.LastRow >= this.Count) // Range.LastRow=this.Count -1; Row r2 = this[Range.LastRow]; if (r2 == null) return ""; int Max = Math.Min(r2.Text.Length, Range.LastColumn); string s2 = r2.Text.Substring(0, Max); var sb = new StringBuilder(); for (int i = Range.FirstRow + 1; i <= Range.LastRow - 1; i++) { Row r3 = this[i]; sb.Append(r3.Text + Environment.NewLine); } string s3 = sb.ToString(); return s1 + s3 + s2; } else { Row r = this[Range.FirstRow]; int Max = Math.Min(r.Text.Length, Range.LastColumn); int Length = Max - Range.FirstColumn; if (Length <= 0) return ""; string s = r.Text.Substring(Range.FirstColumn, Max - Range.FirstColumn); return s; } }
private void OutdentEndRow() { try { if (Indent == IndentStyle.Scope) { Row xtr = Caret.CurrentRow; var indent1 = new String('\t', Caret.CurrentRow.Depth); var tr = new TextRange { FirstColumn = 0, LastColumn = xtr.GetLeadingWhitespace().Length, FirstRow = xtr.Index, LastRow = xtr.Index }; Document.DeleteRange(tr); Document.InsertText(indent1, 0, xtr.Index, true); int diff = indent1.Length - tr.LastColumn; Caret.Position.X += diff; Caret.SetPos(Caret.Position); Caret.CropPosition(); Selection.ClearSelection(); Caret.CurrentRow.Parse(false); Caret.CurrentRow.Parse(true); } else if (Indent == IndentStyle.Smart) { Row xtr = Caret.CurrentRow; if (xtr.FirstNonWsWord == xtr.expansion_EndSpan.EndWord) { //int j=xtr.Expansion_StartRow.StartWordIndex; string indent1 = xtr.startSpan.StartWord.Row.GetVirtualLeadingWhitespace(); var tr = new TextRange { FirstColumn = 0, LastColumn = xtr.GetLeadingWhitespace().Length, FirstRow = xtr.Index, LastRow = xtr.Index }; Document.DeleteRange(tr); string ts = "\t" + new String(' ', TabSize); while (indent1.IndexOf(ts) >= 0) { indent1 = indent1.Replace(ts, "\t\t"); } Document.InsertText(indent1, 0, xtr.Index, true); int diff = indent1.Length - tr.LastColumn; Caret.Position.X += diff; Caret.SetPos(Caret.Position); Caret.CropPosition(); Selection.ClearSelection(); Caret.CurrentRow.Parse(false); Caret.CurrentRow.Parse(true); } } } catch {} }
private void DeleteForward() { Caret.CropPosition(); if (Selection.IsValid) Selection.DeleteSelection(); else { Row xtr = Caret.CurrentRow; if (Caret.Position.X == xtr.Text.Length) { if (Caret.Position.Y <= Document.Count - 2) { var r = new TextRange {FirstColumn = Caret.Position.X, FirstRow = Caret.Position.Y}; r.LastRow = r.FirstRow + 1; r.LastColumn = 0; Document.DeleteRange(r); Document.ResetVisibleRows(); } } else { var r = new TextRange {FirstColumn = Caret.Position.X, FirstRow = Caret.Position.Y}; r.LastRow = r.FirstRow; r.LastColumn = r.FirstColumn + 1; Document.DeleteRange(r); Document.ResetVisibleRows(); Caret.CurrentRow.Parse(false); Caret.CurrentRow.Parse(true); } } }
public void InsertAutolistText() { var tr = new TextRange { FirstRow = Caret.Position.Y, LastRow = Caret.Position.Y, FirstColumn = AutoListStartPos.X, LastColumn = Caret.Position.X }; Document.DeleteRange(tr, true); Caret.Position.X = AutoListStartPos.X; InsertText(AutoList.SelectedText); SetFocus(); }
private void InsertText(string text) { Caret.CropPosition(); if (Selection.IsValid) { Selection.DeleteSelection(); InsertText(text); } else { if (!_OverWrite || text.Length > 1) { TextPoint p = Document.InsertText(text, Caret.Position.X, Caret.Position.Y); Caret.CurrentRow.Parse(true); if (text.Length == 1) { Caret.SetPos(p); Caret.CaretMoved(false); } else { //Document.i = true; Document.ResetVisibleRows(); Caret.SetPos(p); Caret.CaretMoved(false); } } else { var r = new TextRange { FirstColumn = Caret.Position.X, FirstRow = Caret.Position.Y, LastColumn = (Caret.Position.X + 1), LastRow = Caret.Position.Y }; var ag = new UndoBlockCollection(); var b = new UndoBlock { Action = UndoAction.DeleteRange, Text = Document.GetRange(r), Position = Caret.Position }; ag.Add(b); Document.DeleteRange(r, false); b = new UndoBlock { Action = UndoAction.InsertRange }; string NewChar = text; b.Text = NewChar; b.Position = Caret.Position; ag.Add(b); Document.AddToUndoList(ag); Document.InsertText(NewChar, Caret.Position.X, Caret.Position.Y, false); Caret.CurrentRow.Parse(true); Caret.MoveRight(false); } } // this.ScrollIntoView (); }
public void InsertAutolistText() { string text = AutoList.SelectedText; var tr = new TextRange { FirstRow = Caret.Position.Y, LastRow = Caret.Position.Y, FirstColumn = AutoListStartPos.X, LastColumn = Caret.Position.X }; // Vic says - why do we delete range here? // It seems to cause bugs. Document.DeleteRange(tr, true); //Caret.Position.X = AutoListStartPos.X; InsertText(text); SetFocus(); }
/// <summary> /// Assigns a new text to the row. /// </summary> /// <param name="text"></param> public void SetText(string text) { Document.StartUndoCapture(); var tp = new TextPoint(0, Index); var tr = new TextRange {FirstColumn = 0, FirstRow = tp.Y, LastColumn = Text.Length, LastRow = tp.Y}; Document.StartUndoCapture(); //delete the current line Document.PushUndoBlock(UndoAction.DeleteRange, Document.GetRange(tr), tr.FirstColumn, tr.FirstRow); //alter the text Document.PushUndoBlock(UndoAction.InsertRange, text, tp.X, tp.Y); Text = text; Document.EndUndoCapture(); Document.InvokeChange(); }
/// <summary> /// Selection Constructor. /// </summary> /// <param name="control">Control that will use this selection</param> public Selection(EditViewControl control) { Control = control; Bounds = new TextRange(); }
/// <summary> /// Assigns a new text to the row. /// </summary> /// <param name="text"></param> public void SetText(string Text) { this.Document.StartUndoCapture(); TextPoint tp = new TextPoint(0, this.Index); TextRange tr = new TextRange(); tr.FirstColumn = 0; tr.FirstRow = tp.Y; tr.LastColumn = this.Text.Length; tr.LastRow = tp.Y; this.Document.StartUndoCapture(); //delete the current line this.Document.PushUndoBlock(UndoAction.DeleteRange, this.Document.GetRange(tr), tr.FirstColumn, tr.FirstRow, this.RevisionMark); //alter the text this.Document.PushUndoBlock(UndoAction.InsertRange, Text, tp.X, tp.Y, this.RevisionMark); this.Text = Text; this.Document.EndUndoCapture(); this.Document.InvokeChange(); }
public void InsertAutolistText() { int caretx = 0; TextRange tr = new TextRange(); tr.FirstRow = Caret.Position.Y; tr.LastRow = Caret.Position.Y; if (Caret.CurrentWord != null && Caret.CurrentWord.Type == WordType.Word) { tr.FirstColumn = Caret.CurrentWord.Column; tr.LastColumn = Caret.CurrentWord.Column + Caret.CurrentWord.Text.Length; caretx = Caret.CurrentWord.Column + AutoList.SelectedText.Length; } else { if (AutoListStartPos == null) return; tr.FirstColumn = AutoListStartPos.X; tr.LastColumn = Caret.Position.X; caretx = tr.FirstColumn + AutoList.SelectedText.Length + 1; } Document.DeleteRange(tr, true); Caret.Position.X = caretx; this.InsertText(AutoList.SelectedText); if (AutolistTextInserted != null) AutolistTextInserted(AutoList.SelectedText); SetFocus(); }