public Document(string filePath, bool needSaveAs) { this.needSaveAs = needSaveAs; if (!File.Exists(filePath)) throw new FileNotFoundException("\"{0}\" does not exist.".F(filePath), filePath); try { foreach (string line in GetFileContent(filePath)) lines.Add(new Line(line, LineModification.Clean, this)); } catch { throw new FileNotFoundException("\"{0}\" could not be opened.".F(filePath), filePath); } point = new Caret(this); mark = new Caret(this); this.filePath = filePath; this.fileType = Config.ChooseFileType(filePath); if (lines.Count == 0) lines.Add(new Line("", LineModification.Clean, this)); lastModified = File.GetLastWriteTime(filePath); }
public void ReplaceText(string newText, bool indent) { var before = Caret.Order(point, mark); BeforeReplace(Selection); if (before.Second != before.First) { if (before.Second.Line > before.First.Line + 1) { lines.RemoveRange(before.First.Line + 1, before.Second.Line - before.First.Line - 1); before.Second.Line = before.First.Line + 1; } while (before.Second > before.First) before.Second.DeleteLeft(); Mark = Point = new Caret(before.First); } point.Insert(newText, indent); AfterReplace(Selection); }
internal Caret(Caret other) : this(other.document, other.line, other.column) { }
public static Pair<Caret, Caret> Order(Caret a, Caret b) { return new Pair<Caret, Caret>(Min(a, b), Max(a, b)); }
public static Caret Min(Caret l, Caret r) { return l > r ? r : l; }
public static Caret Max(Caret l, Caret r) { return l > r ? l : r; }
public bool Contains(Caret c) { return Start <= c && End >= c; }
public Region(Caret start, Caret end) { Start = new Caret(Caret.Min(start, end)); End = new Caret(Caret.Max(start, end)); }
public void MovePoint(Direction direction) { switch (direction) { case Direction.Left: point.MoveLeft(); break; case Direction.Right: point.MoveRight(); break; case Direction.Up: point.MoveVertically( -1 ); break; case Direction.Down: point.MoveVertically( 1 ); break; case Direction.DownLineStart: point.MoveDownStart( 1 ); break; case Direction.LineStart: point.MoveHome(); break; case Direction.LineEnd: point.MoveEnd(); break; case Direction.AbsoluteLineStart: point.MoveHome(); if (point.Column > 0) point.MoveHome(); break; case Direction.DocumentStart: point = Caret.AtStartOfDocument(this); break; case Direction.DocumentEnd: point = Caret.AtEndOfLine(this, lines.Count - 1); break; } }