Skip to content

Commit 6c53714

Browse files
committed
Refactor comment functionality in CodeEditor to streamline selection and line commenting
1 parent 4e8137b commit 6c53714

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

src/CodeEditor.cpp

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,57 +74,67 @@ QString CodeEditor::getFileExtension()
7474
return QFileInfo(filePath).suffix().toLower();
7575
}
7676

77-
void CodeEditor::addLanguageSymbol(QTextCursor &cursor, const QString commentSymbol)
77+
void CodeEditor::addLanguageSymbol(QTextCursor &cursor, const QString &commentSymbol)
7878
{
79-
// If text is selected, comment/uncomment selected text
8079
if (cursor.hasSelection())
8180
{
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);
10882
}
10983
else
11084
{
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();
114105

115106
if (lineText.startsWith(commentSymbol))
116107
{
117-
lineText.remove(0, 3);
108+
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 3);
109+
cursor.removeSelectedText();
118110
}
119111
else
120112
{
121-
lineText.prepend(commentSymbol + " ");
113+
cursor.insertText(commentSymbol + " ");
122114
}
123115

124-
cursor.insertText(lineText);
116+
cursor.movePosition(QTextCursor::NextBlock);
125117
}
126118
}
127119

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+
128138
void CodeEditor::addComment()
129139
{
130140
QTextCursor cursor = textCursor();

0 commit comments

Comments
 (0)