public override bool InsertNewLine() { string lineBreak = null; if (_editorOptions.GetReplicateNewLineCharacter()) { ITextSnapshot snapshot = _textBuffer.AdvancedTextBuffer.CurrentSnapshot; int position = _trackingPoint.GetPosition(snapshot); ITextSnapshotLine currentSnapshotLine = snapshot.GetLineFromPosition(position); if (currentSnapshotLine.LineBreakLength > 0) { // use the same line ending as the current line lineBreak = currentSnapshotLine.GetLineBreakText(); } else { // we are on the last line of the buffer if (snapshot.LineCount > 1) { // use the same line ending as the penultimate line in the buffer lineBreak = snapshot.GetLineFromLineNumber(snapshot.LineCount - 2).GetLineBreakText(); } } } return(InsertText(lineBreak ?? _editorOptions.GetNewLineCharacter())); }
/// <summary> /// For a given <see cref="ITextSnapshotLine"/> gets the new line character to be inserted to the line based on /// either the given line, or the second last line or the default new line charcter provided by <see cref="IEditorOptions"/> /// </summary> /// <param name="line">The <see cref="ITextSnapshotLine"/> for whcih the new line character is to be decied for</param> /// <param name="editorOptions">The current set of <see cref="IEditorOptions"/> applicable for the given <see cref="ITextSnapshotLine"/></param> /// <returns>The new line character to be inserted</returns> public static string GetNewLineCharacterToInsert(ITextSnapshotLine line, IEditorOptions editorOptions) { string lineBreak = null; var snapshot = line.Snapshot; if (editorOptions.GetReplicateNewLineCharacter()) { if (line.LineBreakLength > 0) { // use the same line ending as the current line lineBreak = line.GetLineBreakText(); } else { if (snapshot.LineCount > 1) { // use the same line ending as the penultimate line in the buffer lineBreak = snapshot.GetLineFromLineNumber(snapshot.LineCount - 2).GetLineBreakText(); } } } string textToInsert = lineBreak ?? editorOptions.GetNewLineCharacter(); return(textToInsert); }
public static string GetLineBreak(this IEditorOptions editorOptions, SnapshotPoint pos) { if (editorOptions.GetReplicateNewLineCharacter()) { var line = pos.GetContainingLine(); if (line.LineBreakLength != 0) { return(pos.Snapshot.GetText(line.Extent.End.Position, line.LineBreakLength)); } if (line.LineNumber != 0) { line = pos.Snapshot.GetLineFromLineNumber(line.LineNumber - 1); return(pos.Snapshot.GetText(line.Extent.End.Position, line.LineBreakLength)); } } var linebreak = editorOptions.GetNewLineCharacter(); return(linebreak.Length != 0 ? linebreak : Environment.NewLine); }