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>
|
2013-03-14 11:17:37 +01:00
|
|
|
#include <QStringList>
|
2010-05-26 10:58:44 +02:00
|
|
|
|
2021-09-27 19:05:08 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
2010-05-26 10:58:44 +02:00
|
|
|
namespace TextEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2021-09-27 19:05:08 +02:00
|
|
|
FilePath findFallbackDefinitionsLocation()
|
2010-05-26 10:58:44 +02:00
|
|
|
{
|
2021-09-27 19:05:08 +02:00
|
|
|
if (HostOsInfo::isAnyUnixHost() && !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) {
|
2021-09-27 19:05:08 +02:00
|
|
|
const FilePath paths[] = {
|
|
|
|
|
FilePath("/usr") / kateSyntaxPath,
|
|
|
|
|
FilePath("/usr/local") / kateSyntaxPath,
|
|
|
|
|
FilePath("/opt") / kateSyntaxPath
|
2018-11-25 18:52:41 +01:00
|
|
|
};
|
2021-09-27 19:05:08 +02:00
|
|
|
for (const FilePath &path : paths) {
|
2022-01-21 12:22:54 +01:00
|
|
|
if (path.exists() && !path.dirEntries({{"*.xml"}}).isEmpty())
|
2021-09-27 19:05:08 +02:00
|
|
|
return 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-09-27 19:05:08 +02:00
|
|
|
const FilePath programs[] = {"kde-config", "kde4-config"};
|
|
|
|
|
for (const FilePath &program : programs) {
|
|
|
|
|
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();
|
2022-03-02 04:12:25 +01:00
|
|
|
if (process.result() == ProcessResult::FinishedWithSuccess) {
|
2022-06-17 14:17:14 +02:00
|
|
|
QString output = process.cleanedStdOut();
|
2021-09-27 19:05:08 +02:00
|
|
|
output.remove('\n');
|
|
|
|
|
const FilePath dir = FilePath::fromString(output);
|
2018-11-25 18:52:41 +01:00
|
|
|
for (auto &kateSyntaxPath : kateSyntaxPaths) {
|
2021-09-27 19:05:08 +02:00
|
|
|
const FilePath path = dir / kateSyntaxPath;
|
2022-01-21 12:22:54 +01:00
|
|
|
if (path.exists() && !path.dirEntries({{"*.xml"}}).isEmpty())
|
2021-09-27 19:05:08 +02:00
|
|
|
return path;
|
2013-03-14 11:17:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-26 10:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-27 19:05:08 +02:00
|
|
|
const FilePath dir = Core::ICore::resourcePath("generic-highlighter");
|
2022-01-21 12:22:54 +01:00
|
|
|
if (dir.exists() && !dir.dirEntries({{"*.xml"}}).isEmpty())
|
2021-09-27 19:05:08 +02:00
|
|
|
return dir;
|
2010-11-09 16:01:58 +01:00
|
|
|
|
2021-09-27 19:05:08 +02:00
|
|
|
return {};
|
2010-05-26 10:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
|
2021-09-27 19:05:08 +02:00
|
|
|
const QLatin1String kDefinitionFilesPath("UserDefinitionFilesPath");
|
|
|
|
|
const QLatin1String kIgnoredFilesPatterns("IgnoredFilesPatterns");
|
2010-05-26 10:58:44 +02:00
|
|
|
|
2021-09-27 19:05:08 +02:00
|
|
|
static QString groupSpecifier(const QString &postFix, const QString &category)
|
2010-05-26 10:58:44 +02:00
|
|
|
{
|
|
|
|
|
if (category.isEmpty())
|
|
|
|
|
return postFix;
|
|
|
|
|
return QString(category + postFix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2021-09-27 19:05:08 +02:00
|
|
|
s->setValue(kDefinitionFilesPath, m_definitionFilesPath.toVariant());
|
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);
|
2021-09-27 19:05:08 +02:00
|
|
|
m_definitionFilesPath = FilePath::fromVariant(s->value(kDefinitionFilesPath));
|
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
|
2021-09-27 19:05:08 +02:00
|
|
|
m_definitionFilesPath = FilePath::fromVariant(s->value(kDefinitionFilesPath));
|
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)
|
|
|
|
|
{
|
2021-09-27 19:05:08 +02:00
|
|
|
setExpressionsFromList(patterns.split(',', Qt::SkipEmptyParts));
|
2010-06-04 10:13:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString HighlighterSettings::ignoredFilesPatterns() const
|
|
|
|
|
{
|
2021-09-27 19:05:08 +02:00
|
|
|
return listFromExpressions().join(',');
|
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
|
|
|
{
|
2021-09-27 19:05:08 +02:00
|
|
|
setExpressionsFromList({"*.txt",
|
|
|
|
|
"LICENSE*",
|
|
|
|
|
"README",
|
|
|
|
|
"INSTALL",
|
|
|
|
|
"COPYING",
|
|
|
|
|
"NEWS",
|
|
|
|
|
"qmldir"});
|
2010-06-04 10:13:38 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-29 13:52:58 +02:00
|
|
|
void HighlighterSettings::assignDefaultDefinitionsPath()
|
|
|
|
|
{
|
2021-09-27 19:05:08 +02:00
|
|
|
const FilePath path = Core::ICore::userResourcePath("generic-highlighter");
|
|
|
|
|
if (path.exists() || path.ensureWritableDir())
|
2010-09-29 13:52:58 +02:00
|
|
|
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
|
|
|
}
|
2021-09-27 19:05:08 +02:00
|
|
|
|
|
|
|
|
} // TextEditor
|