2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-11-05 14:27:16 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-11-05 14:27:16 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-11-05 14:27:16 +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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-11-05 14:27:16 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 17:14:20 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-11-05 14:27:16 +01:00
|
|
|
|
|
|
|
|
#include "qmljsindenter.h"
|
|
|
|
|
|
2010-11-11 10:05:05 +01:00
|
|
|
#include <qmljstools/qmljsqtstylecodeformatter.h>
|
2010-11-05 14:27:16 +01:00
|
|
|
#include <texteditor/tabsettings.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QChar>
|
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
#include <QTextBlock>
|
2010-11-05 14:27:16 +01:00
|
|
|
|
|
|
|
|
using namespace QmlJSEditor;
|
|
|
|
|
using namespace Internal;
|
|
|
|
|
|
|
|
|
|
Indenter::Indenter()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
Indenter::~Indenter()
|
|
|
|
|
{}
|
|
|
|
|
|
2010-11-30 14:14:33 +01:00
|
|
|
bool Indenter::isElectricCharacter(const QChar &ch) const
|
2010-11-05 14:27:16 +01:00
|
|
|
{
|
2011-01-03 15:43:07 +01:00
|
|
|
if (ch == QLatin1Char('{')
|
|
|
|
|
|| ch == QLatin1Char('}')
|
2010-11-05 14:27:16 +01:00
|
|
|
|| ch == QLatin1Char(']')
|
|
|
|
|
|| ch == QLatin1Char(':'))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-30 14:14:33 +01:00
|
|
|
void Indenter::indentBlock(QTextDocument *doc,
|
|
|
|
|
const QTextBlock &block,
|
|
|
|
|
const QChar &typedChar,
|
2011-08-04 11:19:25 +02:00
|
|
|
const TextEditor::TabSettings &tabSettings)
|
2010-11-05 14:27:16 +01:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(doc)
|
|
|
|
|
|
2016-01-13 14:32:23 +01:00
|
|
|
const int depth = indentFor(block, tabSettings);
|
2011-10-24 12:29:54 +02:00
|
|
|
if (depth == -1)
|
|
|
|
|
return;
|
2011-01-03 15:32:28 +01:00
|
|
|
|
2016-01-13 14:32:23 +01:00
|
|
|
QmlJSTools::CreatorCodeFormatter codeFormatter(tabSettings);
|
|
|
|
|
codeFormatter.updateStateUntil(block);
|
|
|
|
|
|
2011-01-03 15:32:28 +01:00
|
|
|
if (isElectricCharacter(typedChar)) {
|
|
|
|
|
// only reindent the current line when typing electric characters if the
|
|
|
|
|
// indent is the same it would be if the line were empty
|
|
|
|
|
const int newlineIndent = codeFormatter.indentForNewLineAfter(block.previous());
|
2011-08-04 11:19:25 +02:00
|
|
|
if (tabSettings.indentationColumn(block.text()) != newlineIndent)
|
2011-01-03 15:32:28 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-04 11:19:25 +02:00
|
|
|
tabSettings.indentLine(block, depth);
|
2010-11-05 14:27:16 +01:00
|
|
|
}
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
void Indenter::invalidateCache(QTextDocument *doc)
|
|
|
|
|
{
|
2011-10-17 13:56:48 +02:00
|
|
|
QmlJSTools::CreatorCodeFormatter codeFormatter;
|
2011-08-16 10:45:23 +02:00
|
|
|
codeFormatter.invalidateCache(doc);
|
|
|
|
|
}
|
2016-01-13 14:32:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
int Indenter::indentFor(const QTextBlock &block,
|
|
|
|
|
const TextEditor::TabSettings &tabSettings)
|
|
|
|
|
{
|
|
|
|
|
QmlJSTools::CreatorCodeFormatter codeFormatter(tabSettings);
|
|
|
|
|
codeFormatter.updateStateUntil(block);
|
|
|
|
|
return codeFormatter.indentFor(block);
|
|
|
|
|
}
|