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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
|
#include <qwhatsthis.h>
#include <qtimer.h>
#include <qfileinfo.h>
#include <qpopupmenu.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kdevgenericfactory.h>
#include <kprocess.h>
#include <kdebug.h>
#include <kaction.h>
#include <kparts/part.h>
#include <kdialogbase.h>
#include <kapplication.h>
#include <qregexp.h>
#include "kdevcore.h"
#include "kdevmainwindow.h"
#include "kdevlanguagesupport.h"
#include "kdevpartcontroller.h"
#include "kdevproject.h"
#include "kdevappfrontend.h"
#include "kdevplugininfo.h"
#include "rubysupport_part.h"
#include "rubyconfigwidget.h"
#include "domutil.h"
#include "qtdesignerrubyintegration.h"
#include "rubyimplementationwidget.h"
typedef KDevGenericFactory<RubySupportPart> RubySupportFactory;
static const KDevPluginInfo data("kdevrubysupport");
K_EXPORT_COMPONENT_FACTORY( libkdevrubysupport, RubySupportFactory( data ) )
RubySupportPart::RubySupportPart(QObject *parent, const char *name, const QStringList& )
: KDevLanguageSupport (&data, parent, name ? name : "RubySupportPart" )
{
setInstance(RubySupportFactory::instance());
setXMLFile("kdevrubysupport.rc");
KAction *action;
action = new KAction( i18n("&Run"), "exec",Key_F9,this, SLOT(slotRun()),actionCollection(), "build_execute" );
action->setToolTip(i18n("Run"));
action->setWhatsThis(i18n("<b>Run</b><p>Starts an application."));
action->setIcon("ruby_run.png");
kdDebug() << "Creating RubySupportPart" << endl;
connect( core(), SIGNAL(projectOpened()), this, SLOT(projectOpened()) );
connect( core(), SIGNAL(projectClosed()), this, SLOT(projectClosed()) );
connect( core(), SIGNAL(contextMenu(QPopupMenu *, const Context *)),
this, SLOT(contextMenu(QPopupMenu *, const Context *)) );
connect( partController(), SIGNAL(savedFile(const KURL&)),
this, SLOT(savedFile(const KURL&)) );
connect( core(), SIGNAL(projectConfigWidget(KDialogBase*)),
this, SLOT(projectConfigWidget(KDialogBase*)) );
}
RubySupportPart::~RubySupportPart() {
}
void RubySupportPart::projectConfigWidget(KDialogBase *dlg)
{
QVBox *vbox = dlg->addVBoxPage(i18n("Ruby"), i18n("Ruby"), BarIcon("ruby_config.png", KIcon::SizeMedium, KIcon::DefaultState, RubySupportPart::instance()));
RubyConfigWidget *w = new RubyConfigWidget(*projectDom(), (QWidget *)vbox, "ruby config widget");
connect( dlg, SIGNAL(okClicked()), w, SLOT(accept()) );
}
void RubySupportPart::projectOpened()
{
kdDebug() << "projectOpened()" << endl;
connect( project(), SIGNAL(addedFilesToProject(const QStringList &)),
this, SLOT(addedFilesToProject(const QStringList &)) );
connect( project(), SIGNAL(removedFilesFromProject(const QStringList &)),
this, SLOT(removedFilesFromProject(const QStringList &)) );
// We want to parse only after all components have been
// properly initialized
QTimer::singleShot(0, this, SLOT(initialParse()));
}
void RubySupportPart::maybeParse(const QString fileName)
{
QFileInfo fi(fileName);
if (fi.extension() == "rb") {
if (codeModel()->hasFile(fileName)) {
emit aboutToRemoveSourceInfo(fileName);
codeModel()->removeFile(codeModel()->fileByName(fileName));
}
parse(fileName);
}
}
void RubySupportPart::initialParse()
{
kdDebug() << "initialParse()" << endl;
if (project()) {
kapp->setOverrideCursor(waitCursor);
QStringList files = project()->allFiles();
for (QStringList::Iterator it = files.begin(); it != files.end() ;++it) {
kdDebug() << "maybe parse " << project()->projectDirectory() + "/" + (*it) << endl;
maybeParse(project()->projectDirectory() + "/" + *it);
}
emit updatedSourceInfo();
kapp->restoreOverrideCursor();
} else {
kdDebug() << "No project" << endl;
}
}
void RubySupportPart::addedFilesToProject(const QStringList &fileList)
{
kdDebug() << "addedFilesToProject()" << endl;
QStringList::ConstIterator it;
for ( it = fileList.begin(); it != fileList.end(); ++it )
{
QString fileName = project()->projectDirectory() + "/" + ( *it );
maybeParse( fileName );
emit addedSourceInfo( fileName );
}
}
void RubySupportPart::removedFilesFromProject(const QStringList &fileList)
{
kdDebug() << "removedFilesFromProject()" << endl;
QStringList::ConstIterator it;
for ( it = fileList.begin(); it != fileList.end(); ++it )
{
QString fileName = project()->projectDirectory() + "/" + ( *it );
if( codeModel()->hasFile(fileName) ){
emit aboutToRemoveSourceInfo( fileName );
codeModel()->removeFile( codeModel()->fileByName(fileName) );
}
}
}
void RubySupportPart::savedFile(const KURL &fileName)
{
kdDebug() << "savedFile()" << endl;
if (project()->allFiles().contains(fileName.path().mid ( project()->projectDirectory().length() + 1 ))) {
maybeParse(fileName.path());
emit addedSourceInfo( fileName.path() );
}
}
KDevLanguageSupport::Features RubySupportPart::features()
{
return Features(Classes | Functions | Variables | Declarations | Signals | Slots);
}
void RubySupportPart::parse(const QString &fileName)
{
QFile f(QFile::encodeName(fileName));
if (!f.open(IO_ReadOnly))
return;
QTextStream stream(&f);
QRegExp classre("^\\s*(class|module)\\s+([A-Z][A-Za-z0-9_]+)\\s*(<\\s*([A-Z][A-Za-z0-9_:]+))?$");
QRegExp methodre("^\\s*def\\s+([A-Z][A-Za-z0-9_:]+\\.)?([A-Za-z0-9_]+[!?=]?|\\[\\]=?|\\*\\*||\\-|[!~+*/%&|><^]|>>|<<||<=>|<=|>=|==|===|!=|=~|!~).*$");
QRegExp accessre("^\\s*(private|protected|public)\\s*((:([A-Za-z0-9_]+[!?=]?|\\[\\]=?|\\*\\*||\\-|[!~+*/%&|><^]|>>|<<||<=>|<=|>=|==|===|!=|=~|!~),?\\s*)*)$");
QRegExp attr_accessorre("^\\s*(attr_accessor|attr_reader|attr_writer)\\s*((:([A-Za-z0-9_]+),?\\s*)*)$");
QRegExp symbolre(":([^,]+),?");
QRegExp line_contre(",\\s*$");
QRegExp slot_signalre("^\\s*(slots|signals|k_dcop|k_dcop_signals)\\s*(('[^)]+\\)',?\\s*)*)$");
QRegExp memberre("'([A-Za-z0-9_ &*]+\\s)?([A-Za-z0-9_]+)\\([^)]*\\)',?");
QRegExp begin_commentre("^*=begin");
QRegExp end_commentre("^*=end");
QRegExp variablere("(@@?[A-Za-z0-9_]+)\\s*=\\s*((?:([A-Za-z0-9_:.]+)\\.new)|[\\[\"'%:/\\?\\{]|%r|<<|true|false|^\\?|0[0-7]+|[-+]?0b[01]+|[-+]?0x[1-9a-fA-F]+|[-+]?[0-9_\\.e]+|nil)?");
FileDom m_file = codeModel()->create<FileModel>();
m_file->setName(fileName);
ClassDom lastClass;
FunctionDom lastMethod;
int lastAccess = CodeModelItem::Public;
QString rawline;
QCString line;
int lineNo = 0;
while (!stream.atEnd()) {
rawline = stream.readLine();
line = rawline.stripWhiteSpace().local8Bit();
if (classre.search(line) != -1) {
if (m_file->hasClass(classre.cap(2))) {
lastClass = m_file->classByName( classre.cap(2) )[ 0 ];
} else {
lastClass = codeModel()->create<ClassModel>();
lastClass->setName(classre.cap(2));
lastClass->setFileName( fileName );
lastClass->setStartPosition( lineNo, 0 );
m_file->addClass( lastClass );
}
QString parent = classre.cap(4);
if (!parent.isEmpty())
{
kdDebug() << "Add parent " << parent << endl;
lastClass->addBaseClass( parent );
}
lastAccess = CodeModelItem::Public;
} else if (methodre.search(line) != -1) {
FunctionDom methodDecl;
if ( lastClass != 0 && lastClass->hasFunction( methodre.cap(2) ) ) {
FunctionList methods = lastClass->functionByName( methodre.cap(2) );
methodDecl = methods[0];
} else {
methodDecl = codeModel()->create<FunctionModel>();
methodDecl->setFileName( fileName );
methodDecl->setStartPosition( lineNo, 0 );
methodDecl->setName(methodre.cap(2));
}
FunctionDefinitionDom method = codeModel()->create<FunctionDefinitionModel>();
method->setName(methodre.cap(2));
kdDebug() << "Add method: " << method->name() << endl;
method->setFileName( fileName );
method->setStartPosition( lineNo, 0 );
if (methodDecl->name() == "initialize") {
// Ruby constructors are alway private
methodDecl->setAccess( CodeModelItem::Private );
} else {
methodDecl->setAccess( lastAccess );
}
if (methodre.cap(1) != "") {
// A ruby class/singleton method of the form <classname>.<methodname>
methodDecl->setStatic( true );
}
lastMethod = method;
if (lastClass != 0) {
QStringList scope( lastClass->name() );
method->setScope( scope );
methodDecl->setScope( scope );
if( !lastClass->hasFunction(methodDecl->name()) ) {
lastClass->addFunction( methodDecl );
}
if( !lastClass->hasFunctionDefinition(method->name()) ) {
lastClass->addFunctionDefinition( method );
}
} else if( !m_file->hasFunctionDefinition(method->name()) ){
m_file->addFunction( methodDecl );
m_file->addFunctionDefinition( method );
lastClass = 0;
}
} else if (accessre.search(line) != -1 && lastClass != 0) {
int currentAccess = lastAccess;
if (accessre.cap(1) == "public") {
currentAccess = CodeModelItem::Public;
} else if (accessre.cap(1) == "protected") {
currentAccess = CodeModelItem::Protected;
} else if (accessre.cap(1) == "private") {
currentAccess = CodeModelItem::Private;
}
if (accessre.cap(2) == "") {
lastAccess = currentAccess;
} else {
QString symbolList( accessre.cap(2) );
int pos = 0;
while ( pos >= 0 ) {
pos = symbolre.search( symbolList, pos );
if (pos == -1) {
if (line_contre.search(line) != -1) {
rawline = stream.readLine();
if (!stream.atEnd()) {
line = rawline.stripWhiteSpace().local8Bit();
++lineNo;
symbolList = line;
pos = 0;
}
}
} else {
if ( lastClass->hasFunction( symbolre.cap(1) ) ) {
FunctionList methods = lastClass->functionByName( symbolre.cap(1) );
methods[0]->setAccess( currentAccess );
}
pos += symbolre.matchedLength();
}
}
}
} else if (slot_signalre.search(line) != -1 && lastClass != 0) {
QString memberList( slot_signalre.cap(2) );
int pos = 0;
while ( pos >= 0 ) {
pos = memberre.search( memberList, pos );
if (pos == -1) {
if (line_contre.search(line) != -1) {
rawline = stream.readLine();
if (!stream.atEnd()) {
line = rawline.stripWhiteSpace().local8Bit();
++lineNo;
memberList = line;
pos = 0;
}
}
} else {
FunctionDom method;
if ( lastClass->hasFunction( memberre.cap(2) ) ) {
FunctionList methods = lastClass->functionByName( memberre.cap(2) );
method = methods[0];
} else {
method = codeModel()->create<FunctionModel>();
}
QStringList scope( lastClass->name() );
method->setScope( scope );
method->setName(memberre.cap(2));
method->setFileName( fileName );
method->setStartPosition( lineNo, 0 );
if (slot_signalre.cap(1) == "slots" || slot_signalre.cap(1) == "k_dcop") {
method->setSlot( true );
} else {
method->setSignal( true );
}
if ( !lastClass->hasFunction(method->name()) ) {
lastClass->addFunction( method );
}
pos += memberre.matchedLength();
}
}
} else if (attr_accessorre.search(line) != -1 && lastClass != 0) {
QString attr( attr_accessorre.cap(1) );
QString symbolList( attr_accessorre.cap(2) );
int pos = 0;
while ( pos >= 0 ) {
pos = symbolre.search( symbolList, pos );
if (pos == -1) {
if (line_contre.search(line) != -1) {
rawline = stream.readLine();
if (!stream.atEnd()) {
line = rawline.stripWhiteSpace().local8Bit();
++lineNo;
symbolList = line;
pos = 0;
}
}
} else {
if ( !lastClass->hasFunction(symbolre.cap(1)) ) {
QStringList scope( lastClass->name() );
if (attr == "attr_accessor" || attr == "attr_reader") {
FunctionDefinitionDom method = codeModel()->create<FunctionDefinitionModel>();
method->setName(symbolre.cap(1));
kdDebug() << "Add method: " << method->name() << endl;
method->setFileName( fileName );
method->setStartPosition( lineNo, 0 );
method->setScope(scope);
lastClass->addFunction( model_cast<FunctionDom>(method) );
lastClass->addFunctionDefinition( method );
}
if (attr == "attr_accessor" || attr == "attr_writer") {
FunctionDefinitionDom method = codeModel()->create<FunctionDefinitionModel>();
method->setName(symbolre.cap(1) + "=");
kdDebug() << "Add method: " << method->name() << endl;
method->setFileName( fileName );
method->setStartPosition( lineNo, 0 );
method->setScope(scope);
lastClass->addFunction( model_cast<FunctionDom>(method) );
lastClass->addFunctionDefinition( method );
}
pos += symbolre.matchedLength();
}
}
}
} else if (variablere.search(line) != -1 && lastClass != 0) {
VariableDom attr;
if ( lastClass->hasVariable( variablere.cap(1) ) ) {
attr = lastClass->variableByName( variablere.cap(1) );
} else {
attr = codeModel()->create<VariableModel>();
attr->setName( variablere.cap(1) );
attr->setFileName( fileName );
attr->setStartPosition( lineNo, 0 );
attr->setAccess( CodeModelItem::Private );
if (QRegExp("^@@").search(attr->name()) != -1) {
attr->setStatic( true );
}
lastClass->addVariable( attr );
}
// Give priority to any variable initialized in the constructor
// Otherwise, take the first one found in the source file
if (lastMethod != 0 && lastMethod->name() == "initialize") {
attr->setFileName( fileName );
attr->setStartPosition( lineNo, 0 );
}
if (QRegExp("^(/|%r)").search(variablere.cap(2)) != -1) {
attr->setType( "Regexp" );
} else if (QRegExp("^[\"'%<]").search(variablere.cap(2)) != -1) {
attr->setType( "String" );
} else if (QRegExp("^\\[").search(variablere.cap(2)) != -1) {
attr->setType( "Array" );
} else if (QRegExp("^\\{").search(variablere.cap(2)) != -1) {
attr->setType( "Hash" );
} else if (QRegExp("^:").search(variablere.cap(2)) != -1) {
attr->setType( "Symbol" );
} else if (QRegExp("\\.\\.").search(variablere.cap(2)) != -1) {
attr->setType( "Range" );
} else if (variablere.cap(2) == "true" || variablere.cap(2) == "false") {
attr->setType( "Boolean" );
} else if ( QRegExp("^[-+]?[0-9_]+").exactMatch(variablere.cap(2))
|| QRegExp("^[-+]?(0x|0|0b|\\?)").search(variablere.cap(2)) != -1 )
{
attr->setType( "Integer" );
} else if (QRegExp("[0-9._]+(e[-+0-9]+)?").exactMatch(variablere.cap(2))) {
attr->setType( "Float" );
} else if (variablere.cap(2) != "nil" && variablere.cap(3) != "") {
attr->setType( variablere.cap(3) );
}
} else if (begin_commentre.search(line) != -1) {
while (!stream.atEnd() && end_commentre.search(line) == -1) {
rawline = stream.readLine();
line = rawline.stripWhiteSpace().local8Bit();
++lineNo;
}
}
++lineNo;
}
f.close();
codeModel()->addFile( m_file );
}
void RubySupportPart::slotRun () {
QFileInfo program(mainProgram());
QString cmd = QString("%1 -K%2 -C%3 -I%4 %5 %6")
.arg(interpreter())
.arg(characterCoding())
.arg(program.dirPath())
.arg(program.dirPath())
.arg(program.fileName())
.arg(programArgs());
startApplication(cmd);
}
QString RubySupportPart::interpreter() {
QString prog = DomUtil::readEntry(*projectDom(), "/kdevrubysupport/run/interpreter");
if (prog.isEmpty()) prog = "ruby";
return prog;
}
QString RubySupportPart::mainProgram() {
QString prog;
int runMainProgram = DomUtil::readIntEntry(*projectDom(), "/kdevrubysupport/run/runmainprogram");
if (runMainProgram == 0) {
prog = project()->projectDirectory() + "/" + DomUtil::readEntry(*projectDom(), "/kdevrubysupport/run/mainprogram");
} else {
KParts::ReadOnlyPart *ro_part = dynamic_cast<KParts::ReadOnlyPart*>(partController()->activePart());
if (ro_part != 0) {
prog = ro_part->url().path();
}
}
return prog;
}
QString RubySupportPart::programArgs() {
QString args = DomUtil::readEntry(*projectDom(), "/kdevrubysupport/run/programargs");
return args;
}
QString RubySupportPart::characterCoding() {
int coding = DomUtil::readIntEntry(*projectDom(), "/kdevrubysupport/run/charactercoding");
QString code("A");
switch (coding) {
case 0:
code = "A";
break;
case 1:
code = "E";
break;
case 2:
code = "S";
break;
case 3:
code = "U";
break;
}
return code;
}
void RubySupportPart::startApplication(const QString &program) {
bool inTerminal = DomUtil::readBoolEntry(*projectDom(), "/kdevrubysupport/run/terminal");
if (KDevAppFrontend *appFrontend = extension<KDevAppFrontend>("KDevelop/AppFrontend"))
appFrontend->startAppCommand(QString::QString(), program, inTerminal);
}
KMimeType::List RubySupportPart::mimeTypes( )
{
KMimeType::List list;
KMimeType::Ptr mime = KMimeType::mimeType( "text/x-ruby" );
if( mime )
list << mime;
return list;
}
KDevDesignerIntegration *RubySupportPart::designer(KInterfaceDesigner::DesignerType type)
{
KDevDesignerIntegration *des = 0;
switch (type)
{
case KInterfaceDesigner::QtDesigner:
des = m_designers[type];
if (des == 0)
{
RubyImplementationWidget *impl = new RubyImplementationWidget(this);
des = new QtDesignerRubyIntegration(this, impl);
des->loadSettings(*project()->projectDom(),
"kdevrubysupport/designerintegration");
m_designers[type] = des;
}
break;
case KInterfaceDesigner::Glade:
break;
}
return des;
}
void RubySupportPart::projectClosed( )
{
for (QMap<KInterfaceDesigner::DesignerType, KDevDesignerIntegration*>::const_iterator it = m_designers.begin();
it != m_designers.end(); ++it)
{
kdDebug() << "calling save settings fro designer integration" << endl;
it.data()->saveSettings(*project()->projectDom(), "kdevrubysupport/designerintegration");
}
}
void RubySupportPart::contextMenu( QPopupMenu * popup, const Context * context )
{
if (context->hasType(Context::FileContext)){
const FileContext *fc = static_cast<const FileContext*>(context);
//this is a .ui file and only selection contains only one such file
KURL url = fc->urls().first();
if (url.fileName().endsWith(".ui"))
{
m_contextFileName = url.fileName();
int id = popup->insertItem(i18n("Create or Select Implementation..."), this, SLOT(slotCreateSubclass()));
popup->setWhatsThis(id, i18n("<b>Create or select implementation</b><p>Creates or selects a subclass of selected form for use with integrated KDevDesigner."));
}
}
}
void RubySupportPart::slotCreateSubclass()
{
QFileInfo fi(m_contextFileName);
if (fi.extension(false) != "ui")
return;
QtDesignerRubyIntegration *des = dynamic_cast<QtDesignerRubyIntegration*>(designer(KInterfaceDesigner::QtDesigner));
if (des)
des->selectImplementation(m_contextFileName);
}
#include "rubysupport_part.moc"
|