public bool Insert(Range range, string text, bool addNewline) { if (text.Length == 0) { return true; } bool success = true; string fileContents = File.ReadAllText(_filename); using (StringReader reader = new StringReader(fileContents)) using (TextWriter writer = new StreamWriter(File.Open(_filename, FileMode.Create))) { string lineText; if (SeekTo(reader, writer, range, out lineText)) { writer.WriteLine(lineText.Substring(0, range.LineRange.Start) + text + (addNewline ? Environment.NewLine : string.Empty) + lineText.Substring(range.LineRange.Start)); } lineText = reader.ReadLine(); while (lineText != null) { writer.WriteLine(lineText); lineText = reader.ReadLine(); } } return success; }
public bool Delete(Range range) { if (range.LineRange.Length == 0) { return true; } bool success = true; string fileContents = File.ReadAllText(_filename); using (StringReader reader = new StringReader(fileContents)) using (TextWriter writer = new StreamWriter(File.Open(_filename, FileMode.Create))) { string lineText; if (SeekTo(reader, writer, range, out lineText)) { writer.WriteLine(lineText.Substring(0, range.LineRange.Start) + lineText.Substring(range.LineRange.Start + range.LineRange.Length)); } lineText = reader.ReadLine(); while (lineText != null) { writer.WriteLine(lineText); lineText = reader.ReadLine(); } } return success; }
public bool Insert(Range position, string text, bool addNewline) { try { GetEditPointForRange(position)?.Insert(text + (addNewline ? Environment.NewLine : string.Empty)); return true; } catch { return false; } }
public bool Delete(Range range) { try { GetEditPointForRange(range)?.Delete(range.LineRange.Length); return true; } catch { return false; } }
private EditPoint GetEditPointForRange(Range range) { IVsTextLines textLines; int hr = _view.GetBuffer(out textLines); if (hr != VSConstants.S_OK || textLines == null) { return null; } object editPointObject; hr = textLines.CreateEditPoint(range.LineNumber, range.LineRange.Start, out editPointObject); EditPoint editPoint = editPointObject as EditPoint; if (hr != VSConstants.S_OK || editPoint == null) { return null; } return editPoint; }
private bool SeekTo(StringReader reader, TextWriter writer, Range range, out string lineText) { bool success = true; for (int lineNumber = 0; lineNumber < range.LineNumber; ++lineNumber) { string line = reader.ReadLine(); if (line != null) { writer.WriteLine(line); } else { success = false; break; } } lineText = reader.ReadLine(); if (success) { if (lineText != null) { if (lineText.Length < range.LineRange.Start) { success = false; writer.WriteLine(lineText); } } } return success; }