aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmakeprojectmanager/profilehoverhandler.cpp
blob: 3080888780345a308c0e141449975459038cc3ec (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "profilehoverhandler.h"
#include "profilecompletionassist.h"
#include "qmakeprojectmanagerconstants.h"

#include <coreplugin/helpmanager.h>
#include <texteditor/texteditor.h>
#include <utils/htmldocextractor.h>

#include <QScopeGuard>
#include <QTextBlock>
#include <QUrl>

using namespace Core;
using namespace Utils;

namespace QmakeProjectManager {
namespace Internal {

ProFileHoverHandler::ProFileHoverHandler()
    : m_keywords(qmakeKeywords())
{
}

void ProFileHoverHandler::identifyMatch(TextEditor::TextEditorWidget *editorWidget,
                                        int pos,
                                        ReportPriority report)
{
    const QScopeGuard cleanup([this, report] { report(priority()); });

    m_docFragment.clear();
    m_manualKind = UnknownManual;
    if (!editorWidget->extraSelectionTooltip(pos).isEmpty()) {
        setToolTip(editorWidget->extraSelectionTooltip(pos));
    } else {
        QTextDocument *document = editorWidget->document();
        QTextBlock block = document->findBlock(pos);
        identifyQMakeKeyword(block.text(), pos - block.position());

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

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)) {
                    if (m_keywords.isFunction(buf))
                        identifyDocFragment(FunctionManual, buf);
                    else if (m_keywords.isVariable(buf))
                        identifyDocFragment(VariableManual, buf);
                    break;
                }
                buf.clear();
            } else {
                if (i >= pos)
                    break; // we are after the tooltip pos
            }
            if (c == QLatin1Char('#'))
                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://org.qt-project.qmake/qmake/qmake-%1-reference.html").arg(manualName()));
        const QByteArray html = Core::HelpManager::fileData(url);

        HtmlDocExtractor htmlExtractor;
        htmlExtractor.setMode(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".
        m_docFragment = htmlExtractor.getQMakeFunctionId(QString::fromUtf8(html), m_docFragment);
    }
}

} // namespace Internal
} // namespace QmakeProjectManager