diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp
index 3ba2967a9b078ff4844b7ae8941180400935f853..8f9b4075db359a81b0c714778bedfb3fd37fba3d 100644
--- a/src/plugins/cppeditor/cppeditor.cpp
+++ b/src/plugins/cppeditor/cppeditor.cpp
@@ -1268,6 +1268,13 @@ bool CPPEditor::isElectricCharacter(const QChar &ch) const
     return false;
 }
 
+bool CPPEditor::contextAllowsAutoParenthesis(const QTextCursor &cursor) const
+{
+    CPlusPlus::TokenUnderCursor tokenUnderCursor;
+    const SimpleToken tk = tokenUnderCursor(cursor);
+    return !(tk.isComment() || tk.isLiteral());
+}
+
 void CPPEditor::indentInsertedText(const QTextCursor &tc)
 {
     indent(tc.document(), tc, QChar::Null);
diff --git a/src/plugins/cppeditor/cppeditor.h b/src/plugins/cppeditor/cppeditor.h
index 40e2e71972f22517d492875c3892b727b66a76ae..224033d182ea619d60d4f46c0ea5d788d67ca10b 100644
--- a/src/plugins/cppeditor/cppeditor.h
+++ b/src/plugins/cppeditor/cppeditor.h
@@ -211,8 +211,9 @@ protected:
 
     TextEditor::BaseTextEditorEditable *createEditableInterface();
 
-    // Rertuns true if key triggers anindent.
-    virtual bool isElectricCharacter(const QChar &ch) const;
+    // These override BaseTextEditor
+    bool isElectricCharacter(const QChar &ch) const;
+    bool contextAllowsAutoParenthesis(const QTextCursor &cursor) const;
 
 private Q_SLOTS:
     void updateFileName();
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index d5d6e2d5261b1c7e902323f06177e85145542947..b766e672118ba7981c90a1f4a10128fd6722b49b 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -1073,9 +1073,8 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
         QString text = e->text();
         QString autoText;
 
-        // TODO disable this inside string or character literals
         if (d->m_autoParenthesesEnabled && d->m_document->tabSettings().m_autoParentheses) {
-            foreach(QChar c, text) {
+            foreach (QChar c, text) {
                 QChar close;
                 if (c == QLatin1Char('('))
                     close = QLatin1Char(')');
@@ -1101,7 +1100,7 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
         }
         QChar electricChar;
         if (d->m_document->tabSettings().m_autoIndent) {
-            foreach(QChar c, text) {
+            foreach (QChar c, text) {
                 if (isElectricCharacter(c)) {
                     electricChar = c;
                     break;
@@ -1110,8 +1109,11 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
         }
         if (!electricChar.isNull())
             cursor.beginEditBlock();
+
+        bool insertAutoParenthesis = !autoText.isEmpty() && contextAllowsAutoParenthesis(cursor);
         cursor.insertText(text);
-        if (!autoText.isEmpty()) {
+
+        if (insertAutoParenthesis) {
             int pos = cursor.position();
             cursor.insertText(autoText);
             cursor.setPosition(pos);
@@ -3316,6 +3318,11 @@ bool BaseTextEditor::isElectricCharacter(const QChar &) const
     return false;
 }
 
+bool BaseTextEditor::contextAllowsAutoParenthesis(const QTextCursor &) const
+{
+    return true;
+}
+
 void BaseTextEditor::indentBlock(QTextDocument *, QTextBlock, QChar)
 {
 }
diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h
index 691365cecd219c3cf060e9de12c079c5e866e341..6477ab71e1dd9db2e427386f507f0599dacac3d4 100644
--- a/src/plugins/texteditor/basetexteditor.h
+++ b/src/plugins/texteditor/basetexteditor.h
@@ -508,6 +508,8 @@ protected:
 
     // Returns true if key triggers an indent.
     virtual bool isElectricCharacter(const QChar &ch) const;
+    // Returns true if automatic brace matching should be enabled in the context of the given cursor
+    virtual bool contextAllowsAutoParenthesis(const QTextCursor &cursor) const;
     // Indent a text block based on previous line. Default does nothing
     virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar);
     // Indent at cursor. Calls indentBlock for selection or current line.