/* TextEditor::onUpdateUI * Called when anything is modified in the text editor (cursor * position, styling, text, etc) *******************************************************************/ void TextEditor::onUpdateUI(wxStyledTextEvent& e) { // Check for brace match if (txed_brace_match) checkBraceMatch(); // If a calltip is open, update it if (call_tip->IsShown()) updateCalltip(); // Do word matching if appropriate if (txed_match_cursor_word && language) { int word_start = WordStartPosition(GetCurrentPos(), true); int word_end = WordEndPosition(GetCurrentPos(), true); string current_word = GetTextRange(word_start, word_end); if (!current_word.IsEmpty() && HasFocus()) { if (current_word != prev_word_match) { prev_word_match = current_word; SetIndicatorCurrent(8); IndicatorClearRange(0, GetTextLength()); SetTargetStart(0); SetTargetEnd(GetTextLength()); SetSearchFlags(0); while (SearchInTarget(current_word) != -1) { IndicatorFillRange(GetTargetStart(), GetTargetEnd() - GetTargetStart()); SetTargetStart(GetTargetEnd()); SetTargetEnd(GetTextLength()); } } } else { SetIndicatorCurrent(8); IndicatorClearRange(0, GetTextLength()); prev_word_match = ""; } } // Hilight current line MarkerDeleteAll(1); MarkerDeleteAll(2); if (txed_hilight_current_line > 0 && HasFocus()) { int line = LineFromPosition(GetCurrentPos()); MarkerAdd(line, 1); if (txed_hilight_current_line > 1) MarkerAdd(line, 2); } e.Skip(); }
/* TextEditor::replaceCurrent * Replaces the currently selected occurrence of [find] with * [replace], then selects and scrolls to the next occurrence of * [find] in the text. Returns false if [find] is invalid or the * current selection does not match it, true otherwise *******************************************************************/ bool TextEditor::replaceCurrent(string find, string replace) { // Check search string if (find.IsEmpty()) return false; // Check that we've done a find previously // (by searching for the find string within the current selection) if (GetSelectedText().Length() != find.Length()) return false; SetTargetStart(GetSelectionStart()); SetTargetEnd(GetSelectionEnd()); if (SearchInTarget(find) < 0) return false; // Do the replace ReplaceTarget(replace); // Update selection SetSelection(GetTargetStart(), GetTargetEnd()); // Do find next findNext(find); return true; }
/* TextEditor::findNext * Finds the next occurrence of the [find] after the caret position, * selects it and scrolls to it if needed. Returns false if the * [find] was invalid or no match was found, true otherwise *******************************************************************/ bool TextEditor::findNext(string find) { // Check search string if (find.IsEmpty()) return false; // Setup target range SetTargetEnd(GetTextLength()); SetTargetStart(GetSelectionEnd()); // Search within current target range if (SearchInTarget(find) < 0) { // None found, search again from start SetTargetStart(0); SetTargetEnd(GetTextLength()); if (SearchInTarget(find) < 0) { // No matches found in entire text return false; } } // Select matched text SetSelection(GetTargetStart(), GetTargetEnd()); // Scroll to selection EnsureCaretVisible(); return true; }
void CodeEdit::CommentSelection(const wxString& comment) { if (GetSelectionStart() < GetSelectionEnd()) { int startLine = LineFromPosition(GetSelectionStart()); int endLine = LineFromPosition(GetSelectionEnd()); // Find the minimum indentation level for all of the selected lines. int minIndentation = INT_MAX; for (int i = startLine; i <= endLine; ++i) { wxString lineText = GetLine(i); int firstNonWhitespace = GetFirstNonWhitespace(lineText); if (firstNonWhitespace != -1) { minIndentation = wxMin(firstNonWhitespace, minIndentation); } } // Insert the comment string on each non-blank line. wxString result; for (int i = startLine; i <= endLine; ++i) { wxString lineText = GetLine(i); if (GetFirstNonWhitespace(lineText) != -1) { lineText.insert(minIndentation, comment); } result += lineText; } SetTargetStart(PositionFromLine(startLine)); SetTargetEnd(PositionFromLine(endLine + 1)); ReplaceTarget(result); // Select the replaced text. SetSelectionStart(GetTargetStart()); SetSelectionEnd(GetTargetEnd() - 1); } }
void CodeEdit::UncommentSelection(const wxString& commentString) { if (GetSelectionStart() < GetSelectionEnd()) { int startLine = LineFromPosition(GetSelectionStart()); int endLine = LineFromPosition(GetSelectionEnd()); wxString result; for (int i = startLine; i <= endLine; ++i) { wxString lineText = GetLine(i); unsigned int c = GetFirstNonWhitespace(lineText); if (c != -1 && lineText.compare(c, commentString.Length(), commentString) == 0) { lineText.erase(c, commentString.Length()); } result += lineText; } SetTargetStart(PositionFromLine(startLine)); SetTargetEnd(PositionFromLine(endLine + 1)); ReplaceTarget(result); // Select the replaced text. SetSelectionStart(GetTargetStart()); SetSelectionEnd(GetTargetEnd() - 1); } }