Created
March 26, 2013 20:05
-
-
Save gusano/5248700 to your computer and use it in GitHub Desktop.
First try at implementing scide auto-insertion of matching parentheses, brackets and quotes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From f697c99198f1c4d600fb487ff3d149a4ddbd468f Mon Sep 17 00:00:00 2001 | |
From: Yvan Volochine <[email protected]> | |
Date: Tue, 26 Mar 2013 13:49:34 -0500 | |
Subject: [PATCH] Scide: first implementation of parentheses, brackets, quotes | |
auto-pairing | |
Signed-off-by: Yvan Volochine <[email protected]> | |
--- | |
editors/sc-ide/widgets/code_editor/sc_editor.cpp | 53 ++++++++++++++++++++++++ | |
editors/sc-ide/widgets/code_editor/sc_editor.hpp | 1 + | |
2 files changed, 54 insertions(+) | |
diff --git a/editors/sc-ide/widgets/code_editor/sc_editor.cpp b/editors/sc-ide/widgets/code_editor/sc_editor.cpp | |
index 7cd6bb8..2357311 100644 | |
--- a/editors/sc-ide/widgets/code_editor/sc_editor.cpp | |
+++ b/editors/sc-ide/widgets/code_editor/sc_editor.cpp | |
@@ -114,6 +114,15 @@ void ScCodeEditor::keyPressEvent( QKeyEvent *e ) | |
} | |
switch (e->key()) { | |
+ // auto-pair ', ", (, {, [ | |
+ case Qt::Key_Apostrophe: | |
+ case Qt::Key_BraceLeft: | |
+ case Qt::Key_BracketLeft: | |
+ case Qt::Key_ParenLeft: | |
+ case Qt::Key_QuoteDbl: { | |
+ keyAutoPair(e); | |
+ return; | |
+ } | |
case Qt::Key_Home: | |
{ | |
Qt::KeyboardModifiers mods(e->modifiers()); | |
@@ -184,6 +193,50 @@ void ScCodeEditor::keyPressEvent( QKeyEvent *e ) | |
mAutoCompleter->keyPress(e); | |
} | |
+void ScCodeEditor::keyAutoPair( QKeyEvent *e) | |
+{ | |
+ QString key = e->text(); | |
+ QString nextKey = e->text(); | |
+ | |
+ switch (e->key()) { | |
+ case Qt::Key_BraceLeft: { | |
+ nextKey = "}"; | |
+ break; | |
+ } | |
+ case Qt::Key_BracketLeft: { | |
+ nextKey = "]"; | |
+ break; | |
+ } | |
+ case Qt::Key_ParenLeft: { | |
+ nextKey = ")"; | |
+ break; | |
+ } | |
+ default: | |
+ break; | |
+ } | |
+ | |
+ QTextCursor cursor = textCursor(); | |
+ if (cursor.hasSelection()) { | |
+ int start = cursor.selectionStart(); | |
+ int end = cursor.selectionEnd(); | |
+ | |
+ cursor.beginEditBlock(); | |
+ cursor.setPosition(start); | |
+ cursor.insertText(key); | |
+ cursor.setPosition(end + 1); | |
+ cursor.insertText(nextKey); | |
+ cursor.endEditBlock(); | |
+ } else { | |
+ GenericCodeEditor::keyPressEvent(e); | |
+ cursor.insertText(nextKey); | |
+ cursor.movePosition(QTextCursor::PreviousCharacter); | |
+ } | |
+ | |
+ setTextCursor(cursor); | |
+ | |
+ mAutoCompleter->keyPress(e); | |
+} | |
+ | |
void ScCodeEditor::mouseReleaseEvent ( QMouseEvent *e ) | |
{ | |
// Prevent deselection of bracket match: | |
diff --git a/editors/sc-ide/widgets/code_editor/sc_editor.hpp b/editors/sc-ide/widgets/code_editor/sc_editor.hpp | |
index 48070ea..95a18bd 100644 | |
--- a/editors/sc-ide/widgets/code_editor/sc_editor.hpp | |
+++ b/editors/sc-ide/widgets/code_editor/sc_editor.hpp | |
@@ -77,6 +77,7 @@ class ScCodeEditor : public GenericCodeEditor | |
virtual void dragEnterEvent( QDragEnterEvent * ); | |
virtual bool canInsertFromMimeData ( const QMimeData * data ) const; | |
virtual void insertFromMimeData ( const QMimeData * data ); | |
+ virtual void keyAutoPair( QKeyEvent * ); | |
private slots: | |
void matchBrackets(); | |
-- | |
1.8.1.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment