2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** No Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** This file contains pre-release code and may not be distributed.
|
|
|
|
|
** You may use this file in accordance with the terms and conditions
|
|
|
|
|
** contained in the Technology Preview License Agreement accompanying
|
|
|
|
|
** this package.
|
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
|
|
|
**
|
2009-02-25 09:15:00 +01: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.
|
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
|
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please contact
|
|
|
|
|
** 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>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QSharedPointer>
|
|
|
|
|
#include <QtCore/QFileInfo>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-01-24 12:29:48 +01:00
|
|
|
#include <QtCore/QDebug>
|
2010-05-28 14:37:25 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace TextEditor;
|
|
|
|
|
using namespace TextEditor::Internal;
|
|
|
|
|
|
|
|
|
|
PlainTextEditorEditable::PlainTextEditorEditable(PlainTextEditor *editor)
|
2010-06-25 17:37:59 +02:00
|
|
|
: BaseTextEditorEditable(editor),
|
|
|
|
|
m_context(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID, TextEditor::Constants::C_TEXTEDITOR)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-21 18:30:45 +01:00
|
|
|
PlainTextEditor::PlainTextEditor(QWidget *parent)
|
2010-05-28 14:37:25 +02:00
|
|
|
: BaseTextEditor(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
|
|
|
}
|
|
|
|
|
|
2010-05-21 14:15:04 +02:00
|
|
|
PlainTextEditor::~PlainTextEditor()
|
|
|
|
|
{}
|
|
|
|
|
|
2010-06-25 12:56:16 +02:00
|
|
|
Core::Context PlainTextEditorEditable::context() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
return m_context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::IEditor *PlainTextEditorEditable::duplicate(QWidget *parent)
|
|
|
|
|
{
|
|
|
|
|
PlainTextEditor *newEditor = new PlainTextEditor(parent);
|
|
|
|
|
newEditor->duplicateFrom(editor());
|
|
|
|
|
TextEditorPlugin::instance()->initializeEditor(newEditor);
|
|
|
|
|
return newEditor->editableInterface();
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-07 18:17:24 +01:00
|
|
|
QString PlainTextEditorEditable::id() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-01-07 18:17:24 +01:00
|
|
|
return QLatin1String(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-05-20 13:56:11 +02:00
|
|
|
void PlainTextEditor::unCommentSelection()
|
|
|
|
|
{
|
|
|
|
|
Utils::unCommentSelection(this, m_commentDefinition);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-31 13:56:46 +02:00
|
|
|
void PlainTextEditor::setFontSettings(const FontSettings &fs)
|
2010-05-20 13:56:11 +02:00
|
|
|
{
|
2010-05-21 14:15:04 +02:00
|
|
|
BaseTextEditor::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
|
|
|
}
|
|
|
|
|
|
2010-06-21 13:37:53 +02:00
|
|
|
void PlainTextEditor::setTabSettings(const TextEditor::TabSettings &ts)
|
|
|
|
|
{
|
|
|
|
|
BaseTextEditor::setTabSettings(ts);
|
|
|
|
|
|
|
|
|
|
if (baseTextDocument()->syntaxHighlighter()) {
|
|
|
|
|
Highlighter *highlighter =
|
|
|
|
|
static_cast<Highlighter *>(baseTextDocument()->syntaxHighlighter());
|
|
|
|
|
highlighter->setTabSettings(ts);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-18 15:33:52 +02:00
|
|
|
void PlainTextEditor::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())
|
2010-09-29 09:14:30 +02:00
|
|
|
mimeType = Core::ICore::instance()->mimeDatabase()->findByFile(file()->fileName());
|
|
|
|
|
configure(mimeType);
|
2010-05-21 14:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PlainTextEditor::configure(const Core::MimeType &mimeType)
|
|
|
|
|
{
|
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());
|
|
|
|
|
|
2010-09-20 11:47:01 +02:00
|
|
|
emit configured(editableInterface());
|
2010-05-21 14:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
2010-05-28 14:37:25 +02:00
|
|
|
bool PlainTextEditor::isMissingSyntaxDefinition() const
|
|
|
|
|
{
|
|
|
|
|
return m_isMissingSyntaxDefinition;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 12:40:09 +02:00
|
|
|
bool PlainTextEditor::ignoreMissingSyntaxDefinition() const
|
|
|
|
|
{
|
|
|
|
|
return m_ignoreMissingSyntaxDefinition;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-21 14:15:04 +02:00
|
|
|
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 =
|
2010-05-28 14:37:25 +02:00
|
|
|
Core::ICore::instance()->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
|
|
|
}
|
|
|
|
|
|
2010-10-11 12:40:09 +02:00
|
|
|
void PlainTextEditor::acceptMissingSyntaxDefinitionInfo()
|
|
|
|
|
{
|
|
|
|
|
Core::ICore::instance()->showOptionsDialog(Constants::TEXT_EDITOR_SETTINGS_CATEGORY,
|
|
|
|
|
Constants::TEXT_EDITOR_HIGHLIGHTER_SETTINGS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PlainTextEditor::ignoreMissingSyntaxDefinitionInfo()
|
|
|
|
|
{
|
|
|
|
|
m_ignoreMissingSyntaxDefinition = true;
|
|
|
|
|
}
|