feature: auto link

This commit is contained in:
royqh1979 2021-09-04 11:37:04 +08:00
parent 1a30d73d96
commit 3c4a2ac9d2
7 changed files with 214 additions and 0 deletions

View File

@ -14,6 +14,7 @@ QMAKE_CXXFLAGS_DEBUG += -Werror=return-type
SOURCES += \ SOURCES += \
HighlighterManager.cpp \ HighlighterManager.cpp \
autolinkmanager.cpp \
caretlist.cpp \ caretlist.cpp \
codeformatter.cpp \ codeformatter.cpp \
colorscheme.cpp \ colorscheme.cpp \
@ -89,6 +90,7 @@ SOURCES += \
HEADERS += \ HEADERS += \
HighlighterManager.h \ HighlighterManager.h \
autolinkmanager.h \
caretlist.h \ caretlist.h \
codeformatter.h \ codeformatter.h \
colorscheme.h \ colorscheme.h \
@ -196,6 +198,7 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
RESOURCES += \ RESOURCES += \
codes.qrc \ codes.qrc \
colorschemes.qrc \ colorschemes.qrc \
defaultconfigs.qrc \
themes/dark/dark.qrc \ themes/dark/dark.qrc \
themes/light/light.qrc \ themes/light/light.qrc \
themes/dracula/dracula.qrc \ themes/dracula/dracula.qrc \

View File

@ -0,0 +1,101 @@
#include "autolinkmanager.h"
#include "settings.h"
#include <QDir>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
AutolinkManager* pAutolinkManager;
AutolinkManager::AutolinkManager()
{
}
PAutolink AutolinkManager::getLink(const QString &header) const
{
return mLinks.value(header,PAutolink());
}
void AutolinkManager::load()
{
QDir dir(pSettings->dirs().config());
QString filename=dir.filePath(AUTOLINK_CONFIG);
QFile file(filename);
if (!file.exists()) {
QFile::copy(":/config/autolink.json",filename);
if (!file.exists()) {
throw FileError(QObject::tr("Can't open file '%1' for write.")
.arg(filename));
}
}
if (file.open(QFile::ReadOnly)) {
QByteArray content = file.readAll();
QJsonDocument doc(QJsonDocument::fromJson(content));
fromJson(doc.array());
file.close();
} else {
throw FileError(QObject::tr("Can't open file '%1' for read.")
.arg(filename));
}
}
void AutolinkManager::save()
{
QDir dir(pSettings->dirs().config());
QString filename=dir.filePath(AUTOLINK_CONFIG);
QFile file(filename);
if (file.open(QFile::WriteOnly|QFile::Truncate)) {
QJsonDocument doc(toJson());
file.write(doc.toJson(QJsonDocument::Indented));
file.close();
} else {
throw FileError(QObject::tr("Can't open file '%1' for write.")
.arg(filename));
}
}
void AutolinkManager::setLink(const QString &header, const QString &linkOption)
{
PAutolink link = mLinks.value(header,PAutolink());
if (link) {
link->linkOption = linkOption;
} else {
link = std::make_shared<Autolink>();
link->header = header;
link->linkOption = linkOption;
mLinks.insert(header,link);
}
}
const QMap<QString, PAutolink> &AutolinkManager::links() const
{
return mLinks;
}
void AutolinkManager::clear()
{
mLinks.clear();
}
QJsonArray AutolinkManager::toJson()
{
QJsonArray result;
foreach (const QString& header, mLinks.keys()){
QJsonObject autolink;
autolink["header"]=header;
autolink["links"]=mLinks[header]->linkOption;
result.append(autolink);
}
return result;
}
void AutolinkManager::fromJson(QJsonArray json)
{
clear();
for (int i=0;i<json.size();i++) {
QJsonObject obj = json[i].toObject();
setLink(obj["header"].toString(),obj["links"].toString());
}
}

View File

@ -0,0 +1,38 @@
#ifndef AUTOLINKMANAGER_H
#define AUTOLINKMANAGER_H
#include <QObject>
#include <QString>
#include <memory>
#include <QVector>
#include <QMap>
#define AUTOLINK_CONFIG "autolink.json"
struct Autolink {
QString header;
QString linkOption;
};
using PAutolink = std::shared_ptr<Autolink>;
class AutolinkManager
{
public:
explicit AutolinkManager();
PAutolink getLink(const QString& header) const;
void load();
void save();
void setLink(const QString& header,
const QString& linkOption);
void removeLink(const QString& header);
const QMap<QString,PAutolink>& links() const;
void clear();
QJsonArray toJson();
void fromJson(QJsonArray json);
private:
QMap<QString,PAutolink> mLinks;
};
extern AutolinkManager* pAutolinkManager;
#endif // AUTOLINKMANAGER_H

View File

@ -8,6 +8,12 @@
#include <QTextCodec> #include <QTextCodec>
#include <QDebug> #include <QDebug>
#include <QTime> #include <QTime>
#include <QApplication>
#include "../editor.h"
#include "../mainwindow.h"
#include "../editorlist.h"
#include "../parser/cppparser.h"
#include "../autolinkmanager.h"
#include "../platform.h" #include "../platform.h"
#define COMPILE_PROCESS_END "---//END//----" #define COMPILE_PROCESS_END "---//END//----"
@ -336,6 +342,35 @@ QString Compiler::getLibraryArguments()
result += QString(" -L\"%1\"").arg(folder); result += QString(" -L\"%1\"").arg(folder);
} }
//Add auto links
// is file and auto link enabled
{
Editor* editor = pMainWindow->editorList()->getEditor();
if (editor) {
PCppParser parser = editor->parser();
if (parser) {
int waitCount = 0;
//wait parsing ends, at most 1 second
while(parser->parsing()) {
if (waitCount>0)
break;
waitCount++;
QThread::msleep(100);
QApplication *app=dynamic_cast<QApplication*>(
QApplication::instance());
app->processEvents();
QSet<QString> parsedFiles;
result += parseFileIncludesForAutolink(
editor->filename(),
parsedFiles,
parser);
}
}
}
}
// Add global compiler linker extras // Add global compiler linker extras
if (compilerSet()->useCustomLinkParams() && !compilerSet()->customLinkParams().isEmpty()) { if (compilerSet()->useCustomLinkParams() && !compilerSet()->customLinkParams().isEmpty()) {
result += " "+compilerSet()->customCompileParams(); result += " "+compilerSet()->customCompileParams();
@ -361,6 +396,30 @@ QString Compiler::getLibraryArguments()
return result; return result;
} }
QString Compiler::parseFileIncludesForAutolink(
const QString &filename,
QSet<QString> parsedFiles,
PCppParser& parser)
{
QString result;
QString baseName = baseFileName(filename);
if (parsedFiles.contains(filename))
return result;
parsedFiles.insert(filename);
PAutolink autolink = pAutolinkManager->getLink(baseName);
if (autolink) {
result += ' '+autolink->linkOption;
}
QSet<QString> includedFiles = parser->getFileDirectIncludes(filename);
foreach (const QString& includeFilename, includedFiles) {
result += parseFileIncludesForAutolink(
includeFilename,
parsedFiles,
parser);
}
return result;
}
void Compiler::runCommand(const QString &cmd, const QString &arguments, const QString &workingDir, const QString& inputText) void Compiler::runCommand(const QString &cmd, const QString &arguments, const QString &workingDir, const QString& inputText)
{ {
QProcess process; QProcess process;

View File

@ -4,6 +4,7 @@
#include <QThread> #include <QThread>
#include "settings.h" #include "settings.h"
#include "../common.h" #include "../common.h"
#include "../parser/cppparser.h"
class Compiler : public QThread class Compiler : public QThread
{ {
@ -49,6 +50,10 @@ protected:
virtual QString getCIncludeArguments(); virtual QString getCIncludeArguments();
virtual QString getCppIncludeArguments(); virtual QString getCppIncludeArguments();
virtual QString getLibraryArguments(); virtual QString getLibraryArguments();
virtual QString parseFileIncludesForAutolink(
const QString& filename,
QSet<QString> parsedFiles,
PCppParser& parser);
void log(const QString& msg); void log(const QString& msg);
void error(const QString& msg); void error(const QString& msg);
void runCommand(const QString& cmd, const QString& arguments, const QString& workingDir, const QString& inputText=QString()); void runCommand(const QString& cmd, const QString& arguments, const QString& workingDir, const QString& inputText=QString());

View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/config">
<file alias="autolink.json">resources/autolink.json</file>
</qresource>
</RCC>

View File

@ -12,6 +12,7 @@
#include "common.h" #include "common.h"
#include "colorscheme.h" #include "colorscheme.h"
#include "iconsmanager.h" #include "iconsmanager.h"
#include "autolinkmanager.h"
#include "parser/parserutils.h" #include "parser/parserutils.h"
QString getSettingFilename(const QString& filepath = QString()) { QString getSettingFilename(const QString& filepath = QString()) {
@ -82,6 +83,8 @@ int main(int argc, char *argv[])
//Color scheme settings must be loaded after translation //Color scheme settings must be loaded after translation
pColorManager = new ColorManager(); pColorManager = new ColorManager();
pIconsManager = new IconsManager(); pIconsManager = new IconsManager();
pAutolinkManager = new AutolinkManager();
pAutolinkManager->load();
MainWindow mainWindow; MainWindow mainWindow;
pMainWindow = &mainWindow; pMainWindow = &mainWindow;