2010-07-07 11:45:18 +02: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).
|
2010-07-07 11:45:18 +02:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Contact: Nokia Corporation (info@qt.nokia.com)
|
2010-07-07 11:45:18 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
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.
|
2010-07-07 11:45:18 +02: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-05-06 15:05:37 +02:00
|
|
|
** Nokia at info@qt.nokia.com.
|
2010-07-07 11:45:18 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2010-11-11 10:05:05 +01:00
|
|
|
#include "qmljsqtstylecodeformatter.h"
|
2010-07-07 11:45:18 +02:00
|
|
|
|
2010-08-12 14:00:15 +02:00
|
|
|
#include <texteditor/tabsettings.h>
|
|
|
|
|
|
2010-07-07 11:45:18 +02:00
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
2010-11-11 10:05:05 +01:00
|
|
|
using namespace QmlJSTools;
|
2010-07-07 11:45:18 +02:00
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
QtStyleCodeFormatter::QtStyleCodeFormatter()
|
|
|
|
|
: m_indentSize(4)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-12 14:00:15 +02:00
|
|
|
QtStyleCodeFormatter::QtStyleCodeFormatter(const TextEditor::TabSettings &tabSettings)
|
|
|
|
|
: m_indentSize(tabSettings.m_indentSize)
|
|
|
|
|
{
|
|
|
|
|
setTabSize(tabSettings.m_tabSize);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-07 11:45:18 +02:00
|
|
|
void QtStyleCodeFormatter::setIndentSize(int size)
|
|
|
|
|
{
|
|
|
|
|
m_indentSize = size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtStyleCodeFormatter::saveBlockData(QTextBlock *block, const BlockData &data) const
|
|
|
|
|
{
|
|
|
|
|
TextBlockUserData *userData = BaseTextDocumentLayout::userData(*block);
|
|
|
|
|
QmlJSCodeFormatterData *cppData = static_cast<QmlJSCodeFormatterData *>(userData->codeFormatterData());
|
|
|
|
|
if (!cppData) {
|
|
|
|
|
cppData = new QmlJSCodeFormatterData;
|
|
|
|
|
userData->setCodeFormatterData(cppData);
|
|
|
|
|
}
|
|
|
|
|
cppData->m_data = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QtStyleCodeFormatter::loadBlockData(const QTextBlock &block, BlockData *data) const
|
|
|
|
|
{
|
|
|
|
|
TextBlockUserData *userData = BaseTextDocumentLayout::testUserData(block);
|
|
|
|
|
if (!userData)
|
|
|
|
|
return false;
|
|
|
|
|
QmlJSCodeFormatterData *cppData = static_cast<QmlJSCodeFormatterData *>(userData->codeFormatterData());
|
|
|
|
|
if (!cppData)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
*data = cppData->m_data;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtStyleCodeFormatter::saveLexerState(QTextBlock *block, int state) const
|
|
|
|
|
{
|
|
|
|
|
BaseTextDocumentLayout::setLexerState(*block, state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int QtStyleCodeFormatter::loadLexerState(const QTextBlock &block) const
|
|
|
|
|
{
|
|
|
|
|
return BaseTextDocumentLayout::lexerState(block);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedIndentDepth) const
|
|
|
|
|
{
|
|
|
|
|
const State &parentState = state();
|
|
|
|
|
const Token &tk = currentToken();
|
|
|
|
|
const int tokenPosition = column(tk.begin());
|
|
|
|
|
const bool firstToken = (tokenIndex() == 0);
|
|
|
|
|
const bool lastToken = (tokenIndex() == tokenCount() - 1);
|
|
|
|
|
|
|
|
|
|
switch (newState) {
|
|
|
|
|
case objectdefinition_open: {
|
|
|
|
|
// special case for things like "gradient: Gradient {"
|
2011-09-07 12:06:46 +02:00
|
|
|
if (parentState.type == binding_assignment)
|
2010-07-07 11:45:18 +02:00
|
|
|
*savedIndentDepth = state(1).savedIndentDepth;
|
|
|
|
|
|
2010-09-08 15:49:09 +02:00
|
|
|
if (firstToken)
|
2010-07-07 11:45:18 +02:00
|
|
|
*savedIndentDepth = tokenPosition;
|
|
|
|
|
|
2010-09-08 15:49:09 +02:00
|
|
|
*indentDepth = *savedIndentDepth + m_indentSize;
|
2010-07-07 11:45:18 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case binding_or_objectdefinition:
|
|
|
|
|
if (firstToken)
|
|
|
|
|
*indentDepth = *savedIndentDepth = tokenPosition;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case binding_assignment:
|
2011-04-21 12:21:23 +02:00
|
|
|
case objectliteral_assignment:
|
2010-07-07 11:45:18 +02:00
|
|
|
if (lastToken)
|
|
|
|
|
*indentDepth = *savedIndentDepth + 4;
|
|
|
|
|
else
|
|
|
|
|
*indentDepth = column(tokenAt(tokenIndex() + 1).begin());
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case expression_or_objectdefinition:
|
|
|
|
|
*indentDepth = tokenPosition;
|
|
|
|
|
break;
|
|
|
|
|
|
2011-09-07 13:30:48 +02:00
|
|
|
case expression_or_label:
|
|
|
|
|
if (*indentDepth == tokenPosition)
|
|
|
|
|
*indentDepth += 2*m_indentSize;
|
|
|
|
|
else
|
|
|
|
|
*indentDepth = tokenPosition;
|
|
|
|
|
break;
|
|
|
|
|
|
2010-07-07 11:45:18 +02:00
|
|
|
case expression:
|
2011-09-07 13:30:48 +02:00
|
|
|
if (*indentDepth == tokenPosition) {
|
|
|
|
|
// expression_or_objectdefinition doesn't want the indent
|
|
|
|
|
// expression_or_label already has it
|
|
|
|
|
if (parentState.type != expression_or_objectdefinition
|
|
|
|
|
&& parentState.type != expression_or_label
|
2011-10-14 12:52:42 +02:00
|
|
|
&& parentState.type != binding_assignment) {
|
2011-09-07 13:30:48 +02:00
|
|
|
*indentDepth += 2*m_indentSize;
|
|
|
|
|
}
|
2010-07-07 11:45:18 +02:00
|
|
|
}
|
2011-09-07 13:30:48 +02:00
|
|
|
// expression_or_objectdefinition and expression_or_label have already consumed the first token
|
|
|
|
|
else if (parentState.type != expression_or_objectdefinition
|
2011-10-14 12:52:42 +02:00
|
|
|
&& parentState.type != expression_or_label) {
|
2010-07-07 11:45:18 +02:00
|
|
|
*indentDepth = tokenPosition;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case expression_maybe_continuation:
|
|
|
|
|
// set indent depth to indent we'd get if the expression ended here
|
|
|
|
|
for (int i = 1; state(i).type != topmost_intro; ++i) {
|
|
|
|
|
const int type = state(i).type;
|
|
|
|
|
if (isExpressionEndState(type) && !isBracelessState(type)) {
|
|
|
|
|
*indentDepth = state(i - 1).savedIndentDepth;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case bracket_open:
|
2011-09-07 12:06:46 +02:00
|
|
|
if (parentState.type == expression && state(1).type == binding_assignment) {
|
2010-07-07 11:45:18 +02:00
|
|
|
*savedIndentDepth = state(2).savedIndentDepth;
|
|
|
|
|
*indentDepth = *savedIndentDepth + m_indentSize;
|
2011-10-05 12:40:27 +02:00
|
|
|
} else if (parentState.type == objectliteral_assignment) {
|
|
|
|
|
*savedIndentDepth = parentState.savedIndentDepth;
|
|
|
|
|
*indentDepth = *savedIndentDepth + m_indentSize;
|
2010-07-07 11:45:18 +02:00
|
|
|
} else if (!lastToken) {
|
|
|
|
|
*indentDepth = tokenPosition + 1;
|
|
|
|
|
} else {
|
|
|
|
|
*indentDepth = *savedIndentDepth + m_indentSize;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case function_start:
|
|
|
|
|
if (parentState.type == expression) {
|
|
|
|
|
// undo the continuation indent of the expression
|
|
|
|
|
*indentDepth = parentState.savedIndentDepth;
|
|
|
|
|
*savedIndentDepth = *indentDepth;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case do_statement_while_paren_open:
|
|
|
|
|
case statement_with_condition_paren_open:
|
|
|
|
|
case signal_arglist_open:
|
|
|
|
|
case function_arglist_open:
|
|
|
|
|
case paren_open:
|
|
|
|
|
case condition_paren_open:
|
|
|
|
|
if (!lastToken)
|
|
|
|
|
*indentDepth = tokenPosition + 1;
|
|
|
|
|
else
|
|
|
|
|
*indentDepth += m_indentSize;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ternary_op:
|
|
|
|
|
if (!lastToken)
|
|
|
|
|
*indentDepth = tokenPosition + tk.length + 1;
|
|
|
|
|
else
|
|
|
|
|
*indentDepth += m_indentSize;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case jsblock_open:
|
|
|
|
|
// closing brace should be aligned to case
|
|
|
|
|
if (parentState.type == case_cont) {
|
|
|
|
|
*savedIndentDepth = parentState.savedIndentDepth;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// fallthrough
|
|
|
|
|
case substatement_open:
|
2011-09-07 12:06:46 +02:00
|
|
|
// special case for "foo: {" and "property int foo: {"
|
|
|
|
|
if (parentState.type == binding_assignment)
|
2010-07-07 11:45:18 +02:00
|
|
|
*savedIndentDepth = state(1).savedIndentDepth;
|
|
|
|
|
*indentDepth = *savedIndentDepth + m_indentSize;
|
|
|
|
|
break;
|
|
|
|
|
|
2011-09-07 13:30:48 +02:00
|
|
|
case substatement:
|
|
|
|
|
*indentDepth += m_indentSize;
|
|
|
|
|
break;
|
|
|
|
|
|
2011-04-21 12:21:23 +02:00
|
|
|
case objectliteral_open:
|
2011-04-21 12:56:37 +02:00
|
|
|
if (parentState.type == expression
|
2011-09-07 12:06:46 +02:00
|
|
|
|| parentState.type == objectliteral_assignment) {
|
2011-04-21 12:21:23 +02:00
|
|
|
// undo the continuation indent of the expression
|
2011-09-07 13:30:48 +02:00
|
|
|
if (state(1).type == expression_or_label)
|
|
|
|
|
*indentDepth = state(1).savedIndentDepth;
|
|
|
|
|
else
|
|
|
|
|
*indentDepth = parentState.savedIndentDepth;
|
2011-04-21 12:21:23 +02:00
|
|
|
*savedIndentDepth = *indentDepth;
|
|
|
|
|
}
|
|
|
|
|
*indentDepth += m_indentSize;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
2010-07-07 11:45:18 +02:00
|
|
|
case statement_with_condition:
|
|
|
|
|
case statement_with_block:
|
|
|
|
|
case if_statement:
|
|
|
|
|
case do_statement:
|
|
|
|
|
case switch_statement:
|
|
|
|
|
if (firstToken || parentState.type == binding_assignment)
|
|
|
|
|
*savedIndentDepth = tokenPosition;
|
|
|
|
|
// ### continuation
|
|
|
|
|
*indentDepth = *savedIndentDepth; // + 2*m_indentSize;
|
2011-09-07 13:30:48 +02:00
|
|
|
// special case for 'else if'
|
|
|
|
|
if (!firstToken
|
|
|
|
|
&& newState == if_statement
|
|
|
|
|
&& parentState.type == substatement
|
|
|
|
|
&& state(1).type == else_clause) {
|
|
|
|
|
*indentDepth = state(1).savedIndentDepth;
|
|
|
|
|
*savedIndentDepth = *indentDepth;
|
|
|
|
|
}
|
2010-07-07 11:45:18 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case maybe_else: {
|
|
|
|
|
// set indent to outermost braceless savedIndent
|
|
|
|
|
int outermostBraceless = 0;
|
|
|
|
|
while (isBracelessState(state(outermostBraceless + 1).type))
|
|
|
|
|
++outermostBraceless;
|
|
|
|
|
*indentDepth = state(outermostBraceless).savedIndentDepth;
|
|
|
|
|
// this is where the else should go, if one appears - aligned to if_statement
|
|
|
|
|
*savedIndentDepth = state().savedIndentDepth;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case condition_open:
|
|
|
|
|
// fixed extra indent when continuing 'if (', but not for 'else if ('
|
|
|
|
|
if (tokenPosition <= *indentDepth + m_indentSize)
|
|
|
|
|
*indentDepth += 2*m_indentSize;
|
|
|
|
|
else
|
|
|
|
|
*indentDepth = tokenPosition + 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case case_start:
|
|
|
|
|
*savedIndentDepth = tokenPosition;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case case_cont:
|
|
|
|
|
*indentDepth += m_indentSize;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case multiline_comment_start:
|
|
|
|
|
*indentDepth = tokenPosition + 2;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case multiline_comment_cont:
|
|
|
|
|
*indentDepth = tokenPosition;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QtStyleCodeFormatter::adjustIndent(const QList<Token> &tokens, int lexerState, int *indentDepth) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(lexerState)
|
|
|
|
|
|
|
|
|
|
State topState = state();
|
|
|
|
|
State previousState = state(1);
|
|
|
|
|
|
|
|
|
|
// keep user-adjusted indent in multiline comments
|
|
|
|
|
if (topState.type == multiline_comment_start
|
|
|
|
|
|| topState.type == multiline_comment_cont) {
|
|
|
|
|
if (!tokens.isEmpty()) {
|
|
|
|
|
*indentDepth = column(tokens.at(0).begin());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int kind = extendedTokenKind(tokenAt(0));
|
|
|
|
|
switch (kind) {
|
|
|
|
|
case LeftBrace:
|
|
|
|
|
if (topState.type == substatement
|
|
|
|
|
|| topState.type == binding_assignment
|
|
|
|
|
|| topState.type == case_cont) {
|
|
|
|
|
*indentDepth = topState.savedIndentDepth;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case RightBrace: {
|
|
|
|
|
if (topState.type == jsblock_open && previousState.type == case_cont) {
|
|
|
|
|
*indentDepth = previousState.savedIndentDepth;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; state(i).type != topmost_intro; ++i) {
|
|
|
|
|
const int type = state(i).type;
|
|
|
|
|
if (type == objectdefinition_open
|
|
|
|
|
|| type == jsblock_open
|
2011-04-21 12:21:23 +02:00
|
|
|
|| type == substatement_open
|
|
|
|
|
|| type == objectliteral_open) {
|
2010-07-07 11:45:18 +02:00
|
|
|
*indentDepth = state(i).savedIndentDepth;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case RightBracket:
|
|
|
|
|
for (int i = 0; state(i).type != topmost_intro; ++i) {
|
|
|
|
|
const int type = state(i).type;
|
|
|
|
|
if (type == bracket_open) {
|
|
|
|
|
*indentDepth = state(i).savedIndentDepth;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case LeftBracket:
|
|
|
|
|
case LeftParenthesis:
|
|
|
|
|
case Delimiter:
|
|
|
|
|
if (topState.type == expression_maybe_continuation)
|
|
|
|
|
*indentDepth = topState.savedIndentDepth;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Else:
|
|
|
|
|
if (topState.type == maybe_else) {
|
|
|
|
|
*indentDepth = topState.savedIndentDepth;
|
|
|
|
|
} else if (topState.type == expression_maybe_continuation) {
|
|
|
|
|
bool hasElse = false;
|
|
|
|
|
for (int i = 1; state(i).type != topmost_intro; ++i) {
|
|
|
|
|
const int type = state(i).type;
|
|
|
|
|
if (type == else_clause)
|
|
|
|
|
hasElse = true;
|
|
|
|
|
if (type == if_statement) {
|
|
|
|
|
if (hasElse) {
|
|
|
|
|
hasElse = false;
|
|
|
|
|
} else {
|
|
|
|
|
*indentDepth = state(i).savedIndentDepth;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Colon:
|
|
|
|
|
if (topState.type == ternary_op) {
|
|
|
|
|
*indentDepth -= 2;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Question:
|
|
|
|
|
if (topState.type == expression_maybe_continuation)
|
|
|
|
|
*indentDepth = topState.savedIndentDepth;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Default:
|
|
|
|
|
case Case:
|
|
|
|
|
for (int i = 0; state(i).type != topmost_intro; ++i) {
|
|
|
|
|
const int type = state(i).type;
|
|
|
|
|
if (type == switch_statement || type == case_cont) {
|
|
|
|
|
*indentDepth = state(i).savedIndentDepth;
|
|
|
|
|
break;
|
|
|
|
|
} else if (type == topmost_intro) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|