Skip to content

File Extension Detection and Commenting Feature #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/CodeEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class CodeEditor : public QPlainTextEdit
Mode mode = NORMAL;
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
QString getCurrentFileName() const { return m_currentFileName; }
void setCurrentFileName(const QString &fileName) { m_currentFileName = fileName; }

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

private:
QWidget *m_lineNumberArea;
QString m_currentFileName;

QString getFileExtension();
void addLanguageSymbol(QTextCursor &cursor, const QString &commentSymbol);
void commentSelection(QTextCursor &cursor, const QString &commentSymbol);
void commentLine(QTextCursor &cursor, const QString &commentSymbol);
void addComment();
};
109 changes: 109 additions & 0 deletions src/CodeEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QPainter>
#include <QTextBlock>
#include <QStatusBar>
#include <QFileInfo>

CodeEditor::CodeEditor(QWidget *parent)
: QPlainTextEdit(parent),
Expand All @@ -27,6 +28,11 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
moveCursor(QTextCursor::WordLeft, QTextCursor::KeepAnchor);
return;
}
if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Slash)
{
addComment();
return;
}

if (mode == NORMAL)
{
Expand Down Expand Up @@ -67,6 +73,109 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
}
}

QString CodeEditor::getFileExtension()
{
QString filePath = getCurrentFileName();
if (!QFile::exists(filePath))
{
return QString();
}

// Extract the file extension from the file path
return QFileInfo(filePath).suffix().toLower();
}

void CodeEditor::addLanguageSymbol(QTextCursor &cursor, const QString &commentSymbol)
{
if (cursor.hasSelection())
{
commentSelection(cursor, commentSymbol);
}
else
{
commentLine(cursor, commentSymbol);
}
}

// Comment/uncomment the selected text or the current line
void CodeEditor::commentSelection(QTextCursor &cursor, const QString &commentSymbol)
{
int start = cursor.selectionStart();
int end = cursor.selectionEnd();

cursor.setPosition(start);
int startBlockNumber = cursor.blockNumber();
cursor.setPosition(end);
int endBlockNumber = cursor.blockNumber();

cursor.setPosition(start);
for (int i = startBlockNumber; i <= endBlockNumber; ++i)
{
cursor.movePosition(QTextCursor::StartOfLine);
QString lineText = cursor.block().text();

if (lineText.startsWith(commentSymbol))
{
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 3);
cursor.removeSelectedText();
}
else
{
cursor.insertText(commentSymbol + " ");
}

cursor.movePosition(QTextCursor::NextBlock);
}
}

// Comment/uncomment the single current line
void CodeEditor::commentLine(QTextCursor &cursor, const QString &commentSymbol)
{
cursor.select(QTextCursor::LineUnderCursor);
QString lineText = cursor.selectedText();

if (lineText.startsWith(commentSymbol))
{
lineText.remove(0, 3);
}
else
{
lineText.prepend(commentSymbol + " ");
}

cursor.insertText(lineText);
}

void CodeEditor::addComment()
{
QTextCursor cursor = textCursor();
QString fileExtension = getFileExtension();
qDebug() << "File Extension:" << fileExtension;

if (fileExtension == "cpp" || fileExtension == "h" ||
fileExtension == "hpp" || fileExtension == "c" ||
fileExtension == "java" || fileExtension == "go" ||
fileExtension == "json")
{
addLanguageSymbol(cursor, "//");
}
else if (fileExtension == "py" || fileExtension == "yaml" ||
fileExtension == "yml" || fileExtension == "sh" ||
fileExtension == "bash")
{
addLanguageSymbol(cursor, "#");
}
else if (fileExtension == "sql")
{
addLanguageSymbol(cursor, "--");
}
else
{
qDebug() << "Unsupported file extension for commenting.";
return;
}
}

int CodeEditor::lineNumberAreaWidth()
{
int digits = 1;
Expand Down
9 changes: 5 additions & 4 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ void MainWindow::openFile()

void MainWindow::saveFile()
{
if (m_currentFileName.isEmpty())
if (m_editor->getCurrentFileName().isEmpty())
{
saveFileAs();
return;
}

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

if (!fileName.isEmpty())
{
m_currentFileName = fileName;
m_editor->setCurrentFileName(fileName);
saveFile();
}
}
Expand All @@ -231,6 +231,7 @@ void MainWindow::loadFileInEditor(const QString &filePath)
}
file.close();

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