qmljseditor.h 4.83 KB
Newer Older
1
2
3
4
5
6
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** 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.
27
28
29
**
**************************************************************************/

30
31
#ifndef QMLJSEDITOR_H
#define QMLJSEDITOR_H
32

33
34
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljsscanner.h>
35
#include <texteditor/basetexteditor.h>
36

37
38
39
40
41
42
QT_BEGIN_NAMESPACE
class QComboBox;
class QTimer;
QT_END_NAMESPACE

namespace Core {
43
class ICore;
44
45
}

46
namespace QmlJSEditor {
47

48
class QmlModelManagerInterface;
49

50
51
namespace Internal {

52
class QmlHighlighter;
53
class QmlJSTextEditor;
54

55
class QmlJSEditorEditable : public TextEditor::BaseTextEditorEditable
56
{
57
    Q_OBJECT
58

59
public:
60
    QmlJSEditorEditable(QmlJSTextEditor *);
61
    QList<int> context() const;
62

63
64
    bool duplicateSupported() const { return true; }
    Core::IEditor *duplicate(QWidget *parent);
65
    QString id() const;
66
    bool isTemporary() const { return false; }
67
    virtual bool open(const QString & fileName);
68
69

private:
70
    QList<int> m_context;
71
72
73
74
75
};


struct Declaration
{
76
77
78
79
80
81
82
83
84
85
86
87
    QString text;
    int startLine;
    int startColumn;
    int endLine;
    int endColumn;

    Declaration()
        : startLine(0),
        startColumn(0),
        endLine(0),
        endColumn(0)
    { }
88
89
};

90
91
92
93
94
95
class Range
{
public:
    Range(): ast(0) {}

public: // attributes
96
    QmlJS::AST::Node *ast;
97
98
99
100
101
102
103
104
105
    QTextCursor begin;
    QTextCursor end;
};

class SemanticInfo
{
public:
    SemanticInfo() {}

106
107
    int revision() const;

108
    // Returns the declaring member
109
    QmlJS::AST::Node *declaringMember(int cursorPosition) const;
110

111
112
113
    // Returns the AST node under cursor
    QmlJS::AST::Node *nodeUnderCursor(int cursorPosition) const;

114
115
public: // attributes
    QmlJS::Document::Ptr document;
116
    QmlJS::Snapshot snapshot;
117
    QList<Range> ranges;
118
    QHash<QString, QList<QmlJS::AST::SourceLocation> > idLocations;
119
    QList<Declaration> declarations;
120
121
};

122
class QmlJSTextEditor : public TextEditor::BaseTextEditor
123
{
124
    Q_OBJECT
125
126

public:
127
    typedef QList<int> Context;
128

129
130
    QmlJSTextEditor(QWidget *parent = 0);
    ~QmlJSTextEditor();
131

132
    virtual void unCommentSelection();
133

134
135
    SemanticInfo semanticInfo() const;
    int documentRevision() const;
136

137
public slots:
138
    virtual void setFontSettings(const TextEditor::FontSettings &);
139
140

private slots:
141
    void onDocumentUpdated(QmlJS::Document::Ptr doc);
142

143
144
145
146
147
148
    void updateDocument();
    void updateDocumentNow();
    void jumpToMethod(int index);
    void updateMethodBoxIndex();
    void updateMethodBoxToolTip();
    void updateFileName();
149

150
151
152
    void updateUses();
    void updateUsesNow();

153
154
    // refactoring ops
    void renameIdUnderCursor();
Roberto Raggi's avatar
Roberto Raggi committed
155

156
protected:
157
158
    void contextMenuEvent(QContextMenuEvent *e);
    TextEditor::BaseTextEditorEditable *createEditableInterface();
159
    void createToolBar(QmlJSEditorEditable *editable);
160
    TextEditor::BaseTextEditor::Link findLinkAt(const QTextCursor &cursor, bool resolveTarget = true);
161

162
163
164
165
166
167
    //// brace matching
    virtual bool contextAllowsAutoParentheses(const QTextCursor &cursor, const QString &textToInsert = QString()) const;
    virtual bool isInComment(const QTextCursor &cursor) const;
    virtual QString insertMatchingBrace(const QTextCursor &tc, const QString &text, const QChar &la, int *skippedChars) const;
    virtual QString insertParagraphSeparator(const QTextCursor &tc) const;

168
private:
169
170
    virtual bool isElectricCharacter(const QChar &ch) const;
    virtual void indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar);
171
    bool isClosingBrace(const QList<QmlJS::Token> &tokens) const;
172

173
    QString wordUnderCursor() const;
Roberto Raggi's avatar
Roberto Raggi committed
174

175
    const Context m_context;
176

177
    QTimer *m_updateDocumentTimer;
178
    QTimer *m_updateUsesTimer;
179
    QComboBox *m_methodCombo;
180
    QmlModelManagerInterface *m_modelManager;
181
    QTextCharFormat m_occurrencesFormat;
182
183

    SemanticInfo m_semanticInfo;
184
185
186
};

} // namespace Internal
187
} // namespace QmlJSEditor
188

189
#endif // QMLJSEDITOR_H