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: 2007 Andreas Pakulat <apaku@gmx.de>
SPDX-FileCopyrightText: 2007 Piyush verma <piyush.verma@gmail.com>
SPDX-FileCopyrightText: 2012-2016 Sven Brauch <svenbrauch@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "pythonlanguagesupport.h"
#include <QMutexLocker>
#include <QReadWriteLock>
#include <KPluginFactory>
//#include <KPlugin>
#include <interfaces/icore.h>
#include <interfaces/ilanguagecontroller.h>
#include <interfaces/iplugincontroller.h>
#include <interfaces/idocument.h>
#include <interfaces/isourceformatter.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/context.h>
#include <interfaces/contextmenuextension.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iproject.h>
#include <interfaces/isession.h>
#include <language/assistant/renameassistant.h>
#include <language/assistant/staticassistantsmanager.h>
#include <language/interfaces/editorcontext.h>
#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/codecompletion/codecompletion.h>
#include <language/codecompletion/codecompletionmodel.h>
#include "pythonparsejob.h"
#include "pythonhighlighting.h"
#include "duchain/pythoneditorintegrator.h"
#include "codecompletion/model.h"
#include "codegen/refactoring.h"
#include "codegen/correctionfilegenerator.h"
#include "kdevpythonversion.h"
#include "pep8kcm/kcm_pep8.h"
#include "projectconfig/projectconfigpage.h"
#include "docfilekcm/kcm_docfiles.h"
#include "pythonstylechecking.h"
#include "helpers.h"
#include <QDebug>
#include <QProcess>
#include "pythondebug.h"
using namespace KDevelop;
K_PLUGIN_FACTORY_WITH_JSON( KDevPythonSupportFactory, "kdevpythonsupport.json", registerPlugin<Python::LanguageSupport>(); )
namespace Python
{
LanguageSupport* LanguageSupport::m_self = nullptr;
ContextMenuExtension LanguageSupport::contextMenuExtension(Context* context, QWidget* parent)
{
ContextMenuExtension cm;
EditorContext *ec = dynamic_cast<KDevelop::EditorContext *>(context);
if (ec && ICore::self()->languageController()->languagesForUrl(ec->url()).contains(this)) {
// It's a Python file, let's add our context menu.
m_refactoring->fillContextMenu(cm, context, parent);
TypeCorrection::self().doContextMenu(cm, context);
}
return cm;
}
LanguageSupport::LanguageSupport(QObject* parent, const KPluginMetaData& metaData, const QVariantList& /*args*/)
: KDevelop::IPlugin(QStringLiteral("pythonlanguagesupport"), parent , metaData)
, KDevelop::ILanguageSupport()
, m_highlighting( new Highlighting( this ) )
, m_refactoring( new Refactoring( this ) )
, m_styleChecking( new StyleChecking( this ) )
{
m_self = this;
PythonCodeCompletionModel* codeCompletion = new PythonCodeCompletionModel(this);
new KDevelop::CodeCompletion(this, codeCompletion, QStringLiteral("Python"));
auto assistantsManager = core()->languageController()->staticAssistantsManager();
assistantsManager->registerAssistant(StaticAssistant::Ptr(new RenameAssistant(this)));
QObject::connect(ICore::self()->documentController(), &IDocumentController::documentOpened,
this, &LanguageSupport::documentOpened);
}
void LanguageSupport::documentOpened(IDocument* doc)
{
if ( ! ICore::self()->languageController()->languagesForUrl(doc->url()).contains(this) ) {
// not a python file
return;
}
DUChainReadLocker lock;
ReferencedTopDUContext top = DUChain::self()->chainForDocument(doc->url());
lock.unlock();
updateStyleChecking(top);
}
void LanguageSupport::updateStyleChecking(KDevelop::ReferencedTopDUContext top)
{
m_styleChecking->updateStyleChecking(top);
}
LanguageSupport::~LanguageSupport()
{
parseLock()->lockForWrite();
// By locking the parse-mutexes, we make sure that parse jobs get a chance to finish in a good state
parseLock()->unlock();
delete m_highlighting;
m_highlighting = nullptr;
}
KDevelop::ParseJob *LanguageSupport::createParseJob( const IndexedString& url )
{
return new ParseJob(url, this);
}
QString LanguageSupport::name() const
{
return QStringLiteral("Python");
}
LanguageSupport* LanguageSupport::self()
{
return m_self;
}
SourceFormatterItemList LanguageSupport::sourceFormatterItems() const
{
SourceFormatterStyle autopep8(QStringLiteral("autopep8"));
autopep8.setCaption(QStringLiteral("autopep8"));
autopep8.setDescription(i18n("Format source with the autopep8 formatter."));
autopep8.setOverrideSample(QStringLiteral("class klass:\n def method(arg1,arg2):\n a=3+5\n"
"def function(arg,*vararg,**kwargs): return arg+kwarg[0]\nfunction(3, 5, 7)"));
using P = SourceFormatterStyle::MimeHighlightPair;
autopep8.setMimeTypes(SourceFormatterStyle::MimeList{ P{QStringLiteral("text/x-python"), QStringLiteral("Python")},
P{QStringLiteral("text/x-python3"), QStringLiteral("Python 3")} });
QString autopep8path = QStandardPaths::findExecutable(QStringLiteral("autopep8"));
if (autopep8path.isEmpty()) {
// TODO: proper error handling/user notification
qCDebug(KDEV_PYTHON) << "Could not find the autopep8 executable";
autopep8path = QStringLiteral("/usr/bin/autopep8");
}
autopep8.setContent(autopep8path + QStringLiteral(" -i $TMPFILE"));
return SourceFormatterItemList{SourceFormatterStyleItem{QStringLiteral("customscript"), autopep8}};
}
KDevelop::ICodeHighlighting* LanguageSupport::codeHighlighting() const
{
return m_highlighting;
}
BasicRefactoring* LanguageSupport::refactoring() const
{
return m_refactoring;
}
int LanguageSupport::suggestedReparseDelayForChange(KTextEditor::Document* doc, const KTextEditor::Range& changedRange,
const QString& changedText, bool /*removal*/) const
{
if ( changedRange.start().line() != changedRange.end().line() ) {
// instant update
return 0;
}
if ( std::all_of(changedText.begin(), changedText.end(), [](const QChar& c) { return c.isSpace(); }) ) {
qCDebug(KDEV_PYTHON) << changedText << changedRange.end().column() << doc->lineLength(changedRange.end().line());
if ( changedRange.end().column()-1 == doc->lineLength(changedRange.end().line()) ) {
return ILanguageSupport::NoUpdateRequired;
}
}
return ILanguageSupport::DefaultDelay;
}
QList<ILanguageCheck*> LanguageSupport::providedChecks()
{
return {};
}
int LanguageSupport::configPages() const
{
return 2;
}
KDevelop::ConfigPage* LanguageSupport::configPage(int number, QWidget* parent)
{
if (number == 0) {
return new PEP8KCModule(this, parent);
} else if (number == 1) {
return new DocfilesKCModule(this, parent);
}
return nullptr;
}
int LanguageSupport::perProjectConfigPages() const
{
return 1;
}
KDevelop::ConfigPage* LanguageSupport::perProjectConfigPage(int number, const KDevelop::ProjectConfigOptions& options, QWidget* parent)
{
if ( number == 0 ) {
return new Python::ProjectConfigPage(this, options, parent);
}
return nullptr;
}
}
#include "pythonlanguagesupport.moc"
#include "moc_pythonlanguagesupport.cpp"
|