@@ -74,57 +74,67 @@ QString CodeEditor::getFileExtension()
74
74
return QFileInfo (filePath).suffix ().toLower ();
75
75
}
76
76
77
- void CodeEditor::addLanguageSymbol (QTextCursor &cursor, const QString commentSymbol)
77
+ void CodeEditor::addLanguageSymbol (QTextCursor &cursor, const QString & commentSymbol)
78
78
{
79
- // If text is selected, comment/uncomment selected text
80
79
if (cursor.hasSelection ())
81
80
{
82
- int start = cursor.selectionStart ();
83
- int end = cursor.selectionEnd ();
84
-
85
- cursor.setPosition (start);
86
- int startBlockNumber = cursor.blockNumber ();
87
- cursor.setPosition (end);
88
- int endBlockNumber = cursor.blockNumber ();
89
-
90
- cursor.setPosition (start);
91
- for (int i = startBlockNumber; i <= endBlockNumber; ++i)
92
- {
93
- cursor.movePosition (QTextCursor::StartOfLine);
94
- QString lineText = cursor.block ().text ();
95
-
96
- if (lineText.startsWith (commentSymbol))
97
- {
98
- cursor.movePosition (QTextCursor::Right, QTextCursor::KeepAnchor, 3 );
99
- cursor.removeSelectedText ();
100
- }
101
- else
102
- {
103
- cursor.insertText (commentSymbol + " " );
104
- }
105
-
106
- cursor.movePosition (QTextCursor::NextBlock);
107
- }
81
+ commentSelection (cursor, commentSymbol);
108
82
}
109
83
else
110
84
{
111
- // If no text is selected, comment/uncomment the current line
112
- cursor.select (QTextCursor::LineUnderCursor);
113
- QString lineText = cursor.selectedText ();
85
+ commentLine (cursor, commentSymbol);
86
+ }
87
+ }
88
+
89
+ // Comment/uncomment the selected text or the current line
90
+ void CodeEditor::commentSelection (QTextCursor &cursor, const QString &commentSymbol)
91
+ {
92
+ int start = cursor.selectionStart ();
93
+ int end = cursor.selectionEnd ();
94
+
95
+ cursor.setPosition (start);
96
+ int startBlockNumber = cursor.blockNumber ();
97
+ cursor.setPosition (end);
98
+ int endBlockNumber = cursor.blockNumber ();
99
+
100
+ cursor.setPosition (start);
101
+ for (int i = startBlockNumber; i <= endBlockNumber; ++i)
102
+ {
103
+ cursor.movePosition (QTextCursor::StartOfLine);
104
+ QString lineText = cursor.block ().text ();
114
105
115
106
if (lineText.startsWith (commentSymbol))
116
107
{
117
- lineText.remove (0 , 3 );
108
+ cursor.movePosition (QTextCursor::Right, QTextCursor::KeepAnchor, 3 );
109
+ cursor.removeSelectedText ();
118
110
}
119
111
else
120
112
{
121
- lineText. prepend (commentSymbol + " " );
113
+ cursor. insertText (commentSymbol + " " );
122
114
}
123
115
124
- cursor.insertText (lineText );
116
+ cursor.movePosition (QTextCursor::NextBlock );
125
117
}
126
118
}
127
119
120
+ // Comment/uncomment the single current line
121
+ void CodeEditor::commentLine (QTextCursor &cursor, const QString &commentSymbol)
122
+ {
123
+ cursor.select (QTextCursor::LineUnderCursor);
124
+ QString lineText = cursor.selectedText ();
125
+
126
+ if (lineText.startsWith (commentSymbol))
127
+ {
128
+ lineText.remove (0 , 3 );
129
+ }
130
+ else
131
+ {
132
+ lineText.prepend (commentSymbol + " " );
133
+ }
134
+
135
+ cursor.insertText (lineText);
136
+ }
137
+
128
138
void CodeEditor::addComment ()
129
139
{
130
140
QTextCursor cursor = textCursor ();
0 commit comments