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
|
/* This file is part of KDevelop
Copyright 2010 Yannick Motta <yannick.motta@gmail.com>
Copyright 2010 Benjamin Port <port.benjamin@gmail.com>
This library 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 library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "manpageplugin.h"
#include "manpagedocumentation.h"
#include "manpagemodel.h"
#include <interfaces/idocumentation.h>
#include <interfaces/icore.h>
#include <interfaces/iprojectcontroller.h>
#include <language/duchain/duchain.h>
#include <language/duchain/declaration.h>
#include <language/duchain/duchainlock.h>
#include <serialization/indexedstring.h>
#include <language/duchain/parsingenvironment.h>
#include <language/duchain/classdeclaration.h>
#include <language/duchain/functiondeclaration.h>
#include <language/duchain/classmemberdeclaration.h>
#include <language/duchain/classfunctiondeclaration.h>
#include <KPluginFactory>
#include <QFile>
#include <QStringListModel>
using namespace KDevelop;
K_PLUGIN_FACTORY_WITH_JSON(ManPageFactory, "kdevmanpage.json", registerPlugin<ManPagePlugin>(); )
ManPagePlugin::ManPagePlugin(QObject* parent, const QVariantList& args)
: IPlugin(QStringLiteral("kdevmanpage"), parent)
{
Q_UNUSED(args);
ManPageDocumentation::s_provider = this;
m_model = new ManPageModel(this);
}
ManPagePlugin::~ManPagePlugin()
{
delete m_model;
}
QString ManPagePlugin::name() const
{
return i18n("Man Page");
}
QIcon ManPagePlugin::icon() const
{
return QIcon::fromTheme(QStringLiteral("application-x-troff-man"));
}
ManPageModel* ManPagePlugin::model() const{
return m_model;
}
IDocumentation::Ptr ManPagePlugin::documentationForDeclaration( Declaration* dec ) const
{
Q_ASSERT(dec);
Q_ASSERT(dec->topContext());
Q_ASSERT(dec->topContext()->parsingEnvironmentFile());
static const IndexedString cppLanguage("C++"); // TODO remove because of new clang parser?
static const IndexedString clangLanguage("Clang");
const IndexedString declarationLanguage(dec->topContext()->parsingEnvironmentFile()->language());
if (declarationLanguage != cppLanguage && declarationLanguage != clangLanguage)
return {};
// Don't show man-page documentation for files that are part of our project
if (core()->projectController()->findProjectForUrl(dec->topContext()->url().toUrl()))
return {};
// Don't show man-page documentation for files that are not in /usr/include, because then we
// most probably will be confusing the global function-name with a local one
if (!dec->topContext()->url().str().startsWith(QLatin1String("/usr/")))
return {};
///@todo Do more verification to make sure that we're showing the correct documentation for the declaration
QString identifier;
IDocumentation::Ptr result;
// First, try to find help for qualified identifier like 'std::vector'
{
DUChainReadLocker lock;
identifier = dec->qualifiedIdentifier().toString(RemoveTemplateInformation);
}
result = documentationForIdentifier(identifier);
if (result.data())
return result;
// Second, try to find help for simple identifier like 'sin'
{
DUChainReadLocker lock;
identifier = dec->identifier().toString(RemoveTemplateInformation);
}
result = documentationForIdentifier(identifier);
if (result.data())
return result;
return {};
}
KDevelop::IDocumentation::Ptr ManPagePlugin::documentationForIdentifier(const QString& identifier) const
{
if (!m_model->containsIdentifier(identifier))
return KDevelop::IDocumentation::Ptr(nullptr);
if (m_model->identifierInSection(identifier, QStringLiteral("3")))
return IDocumentation::Ptr(new ManPageDocumentation(identifier, QUrl(QLatin1String("man:(3)/") + identifier)));
if (m_model->identifierInSection(identifier, QStringLiteral("2")))
return IDocumentation::Ptr(new ManPageDocumentation(identifier, QUrl(QLatin1String("man:(2)/") + identifier)));
return IDocumentation::Ptr(new ManPageDocumentation(identifier, QUrl(QLatin1String("man:/") + identifier)));
}
KDevelop::IDocumentation::Ptr ManPagePlugin::documentation(const QUrl& url) const
{
if (url.toString().startsWith(QLatin1String("man"))) {
IDocumentation::Ptr newDoc(new ManPageDocumentation(url.path(), url));
return newDoc;
}
return {};
}
QAbstractItemModel* ManPagePlugin::indexModel() const
{
return m_model->indexList();
}
IDocumentation::Ptr ManPagePlugin::documentationForIndex(const QModelIndex& index) const
{
QString name = index.data().toString();
return IDocumentation::Ptr(new ManPageDocumentation(name, QUrl(QLatin1String("man:")+name)));
}
IDocumentation::Ptr ManPagePlugin::homePage() const
{
return IDocumentation::Ptr(new ManPageHomeDocumentation);
}
#include "manpageplugin.moc"
|