Commit 12c236c5 authored by Ahmad Samir's avatar Ahmad Samir Committed by Kurt Hindenburg
Browse files

FileFilter: update the QRE pattern if "word characters" change

Add a new function to update the regex pattern in FileFilter, rather than
recreating a FileFilter object.
parent b21938e5
......@@ -17,14 +17,20 @@
using namespace Konsole;
FileFilter::FileFilter(Session *session) :
// static
QRegularExpression FileFilter::_regex;
FileFilter::FileFilter(Session *session, const QString &wordCharacters) :
_session(session)
, _dirPath(QString())
, _currentDirContents()
{
Profile::Ptr profile = SessionManager::instance()->sessionProfile(_session);
QString wordCharacters = profile->wordCharacters();
_regex = QRegularExpression(concatRegexPattern(wordCharacters), QRegularExpression::DontCaptureOption);
setRegExp(_regex);
}
QString FileFilter::concatRegexPattern(QString wordCharacters) const
{
/* The wordCharacters can be a potentially broken regexp,
* so let's fix it manually if it has some troublesome characters.
*/
......@@ -40,7 +46,7 @@ FileFilter::FileFilter(Session *session) :
wordCharacters.append(QLatin1Char('-'));
}
static const auto re = QRegularExpression(
const QString pattern =
/* First part of the regexp means 'strings with spaces and starting with single quotes'
* Second part means "Strings with double quotes"
* Last part means "Everything else plus some special chars
......@@ -61,12 +67,10 @@ FileFilter::FileFilter(Session *session) :
// next | branch (ctest stuff)
+ QStringLiteral(R"RX(|([^\n\s/\[]/)?[\p{L}\w%1]+(:\d+)?(:\d+:)?)RX").arg(wordCharacters)
// - ctest error output: "[/path/to/file(123)]"
+ QStringLiteral(R"RX(|\[[/\w%1]+\(\d+\)\])RX").arg(wordCharacters),
QRegularExpression::DontCaptureOption
);
setRegExp(re);
}
+ QStringLiteral(R"RX(|\[[/\w%1]+\(\d+\)\])RX").arg(wordCharacters);
return pattern;
}
/**
* File Filter - Construct a filter that works on local file paths using the
......@@ -129,3 +133,9 @@ void FileFilter::process()
RegExpFilter::process();
}
void FileFilter::updateRegex(const QString &wordCharacters)
{
_regex.setPattern(concatRegexPattern(wordCharacters));
setRegExp(_regex);
}
......@@ -26,17 +26,22 @@ class HotSpot;
class FileFilter : public RegExpFilter
{
public:
explicit FileFilter(Session *session);
explicit FileFilter(Session *session, const QString &wordCharacters);
void process() override;
void updateRegex(const QString &wordCharacters);
protected:
QSharedPointer<HotSpot> newHotSpot(int, int, int, int, const QStringList &) override;
private:
QString concatRegexPattern(QString wordCharacters) const;
QPointer<Session> _session;
QString _dirPath;
QSet<QString> _currentDirContents;
static QRegularExpression _regex;
};
}
......
......@@ -1270,6 +1270,9 @@ void SessionController::updateFilterList(Profile::Ptr profile)
return;
}
const QString currentWordCharacters = profile->wordCharacters();
static QString _wordChars = currentWordCharacters;
//TODO: Refactor this code.
bool underlineFiles = profile->underlineFilesEnabled();
FilterChain *filterChain = _sessionDisplayConnection->view()->filterChain();
......@@ -1277,9 +1280,18 @@ void SessionController::updateFilterList(Profile::Ptr profile)
filterChain->removeFilter(_fileFilter);
delete _fileFilter;
_fileFilter = nullptr;
} else if (underlineFiles && (_fileFilter == nullptr)) {
_fileFilter = new FileFilter(_sessionDisplayConnection->session());
filterChain->addFilter(_fileFilter);
} else if (underlineFiles) {
if (_fileFilter == nullptr) {
_fileFilter = new FileFilter(_sessionDisplayConnection->session(), currentWordCharacters);
filterChain->addFilter(_fileFilter);
} else {
// If wordCharacters changed, we need to change the static regex
// pattern in _fileFilter
if (_wordChars != currentWordCharacters) {
_wordChars = currentWordCharacters;
_fileFilter->updateRegex(currentWordCharacters);
}
}
}
bool underlineLinks = profile->underlineLinksEnabled();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment