File: contextbrowser.h

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (291 lines) | stat: -rw-r--r-- 10,029 bytes parent folder | download
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

/*
 * This file is part of KDevelop
 *
 * Copyright 2007 David Nolden <david.nolden.kdevelop@art-master.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef KDEVPLATFORM_PLUGIN_CONTEXTBROWSERPLUGIN_H
#define KDEVPLATFORM_PLUGIN_CONTEXTBROWSERPLUGIN_H

#include <QVariant>
#include <QSet>
#include <QMap>
#include <QList>
#include <QUrl>
#include <QPointer>

#include <KTextEditor/TextHintInterface>
#include <interfaces/iplugin.h>
#include <language/duchain/duchainpointer.h>
#include <language/duchain/declaration.h>
#include <language/duchain/indexedducontext.h>
#include <language/duchain/problem.h>
#include <language/editor/persistentmovingrange.h>
#include <language/interfaces/iquickopen.h>
#include <language/editor/documentcursor.h>

#include <language/interfaces/icontextbrowser.h>

class QHBoxLayout;
class QMenu;
class QToolButton;

namespace Sublime {
class MainWindow;
}

namespace KDevelop {
class IDocument;
class DUContext;
class TopDUContext;
class ReferencedTopDUContext;
class DUChainBase;
class AbstractNavigationWidget;
}

namespace KTextEditor {
class Document;
class View;
}

class ContextBrowserViewFactory;
class ContextBrowserView;
class ContextBrowserPlugin;
class BrowseManager;

class ContextBrowserHintProvider
    : public KTextEditor::TextHintProvider
{
public:
    explicit ContextBrowserHintProvider(ContextBrowserPlugin* plugin);
    QString textHint(KTextEditor::View* view, const KTextEditor::Cursor& position) override;

private:
    ContextBrowserPlugin* m_plugin;
};

QWidget* masterWidget(QWidget* w);

struct ViewHighlights
{
    ViewHighlights() : keep(false)
    {
    }
    // Whether the same highlighting should be kept highlighted (usually during typing)
    bool keep;
    // The declaration that is highlighted for this view
    KDevelop::IndexedDeclaration declaration;
    // Highlighted ranges. Those may also be contained by different views.
    QList<KDevelop::PersistentMovingRange::Ptr> highlights;
};

class ContextBrowserPlugin
    : public KDevelop::IPlugin
    , public KDevelop::IContextBrowser
{
    Q_OBJECT
    Q_INTERFACES(KDevelop::IContextBrowser)

public:
    explicit ContextBrowserPlugin(QObject* parent, const QVariantList& = QVariantList());
    ~ContextBrowserPlugin() override;

    void unload() override;

    void registerToolView(ContextBrowserView* view);
    void unRegisterToolView(ContextBrowserView* view);

    KDevelop::ContextMenuExtension contextMenuExtension(KDevelop::Context* context, QWidget* parent) override;

    KXMLGUIClient* createGUIForMainWindow(Sublime::MainWindow* window) override;

    ///duchain must be locked
    ///@param force When this is true, the history-entry is added, no matter whether the context is "interesting" or not
    void updateHistory(KDevelop::DUContext* context, const KTextEditor::Cursor& cursorPosition,
                       bool force = false);

    void updateDeclarationListBox(KDevelop::DUContext* context);

    void showUses(const KDevelop::DeclarationPointer& declaration) override;

    KTextEditor::Attribute::Ptr highlightedUseAttribute(KTextEditor::View* view) const;
    KTextEditor::Attribute::Ptr highlightedSpecialObjectAttribute(KTextEditor::View* view) const;

public Q_SLOTS:
    void showUsesDelayed(const KDevelop::DeclarationPointer& declaration);
    void previousContextShortcut();
    void nextContextShortcut();

    void startDelayedBrowsing(KTextEditor::View* view);
    void stopDelayedBrowsing();
    void invokeAction(int index);

    void previousUseShortcut();
    void nextUseShortcut();

    void declarationSelectedInUI(const KDevelop::DeclarationPointer& decl);

    void updateReady(const KDevelop::IndexedString& url, const KDevelop::ReferencedTopDUContext& topContext);
    void textDocumentCreated(KDevelop::IDocument* document);
    void documentActivated(KDevelop::IDocument*);
    void viewDestroyed(QObject* obj);
    void cursorPositionChanged(KTextEditor::View* view, const KTextEditor::Cursor& newPosition);
    void viewCreated(KTextEditor::Document*, KTextEditor::View*);
    void updateViews();

    void hideToolTip();
    void findUses();

    void textInserted(KTextEditor::Document* doc, const KTextEditor::Cursor& cursor, const QString& text);
    void selectionChanged(KTextEditor::View*);

    void historyNext();
    void historyPrevious();

    void colorSetupChanged();

private Q_SLOTS:
    // history browsing
    void documentJumpPerformed(KDevelop::IDocument* newDocument,
                               const KTextEditor::Cursor& newCursor,
                               KDevelop::IDocument* previousDocument,
                               const KTextEditor::Cursor& previousCursor);

    void nextMenuAboutToShow();
    void previousMenuAboutToShow();
    void actionTriggered();

    void navigateLeft();
    void navigateRight();
    void navigateUp();
    void navigateDown();
    void navigateAccept();
    void navigateBack();

private:
    QWidget* toolbarWidgetForMainWindow(Sublime::MainWindow* window);
    void createActionsForMainWindow(Sublime::MainWindow* window, QString& xmlFile,
                                    KActionCollection& actions) override;
    QWidget* navigationWidgetForPosition(KTextEditor::View* view, KTextEditor::Cursor position,
                                         KTextEditor::Range& itemRange);
    void switchUse(bool forward);
    void clearMouseHover();

    void addHighlight(KTextEditor::View* view, KDevelop::Declaration* decl);

    /** helper for updateBrowserView().
     *  Tries to find a 'specialLanguageObject' (eg macro) in @p view under cursor @c.
     *  If found returns true and sets @p pickedLanguage to the language this object belongs to */
    KDevelop::Declaration* findDeclaration(KTextEditor::View* view, const KTextEditor::Cursor&, bool mouseHighlight);
    void updateForView(KTextEditor::View* view);

    // history browsing
    bool isPreviousEntry(KDevelop::DUContext*, const KTextEditor::Cursor& cursor) const;
    QString actionTextFor(int historyIndex) const;
    void updateButtonState();
    void openDocument(int historyIndex);
    void fillHistoryPopup(QMenu* menu, const QList<int>& historyIndices);

    enum NavigationActionType {
        Accept,
        Back,
        Down,
        Up,
        Left,
        Right
    };
    void doNavigate(NavigationActionType action);

private:

    // Returns the currently active and visible context browser view that belongs
    // to the same context (mainwindow and area) as the given widget
    ContextBrowserView* browserViewForWidget(QWidget* widget) const;

    void showToolTip(KTextEditor::View* view, KTextEditor::Cursor position);
    QTimer* m_updateTimer;

    //Contains the range, the old attribute, and the attribute it was replaced with
    QSet<KTextEditor::View*> m_updateViews;
    QMap<KTextEditor::View*, ViewHighlights> m_highlightedRanges;

    //Holds a list of all active context browser tool views
    QList<ContextBrowserView*> m_views;

    QVector<KTextEditor::View*> m_textHintProvidedViews;

    //Used to override the next declaration that will be highlighted
    KDevelop::IndexedDeclaration m_useDeclaration;
    KDevelop::IndexedDeclaration m_lastHighlightedDeclaration;

    QUrl m_mouseHoverDocument;
    KTextEditor::Cursor m_mouseHoverCursor;
    ContextBrowserViewFactory* m_viewFactory;
    QPointer<QWidget> m_currentToolTip;
    QPointer<QWidget> m_currentNavigationWidget;
    KDevelop::IndexedDeclaration m_currentToolTipDeclaration;
    QVector<KDevelop::IProblem::Ptr> m_currentToolTipProblems;
    QAction* m_findUses;

    QPointer<KTextEditor::Document> m_lastInsertionDocument;
    KTextEditor::Cursor m_lastInsertionPos;

    // outline toolbar
    QPointer<QLineEdit> m_outlineLine;
    QPointer<QHBoxLayout> m_toolbarWidgetLayout;
    QPointer<QWidget> m_toolbarWidget;

    // history browsing
    struct HistoryEntry
    {
        //Duchain must be locked
        explicit HistoryEntry(
            KDevelop::IndexedDUContext ctx = KDevelop::IndexedDUContext(),
            const KTextEditor::Cursor& cursorPosition = KTextEditor::Cursor());
        explicit HistoryEntry(const KDevelop::DocumentCursor& pos);
        //Duchain must be locked
        void setCursorPosition(const KTextEditor::Cursor& cursorPosition);

        //Duchain does not need to be locked
        KDevelop::DocumentCursor computePosition() const;

        KDevelop::IndexedDUContext context;
        KDevelop::DocumentCursor absoluteCursorPosition;
        KTextEditor::Cursor relativeCursorPosition; //Cursor position relative to the start line of the context
        QString alternativeString;
    };

    QVector<HistoryEntry> m_history;
    QPointer<QToolButton> m_previousButton;
    QPointer<QToolButton> m_nextButton;
    QPointer<QMenu> m_previousMenu, m_nextMenu;
    QList<KDevelop::IndexedDeclaration> m_listDeclarations;
    KDevelop::IndexedString m_listUrl;
    BrowseManager* m_browseManager;
    //Used to not record jumps triggered by the context-browser as history entries
    QPointer<QWidget> m_focusBackWidget;
    int m_nextHistoryIndex;

    mutable KTextEditor::Attribute::Ptr m_highlightAttribute;

    friend class ContextBrowserHintProvider;
    ContextBrowserHintProvider m_textHintProvider;
};

#endif // KDEVPLATFORM_PLUGIN_CONTEXTBROWSERPLUGIN_H