public void Insert(EditorInsertMessage message) { var tempFile = Path.GetTempFileName(); using (var reader = new StreamReader(message.Destination.File)) { using (var writer = new StreamWriter(tempFile)) { long lineNr = 0; string line; while ((line = reader.ReadLine()) != null) { lineNr++; if (lineNr != 1) writer.Write(Environment.NewLine); if (lineNr == message.Destination.Line) { var prefix = ""; for (int i = 0; i < message.Destination.Column; i++) prefix += line[i]; writer.Write(prefix); writer.Write(message.Text); writer.Write(line.Substring(prefix.Length, line.Length - prefix.Length)); } else writer.Write(line); } } } File.Delete(message.Destination.File); File.Move(tempFile, message.Destination.File); }
public void Should_send_insertion_to_editor_if_it_can_handle_it() { _editor.Stub(x => x.IsAlive).Return(true); _editor.Stub(x => x.CanInsertFor("tofile.txt")).Return(true); var msg = new EditorInsertMessage("inectfile.txt", new GoTo() { File = "tofile.txt" }, null); _dispatcher.Consume(msg); _editor.AssertWasCalled(method => method.Insert(msg)); }
public void Insert(EditorInsertMessage message) { }
public void Insert(EditorInsertMessage message) { GoTo(new Location( message.Destination.File, message.Destination.Line, message.Destination.Column)); var location = getLocation(); if (location == null) return; var newline = "\\n"; if (Environment.OSVersion.Platform != PlatformID.Unix && Environment.OSVersion.Platform != PlatformID.MacOSX) newline = "\\r\\n"; var content = runFunction("{0}:getText", location.Buffer.ID); if (content == null) return; var lines = content.Split(new[] { newline }, StringSplitOptions.None); if (lines.Length < location.Line) { Logger.Write("Asked for line {0} but document only contained {1} lines", location.Line, lines.Length); return; } var line = lines[location.Line - 1]; if (line.Length < location.Column) { Logger.Write("Asked for column {0} but line was only {1} chars long", location.Column, line); return; } var insertColumn = location.Column; // + ((message.Destination.Column - 1) - location.Column); var lineModified = line.Substring(0, insertColumn) + message.Text .Replace(Environment.NewLine, newline) .Replace("\"", "\\\"") + line.Substring(insertColumn, line.Length - insertColumn); var length = line.Length; var lastLine = location.Line != lines.Length - 1; if (lastLine) length += newline.Length; send("{0}:remove/0 {1} {2}", location.Buffer.ID, location.Offset - location.Column, length - 1); send("{0}:insert/0 {1} \"{2}\"", location.Buffer.ID, location.Offset - location.Column, lineModified); if (message.MoveOffset != null) { var offsetLocation = new Location( message.Destination.File, message.Destination.Line, message.Destination.Column); offsetLocation.Add(message.MoveOffset); GoTo(offsetLocation); } else { GoTo(new Location( message.Destination.File, message.Destination.Line, message.Destination.Column + message.Text.Length)); } }
// Insert text into a file public void Insert(EditorInsertMessage msg) { Publisher.Run( string.Format( "test-editor insert {0} \"{1}|{2}|{3}\" \"{4}|{5}\"", toMD5(msg.Text), msg.Destination.File, msg.Destination.Line, msg.Destination.Column, msg.MoveOffset.Line, msg.MoveOffset.Column)); }
public void When_asked_to_insert_a_piece_of_text_after_the_end_of_the_file_it_wont_be_inserted() { File.WriteAllText(_content, "(inserted text here)"); var message = new EditorInsertMessage( "(inserted text here)", new GoTo() { File = _file, Line = 4, Column = 17 }, null); _writer.Insert(message); Assert.That( File.ReadAllText(_file), Is.EqualTo( "some text is already" + Environment.NewLine + "in here but will be modified" + Environment.NewLine + "by these lovelly commands")); }
public void Insert(EditorInsertMessage msg) { var message = string.Format( "insert \"{0}\" \"{1}\" {2} {3} {4}", msg.Text .Replace(Environment.NewLine, "||newline||") .Replace("\"", "\\\""), msg.Destination.File, msg.Destination.Line, msg.Destination.Column, msg.MoveOffset); send(message); }
public void Insert(EditorInsertMessage message) { var origin = getLocation(); GoTo(new Location( message.Destination.File, message.Destination.Line, message.Destination.Column)); var location = getLocation(); if (location == null) return; var newline = "\\n"; if (Environment.OSVersion.Platform != PlatformID.Unix && Environment.OSVersion.Platform != PlatformID.MacOSX) newline = "\\r\\n"; var content = getText(location.Buffer.ID); if (content == null) return; var lines = content.Split(new[] { Environment.NewLine }, StringSplitOptions.None); if (lines.Length < location.Line) { Logger.Write("Asked for line {0} but document only contained {1} lines", location.Line, lines.Length); return; } var line = lines[location.Line - 1]; if (line.Length < location.Column) { Logger.Write("Asked for column {0} but line was only {1} chars long", location.Column, line); return; } Logger.Write("Line is: " + line); var removeLength = line.Length - message.Destination.Column + 1; if (removeLength > line.Length) removeLength = line.Length; var insertColumn = location.Column; // + ((message.Destination.Column - 1) - location.Column); var textModified = message.Text .Replace("\\", "\\\\") .Replace("\"", "\\\"") .Replace(Environment.NewLine, newline) + line.Substring(insertColumn, removeLength); //send("{0}:remove/0 {1} {2}", // location.Buffer.ID, // location.Offset - location.Column, // length - 1); //send("{0}:insert/0 {1} \"{2}\"", // location.Buffer.ID, // location.Offset - location.Column, // lineModified); send("{0}:remove/0 {1} {2}", location.Buffer.ID, location.Offset, removeLength); send("{0}:insert/0 {1} \"{2}\"", location.Buffer.ID, location.Offset, textModified); if (message.MoveOffset != null) { var offsetLocation = new Location( message.Destination.File, message.Destination.Line, message.Destination.Column); offsetLocation.Add(message.MoveOffset); GoTo(offsetLocation); } else { if (origin == null) return; var originAdjusted = origin.Line; if (origin.Line > message.Destination.Line) { originAdjusted = origin.Line + message.Text.Split(new[] {Environment.NewLine}, StringSplitOptions.None).Length; } GoTo(new Location( origin.Buffer.Fullpath, origin.Line, origin.Column)); } }
public void Insert(EditorInsertMessage msg) { var message = string.Format( "insert \"{0}\" \"{1}\" {2} {3} {4}", msg.Text, msg.Destination.File, msg.Destination.Line, msg.Destination.Column, msg.MoveOffset); send(message); }