diff --git a/phplanguagesupport.cpp b/phplanguagesupport.cpp index 4ca4008..5b5f78e 100644 --- a/phplanguagesupport.cpp +++ b/phplanguagesupport.cpp @@ -1,189 +1,189 @@ /***************************************************************************** * Copyright (c) 2007 Piyush verma * * Copyright (c) 2009 Niko Sams * * Copyright (c) 2010 Milian Wolff * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * *****************************************************************************/ #include "phplanguagesupport.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "phpparsejob.h" #include "phphighlighting.h" #include "kdevphpversion.h" #include "phpdebug.h" #include "codegen/refactoring.h" #include #include #include "completion/model.h" #include "completion/worker.h" #include "navigation/navigationwidget.h" #include #include "duchain/helper.h" #include using namespace KTextEditor; using namespace KDevelop; K_PLUGIN_FACTORY_WITH_JSON(KDevPhpSupportFactory, "kdevphpsupport.json", registerPlugin(); ) namespace Php { LanguageSupport::LanguageSupport(QObject* parent, const QVariantList& /*args*/) : KDevelop::IPlugin(QStringLiteral("kdevphpsupport"), parent), KDevelop::ILanguageSupport() { Q_ASSERT(internalFunctionFile().toUrl().isValid()); m_highlighting = new Php::Highlighting(this); m_refactoring = new Php::Refactoring(this); CodeCompletionModel* ccModel = new CodeCompletionModel(this); new KDevelop::CodeCompletion(this, ccModel, name()); } LanguageSupport::~LanguageSupport() { parseLock()->lockForWrite(); //By locking the parse-mutexes, we make sure that parse- and preprocess-jobs //get a chance to finish in a good state parseLock()->unlock(); } KDevelop::ParseJob *LanguageSupport::createParseJob(const IndexedString &url) { return new ParseJob(url, this); } QString LanguageSupport::name() const { return QStringLiteral("Php"); } KDevelop::ICodeHighlighting* LanguageSupport::codeHighlighting() const { return m_highlighting; } -KDevelop::ContextMenuExtension LanguageSupport::contextMenuExtension(Context* context) +ContextMenuExtension LanguageSupport::contextMenuExtension(Context* context, QWidget* parent) { ContextMenuExtension cm; EditorContext *ed = dynamic_cast(context); if (ed && ICore::self()->languageController()->languagesForUrl(ed->url()).contains(this)) { // It's safe to add our own ContextMenuExtension. - m_refactoring->fillContextMenu(cm, context); + m_refactoring->fillContextMenu(cm, context, parent); } return cm; } QPair LanguageSupport::wordUnderCursor(const QUrl& url, const Cursor& position) { KDevelop::IDocument* doc = core()->documentController()->documentForUrl(url); if(!doc || !doc->textDocument()) return {}; int lineNumber = position.line(); int lineLength = doc->textDocument()->lineLength(lineNumber); QString line = doc->textDocument()->text(Range(lineNumber, 0, lineNumber, lineLength)); int startCol = position.column(); for ( ; startCol >= 0; --startCol ) { if ( !line[startCol].isLetter() && line[startCol] != '_' ) { // don't include the wrong char if ( startCol != position.column() ) { ++startCol; } break; } } int endCol = position.column(); for ( ; endCol <= lineLength; ++endCol ) { if ( !line[endCol].isLetter() && line[endCol] != '_' ) { break; } } QString word = line.mid(startCol, endCol - startCol); Range range(lineNumber, startCol, lineNumber, endCol); return qMakePair(word, range); } bool isMagicConstant(QPair word) { if ( word.second.isValid() && !word.second.isEmpty() ) { if ( word.first == QLatin1String("__FILE__") || word.first == QLatin1String("__LINE__") || word.first == QLatin1String("__METHOD__") || word.first == QLatin1String("__CLASS__") || word.first == QLatin1String("__FUNCTION__") || word.first == QLatin1String("__NAMESPACE__") ///TODO: php 5.3: __DIR__ ) { ///TODO: maybe we should use the tokenizer to really make sure this is such a token /// and we are not inside a string, comment or similar /// otoh, it doesn't hurt imo return true; } } return false; } QWidget* LanguageSupport::specialLanguageObjectNavigationWidget(const QUrl& url, const Cursor& position) { QPair word = wordUnderCursor(url, position); if ( isMagicConstant(word) ) { DUChainReadLocker lock; if (TopDUContext* top = standardContext(url)) { return new NavigationWidget(TopDUContextPointer(top), position, word.first); } else { return nullptr; } } return ILanguageSupport::specialLanguageObjectNavigationWidget(url, position); } Range LanguageSupport::specialLanguageObjectRange(const QUrl& url, const Cursor& position) { QPair word = wordUnderCursor(url, position); if ( isMagicConstant(word) ) { return word.second; } return ILanguageSupport::specialLanguageObjectRange(url, position); } } #include "phplanguagesupport.moc" diff --git a/phplanguagesupport.h b/phplanguagesupport.h index 61e3d40..e129890 100644 --- a/phplanguagesupport.h +++ b/phplanguagesupport.h @@ -1,96 +1,96 @@ /***************************************************************************** * Copyright (c) 2007 Piyush verma * * Copyright (c) 2009 Niko Sams * * Copyright (c) 2010 Milian Wolff * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * *****************************************************************************/ #ifndef KDEVPHPLANGUAGESUPPORT_H #define KDEVPHPLANGUAGESUPPORT_H #include #include #include namespace KDevelop { class IDocument; class IProject; class CodeHighlighting; class ReferencedTopDUContext; class ParseJob; class IndexedString; } namespace Php { class Highlighting; class Refactoring; /** * \brief Language Support plugin for PHP * * All internal PHP declarations can be found in a central document, hitherto called the * internal function file. See \p internalFunctionsFile(). * * To access the DUContext, include duchain/helper.h and use: * \code * DUChainWriteLocker lock(DUChain::lock()); * TopDUContext* ctx = DUChain::self()->chainForDocument(internalFunctionsFile()); * \endcode * * To access the destination of the internal function file without linking against the LanguageSupport, * like is done in e.g. the PHP-Docs plugin, use: * \code * IndexedString url(KStandardDirs::locate("data", "kdevphpsupport/phpfunctions.php")); * \endcode */ class LanguageSupport : public KDevelop::IPlugin, public KDevelop::ILanguageSupport { Q_OBJECT Q_INTERFACES(KDevelop::ILanguageSupport) public: explicit LanguageSupport(QObject *parent, const QVariantList& args = QVariantList()); ~LanguageSupport() override; /*Name Of the Language*/ QString name() const override; /*Parsejob used by background parser to parse given Url*/ KDevelop::ParseJob *createParseJob(const KDevelop::IndexedString& url) override; static LanguageSupport* self(); /*the code highlighter*/ KDevelop::ICodeHighlighting* codeHighlighting() const override; /** * @returns the ContextMenuExtension for the Php plugin. */ - KDevelop::ContextMenuExtension contextMenuExtension(KDevelop::Context *context) override; + KDevelop::ContextMenuExtension contextMenuExtension(KDevelop::Context* context, QWidget* parent) override; QWidget* specialLanguageObjectNavigationWidget(const QUrl& url, const KTextEditor::Cursor& position) override; KTextEditor::Range specialLanguageObjectRange(const QUrl& url, const KTextEditor::Cursor& position) override; private: KDevelop::CodeHighlighting* m_highlighting; Refactoring *m_refactoring; QPair wordUnderCursor(const QUrl& url, const KTextEditor::Cursor& position); }; } #endif