2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02: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 "texteditorplugin.h"
|
2010-05-20 13:56:11 +02:00
|
|
|
#include "texteditorsettings.h"
|
|
|
|
|
#include "basetextdocument.h"
|
|
|
|
|
#include "highlightdefinition.h"
|
|
|
|
|
#include "highlighter.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>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QSharedPointer>
|
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
|
|
|
|
2012-12-29 01:31:08 +01:00
|
|
|
using namespace Core;
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace TextEditor::Internal;
|
|
|
|
|
|
2012-12-29 01:31:08 +01:00
|
|
|
namespace TextEditor {
|
|
|
|
|
|
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),
|
2012-11-13 23:19:26 +02:00
|
|
|
m_isMissingSyntaxDefinition(false)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
setRevisionsVisible(true);
|
|
|
|
|
setMarksVisible(true);
|
|
|
|
|
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();
|
|
|
|
|
|
2012-02-14 16:43:51 +01:00
|
|
|
connect(editorDocument(), SIGNAL(changed()), this, SLOT(configure()));
|
2010-08-18 15:33:52 +02:00
|
|
|
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
|
|
|
{
|
2012-11-20 07:18:01 +02:00
|
|
|
return Core::Id(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());
|
|
|
|
|
|
2012-04-26 14:17:42 +02:00
|
|
|
highlighter->configureFormat(Highlighter::VisualWhitespace, fs.toTextCharFormat(C_VISUAL_WHITESPACE));
|
|
|
|
|
highlighter->configureFormat(Highlighter::Keyword, fs.toTextCharFormat(C_KEYWORD));
|
|
|
|
|
highlighter->configureFormat(Highlighter::DataType, fs.toTextCharFormat(C_TYPE));
|
|
|
|
|
highlighter->configureFormat(Highlighter::Comment, fs.toTextCharFormat(C_COMMENT));
|
2010-05-31 13:56:46 +02:00
|
|
|
// Using C_NUMBER for all kinds of numbers.
|
2012-04-26 14:17:42 +02:00
|
|
|
highlighter->configureFormat(Highlighter::Decimal, fs.toTextCharFormat(C_NUMBER));
|
|
|
|
|
highlighter->configureFormat(Highlighter::BaseN, fs.toTextCharFormat(C_NUMBER));
|
|
|
|
|
highlighter->configureFormat(Highlighter::Float, fs.toTextCharFormat(C_NUMBER));
|
2010-05-31 13:56:46 +02:00
|
|
|
// Using C_STRING for strings and chars.
|
2012-04-26 14:17:42 +02:00
|
|
|
highlighter->configureFormat(Highlighter::Char, fs.toTextCharFormat(C_STRING));
|
|
|
|
|
highlighter->configureFormat(Highlighter::String, fs.toTextCharFormat(C_STRING));
|
2010-05-31 13:56:46 +02:00
|
|
|
|
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;
|
2012-02-14 16:43:51 +01:00
|
|
|
if (editorDocument())
|
|
|
|
|
mimeType = Core::ICore::mimeDatabase()->findByFile(editorDocument()->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);
|
2012-03-15 11:54:12 -03:00
|
|
|
if (!definition.isNull() && definition->isValid()) {
|
2010-09-29 09:14:30 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2012-02-14 16:43:51 +01:00
|
|
|
} else if (editorDocument()) {
|
|
|
|
|
const QString &fileName = editorDocument()->fileName();
|
2010-09-29 09:14:30 +02:00
|
|
|
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
|
|
|
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-12-29 01:31:08 +01:00
|
|
|
ICore::showOptionsDialog(Constants::TEXT_EDITOR_SETTINGS_CATEGORY,
|
|
|
|
|
Constants::TEXT_EDITOR_HIGHLIGHTER_SETTINGS);
|
2010-10-11 12:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
2012-12-29 01:31:08 +01:00
|
|
|
} // namespace TextEditor
|