Commit 31748e73 authored by Leandro Melo's avatar Leandro Melo
Browse files

Generic highlighter: New option to specify ignored file patterns so the...

Generic highlighter: New option to specify ignored file patterns so the highlight popup does not show up.
parent c2393df0
......@@ -32,10 +32,9 @@
#include <coreplugin/icore.h>
#include <QtCore/QSettings>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QLatin1String>
#include <QtCore/QLatin1Char>
#include <QtCore/QDebug>
#ifdef Q_OS_UNIX
#include <QtCore/QDir>
#include <QtCore/QProcess>
......@@ -94,6 +93,7 @@ namespace {
static const QLatin1String kDefinitionFilesPath("DefinitionFilesPath");
static const QLatin1String kAlertWhenDefinitionIsNotFound("AlertWhenDefinitionsIsNotFound");
static const QLatin1String kIgnoredFilesPatterns("IgnoredFilesPatterns");
static const QLatin1String kGroupPostfix("HighlighterSettings");
QString groupSpecifier(const QString &postFix, const QString &category)
......@@ -117,6 +117,7 @@ void HighlighterSettings::toSettings(const QString &category, QSettings *s) cons
s->beginGroup(group);
s->setValue(kDefinitionFilesPath, m_definitionFilesPath);
s->setValue(kAlertWhenDefinitionIsNotFound, m_alertWhenNoDefinition);
s->setValue(kIgnoredFilesPatterns, ignoredFilesPatterns());
s->endGroup();
}
......@@ -129,11 +130,67 @@ void HighlighterSettings::fromSettings(const QString &category, QSettings *s)
else
m_definitionFilesPath = s->value(kDefinitionFilesPath, QString()).toString();
m_alertWhenNoDefinition = s->value(kAlertWhenDefinitionIsNotFound, true).toBool();
if (!s->contains(kIgnoredFilesPatterns))
assignInitialIgnoredPatterns();
else
setIgnoredFilesPatterns(s->value(kIgnoredFilesPatterns, QString()).toString());
s->endGroup();
}
void HighlighterSettings::setIgnoredFilesPatterns(const QString &patterns)
{
setExpressionsFromList(patterns.split(QLatin1Char(','), QString::SkipEmptyParts));
}
QString HighlighterSettings::ignoredFilesPatterns() const
{
return listFromExpressions().join(QLatin1String(","));
}
void HighlighterSettings::assignInitialIgnoredPatterns()
{
QStringList patterns;
patterns << QLatin1String("*.txt")
<< QLatin1String("LICENSE*")
<< QLatin1String("README")
<< QLatin1String("INSTALL")
<< QLatin1String("COPYING")
<< QLatin1String("NEWS");
setExpressionsFromList(patterns);
}
bool HighlighterSettings::isIgnoredFilePattern(const QString &fileName) const
{
foreach (const QRegExp &regExp, m_ignoredFiles)
if (regExp.indexIn(fileName) != -1)
return true;
return false;
}
bool HighlighterSettings::equals(const HighlighterSettings &highlighterSettings) const
{
return m_definitionFilesPath == highlighterSettings.m_definitionFilesPath &&
m_alertWhenNoDefinition == highlighterSettings.m_alertWhenNoDefinition;
m_alertWhenNoDefinition == highlighterSettings.m_alertWhenNoDefinition &&
m_ignoredFiles == highlighterSettings.m_ignoredFiles;
}
void HighlighterSettings::setExpressionsFromList(const QStringList &patterns)
{
m_ignoredFiles.clear();
QRegExp regExp;
regExp.setCaseSensitivity(Qt::CaseInsensitive);
regExp.setPatternSyntax(QRegExp::Wildcard);
foreach (const QString &s, patterns) {
regExp.setPattern(s);
m_ignoredFiles.append(regExp);
}
}
QStringList HighlighterSettings::listFromExpressions() const
{
QStringList patterns;
foreach (const QRegExp &regExp, m_ignoredFiles)
patterns.append(regExp.pattern());
return patterns;
}
......@@ -31,6 +31,9 @@
#define HIGHLIGHTERSETTINGS_H
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QList>
#include <QtCore/QRegExp>
QT_BEGIN_NAMESPACE
class QSettings;
......@@ -38,17 +41,35 @@ QT_END_NAMESPACE
namespace TextEditor {
struct HighlighterSettings
class HighlighterSettings
{
public:
HighlighterSettings();
void toSettings(const QString &category, QSettings *s) const;
void fromSettings(const QString &category, QSettings *s);
void setDefinitionFilesPath(const QString &path) { m_definitionFilesPath = path; }
const QString &definitionFilesPath() const { return m_definitionFilesPath; }
void setAlertWhenNoDefinition(const bool alert) { m_alertWhenNoDefinition = alert; }
bool alertWhenNoDefinition() const { return m_alertWhenNoDefinition; }
void setIgnoredFilesPatterns(const QString &patterns);
QString ignoredFilesPatterns() const;
bool isIgnoredFilePattern(const QString &fileName) const;
bool equals(const HighlighterSettings &highlighterSettings) const;
private:
void assignInitialIgnoredPatterns();
void setExpressionsFromList(const QStringList &patterns);
QStringList listFromExpressions() const;
bool m_alertWhenNoDefinition;
QString m_definitionFilesPath;
QList<QRegExp> m_ignoredFiles;
};
inline bool operator==(const HighlighterSettings &a, const HighlighterSettings &b)
......
......@@ -97,7 +97,8 @@ QWidget *HighlighterSettingsPage::createPage(QWidget *parent)
if (m_d->m_searchKeywords.isEmpty()) {
QTextStream(&m_d->m_searchKeywords) << m_d->m_page.definitionFilesGroupBox->title()
<< m_d->m_page.locationLabel->text()
<< m_d->m_page.alertWhenNoDefinition->text();
<< m_d->m_page.alertWhenNoDefinition->text()
<< m_d->m_page.ignoreLabel->text();
}
connect(m_d->m_page.resetButton, SIGNAL(clicked()), this, SLOT(resetDefinitionsLocation()));
......@@ -126,11 +127,12 @@ const HighlighterSettings &HighlighterSettingsPage::highlighterSettings() const
void HighlighterSettingsPage::settingsFromUI()
{
bool locationChanged = false;
if (m_d->m_settings.m_definitionFilesPath != m_d->m_page.definitionFilesPath->path())
if (m_d->m_settings.definitionFilesPath() != m_d->m_page.definitionFilesPath->path())
locationChanged = true;
m_d->m_settings.m_definitionFilesPath = m_d->m_page.definitionFilesPath->path();
m_d->m_settings.m_alertWhenNoDefinition = m_d->m_page.alertWhenNoDefinition->isChecked();
m_d->m_settings.setDefinitionFilesPath(m_d->m_page.definitionFilesPath->path());
m_d->m_settings.setAlertWhenNoDefinition(m_d->m_page.alertWhenNoDefinition->isChecked());
m_d->m_settings.setIgnoredFilesPatterns(m_d->m_page.ignoreEdit->text());
if (QSettings *s = Core::ICore::instance()->settings())
m_d->m_settings.toSettings(m_d->m_settingsPrefix, s);
......@@ -140,8 +142,9 @@ void HighlighterSettingsPage::settingsFromUI()
void HighlighterSettingsPage::settingsToUI()
{
m_d->m_page.definitionFilesPath->setPath(m_d->m_settings.m_definitionFilesPath);
m_d->m_page.alertWhenNoDefinition->setChecked(m_d->m_settings.m_alertWhenNoDefinition);
m_d->m_page.definitionFilesPath->setPath(m_d->m_settings.definitionFilesPath());
m_d->m_page.alertWhenNoDefinition->setChecked(m_d->m_settings.alertWhenNoDefinition());
m_d->m_page.ignoreEdit->setText(m_d->m_settings.ignoredFilesPatterns());
}
void HighlighterSettingsPage::resetDefinitionsLocation()
......@@ -151,9 +154,11 @@ void HighlighterSettingsPage::resetDefinitionsLocation()
bool HighlighterSettingsPage::settingsChanged() const
{
if (m_d->m_settings.m_definitionFilesPath != m_d->m_page.definitionFilesPath->path())
if (m_d->m_settings.definitionFilesPath() != m_d->m_page.definitionFilesPath->path())
return true;
if (m_d->m_settings.m_alertWhenNoDefinition != m_d->m_page.alertWhenNoDefinition->isChecked())
if (m_d->m_settings.alertWhenNoDefinition() != m_d->m_page.alertWhenNoDefinition->isChecked())
return true;
if (m_d->m_settings.ignoredFilesPatterns() != m_d->m_page.ignoreEdit->text())
return true;
return false;
}
......@@ -34,7 +34,7 @@
namespace TextEditor {
struct HighlighterSettings;
class HighlighterSettings;
class HighlighterSettingsPage : public TextEditorOptionsPage
{
......
......@@ -7,20 +7,13 @@
<x>0</x>
<y>0</y>
<width>521</width>
<height>230</height>
<height>332</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="alertWhenNoDefinition">
<property name="text">
<string>Alert when a highlight definition is not found.</string>
</property>
</widget>
</item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QGroupBox" name="definitionFilesGroupBox">
<property name="title">
......@@ -34,7 +27,7 @@
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;Files can be downloaded from the &lt;/span&gt;&lt;a href=&quot;http://kate-editor.org/downloads/syntax_highlighting&quot;&gt;&lt;span style=&quot; font-size:8pt; text-decoration: underline; color:#0000ff;&quot;&gt;Kate Text Editor&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt; website and stored at the location below.&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;Definitions can be downloaded from the &lt;/span&gt;&lt;a href=&quot;http://kate-editor.org/downloads/syntax_highlighting&quot;&gt;&lt;span style=&quot; font-size:8pt; text-decoration: underline; color:#0000ff;&quot;&gt;Kate Text Editor&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt; website and stored at the location below.&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
......@@ -75,6 +68,36 @@ p, li { white-space: pre-wrap; }
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="behaviorGroupBox">
<property name="title">
<string>Behavior</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="alertWhenNoDefinition">
<property name="text">
<string>Alert when a highlight definition is not found.</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="ignoreLabel">
<property name="text">
<string>Ignored file patterns:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="ignoreEdit"/>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
......
......@@ -146,7 +146,7 @@ void Manager::registerMimeTypes()
void Manager::gatherDefinitionsMimeTypes(QFutureInterface<Core::MimeType> &future)
{
const HighlighterSettings &settings = TextEditorSettings::instance()->highlighterSettings();
QDir definitionsDir(settings.m_definitionFilesPath);
QDir definitionsDir(settings.definitionFilesPath());
QStringList filter(QLatin1String("*.xml"));
definitionsDir.setNameFilters(filter);
......
......@@ -36,6 +36,7 @@
#include "highlightdefinition.h"
#include "highlighter.h"
#include "highlighterexception.h"
#include "highlightersettings.h"
#include "manager.h"
#include "context.h"
#include "normalindenter.h"
......@@ -189,8 +190,10 @@ void PlainTextEditor::configure(const Core::MimeType &mimeType)
m_isMissingSyntaxDefinition = false;
} catch (const HighlighterException &) {
}
} else if (file() && file()->fileName().endsWith(QLatin1String(".txt"))) {
m_isMissingSyntaxDefinition = false;
} else if (file()) {
const QString &fileName = file()->fileName();
if (TextEditorSettings::instance()->highlighterSettings().isIgnoredFilePattern(fileName))
m_isMissingSyntaxDefinition = false;
}
// @todo: Indentation specification through the definition files is not really being used
......
......@@ -91,7 +91,7 @@ void PlainTextEditorFactory::updateEditorInfoBar(Core::IEditor *editor)
if (editorEditable) {
PlainTextEditor *textEditor = static_cast<PlainTextEditor *>(editorEditable->editor());
if (textEditor->isMissingSyntaxDefinition() &&
TextEditorSettings::instance()->highlighterSettings().m_alertWhenNoDefinition) {
TextEditorSettings::instance()->highlighterSettings().alertWhenNoDefinition()) {
Core::EditorManager::instance()->showEditorInfoBar(
Constants::INFO_SYNTAX_DEFINITION,
tr("A highlight definition was not found for this file. Would you like to try to find one?"),
......
......@@ -43,7 +43,7 @@ struct StorageSettings;
struct BehaviorSettings;
struct DisplaySettings;
struct CompletionSettings;
struct HighlighterSettings;
class HighlighterSettings;
namespace Internal {
class TextEditorSettingsPrivate;
......
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