File: phpdocsplugin.cpp

package info (click to toggle)
kdevelop-php 24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,616 kB
  • sloc: cpp: 20,858; php: 15,243; xml: 136; sh: 58; makefile: 10
file content (221 lines) | stat: -rw-r--r-- 7,542 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
/*
    SPDX-FileCopyrightText: 2012 Milian Wolff <mail@milianw.de>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/

#include "phpdocsplugin.h"
#include "phpdocsmodel.h"
#include "phpdocsconfig.h"

#include <KPluginFactory>
#include <KAboutData>

#include <interfaces/idocumentation.h>
#include <interfaces/icore.h>
#include <interfaces/idocumentationcontroller.h>

#include <language/duchain/duchain.h>
#include <language/duchain/declaration.h>
#include <language/duchain/duchainlock.h>

#include <language/duchain/classdeclaration.h>
#include <language/duchain/functiondeclaration.h>
#include <language/duchain/classmemberdeclaration.h>
#include <language/duchain/classfunctiondeclaration.h>

#include <QFile>

#include "phpdocsdebug.h"
#include "phpdocumentation.h"
#include "phpdocssettings.h"

using namespace KDevelop;

K_PLUGIN_FACTORY_WITH_JSON(PhpDocsFactory, "kdevphpdocs.json", registerPlugin<PhpDocsPlugin>();)

PhpDocsPlugin::PhpDocsPlugin(QObject* parent, const KPluginMetaData& metaData, const QVariantList& args)
    : IPlugin(QStringLiteral("kdevphpdocs"), parent, metaData)
    , m_model(new PhpDocsModel(this))
{
    Q_UNUSED(args);
}

PhpDocsPlugin::~PhpDocsPlugin()
{
}

QString PhpDocsPlugin::name() const
{
    return QStringLiteral("PHP");
}

QIcon PhpDocsPlugin::icon() const
{
    static QIcon icon = QIcon::fromTheme(QStringLiteral("application-x-php"));
    return icon;
}

KDevelop::ConfigPage* PhpDocsPlugin::configPage(int number, QWidget* parent)
{
    if (number == 0) {
        return new PhpDocsConfig(this, parent);
    }
    return nullptr;
}

int PhpDocsPlugin::configPages() const
{
    return 1;
}

///TODO: possibly return multiple filenames (i.e. fallbacks) when doing local lookups
QString PhpDocsPlugin::getDocumentationFilename( KDevelop::Declaration* dec, const bool& isLocal ) const
{
    QString fname;

    //TODO: most of the SPL stuff is not found for me in the deb package php-doc
    //      => check newer documentation or give a fallback to ref.spl.html
    if ( ClassFunctionDeclaration* fdec = dynamic_cast<ClassFunctionDeclaration*>( dec ) ) {
        // class methods -> php.net/CLASS.METHOD
        // local: either CLASS.METHOD.html or function.CLASS-METHOD.html... really sucks :-/
        //        for now, use the latter...
        if ( dec->context() && dec->context()->type() == DUContext::Class && dec->context()->owner() ) {
            QString className = dec->context()->owner()->identifier().toString();

            if ( !isLocal ) {
                fname = className + '.' + fdec->identifier().toString();
            } else {
                if ( fdec->isConstructor() ) {
                    fname = QStringLiteral("construct");
                } else if ( fdec->isDestructor() ) {
                    fname = QStringLiteral("destruct");
                } else {
                    fname = fdec->identifier().toString();
                }
                //TODO: CLASS.METHOD.html e.g. for xmlreader etc. pp.
                fname = "function." + className + '-' + fname;
            }
        }
    } else if ( dynamic_cast<ClassDeclaration*>(dec) ) {
        fname = "class." + dec->identifier().toString();
    } else if ( dynamic_cast<FunctionDeclaration*>(dec) ) {
        fname = "function." + dec->identifier().toString();
    }
    // check for superglobals / reserved variables
    else if ( dec->identifier() == Identifier(QStringLiteral("GLOBALS")) ||
                dec->identifier() == Identifier(QStringLiteral("php_errormsg")) ||
                dec->identifier() == Identifier(QStringLiteral("HTTP_RAW_POST_DATA")) ||
                dec->identifier() == Identifier(QStringLiteral("http_response_header")) ||
                dec->identifier() == Identifier(QStringLiteral("argc")) ||
                dec->identifier() == Identifier(QStringLiteral("argv")) ||
                dec->identifier() == Identifier(QStringLiteral("_GET")) ||
                dec->identifier() == Identifier(QStringLiteral("_POST")) ||
                dec->identifier() == Identifier(QStringLiteral("_FILES")) ||
                dec->identifier() == Identifier(QStringLiteral("_REQUEST")) ||
                dec->identifier() == Identifier(QStringLiteral("_SESSION")) ||
                dec->identifier() == Identifier(QStringLiteral("_ENV")) ||
                dec->identifier() == Identifier(QStringLiteral("_COOKIE")) ) {
        if ( isLocal ) {
            fname = QStringLiteral("reserved.variables.") + dec->identifier().toString().remove('_');
        } else {
            fname = dec->identifier().toString();
        }
    }

    qCDebug(DOCS) << fname;

    if ( !fname.isEmpty() && isLocal ) {
        fname = fname.toLower();
        fname.replace('_', '-');
        fname.append(".html");
    }

    return fname;
}

IDocumentation::Ptr PhpDocsPlugin::documentationForDeclaration( Declaration* dec ) const
{
    if ( dec ) {
        DUChainReadLocker lock( DUChain::lock() );

        // filter non global or non-php declarations
        if ( dec->topContext()->url() != m_model->internalFunctionFile() ) {
            return {};
        }

        QUrl url = PhpDocsSettings::phpDocLocation();
        qCDebug(DOCS) << url;

        QString file = getDocumentationFilename( dec, url.isLocalFile() );
        if ( file.isEmpty() ) {
            qCDebug(DOCS) << "no documentation pattern found for" << dec->toString();
            return {};
        }

        url.setPath( url.path() + '/' + file);
        if ( url.isLocalFile() && !QFile::exists( url.toLocalFile() ) ) {
            qCDebug(DOCS) << "bad path" << url << "for documentation of" << dec->toString() << " - aborting";
            return {};
        }

        qCDebug(DOCS) << "php documentation located at " << url << "for" << dec->toString();
        return documentationForUrl(url, dec->qualifiedIdentifier().toString());
    }

    return {};
}

IDocumentation::Ptr PhpDocsPlugin::documentation(const QUrl& url) const
{
    if (url.toString().startsWith(PhpDocsSettings::phpDocLocation().toString())) {
        return documentationForUrl(url, QString());
    }
    return {};
}


QAbstractListModel* PhpDocsPlugin::indexModel() const
{
    return m_model;
}

IDocumentation::Ptr PhpDocsPlugin::documentationForIndex(const QModelIndex& index) const
{
    return documentationForDeclaration(static_cast<Declaration*>(
        index.data(PhpDocsModel::DeclarationRole).value<DeclarationPointer>().data()
    ));
}

void PhpDocsPlugin::loadUrl(const QUrl& url) const
{
    qCDebug(DOCS) << "loading URL" << url;
    auto doc = documentationForUrl(url, QString());
    ICore::self()->documentationController()->showDocumentation(doc);
}

void PhpDocsPlugin::showDocumentation(const QUrl& url)
{
    auto doc = documentationForUrl(url, url.toString());
    ICore::self()->documentationController()->showDocumentation(doc);
}

IDocumentation::Ptr PhpDocsPlugin::documentationForUrl(const QUrl& url, const QString& name, const QByteArray& description) const
{
    return IDocumentation::Ptr(new PhpDocumentation( url, name, description, const_cast<PhpDocsPlugin*>(this)));
}

IDocumentation::Ptr PhpDocsPlugin::homePage() const
{
    QUrl url = PhpDocsSettings::phpDocLocation();
    if ( url.isLocalFile() ) {
        url.setPath(url.path() + "/index.html");
    } else {
        url.setPath(url.path() + "/manual");
    }
    return documentationForUrl(url, i18n("PHP Documentation"));
}

#include "phpdocsplugin.moc"

#include "moc_phpdocsplugin.cpp"