void InsertString(ICSharpCode.TextEditor.TextArea textArea, int offset, string str) { textArea.Document.Insert(offset, str); //textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.Document, // textArea.Document.OffsetToPosition(offset), // textArea.Document.OffsetToPosition(offset + str.Length))); textArea.Caret.Position = textArea.Document.OffsetToPosition(offset + str.Length); textArea.Refresh(); }
void DoubleClickSelectionExtend() { textArea.SelectionManager.ClearSelection(); if (textArea.TextView.DrawingPosition.Contains(mousepos.X, mousepos.Y)) { FoldMarker marker = textArea.TextView.GetFoldMarkerFromPosition(mousepos.X - textArea.TextView.DrawingPosition.X, mousepos.Y - textArea.TextView.DrawingPosition.Y); if (marker != null && marker.IsFolded) { marker.IsFolded = false; textArea.MotherTextAreaControl.AdjustScrollBars(null, null); } if (textArea.Caret.Offset < textArea.Document.TextLength) { switch (textArea.Document.GetCharAt(textArea.Caret.Offset)) { case '"': if (textArea.Caret.Offset < textArea.Document.TextLength) { int next = FindNext(textArea.Document, textArea.Caret.Offset + 1, '"'); minSelection = textArea.Caret.Position; maxSelection = textArea.Document.OffsetToPosition(next > textArea.Caret.Offset ? next + 1 : next); } break; default: minSelection = textArea.Document.OffsetToPosition(FindWordStart(textArea.Document, textArea.Caret.Offset)); maxSelection = textArea.Document.OffsetToPosition(FindWordEnd(textArea.Document, textArea.Caret.Offset)); break; } textArea.SelectionManager.ExtendSelection(minSelection, maxSelection); } // HACK WARNING !!! // must refresh here, because when a error tooltip is showed and the underlined // code is double clicked the textArea don't update corrctly, updateline doesn't // work ... but the refresh does. // Mike textArea.Refresh(); } }
private void MarkRegisterUsage(string word, IDocument document, TextArea text) { if (_formatter == null) return; var hadRegisterMarks = _registerMarkers.Count > 0; if (hadRegisterMarks) { foreach(var m in _registerMarkers) document.MarkerStrategy.RemoveMarker(m); } if (word == null || !Regex.IsMatch(word, "^[rp][0-9]+$")) { if (hadRegisterMarks) text.Refresh(); return; } foreach (var line in document.LineSegmentCollection) foreach (var w in line.Words) { if (w.Word != word) continue; var marker = new TextMarker(line.Offset + w.Offset, w.Length, TextMarkerType.SolidBlock, Color.LightSalmon); document.MarkerStrategy.AddMarker(marker); _registerMarkers.Add(marker); } if (_registerMarkers.Count > 0 || hadRegisterMarks) { text.Refresh(); } }
private void MarkBranchTargets(string word, string lineText, TextLocation pos, IDocument document, TextArea text) { if (_branchMarkers.Count > 0) { foreach(var marker in _branchMarkers) document.MarkerStrategy.RemoveMarker(marker); _branchMarkers.Clear(); text.Refresh(); } if (word == null) return; bool couldBeJump = Regex.IsMatch(word, "^[0-9a-fA-F]{3,4}$"); if (!couldBeJump) return; int wordIdx; GetWordAtColumn(lineText, pos.Column, out wordIdx); bool isFirstWord = lineText.IndexOf(word, StringComparison.Ordinal) == wordIdx; bool isJumpInstruction = false; if (!isFirstWord) { var words = GetWordAndPrevious(lineText, pos.Column); isJumpInstruction = words != null && words[0] == MethodDisassembly.JumpMarker; } if (!isFirstWord && !isJumpInstruction) return; HashSet<string> exceptionTargetOffsets = new HashSet<string>(); if (isFirstWord) { int offset = int.Parse(word, NumberStyles.HexNumber); foreach (var ex in _methodDef.Body.Exceptions) { if (offset >= ex.TryStart.Offset && offset <= ex.TryEnd.Offset) { foreach(var c in ex.Catches) exceptionTargetOffsets.Add(MethodDisassembly.FormatOffset(c.Instruction.Offset).Trim()); if(ex.CatchAll != null) exceptionTargetOffsets.Add(MethodDisassembly.FormatOffset(ex.CatchAll.Offset).Trim()); } } } LineSegment mainLine = null; int jumpMarkerLen = MethodDisassembly.JumpMarker.Length; // jump target? foreach (var line in document.LineSegmentCollection) { bool isLineFirstWord = true; string curLine = document.GetText(line); int curOffset = 0; foreach (var curWord in SplitAndKeep(curLine, SplitCharacters)) { if (curWord.Trim() == "") { curOffset += curWord.Length; continue; } // mark all words matching the jump instruction if (curWord == word) { if (isLineFirstWord && mainLine == null) { mainLine = line; } else { // add marker. if (curOffset > 4 && curLine.Substring(curOffset - jumpMarkerLen - 1, jumpMarkerLen) == MethodDisassembly.JumpMarker) { AddBranchMarker(document, line.Offset + curOffset - jumpMarkerLen - 1, curWord.Length + jumpMarkerLen + 1); } else { AddBranchMarker(document, line.Offset + curOffset, curWord.Length); } } } else if (/*isLineFirstWord &&*/ exceptionTargetOffsets.Contains(curWord)) { if (!isLineFirstWord) { AddBranchMarker(document, line.Offset + curOffset, curWord.Length, true); } else { AddBranchMarker(document, line.Offset, line.TotalLength, true); } } curOffset += curWord.Length; isLineFirstWord = false; } } if (mainLine != null && _branchMarkers.Count > 0) { // better would be to mark the whole line, not only the words. AddBranchMarker(document, mainLine.Offset, mainLine.TotalLength); } if(_branchMarkers.Count > 0) text.Refresh(); }
/// <summary> /// Insert a string of text into the specified text area at the current /// cursor location. /// </summary> /// <param name="textArea">The text area to use</param> /// <param name="text">The text to insert</param> public static void InsertString(TextArea textArea, string text) { int offset = textArea.Caret.Offset; textArea.BeginUpdate(); try { textArea.Document.UndoStack.StartUndoGroup(); // If inserted in a selection, replace the selection. // Otherwise, clear it. if(textArea.SelectionManager.HasSomethingSelected) if(textArea.SelectionManager.SelectionCollection[0].ContainsOffset(offset)) { offset = textArea.SelectionManager.SelectionCollection[0].Offset; textArea.SelectionManager.RemoveSelectedText(); } else textArea.SelectionManager.ClearSelection(); textArea.Document.Insert(offset, text); textArea.Caret.Position = textArea.Document.OffsetToPosition(offset + text.Length); textArea.Refresh(); textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea)); } finally { textArea.Document.UndoStack.EndUndoGroup(); textArea.EndUpdate(); } }
void DoubleClickSelectionExtend() { Point mousepos; mousepos = textArea.mousepos; textArea.SelectionManager.ClearSelection(); if (textArea.TextView.DrawingPosition.Contains(mousepos.X, mousepos.Y)) { FoldMarker marker = textArea.TextView.GetFoldMarkerFromPosition(mousepos.X - textArea.TextView.DrawingPosition.X, mousepos.Y - textArea.TextView.DrawingPosition.Y); if (marker != null && marker.IsFolded) { marker.IsFolded = false; textArea.MotherTextAreaControl.AdjustScrollBars(); } if (textArea.Caret.Offset < textArea.Document.TextLength) { switch (textArea.Document.GetCharAt(textArea.Caret.Offset)) { case '"': if (textArea.Caret.Offset < textArea.Document.TextLength) { int next = FindNext(textArea.Document, textArea.Caret.Offset + 1, '"'); minSelection = textArea.Caret.Position; if (next > textArea.Caret.Offset && next < textArea.Document.TextLength) { next += 1; } maxSelection = textArea.Document.OffsetToPosition(next); } break; default: minSelection = textArea.Document.OffsetToPosition(FindWordStart(textArea.Document, textArea.Caret.Offset)); maxSelection = textArea.Document.OffsetToPosition(FindWordEnd(textArea.Document, textArea.Caret.Offset)); break; } textArea.Caret.Position = maxSelection; textArea.SelectionManager.ExtendSelection(minSelection, maxSelection); } if (textArea.SelectionManager.selectionCollection.Count > 0) { ISelection selection = textArea.SelectionManager.selectionCollection[0]; selection.StartPosition = minSelection; selection.EndPosition = maxSelection; textArea.SelectionManager.SelectionStart = minSelection; } // after a double-click selection, the caret is placed correctly, // but it is not positioned internally. The effect is when the cursor // is moved up or down a line, the caret will take on the column first // clicked on for the double-click textArea.SetDesiredColumn(); // HACK WARNING !!! // must refresh here, because when a error tooltip is showed and the underlined // code is double clicked the textArea don't update corrctly, updateline doesn't // work ... but the refresh does. // Mike textArea.Refresh(); } }
void SetText(TextArea textArea, string resourceName, string oldText) { // ensure caret is at start of selection textArea.Caret.Position = textArea.SelectionManager.SelectionCollection[0].StartPosition; // deselect text textArea.SelectionManager.ClearSelection(); // replace the selected text with the new text: // Replace() takes the arguments: start offset to replace, length of the text to remove, new text textArea.Document.Replace(textArea.Caret.Offset, oldText.Length, "$" + "{res:" + resourceName + "}"); // Redraw: textArea.Refresh(); }
public override bool InsertAction(TextArea textArea, char ch) { ClassFinder context = new ClassFinder(ParserService.GetParseInformation(textArea.MotherTextEditorControl.FileName), textArea.Caret.Line + 1, textArea.Caret.Column + 1); int caretPosition = textArea.Caret.Offset; LineSegment line = textArea.Document.GetLineSegment(textArea.Caret.Line); string lineText = textArea.Document.GetText(line.Offset, caretPosition - line.Offset); foreach (char c in lineText) { if (!char.IsWhiteSpace(c) && !char.IsLetterOrDigit(c)) { return base.InsertAction(textArea, ch); } } string indentation = lineText.Substring(0, lineText.Length - lineText.TrimStart().Length); CodeGenerator codeGen = ParserService.CurrentProjectContent.Language.CodeGenerator; string text = codeGen.GenerateCode(codeGen.GetOverridingMethod(member, context), indentation); text = text.TrimEnd(); // remove newline from end textArea.Document.Replace(line.Offset, caretPosition - line.Offset, text); int endPos = line.Offset + text.Length; int endLine = textArea.Document.GetLineNumberForOffset(endPos); line = textArea.Document.GetLineSegment(endLine); textArea.MotherTextAreaControl.JumpTo(endLine, endPos - line.Offset); textArea.Refresh(); return true; }
void InsertString(ICSharpCode.TextEditor.TextArea textArea, int offset, string str) { textArea.Document.Insert(offset, str); textArea.Caret.Position = textArea.Document.OffsetToPosition(offset + str.Length); textArea.Refresh(); }
void FormatLineInternal(TextArea textArea, int lineNr, int cursorOffset, char ch) { LineSegment curLine = textArea.Document.GetLineSegment(lineNr); LineSegment lineAbove = lineNr > 0 ? textArea.Document.GetLineSegment(lineNr - 1) : null; string terminator = textArea.TextEditorProperties.LineTerminator; //// local string for curLine segment string curLineText=""; if (ch == '/') { curLineText = textArea.Document.GetText(curLine); string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove); if (curLineText != null && curLineText.EndsWith("///") && (lineAboveText == null || !lineAboveText.Trim().StartsWith("///"))) { string indentation = base.GetIndentation(textArea, lineNr); object member = GetMemberAfter(textArea, lineNr); if (member != null) { StringBuilder sb = new StringBuilder(); sb.Append(" <summary>"); sb.Append(terminator); sb.Append(indentation); sb.Append("/// "); sb.Append(terminator); sb.Append(indentation); sb.Append("/// </summary>"); if (member is IMethod) { IMethod method = (IMethod)member; if (method.Parameters != null && method.Parameters.Count > 0) { for (int i = 0; i < method.Parameters.Count; ++i) { sb.Append(terminator); sb.Append(indentation); sb.Append("/// <param name=\""); sb.Append(method.Parameters[i].Name); sb.Append("\"></param>"); } } if (method.ReturnType != null && !method.IsConstructor && method.ReturnType.FullyQualifiedName != "System.Void") { sb.Append(terminator); sb.Append(indentation); sb.Append("/// <returns></returns>"); } } textArea.Document.Insert(cursorOffset, sb.ToString()); textArea.Refresh(); textArea.Caret.Position = textArea.Document.OffsetToPosition(cursorOffset + indentation.Length + "/// ".Length + " <summary>".Length + terminator.Length); } } return; } if (ch != '\n' && ch != '>') { if (IsInsideStringOrComment(textArea, curLine, cursorOffset)) { return; } } switch (ch) { case '>': if (IsInsideDocumentationComment(textArea, curLine, cursorOffset)) { curLineText = textArea.Document.GetText(curLine); int column = textArea.Caret.Offset - curLine.Offset; int index = Math.Min(column - 1, curLineText.Length - 1); while (index >= 0 && curLineText[index] != '<') { --index; if(curLineText[index] == '/') return; // the tag was an end tag or already } if (index > 0) { StringBuilder commentBuilder = new StringBuilder(""); for (int i = index; i < curLineText.Length && i < column && !Char.IsWhiteSpace(curLineText[i]); ++i) { commentBuilder.Append(curLineText[ i]); } string tag = commentBuilder.ToString().Trim(); if (!tag.EndsWith(">")) { tag += ">"; } if (!tag.StartsWith("/")) { textArea.Document.Insert(textArea.Caret.Offset, "</" + tag.Substring(1)); } } } break; case ':': case ')': case ']': case '}': case '{': if (textArea.Document.TextEditorProperties.IndentStyle == IndentStyle.Smart) { textArea.Document.FormattingStrategy.IndentLine(textArea, lineNr); } break; case '\n': string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove); //// curLine might have some text which should be added to indentation curLineText = ""; if (curLine.Length > 0) { curLineText = textArea.Document.GetText(curLine); } LineSegment nextLine = lineNr + 1 < textArea.Document.TotalNumberOfLines ? textArea.Document.GetLineSegment(lineNr + 1) : null; string nextLineText = lineNr + 1 < textArea.Document.TotalNumberOfLines ? textArea.Document.GetText(nextLine) : ""; int addCursorOffset = 0; if (lineAboveText.Trim().StartsWith("#region") && NeedEndregion(textArea.Document)) { textArea.Document.Insert(curLine.Offset, "#endregion"); textArea.Caret.Column = IndentLine(textArea, lineNr); return; } if (lineAbove.HighlightSpanStack != null && !lineAbove.HighlightSpanStack.IsEmpty) { if (!lineAbove.HighlightSpanStack.Peek().StopEOL) { // case for /* style comments int index = lineAboveText.IndexOf("/*"); if (index > 0) { StringBuilder indentation = new StringBuilder(GetIndentation(textArea, lineNr - 1)); for (int i = indentation.Length; i < index; ++ i) { indentation.Append(' '); } //// adding curline text textArea.Document.Replace(curLine.Offset, curLine.Length, indentation.ToString() + " * " + curLineText); textArea.Caret.Column = indentation.Length + 3 + curLineText.Length; return; } index = lineAboveText.IndexOf("*"); if (index > 0) { StringBuilder indentation = new StringBuilder(GetIndentation(textArea, lineNr - 1)); for (int i = indentation.Length; i < index; ++ i) { indentation.Append(' '); } //// adding curline if present textArea.Document.Replace(curLine.Offset, curLine.Length, indentation.ToString() + "* " + curLineText); textArea.Caret.Column = indentation.Length + 2 + curLineText.Length; return; } } else { // don't handle // lines, because they're only one lined comments int indexAbove = lineAboveText.IndexOf("///"); int indexNext = nextLineText.IndexOf("///"); if (indexAbove > 0 && (indexNext != -1 || indexAbove + 4 < lineAbove.Length)) { StringBuilder indentation = new StringBuilder(GetIndentation(textArea, lineNr - 1)); for (int i = indentation.Length; i < indexAbove; ++ i) { indentation.Append(' '); } //// adding curline text if present textArea.Document.Replace(curLine.Offset, curLine.Length, indentation.ToString() + "/// " + curLineText); textArea.Caret.Column = indentation.Length + 4; return; } if (IsInNonVerbatimString(lineAboveText, curLineText)) { textArea.Document.Insert(lineAbove.Offset + lineAbove.Length, "\" +"); curLine = textArea.Document.GetLineSegment(lineNr); textArea.Document.Insert(curLine.Offset, "\""); addCursorOffset = 1; } } } int result = IndentLine(textArea, lineNr) + addCursorOffset; if (textArea.TextEditorProperties.AutoInsertCurlyBracket) { string oldLineText = TextUtilities.GetLineAsString(textArea.Document, lineNr - 1); if (oldLineText.EndsWith("{")) { if (NeedCurlyBracket(textArea.Document.TextContent)) { textArea.Document.Insert(curLine.Offset + curLine.Length, terminator + "}"); IndentLine(textArea, lineNr + 1); } } } textArea.Caret.Column = result; return; } }