5
5
#include < QPainter>
6
6
#include < QTextBlock>
7
7
#include < QStatusBar>
8
+ #include < QFileInfo>
8
9
9
10
CodeEditor::CodeEditor (QWidget *parent)
10
11
: QPlainTextEdit(parent),
@@ -27,6 +28,11 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
27
28
moveCursor (QTextCursor::WordLeft, QTextCursor::KeepAnchor);
28
29
return ;
29
30
}
31
+ if (event->modifiers () == Qt::ControlModifier && event->key () == Qt::Key_Slash)
32
+ {
33
+ addComment ();
34
+ return ;
35
+ }
30
36
31
37
if (mode == NORMAL)
32
38
{
@@ -67,6 +73,109 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
67
73
}
68
74
}
69
75
76
+ QString CodeEditor::getFileExtension ()
77
+ {
78
+ QString filePath = getCurrentFileName ();
79
+ if (!QFile::exists (filePath))
80
+ {
81
+ return QString ();
82
+ }
83
+
84
+ // Extract the file extension from the file path
85
+ return QFileInfo (filePath).suffix ().toLower ();
86
+ }
87
+
88
+ void CodeEditor::addLanguageSymbol (QTextCursor &cursor, const QString &commentSymbol)
89
+ {
90
+ if (cursor.hasSelection ())
91
+ {
92
+ commentSelection (cursor, commentSymbol);
93
+ }
94
+ else
95
+ {
96
+ commentLine (cursor, commentSymbol);
97
+ }
98
+ }
99
+
100
+ // Comment/uncomment the selected text or the current line
101
+ void CodeEditor::commentSelection (QTextCursor &cursor, const QString &commentSymbol)
102
+ {
103
+ int start = cursor.selectionStart ();
104
+ int end = cursor.selectionEnd ();
105
+
106
+ cursor.setPosition (start);
107
+ int startBlockNumber = cursor.blockNumber ();
108
+ cursor.setPosition (end);
109
+ int endBlockNumber = cursor.blockNumber ();
110
+
111
+ cursor.setPosition (start);
112
+ for (int i = startBlockNumber; i <= endBlockNumber; ++i)
113
+ {
114
+ cursor.movePosition (QTextCursor::StartOfLine);
115
+ QString lineText = cursor.block ().text ();
116
+
117
+ if (lineText.startsWith (commentSymbol))
118
+ {
119
+ cursor.movePosition (QTextCursor::Right, QTextCursor::KeepAnchor, 3 );
120
+ cursor.removeSelectedText ();
121
+ }
122
+ else
123
+ {
124
+ cursor.insertText (commentSymbol + " " );
125
+ }
126
+
127
+ cursor.movePosition (QTextCursor::NextBlock);
128
+ }
129
+ }
130
+
131
+ // Comment/uncomment the single current line
132
+ void CodeEditor::commentLine (QTextCursor &cursor, const QString &commentSymbol)
133
+ {
134
+ cursor.select (QTextCursor::LineUnderCursor);
135
+ QString lineText = cursor.selectedText ();
136
+
137
+ if (lineText.startsWith (commentSymbol))
138
+ {
139
+ lineText.remove (0 , 3 );
140
+ }
141
+ else
142
+ {
143
+ lineText.prepend (commentSymbol + " " );
144
+ }
145
+
146
+ cursor.insertText (lineText);
147
+ }
148
+
149
+ void CodeEditor::addComment ()
150
+ {
151
+ QTextCursor cursor = textCursor ();
152
+ QString fileExtension = getFileExtension ();
153
+ qDebug () << " File Extension:" << fileExtension;
154
+
155
+ if (fileExtension == " cpp" || fileExtension == " h" ||
156
+ fileExtension == " hpp" || fileExtension == " c" ||
157
+ fileExtension == " java" || fileExtension == " go" ||
158
+ fileExtension == " json" )
159
+ {
160
+ addLanguageSymbol (cursor, " //" );
161
+ }
162
+ else if (fileExtension == " py" || fileExtension == " yaml" ||
163
+ fileExtension == " yml" || fileExtension == " sh" ||
164
+ fileExtension == " bash" )
165
+ {
166
+ addLanguageSymbol (cursor, " #" );
167
+ }
168
+ else if (fileExtension == " sql" )
169
+ {
170
+ addLanguageSymbol (cursor, " --" );
171
+ }
172
+ else
173
+ {
174
+ qDebug () << " Unsupported file extension for commenting." ;
175
+ return ;
176
+ }
177
+ }
178
+
70
179
int CodeEditor::lineNumberAreaWidth ()
71
180
{
72
181
int digits = 1 ;
0 commit comments