Commit f6b48950 authored by hjk's avatar hjk

TextEditor: Re-work comment definition handling

No need for most of the machinery.

Change-Id: I9078174582d83da94c6c7f20282fd3a5f1742911
Reviewed-by: default avatarChristian Stenger <christian.stenger@digia.com>
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
parent f3a83367
......@@ -34,12 +34,30 @@
using namespace Utils;
CommentDefinition::CommentDefinition() :
isAfterWhiteSpaces(false),
singleLine(QLatin1String("//")),
multiLineStart(QLatin1String("/*")),
multiLineEnd(QLatin1String("*/"))
isAfterWhiteSpaces(false)
{}
void CommentDefinition::setStyle(Style style)
{
switch (style) {
case CppStyle:
singleLine = QLatin1String("//");
multiLineStart = QLatin1String("/*");
multiLineEnd = QLatin1String("*/");
break;
case HashStyle:
singleLine = QLatin1String("#");
multiLineStart.clear();
multiLineEnd.clear();
break;
}
}
bool CommentDefinition::isValid() const
{
return hasSingleLineStyle() || hasMultiLineStyle();
}
bool CommentDefinition::hasSingleLineStyle() const
{
return !singleLine.isEmpty();
......@@ -50,13 +68,6 @@ bool CommentDefinition::hasMultiLineStyle() const
return !multiLineStart.isEmpty() && !multiLineEnd.isEmpty();
}
void CommentDefinition::clearCommentStyles()
{
singleLine.clear();
multiLineStart.clear();
multiLineEnd.clear();
}
static bool isComment(const QString &text, int index,
const QString &commentType)
{
......@@ -76,7 +87,7 @@ static bool isComment(const QString &text, int index,
void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &definition)
{
if (!definition.hasSingleLineStyle() && !definition.hasMultiLineStyle())
if (!definition.isValid())
return;
QTextCursor cursor = edit->textCursor();
......
......@@ -45,11 +45,13 @@ class QTCREATOR_UTILS_EXPORT CommentDefinition
public:
CommentDefinition();
enum Style { CppStyle, HashStyle };
void setStyle(Style style);
bool isValid() const;
bool hasSingleLineStyle() const;
bool hasMultiLineStyle() const;
void clearCommentStyles();
public:
bool isAfterWhiteSpaces;
QString singleLine;
......
......@@ -55,6 +55,7 @@ JavaEditor::JavaEditor(JavaEditorWidget *editor)
setContext(Core::Context(Constants::C_JAVA_EDITOR,
TextEditor::Constants::C_TEXTEDITOR));
setDuplicateSupported(true);
setCommentStyle(Utils::CommentDefinition::CppStyle);
}
Core::IEditor *JavaEditor::duplicate()
......@@ -88,18 +89,9 @@ JavaEditorWidget::JavaEditorWidget(JavaEditorWidget *other)
void JavaEditorWidget::ctor()
{
m_commentDefinition.clearCommentStyles();
m_commentDefinition.singleLine = QLatin1String("//");
m_commentDefinition.multiLineStart = QLatin1String("/*");
m_commentDefinition.multiLineEnd = QLatin1String("*/");
setAutoCompleter(new JavaAutoCompleter);
}
void JavaEditorWidget::unCommentSelection()
{
Utils::unCommentSelection(this, m_commentDefinition);
}
TextEditor::BaseTextEditor *JavaEditorWidget::createEditor()
{
return new JavaEditor(this);
......
......@@ -59,15 +59,12 @@ public:
JavaEditorWidget(QWidget *parent = 0);
JavaEditorWidget(JavaEditorWidget *other);
void unCommentSelection();
protected:
TextEditor::BaseTextEditor *createEditor();
private:
JavaEditorWidget(BaseTextEditorWidget *); // avoid stupidity
void ctor();
Utils::CommentDefinition m_commentDefinition;
};
class JavaDocument : public TextEditor::BaseTextDocument
......
......@@ -62,6 +62,7 @@ CMakeEditor::CMakeEditor(CMakeEditorWidget *editor)
setContext(Core::Context(CMakeProjectManager::Constants::C_CMAKEEDITOR,
TextEditor::Constants::C_TEXTEDITOR));
setDuplicateSupported(true);
setCommentStyle(Utils::CommentDefinition::HashStyle);
connect(document(), SIGNAL(changed()), this, SLOT(markAsChanged()));
}
......@@ -154,32 +155,17 @@ QString CMakeEditor::contextHelpId() const
CMakeEditorWidget::CMakeEditorWidget(QWidget *parent)
: BaseTextEditorWidget(new CMakeDocument(), parent)
{
ctor();
}
{}
CMakeEditorWidget::CMakeEditorWidget(CMakeEditorWidget *other)
: BaseTextEditorWidget(other)
{
ctor();
}
void CMakeEditorWidget::ctor()
{
m_commentDefinition.clearCommentStyles();
m_commentDefinition.singleLine = QLatin1Char('#');
}
{}
TextEditor::BaseTextEditor *CMakeEditorWidget::createEditor()
{
return new CMakeEditor(this);
}
void CMakeEditorWidget::unCommentSelection()
{
Utils::unCommentSelection(this, m_commentDefinition);
}
void CMakeEditorWidget::contextMenuEvent(QContextMenuEvent *e)
{
showDefaultContextMenu(e, Constants::M_CONTEXT);
......
......@@ -78,13 +78,8 @@ protected:
TextEditor::BaseTextEditor *createEditor();
void contextMenuEvent(QContextMenuEvent *e);
public slots:
void unCommentSelection();
private:
CMakeEditorWidget(TextEditor::BaseTextEditorWidget *); // avoid stupidity
void ctor();
Utils::CommentDefinition m_commentDefinition;
};
class CMakeDocument : public TextEditor::BaseTextDocument
......
......@@ -113,6 +113,7 @@ CPPEditor::CPPEditor(CPPEditorWidget *editor)
m_context.add(ProjectExplorer::Constants::LANG_CXX);
m_context.add(TextEditor::Constants::C_TEXTEDITOR);
setDuplicateSupported(true);
setCommentStyle(Utils::CommentDefinition::CppStyle);
}
Q_GLOBAL_STATIC(CppTools::SymbolFinder, symbolFinder)
......@@ -796,11 +797,6 @@ bool CPPEditor::open(QString *errorString, const QString &fileName, const QStrin
return true;
}
const Utils::CommentDefinition *CPPEditor::commentDefinition() const
{
return &m_commentDefinition;
}
TextEditor::CompletionAssistProvider *CPPEditor::completionAssistProvider()
{
return CppModelManagerInterface::instance()->cppEditorSupport(this)->completionAssistProvider();
......@@ -836,11 +832,6 @@ void CPPEditorWidget::applyFontSettings()
semanticRehighlight(true);
}
void CPPEditorWidget::unCommentSelection()
{
Utils::unCommentSelection(this);
}
void CPPEditorWidget::slotCodeStyleSettingsChanged(const QVariant &)
{
CppTools::QtStyleCodeFormatter formatter;
......
......@@ -66,11 +66,7 @@ public:
const QString &fileName,
const QString &realFileName) QTC_OVERRIDE;
const Utils::CommentDefinition *commentDefinition() const QTC_OVERRIDE;
TextEditor::CompletionAssistProvider *completionAssistProvider() QTC_OVERRIDE;
private:
Utils::CommentDefinition m_commentDefinition;
};
class CPPEditorWidget : public TextEditor::BaseTextEditorWidget
......@@ -105,7 +101,6 @@ public slots:
void cut() QTC_OVERRIDE;
void selectAll() QTC_OVERRIDE;
void unCommentSelection() QTC_OVERRIDE;
void switchDeclarationDefinition(bool inNextSplit);
void showPreProcessorWidget();
......
......@@ -263,11 +263,6 @@ void GLSLTextEditorWidget::createToolBar(GLSLEditorEditable *editor)
editor->insertExtraToolBarWidget(TextEditor::BaseTextEditor::Left, m_outlineCombo);
}
void GLSLTextEditorWidget::unCommentSelection()
{
Utils::unCommentSelection(this);
}
void GLSLTextEditorWidget::updateDocument()
{
m_updateDocumentTimer->start();
......
......@@ -90,8 +90,6 @@ public:
GLSLTextEditorWidget(GLSLTextEditorWidget *other);
~GLSLTextEditorWidget();
virtual void unCommentSelection();
int editorRevision() const;
bool isOutdated() const;
......
......@@ -49,6 +49,7 @@ GLSLEditorEditable::GLSLEditorEditable(GLSLTextEditorWidget *editor)
setContext(Core::Context(GLSLEditor::Constants::C_GLSLEDITOR_ID,
TextEditor::Constants::C_TEXTEDITOR));
setDuplicateSupported(true);
setCommentStyle(Utils::CommentDefinition::CppStyle);
}
} // namespace Internal
......
......@@ -53,10 +53,7 @@ PythonEditor::PythonEditor(EditorWidget *editorWidget)
setContext(Core::Context(Constants::C_PYTHONEDITOR_ID,
TextEditor::Constants::C_TEXTEDITOR));
setDuplicateSupported(true);
}
PythonEditor::~PythonEditor()
{
setCommentStyle(Utils::CommentDefinition::HashStyle);
}
Core::IEditor *PythonEditor::duplicate()
......
......@@ -43,7 +43,6 @@ class PythonEditor : public TextEditor::BaseTextEditor
public:
explicit PythonEditor(EditorWidget *editorWidget);
virtual ~PythonEditor();
Core::IEditor *duplicate();
......
......@@ -64,10 +64,6 @@ EditorWidget::EditorWidget(EditorWidget *other)
void EditorWidget::ctor()
{
m_commentDefinition.multiLineStart.clear();
m_commentDefinition.multiLineEnd.clear();
m_commentDefinition.singleLine = QLatin1Char('#');
setParenthesesMatchingEnabled(true);
setMarksVisible(true);
setCodeFoldingSupported(true);
......@@ -75,18 +71,6 @@ void EditorWidget::ctor()
new PythonHighlighter(baseTextDocument());
}
EditorWidget::~EditorWidget()
{
}
/**
Comments or uncomments selection using Python commenting syntax
*/
void EditorWidget::unCommentSelection()
{
Utils::unCommentSelection(this, m_commentDefinition);
}
TextEditor::BaseTextEditor *EditorWidget::createEditor()
{
return new PythonEditor(this);
......
......@@ -43,9 +43,6 @@ class EditorWidget : public TextEditor::BaseTextEditorWidget
public:
EditorWidget(QWidget *parent = 0);
EditorWidget(EditorWidget *other);
virtual ~EditorWidget();
virtual void unCommentSelection();
protected:
TextEditor::BaseTextEditor *createEditor();
......@@ -53,7 +50,6 @@ protected:
private:
EditorWidget(TextEditor::BaseTextEditorWidget *); // avoid stupidity
void ctor();
Utils::CommentDefinition m_commentDefinition;
};
} // namespace Internal
......
......@@ -58,6 +58,7 @@ ProFileEditor::ProFileEditor(ProFileEditorWidget *editor)
setContext(Core::Context(Constants::C_PROFILEEDITOR,
TextEditor::Constants::C_TEXTEDITOR));
setDuplicateSupported(true);
setCommentStyle(Utils::CommentDefinition::HashStyle);
}
Core::IEditor *ProFileEditor::duplicate()
......@@ -79,26 +80,11 @@ TextEditor::CompletionAssistProvider *ProFileEditor::completionAssistProvider()
ProFileEditorWidget::ProFileEditorWidget(QWidget *parent)
: BaseTextEditorWidget(new ProFileDocument(), parent)
{
ctor();
}
{}
ProFileEditorWidget::ProFileEditorWidget(ProFileEditorWidget *other)
: BaseTextEditorWidget(other)
{
ctor();
}
void ProFileEditorWidget::ctor()
{
m_commentDefinition.clearCommentStyles();
m_commentDefinition.singleLine = QLatin1Char('#');
}
void ProFileEditorWidget::unCommentSelection()
{
Utils::unCommentSelection(this, m_commentDefinition);
}
{}
static bool isValidFileNameChar(const QChar &c)
{
......
......@@ -59,8 +59,6 @@ public:
ProFileEditorWidget(QWidget *parent = 0);
ProFileEditorWidget(ProFileEditorWidget *other);
void unCommentSelection();
protected:
virtual Link findLinkAt(const QTextCursor &, bool resolveTarget = true,
bool inNextSplit = false);
......@@ -69,8 +67,6 @@ protected:
private:
ProFileEditorWidget(BaseTextEditorWidget *); // avoid stupidity
void ctor();
Utils::CommentDefinition m_commentDefinition;
};
class ProFileDocument : public TextEditor::BaseTextDocument
......
......@@ -800,11 +800,6 @@ void QmlJSTextEditorWidget::resizeEvent(QResizeEvent *event)
hideContextPane();
}
void QmlJSTextEditorWidget::unCommentSelection()
{
Utils::unCommentSelection(this);
}
QmlJSEditorDocument *QmlJSTextEditorWidget::qmlJsEditorDocument() const
{
return m_qmlJsEditorDocument;
......
......@@ -78,8 +78,6 @@ public:
QmlJSTextEditorWidget(QmlJSTextEditorWidget *other);
~QmlJSTextEditorWidget();
virtual void unCommentSelection();
QmlJSEditorDocument *qmlJsEditorDocument() const;
QModelIndex outlineModelIndex();
......
......@@ -54,6 +54,7 @@ QmlJSEditor::QmlJSEditor(QmlJSTextEditorWidget *editor)
m_context.add(TextEditor::Constants::C_TEXTEDITOR);
m_context.add(ProjectExplorer::Constants::LANG_QMLJS);
setDuplicateSupported(true);
setCommentStyle(Utils::CommentDefinition::CppStyle);
}
bool QmlJSEditor::isDesignModePreferred() const
......@@ -65,11 +66,6 @@ bool QmlJSEditor::isDesignModePreferred() const
return false;
}
const Utils::CommentDefinition *QmlJSEditor::commentDefinition() const
{
return &m_commentDefinition;
}
TextEditor::CompletionAssistProvider *QmlJSEditor::completionAssistProvider()
{
return ExtensionSystem::PluginManager::getObject<Internal::QmlJSCompletionAssistProvider>();
......
......@@ -50,11 +50,7 @@ public:
bool open(QString *errorString, const QString &fileName, const QString &realFileName);
bool isDesignModePreferred() const;
const Utils::CommentDefinition *commentDefinition() const;
TextEditor::CompletionAssistProvider *completionAssistProvider();
private:
Utils::CommentDefinition m_commentDefinition;
};
} // namespace Internal
......
......@@ -197,6 +197,7 @@ public:
Utils::LineColumnLabel *m_cursorPositionLabel;
QAction *m_fileEncodingLabelAction;
Utils::LineColumnLabel *m_fileEncodingLabel;
CommentDefinition m_commentDefinition;
};
class BaseTextEditorWidgetPrivate
......@@ -367,7 +368,6 @@ public:
QScopedPointer<Internal::ClipboardAssistProvider> m_clipboardAssistProvider;
bool m_isMissingSyntaxDefinition;
Utils::CommentDefinition m_commentDefinition;
};
class TextEditExtraArea : public QWidget
......@@ -531,8 +531,6 @@ void BaseTextEditorWidgetPrivate::ctor(const QSharedPointer<BaseTextDocument> &d
QObject::connect(&m_delayedUpdateTimer, SIGNAL(timeout()), q->viewport(), SLOT(update()));
m_moveLineUndoHack = false;
m_commentDefinition.clearCommentStyles();
}
BaseTextEditorWidget::~BaseTextEditorWidget()
......@@ -1396,18 +1394,18 @@ void BaseTextEditorWidgetPrivate::moveLineUpDown(bool up)
m_refactorOverlay->setMarkers(nonAffectedMarkers + affectedMarkers);
bool shouldReindent = true;
const CommentDefinition *commentDefinition = q->editor()->commentDefinition();
if (commentDefinition) {
const CommentDefinition &cd = q->editor()->commentDefinition();
if (cd.isValid()) {
QString trimmedText(text.trimmed());
if (commentDefinition->hasSingleLineStyle()) {
if (trimmedText.startsWith(commentDefinition->singleLine))
if (cd.hasSingleLineStyle()) {
if (trimmedText.startsWith(cd.singleLine))
shouldReindent = false;
}
if (shouldReindent && commentDefinition->hasMultiLineStyle()) {
if (shouldReindent && cd.hasMultiLineStyle()) {
// Don't have any single line comments; try multi line.
if (trimmedText.startsWith(commentDefinition->multiLineStart)
&& trimmedText.endsWith(commentDefinition->multiLineEnd)) {
if (trimmedText.startsWith(cd.multiLineStart)
&& trimmedText.endsWith(cd.multiLineEnd)) {
shouldReindent = false;
}
}
......@@ -5883,7 +5881,7 @@ void BaseTextEditorWidget::rewrapParagraph()
void BaseTextEditorWidget::unCommentSelection()
{
Utils::unCommentSelection(this, d->m_commentDefinition);
Utils::unCommentSelection(this, editor()->commentDefinition());
}
void BaseTextEditorWidget::showEvent(QShowEvent* e)
......@@ -6577,9 +6575,14 @@ void BaseTextEditor::select(int toPos)
d->m_editorWidget->setTextCursor(tc);
}
const CommentDefinition *BaseTextEditor::commentDefinition() const
CommentDefinition &BaseTextEditor::commentDefinition() const
{
return 0;
return d->m_commentDefinition;
}
void BaseTextEditor::setCommentStyle(CommentDefinition::Style style)
{
d->m_commentDefinition.setStyle(style);
}
CompletionAssistProvider *BaseTextEditor::completionAssistProvider()
......@@ -6987,10 +6990,11 @@ void BaseTextEditorWidget::configureMimeType(const MimeType &mimeType)
const QSharedPointer<HighlightDefinition> &definition =
Manager::instance()->definition(definitionId);
if (!definition.isNull() && definition->isValid()) {
d->m_commentDefinition.isAfterWhiteSpaces = definition->isCommentAfterWhiteSpaces();
d->m_commentDefinition.singleLine = definition->singleLineComment();
d->m_commentDefinition.multiLineStart = definition->multiLineCommentStart();
d->m_commentDefinition.multiLineEnd = definition->multiLineCommentEnd();
CommentDefinition &cd = editor()->commentDefinition();
cd.isAfterWhiteSpaces = definition->isCommentAfterWhiteSpaces();
cd.singleLine = definition->singleLineComment();
cd.multiLineStart = definition->multiLineCommentStart();
cd.multiLineEnd = definition->multiLineCommentEnd();
setCodeFoldingSupported(true);
}
......
......@@ -39,6 +39,8 @@
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/find/ifindsupport.h>
#include <utils/uncommentselection.h>
#include <QPlainTextEdit>
QT_BEGIN_NAMESPACE
......@@ -53,11 +55,6 @@ QT_END_NAMESPACE
namespace Core { class MimeType; }
namespace Utils {
class CommentDefinition;
class LineColumnLabel;
}
namespace TextEditor {
class TabSettings;
......@@ -183,7 +180,10 @@ public:
/*! Selects text between current cursor position and \a toPos. */
virtual void select(int toPos);
virtual const Utils::CommentDefinition *commentDefinition() const;
/*! Full access to comment definition, */
Utils::CommentDefinition &commentDefinition() const;
/*! Convenience style setter. */
void setCommentStyle(Utils::CommentDefinition::Style style);
virtual CompletionAssistProvider *completionAssistProvider();
......
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