aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/qmljs/qmljscodeformatter.h
blob: abef85a782ddfc958971486614112c292e584165 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "qmljs_global.h"

#include "qmljsscanner.h"

#include <QStack>
#include <QList>
#include <QVector>
#include <QMetaObject>

QT_BEGIN_NAMESPACE
class QTextDocument;
class QTextBlock;
QT_END_NAMESPACE

namespace QmlJS {

class QMLJS_EXPORT CodeFormatter
{
    Q_GADGET
public:
    CodeFormatter();
    virtual ~CodeFormatter();

    // updates all states up until block if necessary
    // it is safe to call indentFor on block afterwards
    void updateStateUntil(const QTextBlock &block);

    // calculates the state change introduced by changing a single line
    void updateLineStateChange(const QTextBlock &block);

    int indentFor(const QTextBlock &block);
    int indentForNewLineAfter(const QTextBlock &block);

    void setTabSize(int tabSize);

    void invalidateCache(QTextDocument *document);

protected:
    virtual void onEnter(int newState, int *indentDepth, int *savedIndentDepth) const = 0;
    virtual void adjustIndent(const QList<Token> &tokens, int startLexerState, int *indentDepth) const = 0;

    struct State;
    class QMLJS_EXPORT BlockData
    {
    public:
        BlockData();

        QStack<State> m_beginState;
        QStack<State> m_endState;
        int m_indentDepth;
        int m_blockRevision;
    };

    virtual void saveBlockData(QTextBlock *block, const BlockData &data) const = 0;
    virtual bool loadBlockData(const QTextBlock &block, BlockData *data) const = 0;

    virtual void saveLexerState(QTextBlock *block, int state) const = 0;
    virtual int loadLexerState(const QTextBlock &block) const = 0;

public: // must be public to make Q_GADGET introspection work
    enum StateType {
        invalid = 0,

        topmost_intro, // The first line in a "topmost" definition.

        top_qml, // root state for qml
        top_js, // root for js
        objectdefinition_or_js, // file starts with identifier

        multiline_comment_start,
        multiline_comment_cont,

        import_start, // after 'import'
        import_maybe_dot_or_version_or_as, // after string or identifier
        import_dot, // after .
        import_maybe_as, // after version
        import_as,

        property_start, // after 'property'
        property_modifiers, // after 'default' or readonly
        required_property, // after required
        property_list_open, // after 'list' as a type
        property_name, // after the type
        property_maybe_initializer, // after the identifier
        component_start, // after component
        component_name, // after component Name

        enum_start, // after 'enum'

        signal_start, // after 'signal'
        signal_maybe_arglist, // after identifier
        signal_arglist_open, // after '('

        function_start, // after 'function'
        function_arglist_open, // after '(' starting function argument list
        function_arglist_closed, // after ')' in argument list, expecting '{'
        function_type_annotated_return, // after ':' expecting a type

        binding_or_objectdefinition, // after an identifier

        binding_assignment, // after : in a binding
        objectdefinition_open, // after {

        expression,
        expression_continuation, // at the end of the line, when the next line definitely is a continuation
        expression_maybe_continuation, // at the end of the line, when the next line may be an expression
        expression_or_objectdefinition, // after a binding starting with an identifier ("x: foo")
        expression_or_label, // when expecting a statement and getting an identifier

        paren_open, // opening ( in expression
        bracket_open, // opening [ in expression
        objectliteral_open, // opening { in expression

        objectliteral_assignment, // after : in object literal

        bracket_element_start, // after starting bracket_open or after ',' in bracket_open
        bracket_element_maybe_objectdefinition, // after an identifier in bracket_element_start

        ternary_op, // The ? : operator
        ternary_op_after_colon, // after the : in a ternary

        jsblock_open,

        empty_statement, // for a ';', will be popped directly
        breakcontinue_statement, // for continue/break, may be followed by identifier

        if_statement, // After 'if'
        maybe_else, // after the first substatement in an if
        else_clause, // The else line of an if-else construct.

        condition_open, // Start of a condition in 'if', 'while', entered after opening paren

        substatement, // The first line after a conditional or loop construct.
        substatement_open, // The brace that opens a substatement block.

        labelled_statement, // after a label

        return_statement, // After 'return'
        throw_statement, // After 'throw'

        statement_with_condition, // After the 'for', 'while', ... token
        statement_with_condition_paren_open, // While inside the (...)

        try_statement, // after 'try'
        catch_statement, // after 'catch', nested in try_statement
        finally_statement, // after 'finally', nested in try_statement
        maybe_catch_or_finally, // after ther closing '}' of try_statement and catch_statement, nested in try_statement

        do_statement, // after 'do'
        do_statement_while_paren_open, // after '(' in while clause

        switch_statement, // After 'switch' token
        case_start, // after a 'case' or 'default' token
        case_cont // after the colon in a case/default
    };
    Q_ENUM(StateType)

protected:
    // extends Token::Kind from qmljsscanner.h
    // the entries until EndOfExistingTokenKinds must match
    enum TokenKind {
        EndOfFile,
        Keyword,
        Identifier,
        String,
        Comment,
        Number,
        LeftParenthesis,
        RightParenthesis,
        LeftBrace,
        RightBrace,
        LeftBracket,
        RightBracket,
        Semicolon,
        Colon,
        Comma,
        Dot,
        Delimiter,
        RegExp,

        EndOfExistingTokenKinds,

        Break,
        Case,
        Catch,
        Continue,
        Debugger,
        Default,
        Delete,
        Do,
        Else,
        Enum,
        Finally,
        For,
        Function,
        If,
        In,
        Instanceof,
        New,
        Return,
        Switch,
        This,
        Throw,
        Try,
        Typeof,
        Var,
        Void,
        While,
        With,

        Import,
        Signal,
        On,
        As,
        List,
        Property,
        Required,
        Component,
        Readonly,

        Question,
        PlusPlus,
        MinusMinus
    };

    TokenKind extendedTokenKind(const QmlJS::Token &token) const;

    struct State {
        State()
            : savedIndentDepth(0)
            , type(0)
        {}

        State(quint8 ty, quint16 savedDepth)
            : savedIndentDepth(savedDepth)
            , type(ty)
        {}

        quint16 savedIndentDepth;
        quint8 type;

        bool operator==(const State &other) const {
            return type == other.type
                && savedIndentDepth == other.savedIndentDepth;
        }
    };

    State state(int belowTop = 0) const;
    const QVector<State> &newStatesThisLine() const;
    int tokenIndex() const;
    int tokenCount() const;
    const Token &currentToken() const;
    const Token &tokenAt(int idx) const;
    int column(int position) const;

    bool isBracelessState(int type) const;
    bool isExpressionEndState(int type) const;

    void dump() const;
    QString stateToString(int type) const;

private:
    void recalculateStateAfter(const QTextBlock &block);
    void saveCurrentState(const QTextBlock &block);
    void restoreCurrentState(const QTextBlock &block);

    QStringView currentTokenText() const;

    int tokenizeBlock(const QTextBlock &block);

    void turnInto(int newState);

    bool tryInsideExpression(bool alsoExpression = false);
    bool tryStatement();

    void enter(int newState);
    void leave(bool statementDone = false);
    void correctIndentation(const QTextBlock &block);

private:
    static QStack<State> initialState();

    QStack<State> m_beginState;
    QStack<State> m_currentState;
    QStack<State> m_newStates;

    QList<Token> m_tokens;
    QString m_currentLine;
    Token m_currentToken;
    int m_tokenIndex;

    // should store indent level and padding instead
    int m_indentDepth;

    int m_tabSize;
};

class QMLJS_EXPORT QtStyleCodeFormatter : public CodeFormatter
{
public:
    QtStyleCodeFormatter();

    void setIndentSize(int size);

protected:
    void onEnter(int newState, int *indentDepth, int *savedIndentDepth) const override;
    void adjustIndent(const QList<QmlJS::Token> &tokens, int lexerState, int *indentDepth) const override;

private:
    int m_indentSize;
};

} // namespace QmlJS