diff --git a/src/plugins/designer/formwindoweditor.cpp b/src/plugins/designer/formwindoweditor.cpp index 895ac3aae16d31dfcccbb6cc86b2f6d2e0f83bd8..76d606a9a2373af11dc30fdb7824cd6f3117f2a1 100644 --- a/src/plugins/designer/formwindoweditor.cpp +++ b/src/plugins/designer/formwindoweditor.cpp @@ -39,6 +39,7 @@ #include <coreplugin/icore.h> #include <coreplugin/coreconstants.h> #include <coreplugin/uniqueidmanager.h> +#include <coreplugin/mimedatabase.h> #include <texteditor/basetextdocument.h> #include <texteditor/plaintexteditor.h> @@ -82,6 +83,7 @@ FormWindowEditor::FormWindowEditor(Internal::DesignerXmlEditor *editor, // Force update of open editors model. connect(&(d->m_file), SIGNAL(saved()), this, SIGNAL(changed())); connect(&(d->m_file), SIGNAL(changed()), this, SIGNAL(changed())); + connect(this, SIGNAL(changed()), this, SLOT(configureXmlEditor())); } FormWindowEditor::~FormWindowEditor() @@ -164,6 +166,15 @@ void FormWindowEditor::syncXmlEditor() syncXmlEditor(contents()); } +void FormWindowEditor::configureXmlEditor() const +{ + TextEditor::PlainTextEditor *editor = + qobject_cast<TextEditor::PlainTextEditor *>(d->m_textEditable.editor()); + if (editor) + editor->configure(Core::ICore::instance()->mimeDatabase()->findByFile( + d->m_file.fileName())); +} + void FormWindowEditor::syncXmlEditor(const QString &contents) { d->m_textEditable.editor()->setPlainText(contents); diff --git a/src/plugins/designer/formwindoweditor.h b/src/plugins/designer/formwindoweditor.h index d85845e7d2d59bd9239a5b2db3d3df41c966db09..d432189d29434ec5268333504b95b3e30d4069f3 100644 --- a/src/plugins/designer/formwindoweditor.h +++ b/src/plugins/designer/formwindoweditor.h @@ -102,6 +102,7 @@ public: public slots: void syncXmlEditor(); + void configureXmlEditor() const; private slots: void slotOpen(const QString &fileName); diff --git a/src/plugins/qmljseditor/QmlJSEditor.mimetypes.xml b/src/plugins/qmljseditor/QmlJSEditor.mimetypes.xml index 480adbdf30b000d589ed62560bb7484d2b87f913..4f17f907ecf64f97de87ace3cb8242d5a1e4837f 100644 --- a/src/plugins/qmljseditor/QmlJSEditor.mimetypes.xml +++ b/src/plugins/qmljseditor/QmlJSEditor.mimetypes.xml @@ -8,6 +8,7 @@ <mime-type type="application/javascript"> <alias type="application/x-javascript"/> <alias type="text/javascript"/> + <alias type="text/x-javascript"/> <sub-class-of type="text/plain"/> <comment>Qt Script file</comment> <glob pattern="*.js"/> diff --git a/src/plugins/texteditor/TextEditor.mimetypes.xml b/src/plugins/texteditor/TextEditor.mimetypes.xml index 913b9fe0ebaf6e18b99c60e040059a9d607e7635..c2b7765b31695c8d876bca9e233ed6ea705b1d4a 100644 --- a/src/plugins/texteditor/TextEditor.mimetypes.xml +++ b/src/plugins/texteditor/TextEditor.mimetypes.xml @@ -6,6 +6,7 @@ <glob pattern="*.txt"/> </mime-type> <mime-type type="application/xml"> + <alias type="text/xml"/> <sub-class-of type="text/plain"/> <comment>XML document</comment> <glob pattern="*.xml"/> diff --git a/src/plugins/texteditor/generichighlighter/manager.cpp b/src/plugins/texteditor/generichighlighter/manager.cpp index 1c41983e20fd5c5a526cf23e0434d8485c5645ed..ede33fc8e81d603f9d4f320fe8bb875ddca43f5d 100644 --- a/src/plugins/texteditor/generichighlighter/manager.cpp +++ b/src/plugins/texteditor/generichighlighter/manager.cpp @@ -78,8 +78,6 @@ QString Manager::definitionIdByName(const QString &name) const QString Manager::definitionIdByMimeType(const QString &mimeType) const { - Q_ASSERT(!mimeType.isEmpty()); - if (m_idByMimeType.count(mimeType) <= 1) { return m_idByMimeType.value(mimeType); } else { @@ -94,6 +92,17 @@ QString Manager::definitionIdByMimeType(const QString &mimeType) const } } +QString Manager::definitionIdByAnyMimeType(const QStringList &mimeTypes) const +{ + QString definitionId; + foreach (const QString &mimeType, mimeTypes) { + definitionId = definitionIdByMimeType(mimeType); + if (!definitionId.isEmpty()) + break; + } + return definitionId; +} + const QSharedPointer<HighlightDefinition> &Manager::definition(const QString &id) { if (!m_definitions.contains(id)) { diff --git a/src/plugins/texteditor/generichighlighter/manager.h b/src/plugins/texteditor/generichighlighter/manager.h index 2c0073eecc5c36ded119f685eed5cb6d408e2e78..347a86c86b1ee07e1bf1e6e50df5e62f05e7ba80 100644 --- a/src/plugins/texteditor/generichighlighter/manager.h +++ b/src/plugins/texteditor/generichighlighter/manager.h @@ -59,6 +59,7 @@ public: QString definitionIdByName(const QString &name) const; QString definitionIdByMimeType(const QString &mimeType) const; + QString definitionIdByAnyMimeType(const QStringList &mimeTypes) const; bool isBuildingDefinition(const QString &id) const; const QSharedPointer<HighlightDefinition> &definition(const QString &id); diff --git a/src/plugins/texteditor/plaintexteditor.cpp b/src/plugins/texteditor/plaintexteditor.cpp index 2c748b07384923817f9b35e3bb483b438dc7f43f..7b460e4ba5aaf2802e57a401d40075b15a401602 100644 --- a/src/plugins/texteditor/plaintexteditor.cpp +++ b/src/plugins/texteditor/plaintexteditor.cpp @@ -37,6 +37,7 @@ #include "highlighter.h" #include "highlighterexception.h" #include "manager.h" +#include "normalindenter.h" #include <coreplugin/coreconstants.h> #include <coreplugin/uniqueidmanager.h> @@ -65,6 +66,7 @@ PlainTextEditor::PlainTextEditor(QWidget *parent) setRequestMarkEnabled(false); setLineSeparatorsAllowed(true); + setMimeType(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT)); setDisplayName(tr(Core::Constants::K_DEFAULT_TEXT_EDITOR_DISPLAY_NAME)); m_commentDefinition.clearCommentStyles(); @@ -72,6 +74,9 @@ PlainTextEditor::PlainTextEditor(QWidget *parent) connect(file(), SIGNAL(changed()), this, SLOT(configure())); } +PlainTextEditor::~PlainTextEditor() +{} + QList<int> PlainTextEditorEditable::context() const { return m_context; @@ -95,32 +100,42 @@ void PlainTextEditor::unCommentSelection() Utils::unCommentSelection(this, m_commentDefinition); } -void PlainTextEditor::setFontSettings(const TextEditor::FontSettings & fs) +void PlainTextEditor::setFontSettings(const FontSettings & fs) { - TextEditor::BaseTextEditor::setFontSettings(fs); - - Highlighter *highlighter = static_cast<Highlighter *>(baseTextDocument()->syntaxHighlighter()); - if (!highlighter) - return; + BaseTextEditor::setFontSettings(fs); - highlighter->configureFormats(fs); - highlighter->rehighlight(); + if (baseTextDocument()->syntaxHighlighter()) { + Highlighter *highlighter = + static_cast<Highlighter *>(baseTextDocument()->syntaxHighlighter()); + highlighter->configureFormats(fs); + highlighter->rehighlight(); + } } void PlainTextEditor::configure() { - const QString &mimeType = Core::ICore::instance()->mimeDatabase()->findByFile( - QFileInfo(file()->fileName())).type(); - baseTextDocument()->setMimeType(mimeType); + configure(Core::ICore::instance()->mimeDatabase()->findByFile(file()->fileName())); +} + +void PlainTextEditor::configure(const Core::MimeType &mimeType) +{ + if (mimeType.isNull()) + return; + + const QString &type = mimeType.type(); + setMimeType(type); + + QString definitionId = Manager::instance()->definitionIdByMimeType(type); + if (definitionId.isEmpty()) + definitionId = findDefinitionId(mimeType, true); - const QString &definitionId = Manager::instance()->definitionIdByMimeType(mimeType); if (!definitionId.isEmpty()) { try { const QSharedPointer<HighlightDefinition> &definition = Manager::instance()->definition(definitionId); Highlighter *highlighter = new Highlighter(definition->initialContext()); - highlighter->configureFormats(TextEditor::TextEditorSettings::instance()->fontSettings()); + highlighter->configureFormats(TextEditorSettings::instance()->fontSettings()); baseTextDocument()->setSyntaxHighlighter(highlighter); @@ -132,11 +147,27 @@ void PlainTextEditor::configure() } } - // @todo: Indentation specification through the definition files is not really being - // used because Kate recommends to configure indentation through another feature. - // Maybe we should provide something similar in Creator? For now, only normal - // indentation is supported. - m_indenter.reset(new TextEditor::NormalIndenter); + // @todo: Indentation specification through the definition files is not really being used + // because Kate recommends to configure indentation through another feature. Maybe we should + // provide something similar in Creator? For now, only normal indentation is supported. + m_indenter.reset(new NormalIndenter); +} + +QString PlainTextEditor::findDefinitionId(const Core::MimeType &mimeType, + bool considerParents) const +{ + QString definitionId = Manager::instance()->definitionIdByAnyMimeType(mimeType.aliases()); + if (definitionId.isEmpty() && considerParents) { + definitionId = Manager::instance()->definitionIdByAnyMimeType(mimeType.subClassesOf()); + if (definitionId.isEmpty()) { + foreach (const QString &parent, mimeType.subClassesOf()) { + const Core::MimeType &parentMimeType = + Core::ICore::instance()->mimeDatabase()->findByType(parent); + definitionId = findDefinitionId(parentMimeType, considerParents); + } + } + } + return definitionId; } void PlainTextEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar) diff --git a/src/plugins/texteditor/plaintexteditor.h b/src/plugins/texteditor/plaintexteditor.h index fdc85423d2cd76f0d75d4b2bb92c10628881b7ac..15144752f63e06aeb80af37249584b62f51f4d6e 100644 --- a/src/plugins/texteditor/plaintexteditor.h +++ b/src/plugins/texteditor/plaintexteditor.h @@ -31,16 +31,20 @@ #define PLAINTEXTEDITOR_H #include "basetexteditor.h" -#include "normalindenter.h" #include <utils/uncommentselection.h> #include <QtCore/QList> #include <QtCore/QScopedPointer> +namespace Core { +class MimeType; +} + namespace TextEditor { class PlainTextEditor; +class Indenter; class TEXTEDITOR_EXPORT PlainTextEditorEditable : public BaseTextEditorEditable { @@ -63,10 +67,13 @@ class TEXTEDITOR_EXPORT PlainTextEditor : public BaseTextEditor public: PlainTextEditor(QWidget *parent); + ~PlainTextEditor(); + + void configure(const Core::MimeType &mimeType); public slots: virtual void unCommentSelection(); - virtual void setFontSettings(const TextEditor::FontSettings &); + virtual void setFontSettings(const FontSettings &fs); private slots: void configure(); @@ -76,8 +83,10 @@ protected: virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar); private: + QString findDefinitionId(const Core::MimeType &mimeType, bool considerParents) const; + Utils::CommentDefinition m_commentDefinition; - QScopedPointer<TextEditor::Indenter> m_indenter; + QScopedPointer<Indenter> m_indenter; }; } // namespace TextEditor