2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-05-26 10:58:44 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-05-26 10:58:44 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-05-26 10:58:44 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-05-26 10:58:44 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-05-26 10:58:44 +02:00
|
|
|
|
|
|
|
|
#include "highlightersettings.h"
|
|
|
|
|
|
2019-02-07 13:40:39 +01:00
|
|
|
#include "texteditorconstants.h"
|
|
|
|
|
|
2010-05-26 10:58:44 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2019-06-06 16:27:55 +02:00
|
|
|
|
|
|
|
|
#include <utils/fileutils.h>
|
2013-03-14 11:17:37 +01:00
|
|
|
#include <utils/hostosinfo.h>
|
2021-05-05 18:21:22 +02:00
|
|
|
#include <utils/qtcprocess.h>
|
2020-06-17 06:35:31 +02:00
|
|
|
#include <utils/stringutils.h>
|
2010-05-26 10:58:44 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QSettings>
|
|
|
|
|
#include <QLatin1String>
|
|
|
|
|
#include <QLatin1Char>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QFile>
|
2013-03-14 11:17:37 +01:00
|
|
|
#include <QStringList>
|
2010-05-26 10:58:44 +02:00
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2010-11-09 16:01:58 +01:00
|
|
|
QString findFallbackDefinitionsLocation()
|
2010-05-26 10:58:44 +02:00
|
|
|
{
|
2010-11-09 16:01:58 +01:00
|
|
|
QDir dir;
|
|
|
|
|
dir.setNameFilters(QStringList(QLatin1String("*.xml")));
|
|
|
|
|
|
2013-03-14 11:17:37 +01:00
|
|
|
if (Utils::HostOsInfo::isAnyUnixHost() && !Utils::HostOsInfo::isMacHost()) {
|
2018-11-25 18:52:41 +01:00
|
|
|
static const QLatin1String kateSyntaxPaths[] = {
|
2013-03-14 11:17:37 +01:00
|
|
|
QLatin1String("/share/apps/katepart/syntax"),
|
|
|
|
|
QLatin1String("/share/kde4/apps/katepart/syntax")
|
|
|
|
|
};
|
2010-05-28 14:37:25 +02:00
|
|
|
|
2013-03-14 11:17:37 +01:00
|
|
|
// Some wild guesses.
|
2018-11-25 18:52:41 +01:00
|
|
|
for (const auto &kateSyntaxPath : kateSyntaxPaths) {
|
|
|
|
|
const QStringList paths = {
|
|
|
|
|
QLatin1String("/usr") + kateSyntaxPath,
|
|
|
|
|
QLatin1String("/usr/local") + kateSyntaxPath,
|
|
|
|
|
QLatin1String("/opt") + kateSyntaxPath
|
|
|
|
|
};
|
|
|
|
|
for (const auto &path : paths) {
|
2013-03-14 11:17:37 +01:00
|
|
|
dir.setPath(path);
|
2010-11-09 16:01:58 +01:00
|
|
|
if (dir.exists() && !dir.entryInfoList().isEmpty())
|
2010-09-23 15:38:32 +02:00
|
|
|
return dir.path();
|
2010-09-15 16:09:39 +02:00
|
|
|
}
|
2010-05-26 10:58:44 +02:00
|
|
|
}
|
2013-03-14 11:17:37 +01:00
|
|
|
|
|
|
|
|
// Try kde-config.
|
2021-08-10 16:19:02 +02:00
|
|
|
const Utils::FilePath programs[] = {"kde-config", "kde4-config"};
|
|
|
|
|
for (const auto &program : programs) {
|
2021-06-22 04:33:47 +02:00
|
|
|
Utils::QtcProcess process;
|
2016-04-29 16:52:58 +02:00
|
|
|
process.setTimeoutS(5);
|
2021-05-17 12:02:42 +02:00
|
|
|
process.setCommand({program, {"--prefix"}});
|
|
|
|
|
process.runBlocking();
|
2021-05-28 13:19:38 +02:00
|
|
|
if (process.result() == Utils::QtcProcess::FinishedWithSuccess) {
|
2021-05-12 14:25:50 +02:00
|
|
|
QString output = process.stdOut();
|
2013-03-14 11:17:37 +01:00
|
|
|
output.remove(QLatin1Char('\n'));
|
2018-11-25 18:52:41 +01:00
|
|
|
for (auto &kateSyntaxPath : kateSyntaxPaths) {
|
|
|
|
|
dir.setPath(output + kateSyntaxPath);
|
2013-03-14 11:17:37 +01:00
|
|
|
if (dir.exists() && !dir.entryInfoList().isEmpty())
|
|
|
|
|
return dir.path();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-26 10:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:46:09 +02:00
|
|
|
dir.setPath(Core::ICore::resourcePath("generic-highlighter").toString());
|
2010-11-09 16:01:58 +01:00
|
|
|
if (dir.exists() && !dir.entryInfoList().isEmpty())
|
|
|
|
|
return dir.path();
|
|
|
|
|
|
2010-09-23 15:38:32 +02:00
|
|
|
return QString();
|
2010-05-26 10:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace TextEditor
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2010-08-06 13:10:21 +02:00
|
|
|
static const QLatin1String kDefinitionFilesPath("UserDefinitionFilesPath");
|
2010-06-04 10:13:38 +02:00
|
|
|
static const QLatin1String kIgnoredFilesPatterns("IgnoredFilesPatterns");
|
2010-05-26 10:58:44 +02:00
|
|
|
|
|
|
|
|
QString groupSpecifier(const QString &postFix, const QString &category)
|
|
|
|
|
{
|
|
|
|
|
if (category.isEmpty())
|
|
|
|
|
return postFix;
|
|
|
|
|
return QString(category + postFix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace anonymous
|
|
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
using namespace Internal;
|
|
|
|
|
|
|
|
|
|
void HighlighterSettings::toSettings(const QString &category, QSettings *s) const
|
|
|
|
|
{
|
2019-02-07 13:40:39 +01:00
|
|
|
const QString &group = groupSpecifier(Constants::HIGHLIGHTER_SETTINGS_CATEGORY, category);
|
2010-05-26 10:58:44 +02:00
|
|
|
s->beginGroup(group);
|
|
|
|
|
s->setValue(kDefinitionFilesPath, m_definitionFilesPath);
|
2010-06-04 10:13:38 +02:00
|
|
|
s->setValue(kIgnoredFilesPatterns, ignoredFilesPatterns());
|
2010-05-26 10:58:44 +02:00
|
|
|
s->endGroup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlighterSettings::fromSettings(const QString &category, QSettings *s)
|
|
|
|
|
{
|
2019-02-07 13:40:39 +01:00
|
|
|
const QString &group = groupSpecifier(Constants::HIGHLIGHTER_SETTINGS_CATEGORY, category);
|
2010-05-26 10:58:44 +02:00
|
|
|
s->beginGroup(group);
|
2010-08-06 13:10:21 +02:00
|
|
|
m_definitionFilesPath = s->value(kDefinitionFilesPath, QString()).toString();
|
2010-09-23 16:37:09 +02:00
|
|
|
if (!s->contains(kDefinitionFilesPath))
|
2010-09-29 13:52:58 +02:00
|
|
|
assignDefaultDefinitionsPath();
|
2010-09-23 16:37:09 +02:00
|
|
|
else
|
|
|
|
|
m_definitionFilesPath = s->value(kDefinitionFilesPath).toString();
|
2010-06-04 10:13:38 +02:00
|
|
|
if (!s->contains(kIgnoredFilesPatterns))
|
2010-09-29 13:52:58 +02:00
|
|
|
assignDefaultIgnoredPatterns();
|
2010-06-04 10:13:38 +02:00
|
|
|
else
|
|
|
|
|
setIgnoredFilesPatterns(s->value(kIgnoredFilesPatterns, QString()).toString());
|
2010-05-26 10:58:44 +02:00
|
|
|
s->endGroup();
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-04 10:13:38 +02:00
|
|
|
void HighlighterSettings::setIgnoredFilesPatterns(const QString &patterns)
|
|
|
|
|
{
|
2020-07-21 10:19:36 +02:00
|
|
|
setExpressionsFromList(patterns.split(QLatin1Char(','), Qt::SkipEmptyParts));
|
2010-06-04 10:13:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString HighlighterSettings::ignoredFilesPatterns() const
|
|
|
|
|
{
|
2014-08-23 01:19:53 +02:00
|
|
|
return listFromExpressions().join(QLatin1Char(','));
|
2010-06-04 10:13:38 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-29 13:52:58 +02:00
|
|
|
void HighlighterSettings::assignDefaultIgnoredPatterns()
|
2010-06-04 10:13:38 +02:00
|
|
|
{
|
|
|
|
|
QStringList patterns;
|
|
|
|
|
patterns << QLatin1String("*.txt")
|
|
|
|
|
<< QLatin1String("LICENSE*")
|
|
|
|
|
<< QLatin1String("README")
|
|
|
|
|
<< QLatin1String("INSTALL")
|
|
|
|
|
<< QLatin1String("COPYING")
|
2010-07-22 13:51:14 +02:00
|
|
|
<< QLatin1String("NEWS")
|
|
|
|
|
<< QLatin1String("qmldir");
|
2010-06-04 10:13:38 +02:00
|
|
|
setExpressionsFromList(patterns);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-29 13:52:58 +02:00
|
|
|
void HighlighterSettings::assignDefaultDefinitionsPath()
|
|
|
|
|
{
|
2021-04-26 15:46:09 +02:00
|
|
|
const QString path = Core::ICore::userResourcePath("generic-highlighter").toString();
|
2010-09-29 13:52:58 +02:00
|
|
|
if (QFile::exists(path) || QDir().mkpath(path))
|
|
|
|
|
m_definitionFilesPath = path;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-04 10:13:38 +02:00
|
|
|
bool HighlighterSettings::isIgnoredFilePattern(const QString &fileName) const
|
|
|
|
|
{
|
2020-06-16 08:02:59 +02:00
|
|
|
for (const QRegularExpression ®Exp : m_ignoredFiles)
|
|
|
|
|
if (fileName.indexOf(regExp) != -1)
|
2010-06-04 10:13:38 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 10:58:44 +02:00
|
|
|
bool HighlighterSettings::equals(const HighlighterSettings &highlighterSettings) const
|
|
|
|
|
{
|
2010-05-28 14:37:25 +02:00
|
|
|
return m_definitionFilesPath == highlighterSettings.m_definitionFilesPath &&
|
2010-06-04 10:13:38 +02:00
|
|
|
m_ignoredFiles == highlighterSettings.m_ignoredFiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlighterSettings::setExpressionsFromList(const QStringList &patterns)
|
|
|
|
|
{
|
|
|
|
|
m_ignoredFiles.clear();
|
2020-06-16 08:02:59 +02:00
|
|
|
QRegularExpression regExp;
|
|
|
|
|
regExp.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
|
2018-11-08 11:39:48 +01:00
|
|
|
for (const QString &pattern : patterns) {
|
2020-06-16 08:02:59 +02:00
|
|
|
regExp.setPattern(QRegularExpression::wildcardToRegularExpression(pattern));
|
2010-06-04 10:13:38 +02:00
|
|
|
m_ignoredFiles.append(regExp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList HighlighterSettings::listFromExpressions() const
|
|
|
|
|
{
|
|
|
|
|
QStringList patterns;
|
2020-06-16 08:02:59 +02:00
|
|
|
for (const QRegularExpression ®Exp : m_ignoredFiles)
|
2010-06-04 10:13:38 +02:00
|
|
|
patterns.append(regExp.pattern());
|
|
|
|
|
return patterns;
|
2010-05-26 10:58:44 +02:00
|
|
|
}
|