From 23b651f0c4ff35785d0522bd32ab5105714c4d87 Mon Sep 17 00:00:00 2001 From: royqh1979 Date: Fri, 1 Oct 2021 21:16:22 +0800 Subject: [PATCH] work save --- RedPandaIDE/compiler/compiler.cpp | 8 +-- RedPandaIDE/editor.cpp | 91 +++++++++++++++++-------------- RedPandaIDE/editor.h | 1 + RedPandaIDE/utils.cpp | 74 +++++++++++++++++++++++++ RedPandaIDE/utils.h | 3 + 5 files changed, 133 insertions(+), 44 deletions(-) diff --git a/RedPandaIDE/compiler/compiler.cpp b/RedPandaIDE/compiler/compiler.cpp index 54a711a3..0082d35a 100644 --- a/RedPandaIDE/compiler/compiler.cpp +++ b/RedPandaIDE/compiler/compiler.cpp @@ -307,14 +307,14 @@ QString Compiler::getCCompileArguments(bool checkSyntax) } if (compilerSet()->useCustomCompileParams() && !compilerSet()->customCompileParams().isEmpty()) { - result += " "+compilerSet()->customCompileParams(); + result += " "+ parseMacros(compilerSet()->customCompileParams()); } if (mProject) { QString s = mProject->options().compilerCmd; if (!s.isEmpty()) { s.replace("_@@_", " "); - result += " "+s; + result += " "+parseMacros(s); } } return result; @@ -354,13 +354,13 @@ QString Compiler::getCppCompileArguments(bool checkSyntax) } if (compilerSet()->useCustomCompileParams() && !compilerSet()->customCompileParams().isEmpty()) { - result += " "+compilerSet()->customCompileParams(); + result += " "+ parseMacros(compilerSet()->customCompileParams()); } if (mProject) { QString s = mProject->options().cppCompilerCmd; if (!s.isEmpty()) { s.replace("_@@_", " "); - result += " "+s; + result += " "+parseMacros(s); } } return result; diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 355a85af..82a63f85 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -1737,18 +1737,17 @@ void Editor::insertCodeSnippet(const QString &code) int spaceCount = GetLeftSpacing( leftSpaces(lineText()),true).length(); QStringList newSl; - int insertPos; for (int i=0;i0) lastPos = -spaceCount; while (true) { - insertPos = s.indexOf(USER_CODE_IN_INSERT_POS); + int insertPos = s.indexOf(USER_CODE_IN_INSERT_POS); if (insertPos < 0) // no %INSERT% macro in this line now break; PTabStop p = std::make_shared(); - s.remove(insertPos,USER_CODE_IN_INSERT_POS.length()); + s.remove(insertPos, QString(USER_CODE_IN_INSERT_POS).length()); insertPos--; p->x = insertPos - lastPos; p->endX = p->x; @@ -1757,45 +1756,52 @@ void Editor::insertCodeSnippet(const QString &code) lastI = i; mUserCodeInTabStops.append(p); } - while True do begin - insertPos := Pos(USER_CODE_IN_REPL_POS_BEGIN,s); - if insertPos = 0 then // no %INSERT% macro in this line now - break; - System.new(p); - Delete(s,insertPos,Length(USER_CODE_IN_REPL_POS_BEGIN)); - dec(insertPos); - p.x:=insertPos - lastPos; + lastPos = 0; + while (true) { + int insertPos = s.indexOf(USER_CODE_IN_REPL_POS_BEGIN); + if (insertPos < 0) // no %INSERT% macro in this line now + break; + PTabStop p = std::make_shared(); + s.remove(insertPos, QString(USER_CODE_IN_REPL_POS_BEGIN).length()); + insertPos--; + p->x = insertPos - lastPos; - insertEndPos := insertPos + Pos(USER_CODE_IN_REPL_POS_END,copy(s,insertPos+1,MaxInt)); - if insertEndPos <= insertPos then begin - p.endX := length(s); - end else begin - Delete(s,insertEndPos,Length(USER_CODE_IN_REPL_POS_END)); - dec(insertEndPos); - p.endX := insertEndPos - lastPos; - end; - p.y:=i-lastI; - lastPos := insertEndPos; - lastI:=i; - fUserCodeInTabStops.Add(p); - end; - newSl.Add(s); + + int insertEndPos = insertPos + + s.mid(insertPos).indexOf(USER_CODE_IN_REPL_POS_END); + if (insertEndPos < insertPos) { + p->endX = s.length(); + } else { + s.remove(insertEndPos, QString(USER_CODE_IN_REPL_POS_END).length()); + insertEndPos--; + p->endX = insertEndPos - lastPos; + } + p->y=i-lastI; + lastPos = insertEndPos; + lastI = i; + mUserCodeInTabStops.append(p); + } + newSl.append(s); + } + + BufferCoord cursorPos = caretXY(); + QString s = LinesToText(newSl); +// if EndsStr(#13#10,s) then +// Delete(s,Length(s)-1,2) +// else if EndsStr(#10, s) then +// Delete(s,Length(s),1); + setSelText(s); + setCaretXY(cursorPos); //restore cursor pos before insert +// fText.SelText := s; +// Text.CaretXY := CursorPos; + if (mUserCodeInTabStops.count()>0) { + mTabStopBegin = caretX(); + mTabStopEnd = caretX(); + popUserCodeInTabStops(); + } + if (!code.isEmpty()) { + mLastIdCharPressed = 0; } - CursorPos := Text.CaretXY; - s:=newSl.Text; - if EndsStr(#13#10,s) then - Delete(s,Length(s)-1,2) - else if EndsStr(#10, s) then - Delete(s,Length(s),1); - fText.SelText := s; - Text.CaretXY := CursorPos; //restore cursor pos before insert - if fUserCodeInTabStops.Count > 0 then begin - fTabStopBegin :=Text.CaretX; - fTabStopEnd := Text.CaretX; - PopUserCodeInTabStops; - end; - if Code <> '' then - fLastIdCharPressed := 0; } void Editor::showCompletion(bool autoComplete) @@ -2528,6 +2534,11 @@ void Editor::updateFunctionTip() pMainWindow->functionTip()->show(); } +void Editor::clearUserCodeInTabStops() +{ + mUserCodeInTabStops.clear(); +} + void Editor::setInProject(bool newInProject) { if (mInProject == newInProject) diff --git a/RedPandaIDE/editor.h b/RedPandaIDE/editor.h index 81168ce6..c21d3363 100644 --- a/RedPandaIDE/editor.h +++ b/RedPandaIDE/editor.h @@ -211,6 +211,7 @@ private: const QString& filename, int line); void updateFunctionTip(); + void clearUserCodeInTabStops(); private: diff --git a/RedPandaIDE/utils.cpp b/RedPandaIDE/utils.cpp index 6c9e9522..db81bbcb 100644 --- a/RedPandaIDE/utils.cpp +++ b/RedPandaIDE/utils.cpp @@ -18,6 +18,9 @@ #include "parser/cppparser.h" #include "settings.h" #include "mainwindow.h" +#include "editorlist.h" +#include "editor.h" +#include "project.h" const QByteArray GuessTextEncoding(const QByteArray& text){ bool allAscii; @@ -734,3 +737,74 @@ QString fromByteArray(const QByteArray &s) { return QString::fromLocal8Bit(s); } + +QString LinesToText(const QStringList &lines) +{ + return lines.join("\n"); +} + +QString parseMacros(const QString &s) +{ + QString result = s; + Editor *e = pMainWindow->editorList()->getEditor(); + + result.replace("", pSettings->dirs().app()); + result.replace("", pSettings->dirs().app()); + result.replace("", DEVCPP_VERSION); + result.replace("", pSettings->dirs().app()); + QDate today = QDate::currentDate(); + QDateTime now = QDateTime::currentDateTime(); + + result.replace("", "yyyy-MM-dd"); + result.replace("", "hh::mm::ss"); + + Settings::PCompilerSet compilerSet = pSettings->compilerSets().defaultSet(); + if (compilerSet) { + // Only provide the first cpp include dir + if (compilerSet->defaultCppIncludeDirs().count()>0) + result.replace("", compilerSet->defaultCppIncludeDirs().front()); + else + result.replace("",""); + + // Only provide the first lib dir + if (compilerSet->defaultLibDirs().count()>0) + result.replace("", compilerSet->defaultCppIncludeDirs().front()); + else + result.replace("",""); + } + + // Project-dependent macros + if (pMainWindow->project()) { + result.replace("", pMainWindow->project()->executable()); + result.replace("", pMainWindow->project()->name()); + result.replace("", pMainWindow->project()->filename()); + result.replace("", pMainWindow->project()->directory()); +// result.replace("', MainForm.Project.ListUnitStr(' ')); + result.replace("",""); + } else if (e!=nullptr) { // Non-project editor macros + result.replace("", changeFileExt(e->filename(),EXECUTABLE_EXT)); + result.replace("",e->filename()); + result.replace("",e->filename()); + result.replace("", extractFileDir(e->filename())); + result.replace("", ""); // clear unchanged macros + } else { + result.replace("", ""); + result.replace("", ""); + result.replace("", ""); + result.replace("", ""); + result.replace("", ""); // clear unchanged macros + } + + // Editor macros + if (e!=nullptr) { + result.replace("", extractFileName(e->filename())); + result.replace("", e->filename()); + result.replace("", extractFileDir(e->filename())); + result.replace("", e->wordAtCursor()); + } else { + result.replace("", ""); + result.replace("", ""); + result.replace("", ""); + result.replace("", ""); + } +} diff --git a/RedPandaIDE/utils.h b/RedPandaIDE/utils.h index 3ece7743..c2dbe641 100644 --- a/RedPandaIDE/utils.h +++ b/RedPandaIDE/utils.h @@ -145,6 +145,9 @@ void ReadStreamToLines(QTextStream* stream, LineProcessFunc lineFunc); QStringList TextToLines(const QString& text); void TextToLines(const QString& text, LineProcessFunc lineFunc); +QString LinesToText(const QStringList& lines); + +QString parseMacros(const QString& s); QStringList ReadFileToLines(const QString& fileName, QTextCodec* codec); QStringList ReadFileToLines(const QString& fileName);