/* * This file is part of KDevelop * Copyright 2010 Milian Wolff * * This program 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 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, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "codeutilsplugin.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace KDevelop; using namespace KTextEditor; K_PLUGIN_FACTORY(CodeUtilsPluginFactory, registerPlugin(); ) K_EXPORT_PLUGIN(CodeUtilsPluginFactory(KAboutData("kdevcodeutils","kdevcodeutils", ki18n("Code Utilities"), "0.1", ki18n("Collection of various utilities that increase productivity while programming."), KAboutData::License_GPL) .addAuthor(ki18n("Milian Wolff"), ki18n("Author"), "mail@milianw.de", "http://milianw.de") )) int debugArea() { static int s_area = KDebug::registerArea("kdevcodeutils"); return s_area; } #define debug() kDebug(debugArea()) CodeUtilsPlugin::CodeUtilsPlugin ( QObject* parent, const QVariantList& ) : IPlugin ( CodeUtilsPluginFactory::componentData(), parent ) { setXMLFile( "kdevcodeutils.rc" ); KAction *action = actionCollection()->addAction( "document_declaration" ); // i18n: action name; 'Document' is a verb action->setText( i18n( "Document Declaration" ) ); action->setShortcut( i18n( "Alt+Shift+d" ) ); connect( action, SIGNAL(triggered(bool)), this, SLOT(documentDeclaration()) ); action->setToolTip( i18n( "Add Doxygen skeleton for declaration under cursor." ) ); // i18n: translate title same as the action name action->setWhatsThis( i18n( "Adds a basic Doxygen comment skeleton in front of " "the declaration under the cursor, e.g. with all the " "parameter of a function." ) ); action->setIcon( KIcon( "documentinfo" ) ); } void CodeUtilsPlugin::documentDeclaration() { IDocument* doc = ICore::self()->documentController()->activeDocument(); if ( !doc ) { return; } Document* textDoc = doc->textDocument(); if ( !textDoc ) { return; } View* view = textDoc->activeView(); if ( !view ) { return; } TemplateInterface* tplIface = qobject_cast< TemplateInterface* >( view ); if ( !tplIface ) { return; } DUChainReadLocker lock; TopDUContext* topCtx = DUChainUtils::standardContextForUrl(doc->url()); if ( !topCtx ) { return; } Declaration* dec = DUChainUtils::declarationInLine( SimpleCursor( view->cursorPosition() ), topCtx ); if ( !dec || dec->isForwardDeclaration() ) { return; } // finally - we found the declaration :) int line = dec->range().start.line; Cursor insertPos( line, 0 ); TemplateRenderer renderer; renderer.addVariable("brief", i18n( "..." )); /* QString indentation = textDoc->line( insertPos.line() ); if (!indentation.isEmpty()) { int lastSpace = 0; while (indentation.at(lastSpace).isSpace()) { ++lastSpace; } indentation.truncate(lastSpace); } */ if (dec->isFunctionDeclaration()) { FunctionDescription description = FunctionDescription(DeclarationPointer(dec)); renderer.addVariable("function", QVariant::fromValue(description)); kDebug() << "Found function" << description.name << "with" << description.arguments.size() << "arguments"; } lock.unlock(); // TODO: Choose the template based on the language QString templateName = "doxygen_cpp"; QList languages = core()->languageController()->languagesForUrl(doc->url()); if (!languages.isEmpty()) { QString languageName = languages.first()->name(); if (languageName == "Php") { templateName = "phpdoc_php"; } else if (languageName == "Python") { templateName = "rest_python"; // Python docstrings appear inside functions and classes, not above them insertPos = Cursor(line+1, 0); } } QString fileName = core()->componentData().dirs()->findResource("data", "kdevcodeutils/templates/" + templateName + ".txt"); if (fileName.isEmpty()) { kWarning() << "No suitable template found" << fileName; return; } const QString comment = renderer.renderFile(KUrl(fileName)); tplIface->insertTemplateText(insertPos, comment, QMap()); } CodeUtilsPlugin::~CodeUtilsPlugin() { }