2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "plaintexteditor.h"
|
2009-12-08 17:43:01 +01:00
|
|
|
#include "tabsettings.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "texteditorconstants.h"
|
|
|
|
|
#include "texteditorplugin.h"
|
2010-05-20 13:56:11 +02:00
|
|
|
#include "texteditorsettings.h"
|
|
|
|
|
#include "basetextdocument.h"
|
|
|
|
|
#include "highlightdefinition.h"
|
|
|
|
|
#include "highlighter.h"
|
|
|
|
|
#include "highlighterexception.h"
|
2010-06-04 10:13:38 +02:00
|
|
|
#include "highlightersettings.h"
|
2010-05-20 13:56:11 +02:00
|
|
|
#include "manager.h"
|
2010-06-02 17:31:25 +02:00
|
|
|
#include "context.h"
|
2010-05-21 14:15:04 +02:00
|
|
|
#include "normalindenter.h"
|
2010-05-31 13:56:46 +02:00
|
|
|
#include "fontsettings.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <coreplugin/coreconstants.h>
|
2010-05-20 13:56:11 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/mimedatabase.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QSharedPointer>
|
|
|
|
|
#include <QFileInfo>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
2010-05-28 14:37:25 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace TextEditor;
|
|
|
|
|
using namespace TextEditor::Internal;
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
PlainTextEditor::PlainTextEditor(PlainTextEditorWidget *editor)
|
2011-04-13 13:00:30 +02:00
|
|
|
: BaseTextEditor(editor)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-04-13 13:00:30 +02:00
|
|
|
setContext(Core::Context(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID,
|
|
|
|
|
TextEditor::Constants::C_TEXTEDITOR));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
PlainTextEditorWidget::PlainTextEditorWidget(QWidget *parent)
|
|
|
|
|
: BaseTextEditorWidget(parent),
|
2010-10-11 12:40:09 +02:00
|
|
|
m_isMissingSyntaxDefinition(false),
|
2010-11-05 14:27:16 +01:00
|
|
|
m_ignoreMissingSyntaxDefinition(false)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
setRevisionsVisible(true);
|
|
|
|
|
setMarksVisible(true);
|
|
|
|
|
setRequestMarkEnabled(false);
|
|
|
|
|
setLineSeparatorsAllowed(true);
|
2010-11-05 14:27:16 +01:00
|
|
|
setIndenter(new NormalIndenter); // Currently only "normal" indentation is supported.
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-21 14:15:04 +02:00
|
|
|
setMimeType(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT));
|
2010-01-07 18:17:24 +01:00
|
|
|
setDisplayName(tr(Core::Constants::K_DEFAULT_TEXT_EDITOR_DISPLAY_NAME));
|
2010-05-20 13:56:11 +02:00
|
|
|
|
|
|
|
|
m_commentDefinition.clearCommentStyles();
|
|
|
|
|
|
2010-08-18 15:33:52 +02:00
|
|
|
connect(file(), SIGNAL(changed()), this, SLOT(configure()));
|
|
|
|
|
connect(Manager::instance(), SIGNAL(mimeTypesRegistered()), this, SLOT(configure()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
Core::IEditor *PlainTextEditor::duplicate(QWidget *parent)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-02-21 16:02:26 +01:00
|
|
|
PlainTextEditorWidget *newWidget = new PlainTextEditorWidget(parent);
|
|
|
|
|
newWidget->duplicateFrom(editorWidget());
|
|
|
|
|
TextEditorPlugin::instance()->initializeEditor(newWidget);
|
|
|
|
|
return newWidget->editor();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-10 11:36:51 +01:00
|
|
|
Core::Id PlainTextEditor::id() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-11-10 11:36:51 +01:00
|
|
|
return Core::Constants::K_DEFAULT_TEXT_EDITOR_ID;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
void PlainTextEditorWidget::unCommentSelection()
|
2010-05-20 13:56:11 +02:00
|
|
|
{
|
|
|
|
|
Utils::unCommentSelection(this, m_commentDefinition);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
void PlainTextEditorWidget::setFontSettings(const FontSettings &fs)
|
2010-05-20 13:56:11 +02:00
|
|
|
{
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget::setFontSettings(fs);
|
2010-05-20 13:56:11 +02:00
|
|
|
|
2010-05-21 14:15:04 +02:00
|
|
|
if (baseTextDocument()->syntaxHighlighter()) {
|
|
|
|
|
Highlighter *highlighter =
|
2010-05-31 13:56:46 +02:00
|
|
|
static_cast<Highlighter *>(baseTextDocument()->syntaxHighlighter());
|
|
|
|
|
|
|
|
|
|
highlighter->configureFormat(Highlighter::VisualWhitespace, fs.toTextCharFormat(
|
|
|
|
|
QLatin1String(Constants::C_VISUAL_WHITESPACE)));
|
|
|
|
|
highlighter->configureFormat(Highlighter::Keyword, fs.toTextCharFormat(
|
|
|
|
|
QLatin1String(Constants::C_KEYWORD)));
|
|
|
|
|
highlighter->configureFormat(Highlighter::DataType, fs.toTextCharFormat(
|
|
|
|
|
QLatin1String(Constants::C_TYPE)));
|
|
|
|
|
highlighter->configureFormat(Highlighter::Comment, fs.toTextCharFormat(
|
|
|
|
|
QLatin1String(Constants::C_COMMENT)));
|
|
|
|
|
// Using C_NUMBER for all kinds of numbers.
|
|
|
|
|
highlighter->configureFormat(Highlighter::Decimal, fs.toTextCharFormat(
|
|
|
|
|
QLatin1String(Constants::C_NUMBER)));
|
|
|
|
|
highlighter->configureFormat(Highlighter::BaseN, fs.toTextCharFormat(
|
|
|
|
|
QLatin1String(Constants::C_NUMBER)));
|
|
|
|
|
highlighter->configureFormat(Highlighter::Float, fs.toTextCharFormat(
|
|
|
|
|
QLatin1String(Constants::C_NUMBER)));
|
|
|
|
|
// Using C_STRING for strings and chars.
|
|
|
|
|
highlighter->configureFormat(Highlighter::Char, fs.toTextCharFormat(
|
|
|
|
|
QLatin1String(Constants::C_STRING)));
|
|
|
|
|
highlighter->configureFormat(Highlighter::String, fs.toTextCharFormat(
|
|
|
|
|
QLatin1String(Constants::C_STRING)));
|
|
|
|
|
|
2010-05-21 14:15:04 +02:00
|
|
|
highlighter->rehighlight();
|
|
|
|
|
}
|
2010-05-20 13:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
void PlainTextEditorWidget::setTabSettings(const TextEditor::TabSettings &ts)
|
2010-06-21 13:37:53 +02:00
|
|
|
{
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget::setTabSettings(ts);
|
2010-06-21 13:37:53 +02:00
|
|
|
|
|
|
|
|
if (baseTextDocument()->syntaxHighlighter()) {
|
|
|
|
|
Highlighter *highlighter =
|
|
|
|
|
static_cast<Highlighter *>(baseTextDocument()->syntaxHighlighter());
|
|
|
|
|
highlighter->setTabSettings(ts);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
void PlainTextEditorWidget::configure()
|
2010-05-20 13:56:11 +02:00
|
|
|
{
|
2010-09-29 09:14:30 +02:00
|
|
|
Core::MimeType mimeType;
|
2010-08-19 09:18:58 +02:00
|
|
|
if (file())
|
2012-01-24 15:36:40 +01:00
|
|
|
mimeType = Core::ICore::mimeDatabase()->findByFile(file()->fileName());
|
2010-09-29 09:14:30 +02:00
|
|
|
configure(mimeType);
|
2010-05-21 14:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
2011-06-29 17:45:52 +02:00
|
|
|
void PlainTextEditorWidget::configure(const QString &mimeType)
|
|
|
|
|
{
|
2012-01-24 15:36:40 +01:00
|
|
|
configure(Core::ICore::mimeDatabase()->findByType(mimeType));
|
2011-06-29 17:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
void PlainTextEditorWidget::configure(const Core::MimeType &mimeType)
|
2010-05-21 14:15:04 +02:00
|
|
|
{
|
2010-06-15 10:35:39 +02:00
|
|
|
Highlighter *highlighter = new Highlighter();
|
|
|
|
|
baseTextDocument()->setSyntaxHighlighter(highlighter);
|
2010-06-18 16:30:56 +02:00
|
|
|
|
|
|
|
|
setCodeFoldingSupported(false);
|
2010-05-28 14:37:25 +02:00
|
|
|
|
2010-06-15 10:35:39 +02:00
|
|
|
if (!mimeType.isNull()) {
|
2010-09-29 09:14:30 +02:00
|
|
|
m_isMissingSyntaxDefinition = true;
|
|
|
|
|
|
2010-06-15 10:35:39 +02:00
|
|
|
const QString &type = mimeType.type();
|
|
|
|
|
setMimeType(type);
|
2010-05-21 14:15:04 +02:00
|
|
|
|
2010-09-29 09:14:30 +02:00
|
|
|
QString definitionId = Manager::instance()->definitionIdByMimeType(type);
|
2010-06-15 10:35:39 +02:00
|
|
|
if (definitionId.isEmpty())
|
|
|
|
|
definitionId = findDefinitionId(mimeType, true);
|
2010-05-20 13:56:11 +02:00
|
|
|
|
2010-09-29 09:14:30 +02:00
|
|
|
if (!definitionId.isEmpty()) {
|
2010-06-04 10:13:38 +02:00
|
|
|
m_isMissingSyntaxDefinition = false;
|
2010-09-29 09:14:30 +02:00
|
|
|
const QSharedPointer<HighlightDefinition> &definition =
|
|
|
|
|
Manager::instance()->definition(definitionId);
|
|
|
|
|
if (!definition.isNull()) {
|
|
|
|
|
highlighter->setDefaultContext(definition->initialContext());
|
|
|
|
|
|
|
|
|
|
m_commentDefinition.setAfterWhiteSpaces(definition->isCommentAfterWhiteSpaces());
|
|
|
|
|
m_commentDefinition.setSingleLine(definition->singleLineComment());
|
|
|
|
|
m_commentDefinition.setMultiLineStart(definition->multiLineCommentStart());
|
|
|
|
|
m_commentDefinition.setMultiLineEnd(definition->multiLineCommentEnd());
|
|
|
|
|
|
|
|
|
|
setCodeFoldingSupported(true);
|
|
|
|
|
}
|
|
|
|
|
} else if (file()) {
|
|
|
|
|
const QString &fileName = file()->fileName();
|
|
|
|
|
if (TextEditorSettings::instance()->highlighterSettings().isIgnoredFilePattern(fileName))
|
|
|
|
|
m_isMissingSyntaxDefinition = false;
|
|
|
|
|
}
|
2010-05-20 13:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
2010-06-15 10:35:39 +02:00
|
|
|
setFontSettings(TextEditorSettings::instance()->fontSettings());
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
emit configured(editor());
|
2010-05-21 14:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
bool PlainTextEditorWidget::isMissingSyntaxDefinition() const
|
2010-05-28 14:37:25 +02:00
|
|
|
{
|
|
|
|
|
return m_isMissingSyntaxDefinition;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
bool PlainTextEditorWidget::ignoreMissingSyntaxDefinition() const
|
2010-10-11 12:40:09 +02:00
|
|
|
{
|
|
|
|
|
return m_ignoreMissingSyntaxDefinition;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
QString PlainTextEditorWidget::findDefinitionId(const Core::MimeType &mimeType,
|
2010-05-21 14:15:04 +02:00
|
|
|
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 =
|
2012-01-24 15:36:40 +01:00
|
|
|
Core::ICore::mimeDatabase()->findByType(parent);
|
2010-05-21 14:15:04 +02:00
|
|
|
definitionId = findDefinitionId(parentMimeType, considerParents);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return definitionId;
|
2010-05-20 13:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
void PlainTextEditorWidget::acceptMissingSyntaxDefinitionInfo()
|
2010-10-11 12:40:09 +02:00
|
|
|
{
|
2012-01-24 15:36:40 +01:00
|
|
|
Core::ICore::showOptionsDialog(QLatin1String(Constants::TEXT_EDITOR_SETTINGS_CATEGORY),
|
2012-01-05 11:05:28 +01:00
|
|
|
QLatin1String(Constants::TEXT_EDITOR_HIGHLIGHTER_SETTINGS));
|
2010-10-11 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
void PlainTextEditorWidget::ignoreMissingSyntaxDefinitionInfo()
|
2010-10-11 12:40:09 +02:00
|
|
|
{
|
|
|
|
|
m_ignoreMissingSyntaxDefinition = true;
|
|
|
|
|
}
|