* work done : toggle comments

* if compile failed, use message box to notify the user
This commit is contained in:
royqh1979 2021-06-21 11:21:26 +08:00
parent c0c21245c7
commit 08a89abe59
13 changed files with 263 additions and 80 deletions

View File

@ -1,5 +1,6 @@
#include "compiler.h"
#include "utils.h"
#include "compilermanager.h"
#include <QFileInfo>
#include <QProcess>
@ -19,6 +20,7 @@ Compiler::Compiler(bool silent,bool onlyCheckSyntax):
void Compiler::run()
{
emit compileStarted();
try {
if (prepareForCompile()){
mErrorCount = 0;
mWarningCount = 0;
@ -38,6 +40,9 @@ void Compiler::run()
}
log(tr("- Compilation Time: %1 secs").arg(timer.elapsed() / 1000.0));
}
} catch (CompileError e) {
emit compileErrorOccured(e.reason());
}
emit compileFinished();
}
@ -302,10 +307,16 @@ void Compiler::runCommand(const QString &cmd, const QString &arguments, const Q
{
QProcess process;
mStop = false;
bool errorOccurred = false;
process.setProgram(cmd);
process.setArguments(QProcess::splitCommand(arguments));
process.setWorkingDirectory(workingDir);
process.connect(&process, &QProcess::errorOccurred,
[&](){
errorOccurred= true;
});
process.connect(&process, &QProcess::readyReadStandardError,[&process,this](){
this->error(QString::fromLocal8Bit( process.readAllStandardError()));
});
@ -322,11 +333,32 @@ void Compiler::runCommand(const QString &cmd, const QString &arguments, const Q
if (process.state()!=QProcess::Running) {
break;
}
if (mStop) {
if (mStop || errorOccurred) {
process.kill();
break;
}
}
if (errorOccurred) {
switch (process.error()) {
case QProcess::FailedToStart:
throw CompileError(tr("The compiler process failed to start."));
break;
case QProcess::Crashed:
throw CompileError(tr("The compiler process crashed after starting successfully."));
break;
case QProcess::Timedout:
throw CompileError(tr("The last waitFor...() function timed out."));
break;
case QProcess::WriteError:
throw CompileError(tr("An error occurred when attempting to write to the compiler process."));
break;
case QProcess::ReadError:
throw CompileError(tr("An error occurred when attempting to read from the compiler process."));
break;
default:
throw CompileError(tr("An unknown error occurred."));
}
}
}
void Compiler::log(const QString &msg)
@ -342,4 +374,3 @@ void Compiler::error(const QString &msg)
processOutput(s);
}
}

View File

@ -23,6 +23,7 @@ signals:
void compileFinished();
void compileOutput(const QString& msg);
void compileIssue(PCompileIssue issue);
void compileErrorOccured(const QString& reason);
public slots:
void stopCompile();
@ -58,7 +59,7 @@ protected:
private:
bool mStop;
};
#endif // COMPILER_H

View File

@ -32,6 +32,7 @@ void CompilerManager::compile(const QString& filename, const QByteArray& encodin
connect(mCompiler, &Compiler::compileFinished, this ,&CompilerManager::onCompileFinished);
connect(mCompiler, &Compiler::compileOutput, pMainWindow, &MainWindow::onCompileLog);
connect(mCompiler, &Compiler::compileIssue, pMainWindow, &MainWindow::onCompileIssue);
connect(mCompiler, &Compiler::compileErrorOccured, pMainWindow, &MainWindow::onCompileErrorOccured);
mCompiler->start();
}
@ -71,3 +72,8 @@ void CompilerManager::onRunnerTerminated()
mRunner=nullptr;
}
CompileError::CompileError(const QString &reason):BaseError(reason)
{
}

View File

@ -3,6 +3,7 @@
#include <QObject>
#include <QMutex>
#include "../utils.h"
class ExecutableRunner;
class Compiler;
@ -30,4 +31,9 @@ private:
QMutex runnerMutex;
};
class CompileError : public BaseError {
public:
explicit CompileError(const QString& reason);
};
#endif // COMPILERMANAGER_H

View File

@ -1,6 +1,7 @@
#include "filecompiler.h"
#include "utils.h"
#include "../mainwindow.h"
#include "compilermanager.h"
#include <QFile>
#include <QFileInfo>
@ -59,11 +60,14 @@ bool FileCompiler::prepareForCompile()
mCompiler = compilerSet()->cppCompiler();
break;
default:
error(tr("Can't find the compiler for file %1").arg(mFileName));
return false;
throw CompileError(tr("Can't find the compiler for file %1").arg(mFileName));
}
mArguments += getLibraryArguments();
if (!QFile(mCompiler).exists()) {
throw CompileError(tr("The Compiler '%1' doesn't exists!").arg(mCompiler));
}
log(tr("Processing %1 source file:").arg(strFileType));
log("------------------");
log(tr("%1 Compiler: %2").arg(strFileType).arg(mCompiler));

View File

@ -126,7 +126,7 @@ bool Editor::save(bool force, bool reparse) {
QFileInfo info(mFilename);
//is this file writable;
if (!force && !info.isWritable()) {
QMessageBox::information(pMainWindow,tr("Fail"),
QMessageBox::critical(pMainWindow,tr("Error"),
tr("File %1 is not writable!").arg(mFilename));
return false;
}
@ -137,7 +137,7 @@ bool Editor::save(bool force, bool reparse) {
mIsNew = false;
this->updateCaption();
} catch (SaveException& exception) {
QMessageBox::information(pMainWindow,tr("Fail"),
QMessageBox::critical(pMainWindow,tr("Error"),
exception.reason());
return false;
}
@ -164,7 +164,7 @@ bool Editor::saveAs(){
setModified(false);
this->updateCaption();
} catch (SaveException& exception) {
QMessageBox::information(pMainWindow,tr("Fail"),
QMessageBox::critical(pMainWindow,tr("Error"),
exception.reason());
return false;
}
@ -251,12 +251,12 @@ void Editor::copyToClipboard()
{
if (pSettings->editor().copySizeLimit()) {
if (lines()->count() > pSettings->editor().copyLineLimits()) {
QMessageBox::information(pMainWindow,tr("Fail"),
QMessageBox::critical(pMainWindow,tr("Error"),
tr("The text to be copied exceeds count limit!"));
return;
}
if (lines()->getTextLength() > pSettings->editor().copyCharLimits() * 1000) {
QMessageBox::information(pMainWindow,tr("Fail"),
QMessageBox::critical(pMainWindow,tr("Error"),
tr("The text to be copied exceeds character limit!"));
return;
}
@ -274,12 +274,12 @@ void Editor::cutToClipboard()
{
if (pSettings->editor().copySizeLimit()) {
if (lines()->count() > pSettings->editor().copyLineLimits()) {
QMessageBox::information(pMainWindow,tr("Fail"),
QMessageBox::critical(pMainWindow,tr("Error"),
tr("The text to be cut exceeds count limit!"));
return;
}
if (lines()->getTextLength() > pSettings->editor().copyCharLimits() * 1000) {
QMessageBox::information(pMainWindow,tr("Fail"),
QMessageBox::critical(pMainWindow,tr("Error"),
tr("The text to be cut exceeds character limit!"));
return;
}

View File

@ -30,14 +30,14 @@ Settings* createAppSettings(const QString& filepath = QString()) {
QDir dir(fileInfo.absoluteDir());
if (!dir.exists()) {
if (!dir.mkpath(dir.absolutePath())) {
QMessageBox::information(nullptr, QObject::tr("Error"),
QMessageBox::critical(nullptr, QObject::tr("Error"),
QString(QObject::tr("Can't create configuration folder %1")).arg(dir.absolutePath()));
return nullptr;
}
}
if (fileInfo.exists() && !fileInfo.isWritable()) {
QMessageBox::information(nullptr, QObject::tr("Error"),
QMessageBox::critical(nullptr, QObject::tr("Error"),
QString(QObject::tr("Can't write to configuration file %1")).arg(filename));
return nullptr;
@ -83,6 +83,6 @@ int main(int argc, char *argv[])
// settings->compilerSets().saveSets();
return retCode;
} catch (BaseError e) {
QMessageBox::information(nullptr,QApplication::tr("Error"),e.reason());
QMessageBox::critical(nullptr,QApplication::tr("Error"),e.reason());
}
}

View File

@ -257,7 +257,7 @@ void MainWindow::on_actionNew_triggered()
editor->activate();
updateForEncodingInfo();
} catch (FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
}
}
@ -276,7 +276,7 @@ void MainWindow::on_actionOpen_triggered()
&selectedFileFilter);
openFiles(files);
} catch (FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
}
}
@ -298,7 +298,7 @@ void MainWindow::on_actionSave_triggered()
try {
editor->save();
} catch(FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
}
}
}
@ -310,7 +310,7 @@ void MainWindow::on_actionSaveAs_triggered()
try {
editor->saveAs();
} catch(FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
}
}
}
@ -339,6 +339,11 @@ void MainWindow::onCompileIssue(PCompileIssue issue)
ui->tableIssues->addIssue(issue);
}
void MainWindow::onCompileErrorOccured(const QString &reason)
{
QMessageBox::critical(this,tr("Compile Failed"),reason);
}
void MainWindow::on_actionCompile_triggered()
{
Editor * editor = mEditorList->getEditor();
@ -425,7 +430,7 @@ void MainWindow::on_actionToggleComment_triggered()
{
Editor * editor = mEditorList->getEditor();
if (editor != NULL ) {
//editor->toggleComment();
editor->toggleComment();
}
}
@ -468,7 +473,7 @@ void MainWindow::on_actionEncode_in_ANSI_triggered()
try {
editor->setEncodingOption(ENCODING_SYSTEM_DEFAULT);
} catch(FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
}
}
@ -480,7 +485,7 @@ void MainWindow::on_actionEncode_in_UTF_8_triggered()
try {
editor->setEncodingOption(ENCODING_UTF8);
} catch(FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
}
}
@ -497,7 +502,7 @@ void MainWindow::on_actionConvert_to_ANSI_triggered()
Editor * editor = mEditorList->getEditor();
if (editor == nullptr)
return;
if (QMessageBox::information(this,tr("Confirm Convertion"),
if (QMessageBox::warning(this,tr("Confirm Convertion"),
tr("The editing file will be saved using %1 encoding. <br />This operation can't be reverted. <br />Are you sure to continue?")
.arg(QString(QTextCodec::codecForLocale()->name())),
QMessageBox::Yes, QMessageBox::No)!=QMessageBox::Yes)
@ -511,7 +516,7 @@ void MainWindow::on_actionConvert_to_UTF_8_triggered()
Editor * editor = mEditorList->getEditor();
if (editor == nullptr)
return;
if (QMessageBox::information(this,tr("Confirm Convertion"),
if (QMessageBox::warning(this,tr("Confirm Convertion"),
tr("The editing file will be saved using %1 encoding. <br />This operation can't be reverted. <br />Are you sure to continue?")
.arg(ENCODING_UTF8),
QMessageBox::Yes, QMessageBox::No)!=QMessageBox::Yes)

View File

@ -91,6 +91,7 @@ private slots:
public slots:
void onCompileLog(const QString& msg);
void onCompileIssue(PCompileIssue issue);
void onCompileErrorOccured(const QString& reason);
private:
void setupActions();

View File

@ -1121,6 +1121,149 @@ void SynEdit::doSelectAll()
statusChanged(SynStatusChange::scSelection);
}
void SynEdit::doComment()
{
BufferCoord origBlockBegin, origBlockEnd, origCaret;
int endLine;
if (mReadOnly)
return;
doOnPaintTransient(SynTransientType::ttBefore);
mUndoList->BeginBlock();
auto action = finally([this]{
mUndoList->EndBlock();
});
origBlockBegin = blockBegin();
origBlockEnd = blockEnd();
origCaret = caretXY();
// Ignore the last line the cursor is placed on
if (origBlockEnd.Char == 1)
endLine = std::max(origBlockBegin.Line - 1, origBlockEnd.Line - 2);
else
endLine = origBlockEnd.Line - 1;
for (int i = origBlockBegin.Line - 1; i<=endLine; i++) {
mLines->putString(i, "//" + mLines->getString(i));
mUndoList->AddChange(SynChangeReason::crInsert,
BufferCoord{1, i + 1},
BufferCoord{3, i + 1},
"", SynSelectionMode::smNormal);
}
// When grouping similar commands, process one comment action per undo/redo
mUndoList->AddChange(SynChangeReason::crNothing,
BufferCoord{0, 0},
BufferCoord{0, 0},
"", SynSelectionMode::smNormal);
// Move begin of selection
if (origBlockBegin.Char > 1)
origBlockBegin.Char+=2;
// Move end of selection
if (origBlockEnd.Char > 1)
origBlockEnd.Char+=2;
// Move caret
if (origCaret.Char > 1)
origCaret.Char+=2;
setCaretAndSelection(origCaret, origBlockBegin, origBlockEnd);
}
void SynEdit::doUncomment()
{
BufferCoord origBlockBegin, origBlockEnd, origCaret;
int endLine;
QString s;
if (mReadOnly)
return;
doOnPaintTransient(SynTransientType::ttBefore);
mUndoList->BeginBlock();
auto action = finally([this]{
mUndoList->EndBlock();
});
origBlockBegin = blockBegin();
origBlockEnd = blockEnd();
origCaret = caretXY();
// Ignore the last line the cursor is placed on
if (origBlockEnd.Char == 1)
endLine = std::max(origBlockBegin.Line - 1, origBlockEnd.Line - 2);
else
endLine = origBlockEnd.Line - 1;
for (int i = origBlockBegin.Line - 1; i<= endLine; i++) {
s = mLines->getString(i);
// Find // after blanks only
int j = 0;
while ((j+1 < s.length()) && (s[j] == '\n' || s[j] == '\t'))
j++;
if ((j + 1 < s.length()) && (s[j] == '/') && (s[j + 1] == '/')) {
s.remove(j,2);
mLines->putString(i,s);
mUndoList->AddChange(SynChangeReason::crDelete,
BufferCoord{j+1, i + 1},
BufferCoord{j + 3, i + 1},
"//", SynSelectionMode::smNormal);
// Move begin of selection
if ((i == origBlockBegin.Line - 1) && (origBlockBegin.Char > 1))
origBlockBegin.Char-=2;
// Move end of selection
if ((i == origBlockEnd.Line - 1) && (origBlockEnd.Char > 1))
origBlockEnd.Char-=2;
// Move caret
if ((i == origCaret.Line - 1) && (origCaret.Char > 1))
origCaret.Char-=2;
}
}
// When grouping similar commands, process one uncomment action per undo/redo
mUndoList->AddChange(SynChangeReason::crNothing,
BufferCoord{0, 0},
BufferCoord{0, 0},
"", SynSelectionMode::smNormal);
setCaretAndSelection(origCaret,origBlockBegin,origBlockEnd);
}
void SynEdit::doToggleComment()
{
BufferCoord origBlockBegin, origBlockEnd, origCaret;
int endLine;
QString s;
bool allCommented = true;
if (mReadOnly)
return;
doOnPaintTransient(SynTransientType::ttBefore);
mUndoList->BeginBlock();
auto action = finally([this]{
mUndoList->EndBlock();
});
origBlockBegin = blockBegin();
origBlockEnd = blockEnd();
origCaret = caretXY();
// Ignore the last line the cursor is placed on
if (origBlockEnd.Char == 1)
endLine = std::max(origBlockBegin.Line - 1, origBlockEnd.Line - 2);
else
endLine = origBlockEnd.Line - 1;
for (int i = origBlockBegin.Line - 1; i<= endLine; i++) {
s = mLines->getString(i);
// Find // after blanks only
int j = 0;
while ((j < s.length()) && (s[j] == '\n' || s[j] == '\t'))
j++;
if (j>= s.length())
continue;
if (s[j] != '/'){
allCommented = false;
break;
}
if (j+1>=s.length()) {
allCommented = false;
break;
}
if (s[j + 1] != '/') {
allCommented = false;
break;
}
}
if (allCommented)
doUncomment();
else
doComment();
}
void SynEdit::doDeleteLastChar()
{
// if not ReadOnly then begin
@ -2440,14 +2583,12 @@ int SynEdit::scanRanges()
mHighlighter->resetState();
for (int i =0;i<mLines->count();i++) {
mHighlighter->setLine(mLines->getString(i), i);
qDebug()<<i<<mLines->getString(i);
mHighlighter->nextToEol();
mLines->setRange(i, mHighlighter->getRangeState());
mLines->setParenthesisLevel(i, mHighlighter->getParenthesisLevel());
mLines->setBracketLevel(i, mHighlighter->getBracketLevel());
mLines->setBraceLevel(i, mHighlighter->getBraceLevel());
}
qDebug()<<"finished.";
}
}
@ -4366,6 +4507,15 @@ void SynEdit::ExecuteCommand(SynEditorCommand Command, QChar AChar, void *pData)
case SynEditorCommand::ecZoomOut:
doZoomOut();
break;
case SynEditorCommand::ecComment:
doComment();
break;
case SynEditorCommand::ecUncomment:
doUncomment();
break;
case SynEditorCommand::ecToggleComment:
doToggleComment();
break;
}
// procedure ForceCaretX(aCaretX: integer);
@ -4427,31 +4577,6 @@ void SynEdit::ExecuteCommand(SynEditorCommand Command, QChar AChar, void *pData)
// case Command of
// ecComment:
// DoComment;
// ecUnComment:
// DoUncomment;
// ecToggleComment:
// if not ReadOnly then begin
// OrigBlockBegin := BlockBegin;
// OrigBlockEnd := BlockEnd;
// BeginIndex := OrigBlockBegin.Line - 1;
// // Ignore the last line the cursor is placed on
// if (OrigBlockEnd.Char = 1) and (OrigBlockBegin.Line < OrigBlockEnd.Line) then
// EndIndex := max(0, OrigBlockEnd.Line - 2)
// else
// EndIndex := OrigBlockEnd.Line - 1;
// // if everything is commented, then uncomment
// for I := BeginIndex to EndIndex do begin
// if Pos('//', TrimLeft(fLines[i])) <> 1 then begin // not fully commented
// DoComment; // comment everything
// Exit;
// end;
// end;
// DoUncomment;
// end;
// ecCommentInline: // toggle inline comment
// if not ReadOnly and SelAvail then begin
// Temp := SelText;

View File

@ -229,9 +229,10 @@ public:
virtual void redo() { CommandProcessor(SynEditorCommand::ecRedo);}
virtual void zoomIn() { CommandProcessor(SynEditorCommand::ecZoomIn);}
virtual void zoomOut() { CommandProcessor(SynEditorCommand::ecZoomOut);}
virtual void selectAll() { { CommandProcessor(SynEditorCommand::ecSelectAll);}}
virtual void tab() { { CommandProcessor(SynEditorCommand::ecTab);}}
virtual void untab() { { CommandProcessor(SynEditorCommand::ecShiftTab);}}
virtual void selectAll() { CommandProcessor(SynEditorCommand::ecSelectAll);}
virtual void tab() { CommandProcessor(SynEditorCommand::ecTab);}
virtual void untab() { CommandProcessor(SynEditorCommand::ecShiftTab);}
virtual void toggleComment() { CommandProcessor(SynEditorCommand::ecToggleComment);}
// setter && getters
@ -469,6 +470,9 @@ private:
void doZoomIn();
void doZoomOut();
void doSelectAll();
void doComment();
void doUncomment();
void doToggleComment();
private:
void setBlockBegin(BufferCoord value);

View File

@ -293,7 +293,7 @@ void EditorColorSchemeWidget::doSave()
pSettings->editor().save();
pMainWindow->updateEditorColorSchemes();
} catch (FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
}
}
@ -346,7 +346,7 @@ void EditorColorSchemeWidget::on_actionImport_Scheme_triggered()
name.remove(name.length()-suffix.length(),suffix.length());
name.replace('_',' ');
if (!pColorManager->isValidName(name)) {
QMessageBox::information(this,tr("Error"),tr("'%1' is not a valid name for color scheme file."));
QMessageBox::critical(this,tr("Error"),tr("'%1' is not a valid name for color scheme file."));
return;
}
try {
@ -355,7 +355,7 @@ void EditorColorSchemeWidget::on_actionImport_Scheme_triggered()
ui->cbScheme->addItem(name);
ui->cbScheme->setCurrentText(name);
} catch (FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
return;
}
}
@ -368,7 +368,7 @@ void EditorColorSchemeWidget::on_actionRename_Scheme_triggered()
QLineEdit::Normal,name,&isOk);
if (isOk) {
if (!pColorManager->isValidName(newName)) {
QMessageBox::information(this,tr("Error"),tr("'%1' is not a valid scheme name!").arg(newName));
QMessageBox::critical(this,tr("Error"),tr("'%1' is not a valid scheme name!").arg(newName));
return;
}
try {
@ -381,7 +381,7 @@ void EditorColorSchemeWidget::on_actionRename_Scheme_triggered()
mModifiedSchemes.remove(name);
mModifiedSchemes.insert(newName);
} catch(FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
}
}
}
@ -398,7 +398,7 @@ void EditorColorSchemeWidget::on_actionReset_Scheme_triggered()
//ui->cbScheme->view()->setFont(mDefaultSchemeComboFont);
}
} catch (FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
}
}
@ -413,7 +413,7 @@ void EditorColorSchemeWidget::on_actionExport_Scheme_triggered()
PColorScheme scheme = getCurrentScheme();
scheme->save(filename);
} catch (FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
return;
}
}
@ -422,7 +422,7 @@ void EditorColorSchemeWidget::on_actionDelete_Scheme_triggered()
{
QString name = ui->cbScheme->currentText();
if (QMessageBox::information(this,tr("Confirm Delete Scheme"),
if (QMessageBox::warning(this,tr("Confirm Delete Scheme"),
tr("Scheme '%1' will be deleted!<br />Do you really want to continue?")
.arg(name),
QMessageBox::Yes, QMessageBox::No)!=QMessageBox::Yes)
@ -436,6 +436,6 @@ void EditorColorSchemeWidget::on_actionDelete_Scheme_triggered()
doSave();
}
} catch (FileError e) {
QMessageBox::information(this,tr("Error"),e.reason());
QMessageBox::critical(this,tr("Error"),e.reason());
}
}

View File

@ -137,7 +137,7 @@ void SettingsDialog::saveCurrentPageSettings(bool confirm)
if (!pWidget->isSettingsChanged())
return;
if (confirm) {
if (QMessageBox::information(this,tr("Save Changes"),
if (QMessageBox::warning(this,tr("Save Changes"),
tr("There are changes in the settings, do you want to save them before swtich to other page?"),
QMessageBox::Yes, QMessageBox::No)!=QMessageBox::Yes) {
return;