Generalized Utils::unCommentSelection; Started implementing editor details (configuration by highlight definition, actions).

This commit is contained in:
Leandro Melo
2010-04-30 13:08:06 +02:00
parent c8441462c9
commit 65ca06e0f0
15 changed files with 308 additions and 485 deletions

View File

@@ -28,13 +28,92 @@
**************************************************************************/ **************************************************************************/
#include "uncommentselection.h" #include "uncommentselection.h"
#include <QtCore/QtGlobal>
#include <QtGui/QPlainTextEdit> #include <QtGui/QPlainTextEdit>
#include <QtGui/QTextCursor> #include <QtGui/QTextCursor>
#include <QtGui/QTextBlock> #include <QtGui/QTextBlock>
#include <QtGui/QTextDocument> #include <QtGui/QTextDocument>
void Utils::unCommentSelection(QPlainTextEdit *edit) using namespace Utils;
CommentDefinition::CommentDefinition() :
m_afterWhiteSpaces(false),
m_singleLine(QLatin1String("//")),
m_multiLineStart(QLatin1String("/*")),
m_multiLineEnd(QLatin1String("*/"))
{}
CommentDefinition &CommentDefinition::setAfterWhiteSpaces(const bool afterWhiteSpaces)
{ {
m_afterWhiteSpaces = afterWhiteSpaces;
return *this;
}
CommentDefinition &CommentDefinition::setSingleLine(const QString &singleLine)
{
m_singleLine = singleLine;
return *this;
}
CommentDefinition &CommentDefinition::setMultiLineStart(const QString &multiLineStart)
{
m_multiLineStart = multiLineStart;
return *this;
}
CommentDefinition &CommentDefinition::setMultiLineEnd(const QString &multiLineEnd)
{
m_multiLineEnd = multiLineEnd;
return *this;
}
bool CommentDefinition::isAfterWhiteSpaces() const
{ return m_afterWhiteSpaces; }
const QString &CommentDefinition::singleLine() const
{ return m_singleLine; }
const QString &CommentDefinition::multiLineStart() const
{ return m_multiLineStart; }
const QString &CommentDefinition::multiLineEnd() const
{ return m_multiLineEnd; }
bool CommentDefinition::hasSingleLineStyle() const
{ return !m_singleLine.isEmpty(); }
bool CommentDefinition::hasMultiLineStyle() const
{ return !m_multiLineStart.isEmpty() && !m_multiLineEnd.isEmpty(); }
namespace {
bool isComment(const QString &text,
int index,
const CommentDefinition &definition,
const QString & (CommentDefinition::* comment) () const)
{
const QString &commentType = ((definition).*(comment))();
const int length = commentType.length();
Q_ASSERT(text.length() - index >= length);
int i = 0;
while (i < length) {
if (text.at(index + i) != commentType.at(i))
return false;
++i;
}
return true;
}
} // namespace anynomous
void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &definition)
{
if (!definition.hasSingleLineStyle() && !definition.hasMultiLineStyle())
return;
QTextCursor cursor = edit->textCursor(); QTextCursor cursor = edit->textCursor();
QTextDocument *doc = cursor.document(); QTextDocument *doc = cursor.document();
cursor.beginEditBlock(); cursor.beginEditBlock();
@@ -53,78 +132,107 @@ void Utils::unCommentSelection(QPlainTextEdit *edit)
endBlock = endBlock.previous(); endBlock = endBlock.previous();
} }
bool doCStyleUncomment = false; bool doMultiLineStyleUncomment = false;
bool doCStyleComment = false; bool doMultiLineStyleComment = false;
bool doCppStyleUncomment = false; bool doSingleLineStyleUncomment = false;
bool hasSelection = cursor.hasSelection(); bool hasSelection = cursor.hasSelection();
if (hasSelection) { if (hasSelection && definition.hasMultiLineStyle()) {
QString startText = startBlock.text(); QString startText = startBlock.text();
int startPos = start - startBlock.position(); int startPos = start - startBlock.position();
const int multiLineStartLength = definition.multiLineStart().length();
bool hasLeadingCharacters = !startText.left(startPos).trimmed().isEmpty(); bool hasLeadingCharacters = !startText.left(startPos).trimmed().isEmpty();
if ((startPos >= 2
&& startText.at(startPos-2) == QLatin1Char('/') if (startPos >= multiLineStartLength
&& startText.at(startPos-1) == QLatin1Char('*'))) { && isComment(startText,
startPos -= 2; startPos - multiLineStartLength,
start -= 2; definition,
&CommentDefinition::multiLineStart)) {
startPos -= multiLineStartLength;
start -= multiLineStartLength;
} }
bool hasSelStart = (startPos < startText.length() - 1 bool hasSelStart = (startPos < startText.length() - multiLineStartLength
&& startText.at(startPos) == QLatin1Char('/') && isComment(startText,
&& startText.at(startPos+1) == QLatin1Char('*')); startPos,
definition,
&CommentDefinition::multiLineStart));
QString endText = endBlock.text(); QString endText = endBlock.text();
int endPos = end - endBlock.position(); int endPos = end - endBlock.position();
bool hasTrailingCharacters = !endText.left(endPos).remove(QLatin1String("//")).trimmed().isEmpty() const int multiLineEndLength = definition.multiLineEnd().length();
&& !endText.mid(endPos).trimmed().isEmpty(); bool hasTrailingCharacters =
if ((endPos <= endText.length() - 2 !endText.left(endPos).remove(definition.singleLine()).trimmed().isEmpty()
&& endText.at(endPos) == QLatin1Char('*') && !endText.mid(endPos).trimmed().isEmpty();
&& endText.at(endPos+1) == QLatin1Char('/'))) {
endPos += 2; if (endPos <= endText.length() - multiLineEndLength
end += 2; && isComment(endText, endPos, definition, &CommentDefinition::multiLineEnd)) {
endPos += multiLineEndLength;
end += multiLineEndLength;
} }
bool hasSelEnd = (endPos >= 2 bool hasSelEnd = (endPos >= multiLineEndLength
&& endText.at(endPos-2) == QLatin1Char('*') && isComment(endText,
&& endText.at(endPos-1) == QLatin1Char('/')); endPos - multiLineEndLength,
definition,
&CommentDefinition::multiLineEnd));
doCStyleUncomment = hasSelStart && hasSelEnd; doMultiLineStyleUncomment = hasSelStart && hasSelEnd;
doCStyleComment = !doCStyleUncomment && (hasLeadingCharacters || hasTrailingCharacters); doMultiLineStyleComment = !doMultiLineStyleUncomment
&& (hasLeadingCharacters
|| hasTrailingCharacters
|| !definition.hasSingleLineStyle());
} else if (!hasSelection && !definition.hasSingleLineStyle()) {
QString text = startBlock.text().trimmed();
doMultiLineStyleUncomment = text.startsWith(definition.multiLineStart())
&& text.endsWith(definition.multiLineEnd());
doMultiLineStyleComment = !doMultiLineStyleUncomment && !text.isEmpty();
start = startBlock.position();
end = endBlock.position() + endBlock.length() - 1;
} }
if (doCStyleUncomment) { if (doMultiLineStyleUncomment) {
cursor.setPosition(end); cursor.setPosition(end);
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, 2); cursor.movePosition(QTextCursor::PreviousCharacter,
QTextCursor::KeepAnchor,
definition.multiLineEnd().length());
cursor.removeSelectedText(); cursor.removeSelectedText();
cursor.setPosition(start); cursor.setPosition(start);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 2); cursor.movePosition(QTextCursor::NextCharacter,
QTextCursor::KeepAnchor,
definition.multiLineStart().length());
cursor.removeSelectedText(); cursor.removeSelectedText();
} else if (doCStyleComment) { } else if (doMultiLineStyleComment) {
cursor.setPosition(end); cursor.setPosition(end);
cursor.insertText(QLatin1String("*/")); cursor.insertText(definition.multiLineEnd());
cursor.setPosition(start); cursor.setPosition(start);
cursor.insertText(QLatin1String("/*")); cursor.insertText(definition.multiLineStart());
} else { } else {
endBlock = endBlock.next(); endBlock = endBlock.next();
doCppStyleUncomment = true; doSingleLineStyleUncomment = true;
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) { for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text().trimmed(); QString text = block.text().trimmed();
if (!text.isEmpty() && !text.startsWith(QLatin1String("//"))) { if (!text.isEmpty() && !text.startsWith(definition.singleLine())) {
doCppStyleUncomment = false; doSingleLineStyleUncomment = false;
break; break;
} }
} }
const int singleLineLength = definition.singleLine().length();
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) { for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
if (doCppStyleUncomment) { if (doSingleLineStyleUncomment) {
QString text = block.text(); QString text = block.text();
int i = 0; int i = 0;
while (i < text.size() - 1) { while (i <= text.size() - singleLineLength) {
if (text.at(i) == QLatin1Char('/') if (isComment(text, i, definition, &CommentDefinition::singleLine)) {
&& text.at(i + 1) == QLatin1Char('/')) {
cursor.setPosition(block.position() + i); cursor.setPosition(block.position() + i);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 2); cursor.movePosition(QTextCursor::NextCharacter,
QTextCursor::KeepAnchor,
singleLineLength);
cursor.removeSelectedText(); cursor.removeSelectedText();
break; break;
} }
@@ -136,8 +244,11 @@ void Utils::unCommentSelection(QPlainTextEdit *edit)
QString text = block.text(); QString text = block.text();
foreach(QChar c, text) { foreach(QChar c, text) {
if (!c.isSpace()) { if (!c.isSpace()) {
cursor.setPosition(block.position()); if (definition.isAfterWhiteSpaces())
cursor.insertText(QLatin1String("//")); cursor.setPosition(block.position() + text.indexOf(c));
else
cursor.setPosition(block.position());
cursor.insertText(definition.singleLine());
break; break;
} }
} }
@@ -146,10 +257,10 @@ void Utils::unCommentSelection(QPlainTextEdit *edit)
} }
// adjust selection when commenting out // adjust selection when commenting out
if (hasSelection && !doCStyleUncomment && !doCppStyleUncomment) { if (hasSelection && !doMultiLineStyleUncomment && !doSingleLineStyleUncomment) {
cursor = edit->textCursor(); cursor = edit->textCursor();
if (!doCStyleComment) if (!doMultiLineStyleComment)
start = startBlock.position(); // move the double slashes into the selection start = startBlock.position(); // move the comment into the selection
int lastSelPos = anchorIsStart ? cursor.position() : cursor.anchor(); int lastSelPos = anchorIsStart ? cursor.position() : cursor.anchor();
if (anchorIsStart) { if (anchorIsStart) {
cursor.setPosition(start); cursor.setPosition(start);
@@ -163,4 +274,3 @@ void Utils::unCommentSelection(QPlainTextEdit *edit)
cursor.endEditBlock(); cursor.endEditBlock();
} }

View File

@@ -32,13 +32,42 @@
#include "utils_global.h" #include "utils_global.h"
#include <QtCore/QString>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QPlainTextEdit; class QPlainTextEdit;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Utils { namespace Utils {
QTCREATOR_UTILS_EXPORT void unCommentSelection(QPlainTextEdit *edit); class QTCREATOR_UTILS_EXPORT CommentDefinition
{
public:
CommentDefinition();
CommentDefinition &setAfterWhiteSpaces(const bool);
CommentDefinition &setSingleLine(const QString &singleLine);
CommentDefinition &setMultiLineStart(const QString &multiLineStart);
CommentDefinition &setMultiLineEnd(const QString &multiLineEnd);
bool isAfterWhiteSpaces() const;
const QString &singleLine() const;
const QString &multiLineStart() const;
const QString &multiLineEnd() const;
bool hasSingleLineStyle() const;
bool hasMultiLineStyle() const;
private:
bool m_afterWhiteSpaces;
QString m_singleLine;
QString m_multiLineStart;
QString m_multiLineEnd;
};
QTCREATOR_UTILS_EXPORT
void unCommentSelection(QPlainTextEdit *edit,
const CommentDefinition &definiton = CommentDefinition());
} // end of namespace Utils } // end of namespace Utils

View File

@@ -43,9 +43,50 @@
#include <QtCore/QSharedPointer> #include <QtCore/QSharedPointer>
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QDebug>
using namespace GenericEditor; using namespace GenericEditor;
using namespace Internal; using namespace Internal;
Editor::Editor(QWidget *parent) : TextEditor::BaseTextEditor(parent)
{
connect(file(), SIGNAL(changed()), this, SLOT(configure()));
}
void Editor::unCommentSelection()
{
Utils::unCommentSelection(this, m_commentDefinition);
}
TextEditor::BaseTextEditorEditable *Editor::createEditableInterface()
{
EditorEditable *editable = new EditorEditable(this);
return editable;
}
void Editor::configure()
{
const QString &mimeType = Core::ICore::instance()->mimeDatabase()->findByFile(
QFileInfo(file()->fileName())).type();
baseTextDocument()->setMimeType(mimeType);
try {
const QString &definitionId =
GenericEditorPlugin::instance()->definitionIdByMimeType(mimeType);
QSharedPointer<HighlightDefinition> definition =
GenericEditorPlugin::instance()->definition(definitionId);
baseTextDocument()->setSyntaxHighlighter(new Highlighter(definition->initialContext()));
m_commentDefinition.setAfterWhiteSpaces(definition->isCommentAfterWhiteSpaces());
m_commentDefinition.setSingleLine(definition->singleLineComment());
m_commentDefinition.setMultiLineStart(definition->multiLineCommentStart());
m_commentDefinition.setMultiLineEnd(definition->multiLineCommentEnd());
} catch (const HighlighterException &) {
// No highlighter will be set.
}
}
EditorEditable::EditorEditable(Editor *editor) : EditorEditable::EditorEditable(Editor *editor) :
TextEditor::BaseTextEditorEditable(editor) TextEditor::BaseTextEditorEditable(editor)
{ {
@@ -68,34 +109,7 @@ bool EditorEditable::duplicateSupported() const
Core::IEditor *EditorEditable::duplicate(QWidget *parent) Core::IEditor *EditorEditable::duplicate(QWidget *parent)
{ {
Editor *newEditor = new Editor(editor()->mimeType(), parent); Editor *newEditor = new Editor(parent);
newEditor->duplicateFrom(editor()); newEditor->duplicateFrom(editor());
return newEditor->editableInterface(); return newEditor->editableInterface();
} }
bool EditorEditable::open(const QString &fileName)
{
if (TextEditor::BaseTextEditorEditable::open(fileName)) {
editor()->setMimeType(
Core::ICore::instance()->mimeDatabase()->findByFile(QFileInfo(fileName)).type());
return true;
}
return false;
}
Editor::Editor(const QString &definitionId, QWidget *parent) : TextEditor::BaseTextEditor(parent)
{
try {
QSharedPointer<HighlightDefinition> definition =
GenericEditorPlugin::instance()->definition(definitionId);
baseTextDocument()->setSyntaxHighlighter(new Highlighter(definition->initialContext()));
} catch (const HighlighterException &) {
// No highlighter will be set.
}
}
TextEditor::BaseTextEditorEditable *Editor::createEditableInterface()
{
EditorEditable *editable = new EditorEditable(this);
return editable;
}

View File

@@ -31,6 +31,7 @@
#define GENERICEDITOR_H #define GENERICEDITOR_H
#include <texteditor/basetexteditor.h> #include <texteditor/basetexteditor.h>
#include <utils/uncommentselection.h>
#include <QtCore/QList> #include <QtCore/QList>
@@ -41,12 +42,27 @@ QT_END_NAMESPACE
namespace GenericEditor { namespace GenericEditor {
namespace Internal { namespace Internal {
class Editor; class Editor : public TextEditor::BaseTextEditor
{
Q_OBJECT
public:
Editor(QWidget *parent = 0);
virtual void unCommentSelection();
protected:
virtual TextEditor::BaseTextEditorEditable *createEditableInterface();
private slots:
void configure();
private:
Utils::CommentDefinition m_commentDefinition;
};
class EditorEditable : public TextEditor::BaseTextEditorEditable class EditorEditable : public TextEditor::BaseTextEditorEditable
{ {
Q_OBJECT Q_OBJECT
public: public:
EditorEditable(Editor *editor); EditorEditable(Editor *editor);
@@ -56,22 +72,12 @@ protected:
virtual bool isTemporary() const; virtual bool isTemporary() const;
virtual bool duplicateSupported() const; virtual bool duplicateSupported() const;
virtual Core::IEditor *duplicate(QWidget *parent); virtual Core::IEditor *duplicate(QWidget *parent);
virtual bool open(const QString & fileName);
private: private:
QList<int> m_context; QList<int> m_context;
}; };
class Editor : public TextEditor::BaseTextEditor
{
Q_OBJECT
public:
Editor(const QString &definitionId, QWidget *parent = 0);
protected:
virtual TextEditor::BaseTextEditorEditable *createEditableInterface();
};
} // namespace Internal } // namespace Internal
} // namespace GenericEditor } // namespace GenericEditor

View File

@@ -30,22 +30,41 @@
#include "editorfactory.h" #include "editorfactory.h"
#include "genericeditorconstants.h" #include "genericeditorconstants.h"
#include "editor.h" #include "editor.h"
#include "genericeditorplugin.h"
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
using namespace GenericEditor; using namespace GenericEditor;
using namespace Internal; using namespace Internal;
EditorFactory::EditorFactory(QObject *parent) : EditorFactory::EditorFactory(QObject *parent) : Core::IEditorFactory(parent)
Core::IEditorFactory(parent) {
{} // Note: This is temporary until it is definied how definition files should be "integrated".
m_mimeTypes << QLatin1String(GenericEditor::Constants::C_HEADER_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::C_SOURCE_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::CPP_HEADER_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::CPP_SOURCE_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::CSS_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::FORTRAN_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::HTML_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::JAVA_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::JAVASCRIPT_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::OBJECTIVEC_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::PERL_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::PHP_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::PYTHON_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::RUBY_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::SQL_MIMETYPE)
<< QLatin1String(GenericEditor::Constants::TCL_MIMETYPE);
}
EditorFactory::~EditorFactory() EditorFactory::~EditorFactory()
{} {}
Core::IEditor *EditorFactory::createEditor(QWidget *parent) Core::IEditor *EditorFactory::createEditor(QWidget *parent)
{ {
Editor *genericEditor = createGenericEditor(parent); Editor *genericEditor = new Editor(parent);
GenericEditorPlugin::instance()->initializeEditor(genericEditor);
return genericEditor->editableInterface(); return genericEditor->editableInterface();
} }
@@ -67,6 +86,3 @@ Core::IFile *EditorFactory::open(const QString &fileName)
Core::IEditor *iface = Core::EditorManager::instance()->openEditor(fileName, id()); Core::IEditor *iface = Core::EditorManager::instance()->openEditor(fileName, id());
return iface ? iface->file() : 0; return iface ? iface->file() : 0;
} }
void EditorFactory::addMimeType(const QString &mimeType)
{ m_mimeTypes.append(mimeType); }

View File

@@ -47,21 +47,13 @@ public:
EditorFactory(QObject *parent = 0); EditorFactory(QObject *parent = 0);
virtual ~EditorFactory(); virtual ~EditorFactory();
// Currently there are language specific factores which configure the correct highlighter.
// Would it be a good idea if the createEditor method also received the mime type? This would
// also discard the necessity of overriding the open method.
virtual Core::IEditor *createEditor(QWidget *parent); virtual Core::IEditor *createEditor(QWidget *parent);
virtual QStringList mimeTypes() const; virtual QStringList mimeTypes() const;
virtual QString id() const; virtual QString id() const;
virtual QString displayName() const; virtual QString displayName() const;
virtual Core::IFile *open(const QString &fileName); virtual Core::IFile *open(const QString &fileName);
protected:
void addMimeType(const QString &mimeType);
private: private:
virtual Editor *createGenericEditor(QWidget *parent) = 0;
QStringList m_mimeTypes; QStringList m_mimeTypes;
}; };

View File

@@ -8,7 +8,6 @@ CONFIG += help
HEADERS += \ HEADERS += \
genericeditorplugin.h \ genericeditorplugin.h \
progressdata.h \ progressdata.h \
languagespecificfactories.h \
specificrules.h \ specificrules.h \
rule.h \ rule.h \
reuse.h \ reuse.h \
@@ -28,7 +27,6 @@ HEADERS += \
SOURCES += \ SOURCES += \
genericeditorplugin.cpp \ genericeditorplugin.cpp \
progressdata.cpp \ progressdata.cpp \
languagespecificfactories.cpp \
specificrules.cpp \ specificrules.cpp \
rule.cpp \ rule.cpp \
keywordlist.cpp \ keywordlist.cpp \

View File

@@ -1,2 +1,3 @@
include(../../plugins/coreplugin/coreplugin.pri) include(../../plugins/coreplugin/coreplugin.pri)
include(../../plugins/texteditor/texteditor.pri) include(../../plugins/texteditor/texteditor.pri)
include(../../libs/utils/utils.pri)

View File

@@ -28,12 +28,13 @@
**************************************************************************/ **************************************************************************/
#include "genericeditorplugin.h" #include "genericeditorplugin.h"
#include "languagespecificfactories.h"
#include "highlightdefinition.h" #include "highlightdefinition.h"
#include "highlightdefinitionhandler.h" #include "highlightdefinitionhandler.h"
#include "highlighter.h" #include "highlighter.h"
#include "highlighterexception.h" #include "highlighterexception.h"
#include "genericeditorconstants.h" #include "genericeditorconstants.h"
#include "editor.h"
#include "editorfactory.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h> #include <coreplugin/mimedatabase.h>
@@ -48,6 +49,7 @@
using namespace GenericEditor; using namespace GenericEditor;
using namespace Internal; using namespace Internal;
// Todo: Temp.
const QLatin1String GenericEditorPlugin::kAlertDefinitionId(":/genericeditor/XML/alert.xml"); const QLatin1String GenericEditorPlugin::kAlertDefinitionId(":/genericeditor/XML/alert.xml");
const QLatin1String GenericEditorPlugin::kCDefinitionId(":/genericeditor/XML/c.xml"); const QLatin1String GenericEditorPlugin::kCDefinitionId(":/genericeditor/XML/c.xml");
const QLatin1String GenericEditorPlugin::kCppDefinitionId(":/genericeditor/XML/cpp.xml"); const QLatin1String GenericEditorPlugin::kCppDefinitionId(":/genericeditor/XML/cpp.xml");
@@ -73,7 +75,8 @@ const QLatin1String GenericEditorPlugin::kTclDefinitionId(":/genericeditor/XML/t
GenericEditorPlugin *GenericEditorPlugin::m_instance = 0; GenericEditorPlugin *GenericEditorPlugin::m_instance = 0;
GenericEditorPlugin::GenericEditorPlugin() GenericEditorPlugin::GenericEditorPlugin() :
m_actionHandler(0)
{ {
QTC_ASSERT(!m_instance, return); QTC_ASSERT(!m_instance, return);
m_instance = this; m_instance = this;
@@ -117,7 +120,13 @@ GenericEditorPlugin::GenericEditorPlugin()
} }
GenericEditorPlugin::~GenericEditorPlugin() GenericEditorPlugin::~GenericEditorPlugin()
{} {
delete m_actionHandler;
m_instance = 0;
}
GenericEditorPlugin *GenericEditorPlugin::instance()
{ return m_instance; }
bool GenericEditorPlugin::initialize(const QStringList &arguments, QString *errorString) bool GenericEditorPlugin::initialize(const QStringList &arguments, QString *errorString)
{ {
@@ -129,20 +138,12 @@ bool GenericEditorPlugin::initialize(const QStringList &arguments, QString *erro
return false; return false;
} }
addAutoReleasedObject(new CFactory(this)); addAutoReleasedObject(new EditorFactory(this));
addAutoReleasedObject(new CppFactory(this));
addAutoReleasedObject(new CssFactory(this)); m_actionHandler = new TextEditor::TextEditorActionHandler(
addAutoReleasedObject(new FortranFactory(this)); GenericEditor::Constants::GENERIC_EDITOR,
addAutoReleasedObject(new HtmlFactory(this)); TextEditor::TextEditorActionHandler::UnCommentSelection);
addAutoReleasedObject(new JavaFactory(this)); m_actionHandler->initializeActions();
addAutoReleasedObject(new JavascriptFactory(this));
addAutoReleasedObject(new ObjectiveCFactory(this));
addAutoReleasedObject(new PerlFactory(this));
addAutoReleasedObject(new PhpFactory(this)); // Php definition file is broken.
addAutoReleasedObject(new PythonFactory(this));
addAutoReleasedObject(new RubyFactory(this));
addAutoReleasedObject(new SqlFactory(this));
addAutoReleasedObject(new TclFactory(this));
return true; return true;
} }
@@ -150,8 +151,10 @@ bool GenericEditorPlugin::initialize(const QStringList &arguments, QString *erro
void GenericEditorPlugin::extensionsInitialized() void GenericEditorPlugin::extensionsInitialized()
{} {}
GenericEditorPlugin *GenericEditorPlugin::instance() void GenericEditorPlugin::initializeEditor(Editor *editor)
{ return m_instance; } {
m_actionHandler->setupActions(editor);
}
QString GenericEditorPlugin::definitionIdByName(const QString &name) const QString GenericEditorPlugin::definitionIdByName(const QString &name) const
{ return m_idByName.value(name.toLower()); } { return m_idByName.value(name.toLower()); }
@@ -187,5 +190,4 @@ const QSharedPointer<HighlightDefinition> &GenericEditorPlugin::definition(const
return *m_definitions.constFind(id); return *m_definitions.constFind(id);
} }
Q_EXPORT_PLUGIN(GenericEditorPlugin) Q_EXPORT_PLUGIN(GenericEditorPlugin)

View File

@@ -31,6 +31,7 @@
#define GENERICEDITORPLUGIN_H #define GENERICEDITORPLUGIN_H
#include <extensionsystem/iplugin.h> #include <extensionsystem/iplugin.h>
#include <texteditor/texteditoractionhandler.h>
#include <QtCore/QString> #include <QtCore/QString>
#include <QtCore/QLatin1String> #include <QtCore/QLatin1String>
@@ -42,6 +43,7 @@ namespace GenericEditor {
namespace Internal { namespace Internal {
class HighlightDefinition; class HighlightDefinition;
class Editor;
// Note: The general interface of this class is temporary. Still need discussing details about // Note: The general interface of this class is temporary. Still need discussing details about
// the definition files integration with Creator. // the definition files integration with Creator.
@@ -59,9 +61,10 @@ public:
virtual bool initialize(const QStringList &arguments, QString *errorString); virtual bool initialize(const QStringList &arguments, QString *errorString);
virtual void extensionsInitialized(); virtual void extensionsInitialized();
void initializeEditor(Editor *editor);
QString definitionIdByName(const QString &name) const; QString definitionIdByName(const QString &name) const;
QString definitionIdByMimeType(const QString &mimeType) const; QString definitionIdByMimeType(const QString &mimeType) const;
bool isBuildingDefinition(const QString &id) const; bool isBuildingDefinition(const QString &id) const;
const QSharedPointer<HighlightDefinition> &definition(const QString &id); const QSharedPointer<HighlightDefinition> &definition(const QString &id);
@@ -87,12 +90,14 @@ private:
GenericEditorPlugin(const GenericEditorPlugin &HighlighterPlugin); GenericEditorPlugin(const GenericEditorPlugin &HighlighterPlugin);
const GenericEditorPlugin &operator=(const GenericEditorPlugin &HighlighterPlugin); const GenericEditorPlugin &operator=(const GenericEditorPlugin &HighlighterPlugin);
static GenericEditorPlugin *m_instance;
QSet<QString> m_isBuilding; QSet<QString> m_isBuilding;
QHash<QString, QString> m_idByName; QHash<QString, QString> m_idByName;
QHash<QString, QString> m_idByMimeType; QHash<QString, QString> m_idByMimeType;
QHash<QString, QSharedPointer<HighlightDefinition> > m_definitions; QHash<QString, QSharedPointer<HighlightDefinition> > m_definitions;
static GenericEditorPlugin *m_instance; TextEditor::TextEditorActionHandler *m_actionHandler;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -41,7 +41,6 @@ using namespace Internal;
HighlightDefinition::HighlightDefinition() : HighlightDefinition::HighlightDefinition() :
m_delimiters(QLatin1String(".():!+,-<=>%&/;?[]^{|}~\\*, \t")), m_delimiters(QLatin1String(".():!+,-<=>%&/;?[]^{|}~\\*, \t")),
m_singleLineCommentPosition(0),
m_singleLineCommentAfterWhiteSpaces(false), m_singleLineCommentAfterWhiteSpaces(false),
m_keywordCaseSensitivity(Qt::CaseSensitive) m_keywordCaseSensitivity(Qt::CaseSensitive)
{} {}
@@ -110,22 +109,13 @@ void HighlightDefinition::setSingleLineComment(const QString &start)
const QString &HighlightDefinition::singleLineComment() const const QString &HighlightDefinition::singleLineComment() const
{ return m_singleLineComment; } { return m_singleLineComment; }
void HighlightDefinition::setSingleLineCommentPosition(const QString &position) void HighlightDefinition::setCommentAfterWhitespaces(const QString &after)
{ {
if (position == QLatin1String("afterwhitespace")) { if (after == QLatin1String("afterwhitespace"))
m_singleLineCommentAfterWhiteSpaces = true; m_singleLineCommentAfterWhiteSpaces = true;
} else {
bool ok;
m_singleLineCommentPosition = position.toInt(&ok);
if (!ok)
m_singleLineCommentPosition = 0;
}
} }
int HighlightDefinition::singleLineCommentPosition() const bool HighlightDefinition::isCommentAfterWhiteSpaces() const
{ return m_singleLineCommentPosition; }
bool HighlightDefinition::isSingleLineCommentAfterWhiteSpaces() const
{ return m_singleLineCommentAfterWhiteSpaces; } { return m_singleLineCommentAfterWhiteSpaces; }
void HighlightDefinition::setMultiLineCommentStart(const QString &start) void HighlightDefinition::setMultiLineCommentStart(const QString &start)

View File

@@ -69,9 +69,8 @@ public:
void setSingleLineComment(const QString &start); void setSingleLineComment(const QString &start);
const QString &singleLineComment() const; const QString &singleLineComment() const;
void setSingleLineCommentPosition(const QString &position); void setCommentAfterWhitespaces(const QString &after);
int singleLineCommentPosition() const; bool isCommentAfterWhiteSpaces() const;
bool isSingleLineCommentAfterWhiteSpaces() const;
void setMultiLineCommentStart(const QString &start); void setMultiLineCommentStart(const QString &start);
const QString &multiLineCommentStart() const; const QString &multiLineCommentStart() const;
@@ -112,7 +111,6 @@ private:
QString m_delimiters; QString m_delimiters;
QString m_singleLineComment; QString m_singleLineComment;
int m_singleLineCommentPosition;
bool m_singleLineCommentAfterWhiteSpaces; bool m_singleLineCommentAfterWhiteSpaces;
QString m_multiLineCommentStart; QString m_multiLineCommentStart;

View File

@@ -281,10 +281,10 @@ void HighlightDefinitionHandler::itemDataElementStarted(const QXmlAttributes &at
void HighlightDefinitionHandler::commentElementStarted(const QXmlAttributes &atts) const void HighlightDefinitionHandler::commentElementStarted(const QXmlAttributes &atts) const
{ {
const QString &commentType = atts.value(kName); const QString &commentType = atts.value(kName);
if (commentType == kSingleLine) { if (commentType.compare(kSingleLine, Qt::CaseInsensitive) == 0) {
m_definition->setSingleLineComment(atts.value(kStart)); m_definition->setSingleLineComment(atts.value(kStart));
m_definition->setSingleLineCommentPosition(atts.value(kPosition)); m_definition->setCommentAfterWhitespaces(atts.value(kPosition));
} else if (commentType == kMultiLine) { } else if (commentType.compare(kMultiLine, Qt::CaseInsensitive) == 0) {
m_definition->setMultiLineCommentStart(atts.value(kStart)); m_definition->setMultiLineCommentStart(atts.value(kStart));
m_definition->setMultiLineCommentEnd(atts.value(kEnd)); m_definition->setMultiLineCommentEnd(atts.value(kEnd));
m_definition->setMultiLineCommentRegion(atts.value(kRegion)); m_definition->setMultiLineCommentRegion(atts.value(kRegion));

View File

@@ -1,143 +0,0 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "languagespecificfactories.h"
#include "genericeditorconstants.h"
#include "editor.h"
#include "genericeditorplugin.h"
#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
using namespace GenericEditor;
using namespace Internal;
// C
CFactory::CFactory(QObject *parent) : EditorFactory(parent)
{
addMimeType(QLatin1String(GenericEditor::Constants::C_HEADER_MIMETYPE));
addMimeType(QLatin1String(GenericEditor::Constants::C_SOURCE_MIMETYPE));
}
Editor *CFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kCDefinitionId, parent); }
// C++
CppFactory::CppFactory(QObject *parent) : EditorFactory(parent)
{
addMimeType(QLatin1String(GenericEditor::Constants::CPP_HEADER_MIMETYPE));
addMimeType(QLatin1String(GenericEditor::Constants::CPP_SOURCE_MIMETYPE));
}
Editor *CppFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kCppDefinitionId, parent); }
// Css
CssFactory::CssFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::CSS_MIMETYPE)); }
Editor *CssFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kCssDefinitionId, parent); }
// Fortran
FortranFactory::FortranFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::FORTRAN_MIMETYPE)); }
Editor *FortranFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kFortranDefinitionId, parent); }
// Html
HtmlFactory::HtmlFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::HTML_MIMETYPE)); }
Editor *HtmlFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kHtmlDefinitionId, parent); }
// Java
JavaFactory::JavaFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::JAVA_MIMETYPE)); }
Editor *JavaFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kJavaDefinitionId, parent); }
// Javascript
JavascriptFactory::JavascriptFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::JAVASCRIPT_MIMETYPE)); }
Editor *JavascriptFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kJavascriptDefinitionId, parent); }
// ObjectiveC
ObjectiveCFactory::ObjectiveCFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::OBJECTIVEC_MIMETYPE)); }
Editor *ObjectiveCFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kObjectiveCDefinitionId, parent); }
// Perl
PerlFactory::PerlFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::PERL_MIMETYPE)); }
Editor *PerlFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kPerlDefinitionId, parent); }
// Php
PhpFactory::PhpFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::PHP_MIMETYPE)); }
Editor *PhpFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kPhpDefinitionId, parent); }
// Python
PythonFactory::PythonFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::PYTHON_MIMETYPE)); }
Editor *PythonFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kPythonDefinitionId, parent); }
// Ruby
RubyFactory::RubyFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::RUBY_MIMETYPE)); }
Editor *RubyFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kRubyDefinitionId, parent); }
// SQL
SqlFactory::SqlFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::SQL_MIMETYPE)); }
Editor *SqlFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kSqlDefinitionId, parent); }
// Tcl
TclFactory::TclFactory(QObject *parent) : EditorFactory(parent)
{ addMimeType(QLatin1String(GenericEditor::Constants::TCL_MIMETYPE)); }
Editor *TclFactory::createGenericEditor(QWidget *parent)
{ return new Editor(GenericEditorPlugin::kTclDefinitionId, parent); }

View File

@@ -1,195 +0,0 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef LANGUAGESPECIFICFACTORIES_H
#define LANGUAGESPECIFICFACTORIES_H
#include "editorfactory.h"
namespace GenericEditor {
namespace Internal {
class CFactory : public EditorFactory
{
Q_OBJECT
public:
CFactory(QObject *parent = 0);
virtual ~CFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class CppFactory : public EditorFactory
{
Q_OBJECT
public:
CppFactory(QObject *parent = 0);
virtual ~CppFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class CssFactory : public EditorFactory
{
Q_OBJECT
public:
CssFactory(QObject *parent = 0);
virtual ~CssFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class FortranFactory : public EditorFactory
{
Q_OBJECT
public:
FortranFactory(QObject *parent = 0);
virtual ~FortranFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class HtmlFactory : public EditorFactory
{
Q_OBJECT
public:
HtmlFactory(QObject *parent = 0);
virtual ~HtmlFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class JavaFactory : public EditorFactory
{
Q_OBJECT
public:
JavaFactory(QObject *parent = 0);
virtual ~JavaFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class JavascriptFactory : public EditorFactory
{
Q_OBJECT
public:
JavascriptFactory(QObject *parent = 0);
virtual ~JavascriptFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class ObjectiveCFactory : public EditorFactory
{
Q_OBJECT
public:
ObjectiveCFactory(QObject *parent = 0);
virtual ~ObjectiveCFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class PerlFactory : public EditorFactory
{
Q_OBJECT
public:
PerlFactory(QObject *parent = 0);
virtual ~PerlFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class PhpFactory : public EditorFactory
{
Q_OBJECT
public:
PhpFactory(QObject *parent = 0);
virtual ~PhpFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class PythonFactory : public EditorFactory
{
Q_OBJECT
public:
PythonFactory(QObject *parent = 0);
virtual ~PythonFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class RubyFactory : public EditorFactory
{
Q_OBJECT
public:
RubyFactory(QObject *parent = 0);
virtual ~RubyFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class SqlFactory : public EditorFactory
{
Q_OBJECT
public:
SqlFactory(QObject *parent = 0);
virtual ~SqlFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
class TclFactory : public EditorFactory
{
Q_OBJECT
public:
TclFactory(QObject *parent = 0);
virtual ~TclFactory() {}
private:
virtual Editor *createGenericEditor(QWidget *parent);
};
} // namespace Internal
} // namespace GenericEditor
#endif // LANGUAGESPECIFICFACTORIES_H