kate: reuse single agent, write cwd on project change
Instead of one-agent-per-project-directory, reuse the first agent in the kate session. When the project changes, write the new directory to the agent's ctl file (cwd command). Simpler model — one kate session, one agent, cwd follows the active project.
This commit is contained in:
parent
405095716a
commit
1df0daf9de
|
|
@ -147,58 +147,54 @@ void OllieKateView::ensureAgent()
|
|||
m_sessionId = "kate";
|
||||
}
|
||||
|
||||
// Try to find existing agent for project
|
||||
if (selectAgentForProject()) return;
|
||||
// Look for an existing agent in the kate session
|
||||
QStringList agents = m_9p->ls("session/kate/agent");
|
||||
agents.removeAll("new");
|
||||
agents.removeAll("idx");
|
||||
|
||||
// Create a new agent
|
||||
QString cwd = projectBaseDir();
|
||||
if (cwd.isEmpty()) cwd = QDir::homePath();
|
||||
if (!agents.isEmpty()) {
|
||||
m_agentId = agents.first();
|
||||
} else {
|
||||
// Create one
|
||||
QString cwd = projectBaseDir();
|
||||
if (cwd.isEmpty()) cwd = QDir::homePath();
|
||||
|
||||
QString agentCfg = "cwd=" + cwd + "\n";
|
||||
QByteArray resp = m_9p->rdwr("session/kate/agent/new", agentCfg.toUtf8());
|
||||
if (!resp.isEmpty()) {
|
||||
m_agentId = QString::fromUtf8(resp).trimmed();
|
||||
QString agentCfg = "cwd=" + cwd + "\n";
|
||||
QByteArray resp = m_9p->rdwr("session/kate/agent/new", agentCfg.toUtf8());
|
||||
if (!resp.isEmpty()) {
|
||||
m_agentId = QString::fromUtf8(resp).trimmed();
|
||||
}
|
||||
}
|
||||
updateStatusLabel();
|
||||
}
|
||||
|
||||
bool OllieKateView::selectAgentForProject()
|
||||
{
|
||||
QString baseDir = projectBaseDir();
|
||||
if (baseDir.isEmpty()) return false;
|
||||
|
||||
// Check if kate session exists
|
||||
QByteArray sessCheck = m_9p->read("session/kate/id");
|
||||
if (sessCheck.isEmpty()) return false;
|
||||
m_sessionId = "kate";
|
||||
|
||||
QString canonicalBase = QFileInfo(baseDir).canonicalFilePath();
|
||||
if (canonicalBase.isEmpty()) canonicalBase = baseDir;
|
||||
while (canonicalBase.endsWith('/')) canonicalBase.chop(1);
|
||||
// Find first agent in the session
|
||||
QStringList agents = m_9p->ls("session/kate/agent");
|
||||
agents.removeAll("new");
|
||||
agents.removeAll("idx");
|
||||
|
||||
// Read agent index
|
||||
QByteArray idxRaw = m_9p->read("session/kate/agent/idx");
|
||||
QString idxData = QString::fromUtf8(idxRaw).trimmed();
|
||||
if (agents.isEmpty()) return false;
|
||||
|
||||
for (const QString &line : idxData.split(QChar('\n'), Qt::SkipEmptyParts)) {
|
||||
QStringList fields = line.split(QChar('\t'));
|
||||
if (fields.size() >= 3) {
|
||||
QString agentId = fields[1];
|
||||
QString agentCwd = fields[2];
|
||||
|
||||
QString canonicalCwd = QFileInfo(agentCwd).canonicalFilePath();
|
||||
if (canonicalCwd.isEmpty()) canonicalCwd = agentCwd;
|
||||
while (canonicalCwd.endsWith('/')) canonicalCwd.chop(1);
|
||||
|
||||
if (canonicalCwd == canonicalBase) {
|
||||
if (agentId != m_agentId) {
|
||||
switchToAgent(agentId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
QString agentId = agents.first();
|
||||
if (agentId != m_agentId) {
|
||||
m_agentId = agentId;
|
||||
}
|
||||
return false;
|
||||
|
||||
// Update cwd to match current project
|
||||
QString baseDir = projectBaseDir();
|
||||
if (!baseDir.isEmpty() && !m_agentId.isEmpty()) {
|
||||
m_9p->write(agentPath() + "/ctl", ("cwd " + baseDir).toUtf8());
|
||||
}
|
||||
|
||||
updateStatusLabel();
|
||||
return true;
|
||||
}
|
||||
|
||||
void OllieKateView::switchToAgent(const QString &agentId)
|
||||
|
|
@ -210,7 +206,11 @@ void OllieKateView::switchToAgent(const QString &agentId)
|
|||
|
||||
void OllieKateView::onProjectChanged()
|
||||
{
|
||||
selectAgentForProject();
|
||||
// Update the agent's cwd to the new project directory
|
||||
QString baseDir = projectBaseDir();
|
||||
if (!baseDir.isEmpty() && !m_agentId.isEmpty()) {
|
||||
m_9p->write(agentPath() + "/ctl", ("cwd " + baseDir).toUtf8());
|
||||
}
|
||||
updateStatusLabel();
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in New Issue