plaintexteditor.cpp 8.88 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "plaintexteditor.h"
31
#include "tabsettings.h"
con's avatar
con committed
32
33
#include "texteditorconstants.h"
#include "texteditorplugin.h"
34
35
36
37
38
#include "texteditorsettings.h"
#include "basetextdocument.h"
#include "highlightdefinition.h"
#include "highlighter.h"
#include "highlighterexception.h"
39
#include "highlightersettings.h"
40
#include "manager.h"
41
#include "context.h"
42
#include "normalindenter.h"
43
#include "fontsettings.h"
con's avatar
con committed
44
45

#include <coreplugin/coreconstants.h>
46
47
48
49
50
#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>

#include <QtCore/QSharedPointer>
#include <QtCore/QFileInfo>
con's avatar
con committed
51

52
53
#include <QDebug>

con's avatar
con committed
54
55
56
57
using namespace TextEditor;
using namespace TextEditor::Internal;

PlainTextEditorEditable::PlainTextEditorEditable(PlainTextEditor *editor)
58
59
  : BaseTextEditorEditable(editor),
    m_context(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID, TextEditor::Constants::C_TEXTEDITOR)
con's avatar
con committed
60
61
62
{
}

63
PlainTextEditor::PlainTextEditor(QWidget *parent)
64
  : BaseTextEditor(parent),
65
66
  m_isMissingSyntaxDefinition(false),
  m_ignoreMissingSyntaxDefinition(false)
con's avatar
con committed
67
68
69
70
71
72
{
    setRevisionsVisible(true);
    setMarksVisible(true);
    setRequestMarkEnabled(false);
    setLineSeparatorsAllowed(true);

73
    setMimeType(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT));
74
    setDisplayName(tr(Core::Constants::K_DEFAULT_TEXT_EDITOR_DISPLAY_NAME));
75
76
77

    m_commentDefinition.clearCommentStyles();

78
79
    connect(file(), SIGNAL(changed()), this, SLOT(configure()));
    connect(Manager::instance(), SIGNAL(mimeTypesRegistered()), this, SLOT(configure()));
con's avatar
con committed
80
81
}

82
83
84
PlainTextEditor::~PlainTextEditor()
{}

85
Core::Context PlainTextEditorEditable::context() const
con's avatar
con committed
86
87
88
89
90
91
92
93
94
95
96
97
{
    return m_context;
}

Core::IEditor *PlainTextEditorEditable::duplicate(QWidget *parent)
{
    PlainTextEditor *newEditor = new PlainTextEditor(parent);
    newEditor->duplicateFrom(editor());
    TextEditorPlugin::instance()->initializeEditor(newEditor);
    return newEditor->editableInterface();
}

98
QString PlainTextEditorEditable::id() const
con's avatar
con committed
99
{
100
    return QLatin1String(Core::Constants::K_DEFAULT_TEXT_EDITOR_ID);
con's avatar
con committed
101
102
}

103
104
105
106
107
void PlainTextEditor::unCommentSelection()
{
    Utils::unCommentSelection(this, m_commentDefinition);
}

108
void PlainTextEditor::setFontSettings(const FontSettings &fs)
109
{
110
    BaseTextEditor::setFontSettings(fs);
111

112
113
    if (baseTextDocument()->syntaxHighlighter()) {
        Highlighter *highlighter =
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
            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)));

137
138
        highlighter->rehighlight();
    }
139
140
}

141
142
143
144
145
146
147
148
149
150
151
void PlainTextEditor::setTabSettings(const TextEditor::TabSettings &ts)
{
    BaseTextEditor::setTabSettings(ts);

    if (baseTextDocument()->syntaxHighlighter()) {
        Highlighter *highlighter =
            static_cast<Highlighter *>(baseTextDocument()->syntaxHighlighter());
        highlighter->setTabSettings(ts);
    }
}

152
void PlainTextEditor::configure()
153
{
154
    Core::MimeType mimeType;
155
    if (file())
156
157
        mimeType = Core::ICore::instance()->mimeDatabase()->findByFile(file()->fileName());
    configure(mimeType);
158
159
160
161
}

void PlainTextEditor::configure(const Core::MimeType &mimeType)
{
162
163
    Highlighter *highlighter = new Highlighter();
    baseTextDocument()->setSyntaxHighlighter(highlighter);
164
165
166

    setCodeFoldingSupported(false);
    setCodeFoldingVisible(false);
167

168
    if (!mimeType.isNull()) {
169
170
        m_isMissingSyntaxDefinition = true;

171
172
        const QString &type = mimeType.type();
        setMimeType(type);
173

174
        QString definitionId = Manager::instance()->definitionIdByMimeType(type);
175
176
        if (definitionId.isEmpty())
            definitionId = findDefinitionId(mimeType, true);
177

178
        if (!definitionId.isEmpty()) {
179
            m_isMissingSyntaxDefinition = false;
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
            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);
                setCodeFoldingVisible(true);
            }
        } else if (file()) {
            const QString &fileName = file()->fileName();
            if (TextEditorSettings::instance()->highlighterSettings().isIgnoredFilePattern(fileName))
                m_isMissingSyntaxDefinition = false;
        }
198
199
    }

200
201
    setFontSettings(TextEditorSettings::instance()->fontSettings());

202
203
204
205
    // @todo: Indentation specification through the definition files is not really being used
    // because Kate recommends to configure indentation  through another feature. Maybe we should
    // provide something similar in Creator? For now, only normal indentation is supported.
    m_indenter.reset(new NormalIndenter);
206
207

    emit configured(editableInterface());
208
209
}

210
211
212
213
214
bool PlainTextEditor::isMissingSyntaxDefinition() const
{
    return m_isMissingSyntaxDefinition;
}

215
216
217
218
219
bool PlainTextEditor::ignoreMissingSyntaxDefinition() const
{
    return m_ignoreMissingSyntaxDefinition;
}

220
221
222
223
224
225
226
227
228
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 =
229
                    Core::ICore::instance()->mimeDatabase()->findByType(parent);
230
231
232
233
234
                definitionId = findDefinitionId(parentMimeType, considerParents);
            }
        }
    }
    return definitionId;
235
236
}

237
void PlainTextEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
con's avatar
con committed
238
{
239
    m_indenter->indentBlock(doc, block, typedChar, tabSettings());
con's avatar
con committed
240
}
241
242
243
244
245
246
247
248
249
250
251

void PlainTextEditor::acceptMissingSyntaxDefinitionInfo()
{
    Core::ICore::instance()->showOptionsDialog(Constants::TEXT_EDITOR_SETTINGS_CATEGORY,
                                               Constants::TEXT_EDITOR_HIGHLIGHTER_SETTINGS);
}

void PlainTextEditor::ignoreMissingSyntaxDefinitionInfo()
{
    m_ignoreMissingSyntaxDefinition = true;
}