profilehoverhandler.cpp 6.28 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
** 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.
23
**
hjk's avatar
hjk committed
24
25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29
30
31

#include "profilehoverhandler.h"
#include "profileeditor.h"
32
#include "profilecompletionassist.h"
33
34
35
36
37

#include <coreplugin/helpmanager.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/htmldocextractor.h>

38
39
#include <QTextBlock>
#include <QUrl>
40
41
42
43
44
45
46
47
48

using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;
using namespace Core;

ProFileHoverHandler::ProFileHoverHandler(QObject *parent)
  : BaseHoverHandler(parent),
    m_manualKind(UnknownManual)
{
49
50
51
    ProFileCompletionAssistProvider *pcap
            = ExtensionSystem::PluginManager::instance()->getObject<ProFileCompletionAssistProvider>();
    m_keywords = TextEditor::Keywords(pcap->variables(), pcap->functions(), QMap<QString, QStringList>());
52
53
54
55
56
57
58
59
60
61
62
63
64
65
}

ProFileHoverHandler::~ProFileHoverHandler()
{}

bool ProFileHoverHandler::acceptEditor(IEditor *editor)
{
    if (qobject_cast<ProFileEditor *>(editor) != 0)
        return true;
    return false;
}

void ProFileHoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
{
66
    m_docFragment.clear();
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
    m_manualKind = UnknownManual;
    if (ProFileEditorWidget *proFileEditor = qobject_cast<ProFileEditorWidget *>(editor->widget())) {
        if (!proFileEditor->extraSelectionTooltip(pos).isEmpty()) {
            setToolTip(proFileEditor->extraSelectionTooltip(pos));
        } else {
            QTextDocument *document = proFileEditor->document();
            QTextBlock block = document->findBlock(pos);
            identifyQMakeKeyword(block.text(), pos - block.position());

            if (m_manualKind != UnknownManual) {
                QUrl url(QString::fromLatin1("qthelp://com.trolltech.qmake/qdoc/qmake-%1-reference.html#%2")
                        .arg(manualName()).arg(m_docFragment));
                setLastHelpItemIdentified(TextEditor::HelpItem(url.toString(),
                                          m_docFragment, TextEditor::HelpItem::QMakeVariableOfFunction));
            } else {
                // General qmake manual will be shown outside any function or variable
                setLastHelpItemIdentified(TextEditor::HelpItem(QLatin1String("qmake"),
                                          TextEditor::HelpItem::Unknown));
            }
        }
    }
}

void ProFileHoverHandler::identifyQMakeKeyword(const QString &text, int pos)
{
    if (text.isEmpty())
        return;

    QString buf;

    for (int i = 0; i < text.length(); ++i) {
        const QChar c = text.at(i);
        bool checkBuffer = false;
        if (c.isLetter() || c == QLatin1Char('_') || c == QLatin1Char('.') || c.isDigit()) {
            buf += c;
            if (i == text.length() - 1)
                checkBuffer = true;
        } else {
            checkBuffer = true;
        }
        if (checkBuffer) {
            if (!buf.isEmpty()) {
                if ((i >= pos) && (i - buf.size() <= pos)) {
110
                    if (m_keywords.isFunction(buf))
111
                        identifyDocFragment(FunctionManual, buf);
112
                    else if (m_keywords.isVariable(buf))
113
114
115
116
117
118
119
120
                        identifyDocFragment(VariableManual, buf);
                    break;
                }
                buf.clear();
            } else {
                if (i >= pos)
                    break; // we are after the tooltip pos
            }
121
            if (c == QLatin1Char('#'))
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
                break; // comment start
        }
    }
}

QString ProFileHoverHandler::manualName() const
{
    if (m_manualKind == FunctionManual)
        return QLatin1String("function");
    else if (m_manualKind == VariableManual)
        return QLatin1String("variable");
    return QString();
}

void ProFileHoverHandler::identifyDocFragment(ProFileHoverHandler::ManualKind manualKind,
                                        const QString &keyword)
{
    m_manualKind = manualKind;
    m_docFragment = keyword.toLower();
    // Special case: _PRO_FILE_ and _PRO_FILE_PWD_ ids
    // don't have starting and ending '_'.
    if (m_docFragment.startsWith(QLatin1Char('_')))
        m_docFragment = m_docFragment.mid(1);
    if (m_docFragment.endsWith(QLatin1Char('_')))
        m_docFragment = m_docFragment.left(m_docFragment.size() - 1);
    m_docFragment.replace(QLatin1Char('.'), QLatin1Char('-'));
    m_docFragment.replace(QLatin1Char('_'), QLatin1Char('-'));

    if (m_manualKind == FunctionManual) {
        QUrl url(QString::fromLatin1("qthelp://com.trolltech.qmake/qdoc/qmake-%1-reference.html").arg(manualName()));
        const QByteArray &html = Core::HelpManager::instance()->fileData(url);

        Utils::HtmlDocExtractor htmlExtractor;
        htmlExtractor.setMode(Utils::HtmlDocExtractor::FirstParagraph);

        // Document fragment of qmake function is retrieved from docs.
        // E.g. in case of the keyword "find" the document fragment
        // parsed from docs is "find-variablename-substr".
160
        m_docFragment = htmlExtractor.getQMakeFunctionId(QString::fromUtf8(html), m_docFragment);
161
162
163
    }
}