public static void DuplicateLine(TextEditorData data) { using (var undoGroup = data.OpenUndoGroup()) { if (data.IsSomethingSelected) { var selectedText = data.SelectedText; data.ClearSelection(); data.InsertAtCaret(selectedText); } else { DocumentLine line = data.Document.GetLine(data.Caret.Line); if (line == null) { return; } if (line.DelimiterLength == 0) { data.Insert(line.Offset, data.GetTextAt(line.SegmentIncludingDelimiter) + data.EolMarker); } else { data.Insert(line.Offset, data.GetTextAt(line.SegmentIncludingDelimiter)); } } } }
public static void DuplicateLine (TextEditorData data) { DocumentLine line = data.Document.GetLine (data.Caret.Line); if (line == null) return; data.Insert (line.Offset, data.GetTextAt (line.SegmentIncludingDelimiter)); }
public static void DuplicateLine(TextEditorData data) { if (data.IsSomethingSelected) { data.InsertAtCaret(data.SelectedText); } else { DocumentLine line = data.Document.GetLine(data.Caret.Line); if (line == null) { return; } data.Insert(line.Offset, data.GetTextAt(line.SegmentIncludingDelimiter)); } }
void CopyData (TextEditorData data, Selection selection) { copiedDocument = null; monoDocument = null; if (selection != null && data != null && data.Document != null) { copiedDocument = new TextDocument (); monoDocument = new TextDocument (); this.docStyle = data.ColorStyle; this.options = data.Options; this.mode = SyntaxModeService.GetSyntaxMode (monoDocument, data.MimeType); switch (selection.SelectionMode) { case SelectionMode.Normal: isBlockMode = false; var segment = selection.GetSelectionRange (data); var text = data.GetTextAt (segment); copiedDocument.Text = text; monoDocument.Text = text; var line = data.Document.GetLineByOffset (segment.Offset); var spanStack = line.StartSpan.Clone (); SyntaxModeService.ScanSpans (data.Document, this.mode as SyntaxMode, this.mode as SyntaxMode, spanStack, line.Offset, segment.Offset); this.copiedDocument.GetLine (DocumentLocation.MinLine).StartSpan = spanStack; break; case SelectionMode.Block: isBlockMode = true; DocumentLocation visStart = data.LogicalToVisualLocation (selection.Anchor); DocumentLocation visEnd = data.LogicalToVisualLocation (selection.Lead); int startCol = System.Math.Min (visStart.Column, visEnd.Column); int endCol = System.Math.Max (visStart.Column, visEnd.Column); for (int lineNr = selection.MinLine; lineNr <= selection.MaxLine; lineNr++) { DocumentLine curLine = data.Document.GetLine (lineNr); int col1 = curLine.GetLogicalColumn (data, startCol) - 1; int col2 = System.Math.Min (curLine.GetLogicalColumn (data, endCol) - 1, curLine.Length); if (col1 < col2) { copiedDocument.Insert (copiedDocument.TextLength, data.Document.GetTextAt (curLine.Offset + col1, col2 - col1)); monoDocument.Insert (monoDocument.TextLength, data.Document.GetTextAt (curLine.Offset + col1, col2 - col1)); } if (lineNr < selection.MaxLine) { // Clipboard line end needs to be system dependend and not the document one. copiedDocument.Insert (copiedDocument.TextLength, Environment.NewLine); // \r in mono document stands for block selection line end. monoDocument.Insert (monoDocument.TextLength, "\r"); } } line = data.Document.GetLine (selection.MinLine); spanStack = line.StartSpan.Clone (); SyntaxModeService.ScanSpans (data.Document, this.mode as SyntaxMode, this.mode as SyntaxMode, spanStack, line.Offset, line.Offset + startCol); this.copiedDocument.GetLine (DocumentLocation.MinLine).StartSpan = spanStack; break; } } else { copiedDocument = null; } }
public static bool FixLineStart (TextEditorData textEditorData, DocumentStateTracker<CSharpIndentEngine> stateTracker, int lineNumber) { if (lineNumber > DocumentLocation.MinLine) { DocumentLine line = textEditorData.Document.GetLine (lineNumber); if (line == null) return false; DocumentLine prevLine = textEditorData.Document.GetLine (lineNumber - 1); if (prevLine == null) return false; string trimmedPreviousLine = textEditorData.Document.GetTextAt (prevLine).TrimStart (); //xml doc comments //check previous line was a doc comment //check there's a following line? if (trimmedPreviousLine.StartsWith ("///", StringComparison.Ordinal)) { if (textEditorData.GetTextAt (line.Offset, line.Length).TrimStart ().StartsWith ("///", StringComparison.Ordinal)) return false; //check that the newline command actually inserted a newline textEditorData.EnsureCaretIsNotVirtual (); string nextLine = textEditorData.Document.GetTextAt (textEditorData.Document.GetLine (lineNumber + 1)).TrimStart (); if (trimmedPreviousLine.Length > "///".Length || nextLine.StartsWith ("///", StringComparison.Ordinal)) { var insertionPoint = line.Offset + line.GetIndentation (textEditorData.Document).Length; textEditorData.Insert (insertionPoint, "/// "); return true; } //multi-line comments } else if (stateTracker.Engine.IsInsideMultiLineComment) { if (textEditorData.GetTextAt (line.Offset, line.Length).TrimStart ().StartsWith ("*", StringComparison.Ordinal)) return false; textEditorData.EnsureCaretIsNotVirtual (); string commentPrefix = string.Empty; if (trimmedPreviousLine.StartsWith ("* ", StringComparison.Ordinal)) { commentPrefix = "* "; } else if (trimmedPreviousLine.StartsWith ("/**", StringComparison.Ordinal) || trimmedPreviousLine.StartsWith ("/*", StringComparison.Ordinal)) { commentPrefix = " * "; } else if (trimmedPreviousLine.StartsWith ("*", StringComparison.Ordinal)) { commentPrefix = "*"; } int indentSize = line.GetIndentation (textEditorData.Document).Length; var insertedText = prevLine.GetIndentation (textEditorData.Document) + commentPrefix; textEditorData.Replace (line.Offset, indentSize, insertedText); textEditorData.Caret.Offset = line.Offset + insertedText.Length; return true; } else if (stateTracker.Engine.IsInsideStringLiteral) { var lexer = new CSharpCompletionEngineBase.MiniLexer (textEditorData.Document.GetTextAt (0, prevLine.EndOffset)); lexer.Parse (); if (!lexer.IsInString) return false; textEditorData.EnsureCaretIsNotVirtual (); textEditorData.Insert (prevLine.Offset + prevLine.Length, "\" +"); int indentSize = line.GetIndentation (textEditorData.Document).Length; var insertedText = prevLine.GetIndentation (textEditorData.Document) + (trimmedPreviousLine.StartsWith ("\"", StringComparison.Ordinal) ? "" : "\t") + "\""; textEditorData.Replace (line.Offset, indentSize, insertedText); return true; } } return false; }
static void ConvertVerbatimStringToNormal (TextEditorData textEditorData, int offset) { var endOffset = offset; while (endOffset < textEditorData.Length) { char ch = textEditorData.GetCharAt (endOffset); if (ch == '"' && (endOffset + 1 < textEditorData.Length && textEditorData.GetCharAt (endOffset + 1) == '"')) { endOffset += 2; continue; } if (ch == '"') { break; } endOffset++; } var plainText = TextPasteUtils.VerbatimStringStrategy.Decode (textEditorData.GetTextAt (offset, endOffset - offset)); var newText = TextPasteUtils.StringLiteralPasteStrategy.Instance.Encode (plainText); textEditorData.Replace (offset, endOffset - offset, newText); }
static void ConvertNormalToVerbatimString (TextEditorData textEditorData, int offset) { var endOffset = offset; while (endOffset < textEditorData.Length) { char ch = textEditorData.GetCharAt (endOffset); if (ch == '\\') { if (endOffset + 1 < textEditorData.Length && NewLine.IsNewLine (textEditorData.GetCharAt (endOffset + 1))) return; endOffset += 2; continue; } if (ch == '"') break; if (NewLine.IsNewLine (ch)) return; endOffset++; } if (offset > endOffset || endOffset == textEditorData.Length) return; var plainText = TextPasteUtils.StringLiteralPasteStrategy.Instance.Decode (textEditorData.GetTextAt (offset, endOffset - offset)); var newText = TextPasteUtils.VerbatimStringStrategy.Encode (plainText); textEditorData.Replace (offset, endOffset - offset, newText); }
void CopyData(TextEditorData data, Selection selection) { copiedDocument = null; monoDocument = null; if (selection != null && data != null && data.Document != null) { copiedDocument = new TextDocument(); monoDocument = new TextDocument(); this.docStyle = data.ColorStyle; this.options = data.Options; this.mode = SyntaxModeService.GetSyntaxMode(monoDocument, data.MimeType); switch (selection.SelectionMode) { case SelectionMode.Normal: isBlockMode = false; var segment = selection.GetSelectionRange(data); var text = data.GetTextAt(segment); copiedDocument.Text = text; monoDocument.Text = text; var line = data.Document.GetLineByOffset(segment.Offset); var spanStack = line.StartSpan.Clone(); SyntaxModeService.ScanSpans(data.Document, this.mode as SyntaxMode, this.mode as SyntaxMode, spanStack, line.Offset, segment.Offset); this.copiedDocument.GetLine(DocumentLocation.MinLine).StartSpan = spanStack; break; case SelectionMode.Block: isBlockMode = true; DocumentLocation visStart = data.LogicalToVisualLocation(selection.Anchor); DocumentLocation visEnd = data.LogicalToVisualLocation(selection.Lead); int startCol = System.Math.Min(visStart.Column, visEnd.Column); int endCol = System.Math.Max(visStart.Column, visEnd.Column); for (int lineNr = selection.MinLine; lineNr <= selection.MaxLine; lineNr++) { DocumentLine curLine = data.Document.GetLine(lineNr); int col1 = curLine.GetLogicalColumn(data, startCol) - 1; int col2 = System.Math.Min(curLine.GetLogicalColumn(data, endCol) - 1, curLine.Length); if (col1 < col2) { copiedDocument.Insert(copiedDocument.TextLength, data.Document.GetTextAt(curLine.Offset + col1, col2 - col1)); monoDocument.Insert(monoDocument.TextLength, data.Document.GetTextAt(curLine.Offset + col1, col2 - col1)); } if (lineNr < selection.MaxLine) { // Clipboard line end needs to be system dependend and not the document one. copiedDocument.Insert(copiedDocument.TextLength, Environment.NewLine); // \r in mono document stands for block selection line end. monoDocument.Insert(monoDocument.TextLength, "\r"); } } line = data.Document.GetLine(selection.MinLine); spanStack = line.StartSpan.Clone(); SyntaxModeService.ScanSpans(data.Document, this.mode as SyntaxMode, this.mode as SyntaxMode, spanStack, line.Offset, line.Offset + startCol); this.copiedDocument.GetLine(DocumentLocation.MinLine).StartSpan = spanStack; break; } } else { copiedDocument = null; } }
public static void DuplicateLine (TextEditorData data) { if (data.IsSomethingSelected) { var selectedText = data.SelectedText; data.ClearSelection (); data.InsertAtCaret (selectedText); } else { DocumentLine line = data.Document.GetLine (data.Caret.Line); if (line == null) return; data.Insert (line.Offset, data.GetTextAt (line.SegmentIncludingDelimiter)); } }
public static void DuplicateLine (TextEditorData data) { using (var undoGroup = data.OpenUndoGroup ()) { if (data.IsSomethingSelected) { var selectedText = data.SelectedText; data.ClearSelection (); data.InsertAtCaret (selectedText); } else { DocumentLine line = data.Document.GetLine (data.Caret.Line); if (line == null) return; if (line.DelimiterLength == 0) { data.Insert (line.Offset, data.GetTextAt (line.SegmentIncludingDelimiter) + data.EolMarker); } else { data.Insert (line.Offset, data.GetTextAt (line.SegmentIncludingDelimiter)); } } } }
void ConvertVerbatimStringToNormal (TextEditorData textEditorData, int offset) { var endOffset = offset; while (endOffset < textEditorData.Length) { char ch = textEditorData.GetCharAt (endOffset); if (ch == '"' && (endOffset + 1 < textEditorData.Length && textEditorData.GetCharAt (endOffset + 1) == '"')) { endOffset += 2; continue; } if (ch == '"') break; endOffset++; } textEditorData.Replace (offset, endOffset - offset, ConvertToStringLiteral (ConvertFromVerbatimString (textEditorData.GetTextAt (offset, endOffset - offset)))); }
public ExpressionContext FindExactContextForObjectInitializer (TextEditorData editor, ICompilationUnit unit, string fileName, IType callingType) { int caretOffset = editor.Caret.Offset; // create a table with all opening brackets Dictionary<int, int> brackets = new Dictionary<int, int> (); Stack<int> bracketStack = new Stack<int> (); for (int i = 0; i < caretOffset; i++) { char ch = editor.GetCharAt (i); switch (ch) { case '/': if (i + 1 < caretOffset) { if (editor.GetCharAt (i + 1) == '/') { while (i < caretOffset) { if (editor.GetCharAt (i) == '\n') break; i++; } } else if (editor.GetCharAt (i + 1) == '*') { while (i + 1 < caretOffset) { if (editor.GetCharAt (i) == '*' && editor.GetCharAt (i + 1) == '/') break; i++; } } } break; case '(': case '{': case '[': bracketStack.Push (i); break; case ')': case '}': case ']': if (bracketStack.Count > 0) brackets[i] = bracketStack.Pop (); break; } } bool foundCurlyBrace = false; for (int i = caretOffset - 1; i >= 0; i--) { char ch = editor.GetCharAt (i); if (ch == '{') { foundCurlyBrace = true; } else if (ch == ')' || ch == '}' || ch == ']') { int newPos; if (brackets.TryGetValue (i, out newPos)) { i = newPos; // we've had a Property = new Name (), expression, now search for the '=' // otherwise the "new Name" would be falsly taken as object initializer if (!foundCurlyBrace) { while (i >= 0) { if (editor.GetCharAt (i) == '=' || editor.GetCharAt (i) == ';') break; i--; } } continue; } } if (i + 4 < caretOffset && editor.GetTextAt (i, 4) == "new ") { bool skip = false; for (int j2 = i; j2 < caretOffset; j2++) { if (editor.GetCharAt (j2) == '{') break; if (editor.GetCharAt (j2) == ',') { skip = true; break; } } if (skip) continue; int j = i + 4; while (j < caretOffset && Char.IsWhiteSpace (editor.GetCharAt (j))) j++; // int start = j; while (j < caretOffset && (Char.IsLetterOrDigit (editor.GetCharAt (j)) || editor.GetCharAt (j) == '_' || editor.GetCharAt (j) == '.')) j++; ExpressionResult firstExprs = FindExpression (editor, j); if (firstExprs.Expression != null) { // skip ws while (j < caretOffset && Char.IsWhiteSpace (editor.GetCharAt (j))) j++; if (editor.GetCharAt (j) == '[') { StringBuilder expr = new StringBuilder (firstExprs.Expression); while (j < caretOffset) { ch = editor.GetCharAt (j); switch (ch) { case '[': case ',': case ']': expr.Append (ch); break; case ' ': case '\t': break; default: j = caretOffset; break; } j++; } firstExprs.Expression = expr.ToString (); } IReturnType unresolvedReturnType = NRefactoryResolver.ParseReturnType (firstExprs); if (unresolvedReturnType != null) { IType resolvedType = projectContent.SearchType (unit, (INode)callingType ?? unit, unresolvedReturnType); return ExpressionContext.TypeDerivingFrom (resolvedType != null ? new DomReturnType (resolvedType) : null, unresolvedReturnType, true); } } } } return null; }
public static bool GuessSemicolonInsertionOffset (TextEditorData data, ISegment curLine, int caretOffset, out int outOffset) { int lastNonWsOffset = caretOffset; char lastNonWsChar = '\0'; outOffset = caretOffset; int max = curLine.EndOffset; int end = caretOffset; while (end > 1 && char.IsWhiteSpace (data.GetCharAt (end))) end--; int end2 = end; while (end2 > 1 && char.IsLetter (data.GetCharAt (end2 - 1))) end2--; if (end != end2) { string token = data.GetTextBetween (end2, end + 1); // guess property context if (token == "get" || token == "set") return false; } var offset = curLine.Offset; string lineText = data.GetTextAt (caretOffset, max - caretOffset); var lexer = new CSharpCompletionEngineBase.MiniLexer (lineText); lexer.Parse ((ch, i) => { if (lexer.IsInSingleComment || lexer.IsInMultiLineComment) return true; if (ch == '}' && lexer.IsFistNonWs && !IsSemicolonalreadyPlaced (data, caretOffset)) { lastNonWsChar = ';'; return true; } if (!char.IsWhiteSpace (ch)) { lastNonWsOffset = caretOffset + i; lastNonWsChar = ch; } return false; }); // if the line ends with ';' the line end is not the correct place for a new semicolon. if (lastNonWsChar == ';') return false; outOffset = lastNonWsOffset; return true; }