Ejemplo n.º 1
0
        public bool Delete(Range range)
        {
            if (range.LineRange.Length == 0)
            {
                return 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 true;
        }
Ejemplo n.º 2
0
        public bool Insert(Range range, string text, bool addNewline)
        {
            if (text.Length == 0)
            {
                return 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 true;
        }
 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;
     }
 }
Ejemplo n.º 5
0
        public bool Delete(Range range)
        {
            if (range.LineRange.Length == 0)
            {
                return true;
            }

            string fileContents = File.ReadAllText(_filename);

            using (StringReader reader = new StringReader(fileContents))
            using (TextWriter writer = new StreamWriter(File.Open(_filename, FileMode.Create)))
            {
                int remainingCharacters = range.LineRange.Length;
                int currentStart = range.LineRange.Start;
                string lineText;
                while (remainingCharacters > 0 && SeekTo(reader, writer, range, out lineText))
                {
                    int trimFromLine = Math.Min(lineText.Length - currentStart, remainingCharacters);
                    writer.WriteLine(lineText.Substring(0, currentStart) + lineText.Substring(currentStart + trimFromLine));
                    remainingCharacters -= trimFromLine;
                    range.LineNumber = 0;
                    range.LineRange.Start = 0;
                    range.LineRange.Length = remainingCharacters;
                    currentStart = 0;
                }

                lineText = reader.ReadLine();

                while (lineText != null)
                {
                    writer.WriteLine(lineText);
                    lineText = reader.ReadLine();
                }
            }

            return true;
        }
Ejemplo n.º 6
0
        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;
        }
        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;
        }