Skip to content

Commit 3f22df4

Browse files
committed
qmdiEditor, ProjectManagerPlugin: search for header/impl (178)
The `qmdiEditor` will try to look the file near the original file, like before. But if it fails - we now ask through the plugin system for a better file. Meaning, if the include/source files are in different locations in a project, pressing F4 will work as expected. closes #178
1 parent 358e592 commit 3f22df4

3 files changed

Lines changed: 70 additions & 24 deletions

File tree

src/GlobalCommands.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ inline constexpr const char *KeywordTooltip = "KeywordTooltip";
4242
// Open a new editor, with the attached document
4343
inline constexpr const char *DisplayText = "DisplayText";
4444

45+
// Used when pressing F4 on the editor
46+
inline constexpr const char *FindMatchingFile = "FindMatchingFile";
47+
4548
} // namespace GlobalCommands
4649

4750
namespace GlobalArguments {

src/plugins/ProjectManager/ProjectManagerPlg.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,9 @@ int ProjectManagerPlugin::canHandleAsyncCommand(const QString &command, const Co
850850
if (command == GlobalCommands::ClosedFile) {
851851
return true;
852852
}
853+
if (command == GlobalCommands::FindMatchingFile) {
854+
return true;
855+
}
853856
return false;
854857
}
855858

@@ -891,6 +894,9 @@ auto verifyRunnable(const QString &fileName) -> bool {
891894
return false;
892895
}
893896

897+
static const QStringList cExtensions = {"c", "cpp", "cxx", "cc", "c++"};
898+
static const QStringList headerExtensions = {"h", "hpp", "hh"};
899+
894900
QFuture<CommandArgs> ProjectManagerPlugin::handleCommandAsync(const QString &command,
895901
const CommandArgs &args) {
896902
if (command == GlobalCommands::LoadedFile) {
@@ -920,6 +926,33 @@ QFuture<CommandArgs> ProjectManagerPlugin::handleCommandAsync(const QString &com
920926
}
921927
return {};
922928
}
929+
if (command == GlobalCommands::FindMatchingFile) {
930+
auto filename = args[GlobalArguments::FileName].toString();
931+
auto allFiles = gui->filesList->getAllFiles();
932+
auto fileInfo = QFileInfo(filename);
933+
auto baseName = fileInfo.baseName();
934+
auto suffix = fileInfo.suffix().toLower();
935+
auto targetExtensions = QStringList();
936+
937+
if (cExtensions.contains(suffix)) {
938+
targetExtensions = headerExtensions;
939+
} else if (headerExtensions.contains(suffix)) {
940+
targetExtensions = cExtensions;
941+
}
942+
943+
if (!targetExtensions.isEmpty()) {
944+
for (const auto &file : allFiles) {
945+
auto fi = QFileInfo(file);
946+
if (fi.baseName() == baseName && targetExtensions.contains(fi.suffix().toLower())) {
947+
auto fullFileName =
948+
gui->filesList->getDir() + QDir::separator() + fi.filePath();
949+
fullFileName = QDir::cleanPath(fullFileName);
950+
return QtFuture::makeReadyValueFuture(
951+
CommandArgs{{GlobalArguments::FileName, fullFileName}});
952+
}
953+
}
954+
}
955+
}
923956
return {};
924957
}
925958

src/widgets/qmdieditor.cpp

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,41 @@
6464
#define PLATFORM_LINE_ENDING "\n"
6565
#endif
6666

67-
auto static getCorrespondingFile(const QString &fileName) -> QFuture<QString> {
68-
return QtConcurrent::run([fileName]() {
69-
auto static const cExtensions = QStringList{"c", "cpp", "cxx", "cc", "c++"};
70-
auto static const headerExtensions = QStringList{"h", "hpp", "hh"};
71-
72-
auto fileInfo = QFileInfo(fileName);
73-
auto baseName = fileInfo.baseName();
74-
auto dirPath = fileInfo.absolutePath();
75-
76-
if (cExtensions.contains(fileInfo.suffix(), Qt::CaseInsensitive)) {
77-
for (const auto &headerExt : headerExtensions) {
78-
auto headerFileName = dirPath + QDir::separator() + baseName + "." + headerExt;
79-
if (QFileInfo::exists(headerFileName)) {
80-
return headerFileName;
81-
}
67+
auto static const cExtensions = QStringList{"c", "cpp", "cxx", "cc", "c++"};
68+
auto static const headerExtensions = QStringList{"h", "hpp", "hh"};
69+
70+
auto static getCorrespondingFile(PluginManager *manager, const QString &fileName)
71+
-> QFuture<QString> {
72+
73+
// First - choose easy solution, file aside the original one
74+
auto fileInfo = QFileInfo(fileName);
75+
auto baseName = fileInfo.baseName();
76+
auto dirPath = fileInfo.absolutePath();
77+
78+
if (cExtensions.contains(fileInfo.suffix(), Qt::CaseInsensitive)) {
79+
for (const auto &headerExt : headerExtensions) {
80+
auto headerFileName = dirPath + QDir::separator() + baseName + "." + headerExt;
81+
if (QFileInfo::exists(headerFileName)) {
82+
return QtFuture::makeReadyValueFuture(headerFileName);
8283
}
83-
} else if (headerExtensions.contains(fileInfo.suffix(), Qt::CaseInsensitive)) {
84-
for (const auto &cExt : cExtensions) {
85-
auto cFileName = dirPath + QDir::separator() + baseName + "." + cExt;
86-
if (QFileInfo::exists(cFileName)) {
87-
return cFileName;
88-
}
84+
}
85+
} else if (headerExtensions.contains(fileInfo.suffix(), Qt::CaseInsensitive)) {
86+
for (const auto &cExt : cExtensions) {
87+
auto cFileName = dirPath + QDir::separator() + baseName + "." + cExt;
88+
if (QFileInfo::exists(cFileName)) {
89+
return QtFuture::makeReadyValueFuture(cFileName);
8990
}
9091
}
91-
return QString{};
92-
});
92+
}
93+
94+
// Did not work, lets ask someone - find me the corresponding file
95+
// This should be the project manager replying to this, from currently
96+
// active project. Or maybe from all active projects.
97+
return manager->handleCommandAsync(GlobalCommands::FindMatchingFile,
98+
{
99+
{GlobalArguments::FileName, fileName},
100+
})
101+
.then([](const CommandArgs &args) { return args.value(GlobalArguments::FileName).toString(); });
93102
}
94103

95104
auto static runningUnderGnome() -> bool {
@@ -1365,7 +1374,8 @@ void qmdiEditor::fileMessage_clicked(const QString &s) {
13651374
}
13661375

13671376
void qmdiEditor::toggleHeaderImpl() {
1368-
getCorrespondingFile(fileName).then(this, [this](const QString &otherFile) {
1377+
auto manager = dynamic_cast<PluginManager *>(mdiServer->mdiHost);
1378+
getCorrespondingFile(manager, fileName).then(this, [this](const QString &otherFile) {
13691379
auto nativeFile = QDir::toNativeSeparators(otherFile);
13701380
if (!nativeFile.isEmpty()) {
13711381
auto pluginManager = dynamic_cast<PluginManager *>(mdiServer->mdiHost);

0 commit comments

Comments
 (0)