Skip to content

Commit 513c729

Browse files
committed
git: save repo root
When requesting to show a diff - also pass the repo root. Also save the repo root in the file state. This means that even saved diff will work.
1 parent 8fa5c1a commit 513c729

4 files changed

Lines changed: 43 additions & 20 deletions

File tree

src/plugins/git/GitPlugin.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ void GitPlugin::diffFileHandler() {
198198
return;
199199
}
200200
auto position = manager->getMdiServer()->getClientIndex(client);
201+
repoRoot = detectRepoRoot(repoRoot);
201202
CommandArgs args = {
202203
{GlobalArguments::FileName, QString("%1.diff").arg(client->mdiClientName)},
203204
{GlobalArguments::Content, diff},
204205
{GlobalArguments::ReadOnly, true},
205206
{GlobalArguments::Position, position},
207+
{GlobalArguments::SourceDirectory, repoRoot},
206208
};
207209
manager->handleCommandAsync(GlobalCommands::DisplayText, args);
208210
}
@@ -283,6 +285,7 @@ void GitPlugin::diffBranchHandler() {
283285
{GlobalArguments::Content, diff},
284286
{GlobalArguments::ReadOnly, true},
285287
{GlobalArguments::FoldTopLevel, true},
288+
{GlobalArguments::SourceDirectory, repoRoot},
286289
};
287290
manager->handleCommandAsync(GlobalCommands::DisplayText, args);
288291
}
@@ -384,6 +387,7 @@ void GitPlugin::on_gitCommitClicked(const QModelIndex &mi) {
384387
{GlobalArguments::FileName, displayName},
385388
{GlobalArguments::Content, diff},
386389
{GlobalArguments::ReadOnly, true},
390+
{GlobalArguments::SourceDirectory, getConfig().getGitLastDir()},
387391
};
388392
manager->handleCommandAsync(GlobalCommands::DisplayText, args);
389393
});
@@ -417,6 +421,7 @@ void GitPlugin::on_gitCommitDoubleClicked(const QModelIndex &mi) {
417421
{GlobalArguments::Content, *fullCommit.raw},
418422
{GlobalArguments::ReadOnly, true},
419423
{GlobalArguments::FoldTopLevel, true},
424+
{GlobalArguments::SourceDirectory, getConfig().getGitLastDir()},
420425
};
421426
manager->handleCommandAsync(GlobalCommands::DisplayText, args);
422427
}

src/plugins/texteditor/texteditor_plg.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ QFuture<CommandArgs> TextEditorPlugin::handleCommandAsync(const QString &command
315315
auto content = args[GlobalArguments::Content].toString();
316316
auto isReadOnly = args[GlobalArguments::ReadOnly].toBool();
317317
auto shouldFold = args[GlobalArguments::FoldTopLevel].toBool();
318+
auto sourceDir = args[GlobalArguments::SourceDirectory].toString();
318319
auto ok = false;
319320
auto clientPosition = args[GlobalArguments::Position].toInt(&ok);
320321
if (!ok) {
@@ -334,7 +335,7 @@ QFuture<CommandArgs> TextEditorPlugin::handleCommandAsync(const QString &command
334335
editor->setHistoryModel(historyModel);
335336
editor->setPlainText(content);
336337
editor->setReadOnly(isReadOnly);
337-
editor->updateInternalMappings();
338+
editor->updateInternalMappings(sourceDir);
338339
applySettings(editor);
339340
mdiServer->addClient(editor, clientPosition);
340341
if (shouldFold) {

src/widgets/qmdieditor.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ qmdiClientState qmdiEditor::getState() const {
505505
state[StateConstants::ROW] = row;
506506
state[StateConstants::ZOOM] = zoom;
507507
state[StateConstants::READ_ONLY] = textEditor->isReadOnly();
508+
state[StateConstants::BASE_DIR] = diffMetadata.baseDir;
508509

509510
if (!uid.isEmpty()) {
510511
state[StateConstants::UUID] = uid;
@@ -562,6 +563,7 @@ void qmdiEditor::setState(const qmdiClientState &state) {
562563
if (state.contains(StateConstants::READ_ONLY)) {
563564
textEditor->setReadOnly(state[StateConstants::READ_ONLY].toBool());
564565
}
566+
diffMetadata.baseDir = state[StateConstants::BASE_DIR].toString();
565567
}
566568

567569
void qmdiEditor::on_client_unmerged(qmdiHost *host) {
@@ -934,9 +936,19 @@ bool qmdiEditor::eventFilter(QObject *watched, QEvent *event) {
934936
if (block.isValid()) {
935937
auto blockNumber = block.blockNumber();
936938
auto text = block.text();
937-
if (patchMappings.contains(blockNumber)) {
938-
auto l = patchMappings[blockNumber];
939-
qDebug() << "Should open " << l.file << "line: " << l.newLine;
939+
if (diffMetadata.mappings.contains(blockNumber)) {
940+
auto l = diffMetadata.mappings[blockNumber];
941+
auto pluginManager = dynamic_cast<PluginManager *>(mdiServer->mdiHost);
942+
if (pluginManager) {
943+
// Lines start on the editor from 0
944+
if (l.newLine >= 0) {
945+
pluginManager->openFile(l.file, l.newLine - 1);
946+
} else {
947+
pluginManager->openFile(l.file, l.oldLine - 1);
948+
}
949+
} else {
950+
qDebug() << "qmdiEditor::eventFilter - cannot open file from diff/patch";
951+
}
940952
}
941953
}
942954
}
@@ -1397,7 +1409,7 @@ void qmdiEditor::loadContent(bool useBackup) {
13971409

13981410
updateClientName();
13991411
setState(savedState);
1400-
updateInternalMappings();
1412+
updateInternalMappings(savedState[StateConstants::BASE_DIR].toString());
14011413
}
14021414

14031415
void qmdiEditor::chooseHighliter(const QString &newText) {
@@ -1427,8 +1439,10 @@ void qmdiEditor::findText(const QString &text) {
14271439
textEditor->setTextCursor(c);
14281440
}
14291441

1430-
void qmdiEditor::updateInternalMappings() {
1431-
patchMappings.clear();
1442+
void qmdiEditor::updateInternalMappings(const QString& baseDir) {
1443+
diffMetadata.mappings.clear();
1444+
diffMetadata.baseDir = baseDir;
1445+
14321446
if (!mdiClientName.endsWith(".diff",Qt::CaseInsensitive) && mdiClientName.endsWith(".patch", Qt::CaseInsensitive)) {
14331447
return;
14341448
}
@@ -1511,18 +1525,17 @@ void qmdiEditor::updateInternalMappings() {
15111525

15121526
auto i = line.lineNumber();
15131527
if (text.startsWith(' ')) {
1514-
patchMappings[i] = {current_file,old_line,new_line};
1528+
diffMetadata.mappings[i] = {baseDir + "/" + current_file,old_line,new_line};
15151529
old_line += 1;
15161530
new_line += 1;
15171531
} else if (text.startsWith('-')) {
1518-
patchMappings[i] = {current_file,old_line,-1};
1532+
diffMetadata.mappings[i] = {baseDir + "/" + current_file,old_line,-1};
15191533
old_line += 1;
15201534
} else if (text.startsWith('+')) {
1521-
patchMappings[i] = {current_file,-1,new_line};
1535+
diffMetadata.mappings[i] = {baseDir + "/" + current_file,-1,new_line};
15221536
new_line += 1;
15231537
}
15241538
}
1525-
15261539
}
15271540

15281541
/**

src/widgets/qmdieditor.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class qmdiEditor : public QWidget, public qmdiClient {
121121
void chooseHighliter(const QString &newText);
122122
void chooseIndenter(const QAction *action);
123123
void findText(const QString &text);
124-
void updateInternalMappings();
124+
void updateInternalMappings(const QString &baseDir);
125125

126126
private slots:
127127
void updateFileDetails();
@@ -235,6 +235,7 @@ class qmdiEditor : public QWidget, public qmdiClient {
235235
static constexpr const char *SEL_POSITION = "sel-position";
236236
static constexpr const char *UUID = "uuid";
237237
static constexpr const char *READ_ONLY = "read-only";
238+
static constexpr const char *BASE_DIR = "basedir";
238239
};
239240

240241
QString fileName;
@@ -277,14 +278,17 @@ class qmdiEditor : public QWidget, public qmdiClient {
277278
// and the file and offset inside it.
278279
QMap<int, diffLineNumber> patchMappings;
279280

280-
struct diffLineNumber {
281-
QString file;
282-
int oldLine = -1;
283-
int newLine = -1;
284-
};
285-
// When loading a path/diff file - this maps between lines
286-
// and the file and offset inside it.
287-
QMap<int, diffLineNumber> patchMappings;
281+
struct {
282+
struct diffLineNumber {
283+
QString file;
284+
int oldLine = -1;
285+
int newLine = -1;
286+
};
287+
// When loading a path/diff file - this maps between lines
288+
// and the file and offset inside it.
289+
QMap<int, diffLineNumber> mappings;
290+
QString baseDir;
291+
} diffMetadata;
288292

289293
QTimer *autoSaveTimer = nullptr;
290294
QString uid;

0 commit comments

Comments
 (0)