mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2026-06-30 01:59:11 -04:00
refactor: Move to agent architecture
This commit is contained in:
@@ -25,12 +25,23 @@ QString tomlEscape(const QString &s)
|
||||
out.reserve(s.size());
|
||||
for (QChar c : s) {
|
||||
switch (c.unicode()) {
|
||||
case '\\': out += QLatin1String("\\\\"); break;
|
||||
case '"': out += QLatin1String("\\\""); break;
|
||||
case '\n': out += QLatin1String("\\n"); break;
|
||||
case '\r': out += QLatin1String("\\r"); break;
|
||||
case '\t': out += QLatin1String("\\t"); break;
|
||||
default: out += c;
|
||||
case '\\':
|
||||
out += QLatin1String("\\\\");
|
||||
break;
|
||||
case '"':
|
||||
out += QLatin1String("\\\"");
|
||||
break;
|
||||
case '\n':
|
||||
out += QLatin1String("\\n");
|
||||
break;
|
||||
case '\r':
|
||||
out += QLatin1String("\\r");
|
||||
break;
|
||||
case '\t':
|
||||
out += QLatin1String("\\t");
|
||||
break;
|
||||
default:
|
||||
out += c;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
@@ -41,9 +52,7 @@ constexpr int kMaxUniqueAttempts = 1000;
|
||||
QString uniqueFilename(const QString &userDir, const QString &parentBasename)
|
||||
{
|
||||
QString fileName = parentBasename + QStringLiteral("_custom.toml");
|
||||
for (int n = 2; n < kMaxUniqueAttempts
|
||||
&& QFile::exists(QDir(userDir).filePath(fileName));
|
||||
++n)
|
||||
for (int n = 2; n < kMaxUniqueAttempts && QFile::exists(QDir(userDir).filePath(fileName)); ++n)
|
||||
fileName = QStringLiteral("%1_custom_%2.toml").arg(parentBasename).arg(n);
|
||||
return QDir(userDir).filePath(fileName);
|
||||
}
|
||||
@@ -63,8 +72,7 @@ QString trUser(const char *src)
|
||||
|
||||
} // namespace
|
||||
|
||||
AgentDuplicateResult duplicateAgentInUserDir(
|
||||
const AgentConfig &parent, const AgentFactory &factory)
|
||||
AgentDuplicateResult duplicateAgentInUserDir(const AgentConfig &parent, const AgentFactory &factory)
|
||||
{
|
||||
AgentDuplicateResult result;
|
||||
if (parent.name.trimmed().isEmpty()) {
|
||||
@@ -81,14 +89,14 @@ AgentDuplicateResult duplicateAgentInUserDir(
|
||||
const QString parentBasename = QFileInfo(parent.sourcePath).baseName();
|
||||
result.filePath = uniqueFilename(userDir, parentBasename);
|
||||
if (QFile::exists(result.filePath)) {
|
||||
result.error = trUser("Could not find a free filename after %1 attempts.")
|
||||
.arg(kMaxUniqueAttempts);
|
||||
result.error
|
||||
= trUser("Could not find a free filename after %1 attempts.").arg(kMaxUniqueAttempts);
|
||||
return result;
|
||||
}
|
||||
result.newName = uniqueName(parent.name, factory);
|
||||
if (factory.configByName(result.newName)) {
|
||||
result.error = trUser("Could not find a free agent name after %1 attempts.")
|
||||
.arg(kMaxUniqueAttempts);
|
||||
result.error
|
||||
= trUser("Could not find a free agent name after %1 attempts.").arg(kMaxUniqueAttempts);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -97,18 +105,17 @@ AgentDuplicateResult duplicateAgentInUserDir(
|
||||
result.error = trUser("Cannot create %1: %2").arg(result.filePath, f.errorString());
|
||||
return result;
|
||||
}
|
||||
const QString description
|
||||
= QStringLiteral("User customization of '%1'. Override fields below to taste; "
|
||||
"values not overridden are inherited from the parent.")
|
||||
.arg(parent.name);
|
||||
const QString body = QStringLiteral(
|
||||
"schema_version = 1\n"
|
||||
"name = \"%1\"\n"
|
||||
"extends = \"%2\"\n"
|
||||
"description = \"%3\"\n")
|
||||
.arg(tomlEscape(result.newName),
|
||||
tomlEscape(parent.name),
|
||||
tomlEscape(description));
|
||||
const QString description = QStringLiteral(
|
||||
"User customization of '%1'. Override fields below to taste; "
|
||||
"values not overridden are inherited from the parent.")
|
||||
.arg(parent.name);
|
||||
QString body
|
||||
= QStringLiteral(
|
||||
"schema_version = 1\n"
|
||||
"name = \"%1\"\n"
|
||||
"extends = \"%2\"\n"
|
||||
"description = \"%3\"\n")
|
||||
.arg(tomlEscape(result.newName), tomlEscape(parent.name), tomlEscape(description));
|
||||
const QByteArray payload = body.toUtf8();
|
||||
if (f.write(payload) != payload.size() || !f.commit()) {
|
||||
result.error = trUser("Failed to write %1: %2").arg(result.filePath, f.errorString());
|
||||
|
||||
Reference in New Issue
Block a user