Skip to content

Commit e93db75

Browse files
authored
Merge pull request #19 from sandbox-science/commentCode
File Extension Detection and Commenting Feature
2 parents a936340 + 35016a4 commit e93db75

File tree

3 files changed

+123
-4
lines changed

3 files changed

+123
-4
lines changed

include/CodeEditor.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class CodeEditor : public QPlainTextEdit
1818
Mode mode = NORMAL;
1919
void lineNumberAreaPaintEvent(QPaintEvent *event);
2020
int lineNumberAreaWidth();
21+
QString getCurrentFileName() const { return m_currentFileName; }
22+
void setCurrentFileName(const QString &fileName) { m_currentFileName = fileName; }
2123

2224
signals:
2325
void statusMessageChanged(const QString &message);
@@ -33,4 +35,11 @@ private slots:
3335

3436
private:
3537
QWidget *m_lineNumberArea;
38+
QString m_currentFileName;
39+
40+
QString getFileExtension();
41+
void addLanguageSymbol(QTextCursor &cursor, const QString &commentSymbol);
42+
void commentSelection(QTextCursor &cursor, const QString &commentSymbol);
43+
void commentLine(QTextCursor &cursor, const QString &commentSymbol);
44+
void addComment();
3645
};

src/CodeEditor.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <QPainter>
66
#include <QTextBlock>
77
#include <QStatusBar>
8+
#include <QFileInfo>
89

910
CodeEditor::CodeEditor(QWidget *parent)
1011
: QPlainTextEdit(parent),
@@ -27,6 +28,11 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
2728
moveCursor(QTextCursor::WordLeft, QTextCursor::KeepAnchor);
2829
return;
2930
}
31+
if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Slash)
32+
{
33+
addComment();
34+
return;
35+
}
3036

3137
if (mode == NORMAL)
3238
{
@@ -67,6 +73,109 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
6773
}
6874
}
6975

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+
70179
int CodeEditor::lineNumberAreaWidth()
71180
{
72181
int digits = 1;

src/MainWindow.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ void MainWindow::openFile()
170170

171171
void MainWindow::saveFile()
172172
{
173-
if (m_currentFileName.isEmpty())
173+
if (m_editor->getCurrentFileName().isEmpty())
174174
{
175175
saveFileAs();
176176
return;
177177
}
178178

179-
QFile file(m_currentFileName);
179+
QFile file(m_editor->getCurrentFileName());
180180
if (!file.open(QFile::WriteOnly | QFile::Text))
181181
{
182182
QMessageBox::warning(this, "Error", "Cannot save file: " + file.errorString());
@@ -205,7 +205,7 @@ void MainWindow::saveFileAs()
205205

206206
if (!fileName.isEmpty())
207207
{
208-
m_currentFileName = fileName;
208+
m_editor->setCurrentFileName(fileName);
209209
saveFile();
210210
}
211211
}
@@ -231,6 +231,7 @@ void MainWindow::loadFileInEditor(const QString &filePath)
231231
}
232232
file.close();
233233

234-
m_currentFileName = filePath;
234+
m_editor->setCurrentFileName(filePath);
235235
setWindowTitle("CodeAstra ~ " + QFileInfo(filePath).fileName());
236+
emit m_editor->statusMessageChanged("File loaded successfully: " + QFileInfo(filePath).fileName());
236237
}

0 commit comments

Comments
 (0)