Change filesystem locator filter to FilePath

Allows using the filesystem locator filter on remote systems.

Change-Id: I4753691cdc0ec8a0e598001371ff3cf6be44737c
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Eike Ziller
2023-02-22 11:18:05 +01:00
parent 6efecebb10
commit 89f98659ca
2 changed files with 38 additions and 34 deletions

View File

@@ -60,7 +60,7 @@ FileSystemFilter::FileSystemFilter()
void FileSystemFilter::prepareSearch(const QString &entry) void FileSystemFilter::prepareSearch(const QString &entry)
{ {
Q_UNUSED(entry) Q_UNUSED(entry)
m_currentDocumentDirectory = DocumentManager::fileDialogInitialDirectory().toString(); m_currentDocumentDirectory = DocumentManager::fileDialogInitialDirectory();
m_currentIncludeHidden = m_includeHidden; m_currentIncludeHidden = m_includeHidden;
} }
@@ -71,18 +71,21 @@ QList<LocatorFilterEntry> FileSystemFilter::matchesFor(QFutureInterface<LocatorF
Environment env = Environment::systemEnvironment(); Environment env = Environment::systemEnvironment();
const QString expandedEntry = env.expandVariables(entry); const QString expandedEntry = env.expandVariables(entry);
const auto expandedEntryPath = FilePath::fromUserInput(expandedEntry);
const auto absoluteEntryPath = m_currentDocumentDirectory.isEmpty()
? expandedEntryPath
: m_currentDocumentDirectory.resolvePath(expandedEntryPath);
const QFileInfo entryInfo(expandedEntry); // Consider the entered path a directory if it ends with slash/backslash.
const QString entryFileName = entryInfo.fileName(); // If it is a dir but doesn't end with a backslash, we want to still show all (other) matching
QString directory = entryInfo.path(); // items from the same parent directory.
if (entryInfo.isRelative()) { // Unfortunately fromUserInput removes slash/backslash at the end, so manually check the original.
if (entryInfo.filePath().startsWith("~/")) const bool isDir = expandedEntry.isEmpty() || expandedEntry.endsWith('/')
directory.replace(0, 1, QDir::homePath()); || expandedEntry.endsWith('\\');
else if (!m_currentDocumentDirectory.isEmpty()) const FilePath directory = isDir ? absoluteEntryPath : absoluteEntryPath.parentDir();
directory.prepend(m_currentDocumentDirectory + "/"); const QString entryFileName = isDir ? QString() : absoluteEntryPath.fileName();
}
const QDir dirInfo(directory); QDir::Filters dirFilter = QDir::Dirs | QDir::Drives | QDir::NoDot | QDir::NoDotDot;
QDir::Filters dirFilter = QDir::Dirs|QDir::Drives|QDir::NoDot|QDir::NoDotDot;
QDir::Filters fileFilter = QDir::Files; QDir::Filters fileFilter = QDir::Files;
if (m_currentIncludeHidden) { if (m_currentIncludeHidden) {
dirFilter |= QDir::Hidden; dirFilter |= QDir::Hidden;
@@ -91,25 +94,26 @@ QList<LocatorFilterEntry> FileSystemFilter::matchesFor(QFutureInterface<LocatorF
// use only 'name' for case sensitivity decision, because we need to make the path // use only 'name' for case sensitivity decision, because we need to make the path
// match the case on the file system for case-sensitive file systems // match the case on the file system for case-sensitive file systems
const Qt::CaseSensitivity caseSensitivity_ = caseSensitivity(entryFileName); const Qt::CaseSensitivity caseSensitivity_ = caseSensitivity(entryFileName);
const QStringList dirs = QStringList("..") const FilePaths dirs = FilePaths({directory / ".."})
+ dirInfo.entryList(dirFilter, QDir::Name|QDir::IgnoreCase|QDir::LocaleAware); + directory.dirEntries({{}, dirFilter},
const QStringList files = dirInfo.entryList(fileFilter, QDir::Name | QDir::IgnoreCase | QDir::LocaleAware);
QDir::Name|QDir::IgnoreCase|QDir::LocaleAware); const FilePaths files = directory.dirEntries({{}, fileFilter},
QDir::Name | QDir::IgnoreCase | QDir::LocaleAware);
QRegularExpression regExp = createRegExp(entryFileName, caseSensitivity_); QRegularExpression regExp = createRegExp(entryFileName, caseSensitivity_);
if (!regExp.isValid()) if (!regExp.isValid())
return {}; return {};
for (const QString &dir : dirs) { for (const FilePath &dir : dirs) {
if (future.isCanceled()) if (future.isCanceled())
break; break;
const QRegularExpressionMatch match = regExp.match(dir); const QString dirString = dir.relativeChildPath(directory).nativePath();
const QRegularExpressionMatch match = regExp.match(dirString);
if (match.hasMatch()) { if (match.hasMatch()) {
const MatchLevel level = matchLevelFor(match, dir); const MatchLevel level = matchLevelFor(match, dirString);
const QString fullPath = dirInfo.filePath(dir); LocatorFilterEntry filterEntry(this, dirString);
LocatorFilterEntry filterEntry(this, dir); filterEntry.filePath = dir;
filterEntry.filePath = FilePath::fromString(fullPath);
filterEntry.highlightInfo = highlightInfo(match); filterEntry.highlightInfo = highlightInfo(match);
entries[int(level)].append(filterEntry); entries[int(level)].append(filterEntry);
@@ -120,16 +124,16 @@ QList<LocatorFilterEntry> FileSystemFilter::matchesFor(QFutureInterface<LocatorF
regExp = createRegExp(link.targetFilePath.toString(), caseSensitivity_); regExp = createRegExp(link.targetFilePath.toString(), caseSensitivity_);
if (!regExp.isValid()) if (!regExp.isValid())
return {}; return {};
for (const QString &file : files) { for (const FilePath &file : files) {
if (future.isCanceled()) if (future.isCanceled())
break; break;
const QRegularExpressionMatch match = regExp.match(file); const QString fileString = file.relativeChildPath(directory).nativePath();
const QRegularExpressionMatch match = regExp.match(fileString);
if (match.hasMatch()) { if (match.hasMatch()) {
const MatchLevel level = matchLevelFor(match, file); const MatchLevel level = matchLevelFor(match, fileString);
const QString fullPath = dirInfo.filePath(file); LocatorFilterEntry filterEntry(this, fileString);
LocatorFilterEntry filterEntry(this, file); filterEntry.filePath = file;
filterEntry.filePath = FilePath::fromString(fullPath);
filterEntry.highlightInfo = highlightInfo(match); filterEntry.highlightInfo = highlightInfo(match);
filterEntry.linkForEditor = Link(filterEntry.filePath, link.targetLine, filterEntry.linkForEditor = Link(filterEntry.filePath, link.targetLine,
link.targetColumn); link.targetColumn);
@@ -138,12 +142,12 @@ QList<LocatorFilterEntry> FileSystemFilter::matchesFor(QFutureInterface<LocatorF
} }
// "create and open" functionality // "create and open" functionality
const QString fullFilePath = dirInfo.filePath(entryFileName); const FilePath fullFilePath = directory / entryFileName;
const bool containsWildcard = expandedEntry.contains('?') || expandedEntry.contains('*'); const bool containsWildcard = expandedEntry.contains('?') || expandedEntry.contains('*');
if (!containsWildcard && !QFileInfo::exists(fullFilePath) && dirInfo.exists()) { if (!containsWildcard && !fullFilePath.exists() && directory.exists()) {
LocatorFilterEntry createAndOpen(this, Tr::tr("Create and Open \"%1\"").arg(expandedEntry)); LocatorFilterEntry createAndOpen(this, Tr::tr("Create and Open \"%1\"").arg(expandedEntry));
createAndOpen.filePath = FilePath::fromString(fullFilePath); createAndOpen.filePath = fullFilePath;
createAndOpen.extraInfo = FilePath::fromString(dirInfo.absolutePath()).shortNativePath(); createAndOpen.extraInfo = directory.absoluteFilePath().shortNativePath();
entries[int(MatchLevel::Normal)].append(createAndOpen); entries[int(MatchLevel::Normal)].append(createAndOpen);
} }
@@ -186,7 +190,7 @@ void FileSystemFilter::accept(const LocatorFilterEntry &selection,
if (messageBox.isChecked()) if (messageBox.isChecked())
CheckableMessageBox::doNotAskAgain(ICore::settings(), kAlwaysCreate); CheckableMessageBox::doNotAskAgain(ICore::settings(), kAlwaysCreate);
} }
QFile file(selection.filePath.toString()); QFile file(selection.filePath.toFSPathString());
file.open(QFile::WriteOnly); file.open(QFile::WriteOnly);
file.close(); file.close();
VcsManager::promptToAdd(selection.filePath.absolutePath(), {selection.filePath}); VcsManager::promptToAdd(selection.filePath.absolutePath(), {selection.filePath});

View File

@@ -37,7 +37,7 @@ private:
static const bool kIncludeHiddenDefault = true; static const bool kIncludeHiddenDefault = true;
bool m_includeHidden = kIncludeHiddenDefault; bool m_includeHidden = kIncludeHiddenDefault;
bool m_currentIncludeHidden = kIncludeHiddenDefault; bool m_currentIncludeHidden = kIncludeHiddenDefault;
QString m_currentDocumentDirectory; Utils::FilePath m_currentDocumentDirectory;
}; };
} // namespace Internal } // namespace Internal