public void NewLine() { ITextSnapshotLine line = CursorLine(); SnapshotPoint pos = line.EndIncludingLineBreak; string lineBreak = line.GetLineBreakText(); if (lineBreak.Length == 0) { lineBreak = "\n"; } textView.TextBuffer.Insert(pos.Position, lineBreak); }
public IEnumerable<TextLineCheckerError> CheckLine(ITextSnapshotLine line) { if (_chromiumSourceFiles.ApplyCodingStyle(line)) { var lineBreak = line.GetLineBreakText(); if (lineBreak.Length > 0 && lineBreak != "\n") { var fragment = line.GetFragment(line.End.Position - 1, line.EndIncludingLineBreak.Position, TextLineFragment.Options.IncludeLineBreak); yield return new TextLineCheckerError { Span = fragment.SnapshotSpan, Message = "Line breaks should be \"unix\" (i.e. LF) style only.", }; } } }
public IEnumerable <TextLineCheckerError> CheckLine(ITextSnapshotLine line) { if (_chromiumSourceFiles.ApplyCodingStyle(line)) { var lineBreak = line.GetLineBreakText(); if (lineBreak.Length > 0 && lineBreak != "\n") { var fragment = line.GetFragment(line.End.Position - 1, line.EndIncludingLineBreak.Position, TextLineFragment.Options.IncludeLineBreak); yield return(new TextLineCheckerError { Span = fragment.SnapshotSpan, Message = "Line breaks should be \"unix\" (i.e. LF) style only.", }); } } }
private string GetLineBreakTextFromPreviousLine(ITextBuffer textBuffer, int pos) { int currentLineNumber = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(pos); string lineBreakText = null; if (currentLineNumber > 0) { ITextSnapshotLine previousLine = textBuffer.CurrentSnapshot.GetLineFromLineNumber(currentLineNumber - 1); lineBreakText = previousLine.GetLineBreakText(); } if (string.IsNullOrEmpty(lineBreakText)) { lineBreakText = Environment.NewLine; } return(lineBreakText); }
/// <summary> /// Inserts comment at the given point. /// </summary> /// <param name="currentSnapshotLine">The <see cref="ITextSnapshotLine"/>.</param> /// <returns>True if successful; otherwise false.</returns> /// <remarks>The <see cref="ITextSnapshotLine"/>'s LineNumber is starting from 0.</remarks> private bool InsertCommentAtPoint(ITextSnapshotLine currentSnapshotLine) { // Data validation if (currentSnapshotLine == null || this.dte == null) { return(false); } // Get text selection to insert the comment var textSelection = this.dte.ActiveDocument.Selection as TextSelection; if (textSelection == null) { return(false); } // Initialize var currentLine = currentSnapshotLine.GetText(); var lineBreak = currentSnapshotLine.GetLineBreakText(); lineBreak = String.IsNullOrEmpty(lineBreak) ? "\r\n" : lineBreak; var lineNum = textSelection.ActivePoint.Line; var offset = textSelection.ActivePoint.LineCharOffset; // Move to the end of next line textSelection.LineDown(); textSelection.EndOfLine(); // Update the indent var indent = this.GetIndention(textSelection.CurrentLine); // Get code element var fileCodeModel = this.dte.ActiveDocument.ProjectItem.FileCodeModel; CodeElement codeElement = fileCodeModel == null ? null : fileCodeModel.CodeElementFromPoint(textSelection.ActivePoint, vsCMElement.vsCMElementFunction); // Add comments for function if (codeElement != null && codeElement is CodeFunction) { // Get function element CodeFunction function = codeElement as CodeFunction; // Don't generate comment if inside a function body var startPoint = function.GetStartPoint(); if (startPoint != null && startPoint.Line <= lineNum) { textSelection.MoveToLineAndOffset(lineNum, offset); return(false); } // Generate comment for function StringBuilder sb = new StringBuilder(indent + "/// <summary>" + lineBreak + indent + "/// " + lineBreak + indent + "/// </summary>"); foreach (CodeElement child in codeElement.Children) { CodeParameter parameter = child as CodeParameter; if (parameter != null) { sb.AppendFormat("{0}{1}/// <param name=\"{2}\"></param>", lineBreak, indent, parameter.Name); } } // If there is the return type is not void, generate a returns element if (function.Type.AsString != "void") { sb.AppendFormat("{0}{1}/// <returns></returns>", lineBreak, indent); } // Move to summary element textSelection.MoveToLineAndOffset(lineNum, offset); if (offset > 1) { textSelection.DeleteLeft(offset - 1); } textSelection.Insert(sb.ToString()); textSelection.MoveToLineAndOffset(lineNum + 1, offset); textSelection.EndOfLine(); return(true); } // Add comment for tshe top level function declaration else if (codeElement == null && this.TryAddCommentForTopLevelFunctionDeclaration(textSelection, lineBreak, indent, lineNum, offset)) { return(true); } // TODO(acmore): Check if it's inside a function // Add summary comment else { // Generate a summary element textSelection.MoveToLineAndOffset(lineNum, offset); if (offset > 1) { textSelection.DeleteLeft(offset - 1); } textSelection.Insert(String.Format("{0}/// <summary>{0}{1}/// {0}{1}/// </summary>", indent, lineBreak, indent)); // Move to summary element textSelection.MoveToLineAndOffset(lineNum + 1, offset); textSelection.EndOfLine(); return(true); } }