~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to parts/abbrev/abbrevpart.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2002 Roberto Raggi                                      *
3
 
 *   roberto@kdevelop.org                                                  *
4
 
 *   Copyright (C) 2002 by Bernd Gehrmann                                  *
5
 
 *   bernd@kdevelop.org                                                    *
6
 
 *   Copyright (C) 2003 by Alexander Dymo                                  *
7
 
 *   cloudtemple@mksat.net                                                 *
8
 
 *                                                                         *
9
 
 *   This program is free software; you can redistribute it and/or modify  *
10
 
 *   it under the terms of the GNU General Public License as published by  *
11
 
 *   the Free Software Foundation; either version 2 of the License, or     *
12
 
 *   (at your option) any later version.                                   *
13
 
 *                                                                         *
14
 
 ***************************************************************************/
15
 
 
16
 
#include "abbrevpart.h"
17
 
 
18
 
#include <qfile.h>
19
 
#include <qfileinfo.h>
20
 
#include <qregexp.h>
21
 
#include <qvbox.h>
22
 
#include <kdebug.h>
23
 
#include <kdialogbase.h>
24
 
#include <klocale.h>
25
 
#include <kparts/part.h>
26
 
#include <kstandarddirs.h>
27
 
#include <kdevgenericfactory.h>
28
 
#include <kaction.h>
29
 
#include <kconfig.h>
30
 
#include <kio/netaccess.h>
31
 
#include <kiconloader.h>
32
 
#include <kdevplugininfo.h>
33
 
 
34
 
#include <ktexteditor/document.h>
35
 
#include <ktexteditor/editinterface.h>
36
 
#include <ktexteditor/viewcursorinterface.h>
37
 
#include <ktexteditor/codecompletioninterface.h>
38
 
 
39
 
#include "kdevcore.h"
40
 
#include "kdevpartcontroller.h"
41
 
#include "abbrevconfigwidget.h"
42
 
#include "kdeveditorutil.h"
43
 
 
44
 
static const KDevPluginInfo data("kdevabbrev");
45
 
 
46
 
class AbbrevFactory : public KDevGenericFactory<AbbrevPart>
47
 
{
48
 
public:
49
 
    AbbrevFactory()
50
 
        : KDevGenericFactory<AbbrevPart>( data )
51
 
    { }
52
 
 
53
 
    virtual KInstance *createInstance()
54
 
    {
55
 
        KInstance *instance = KDevGenericFactory<AbbrevPart>::createInstance();
56
 
        KStandardDirs *dirs = instance->dirs();
57
 
        dirs->addResourceType( "codetemplates",
58
 
                               KStandardDirs::kde_default( "data" ) + "kdevabbrev/templates/" );
59
 
        dirs->addResourceType( "sources",
60
 
                               KStandardDirs::kde_default( "data" ) + "kdevabbrev/sources" );
61
 
 
62
 
        return instance;
63
 
    }
64
 
};
65
 
 
66
 
K_EXPORT_COMPONENT_FACTORY( libkdevabbrev, AbbrevFactory )
67
 
 
68
 
AbbrevPart::AbbrevPart(QObject *parent, const char *name, const QStringList &)
69
 
    : KDevPlugin(&data, parent, name ? name : "AbbrevPart")
70
 
{
71
 
    setInstance(AbbrevFactory::instance());
72
 
    setXMLFile("kdevabbrev.rc");
73
 
 
74
 
    connect(partController(), SIGNAL(activePartChanged(KParts::Part*)),
75
 
        this, SLOT(slotActivePartChanged(KParts::Part*)) );
76
 
 
77
 
    connect(core(), SIGNAL(configWidget(KDialogBase*)), this, SLOT(configWidget(KDialogBase*)));
78
 
 
79
 
    KAction *action;
80
 
    action = new KAction( i18n("Expand Text"), CTRL + Key_J,
81
 
                          this, SLOT(slotExpandText()),
82
 
                          actionCollection(), "edit_expandtext" );
83
 
    action->setToolTip( i18n("Expand current word") );
84
 
    action->setWhatsThis( i18n("<b>Expand current word</b><p>Current word can be completed using the list of similar words in source files.") );
85
 
 
86
 
    action = new KAction( i18n("Expand Abbreviation"), CTRL + Key_L,
87
 
                          this, SLOT(slotExpandAbbrev()),
88
 
                          actionCollection(), "edit_expandabbrev" );
89
 
    action->setToolTip( i18n("Expand abbreviation") );
90
 
    action->setWhatsThis( i18n("<b>Expand abbreviation</b><p>Enable and configure abbreviations in <b>KDevelop Settings</b>, <b>Abbreviations</b> tab.") );
91
 
 
92
 
    load();
93
 
 
94
 
    m_inCompletion = false;
95
 
    docIface = 0;
96
 
    editIface = 0;
97
 
    viewCursorIface = 0;
98
 
    completionIface = 0;
99
 
 
100
 
    m_prevLine = -1;
101
 
    m_prevColumn = -1;
102
 
    m_sequenceLength = 0;
103
 
 
104
 
    KConfig* config = AbbrevFactory::instance()->config();
105
 
    KConfigGroupSaver group( config, "General" );
106
 
    m_autoWordCompletionEnabled = config->readBoolEntry( "AutoWordCompletion", false );
107
 
 
108
 
    updateActions();
109
 
 
110
 
    slotActivePartChanged( partController()->activePart() );
111
 
}
112
 
 
113
 
 
114
 
AbbrevPart::~AbbrevPart()
115
 
{
116
 
    save();
117
 
}
118
 
 
119
 
bool AbbrevPart::autoWordCompletionEnabled() const
120
 
{
121
 
    return m_autoWordCompletionEnabled;
122
 
}
123
 
 
124
 
void AbbrevPart::setAutoWordCompletionEnabled( bool enabled )
125
 
{
126
 
    if( enabled == m_autoWordCompletionEnabled )
127
 
        return;
128
 
 
129
 
    KConfig* config = AbbrevFactory::instance()->config();
130
 
    KConfigGroupSaver group( config, "General" );
131
 
 
132
 
    m_autoWordCompletionEnabled = enabled;
133
 
    config->writeEntry( "AutoWordCompletion", m_autoWordCompletionEnabled );
134
 
    config->sync();
135
 
 
136
 
    if( !docIface || !docIface->widget() )
137
 
        return;
138
 
 
139
 
    disconnect( docIface, 0, this, 0 );
140
 
    disconnect( docIface->widget(), 0, this, 0 );
141
 
 
142
 
    if( m_autoWordCompletionEnabled ){
143
 
        connect( docIface->widget(), SIGNAL(completionAborted()),
144
 
                 this, SLOT(slotCompletionAborted()) );
145
 
        connect( docIface->widget(), SIGNAL(completionDone()),
146
 
                 this, SLOT(slotCompletionDone()) );
147
 
        connect( docIface->widget(), SIGNAL(aboutToShowCompletionBox()),
148
 
                 this, SLOT(slotAboutToShowCompletionBox()) );
149
 
 
150
 
        connect( docIface, SIGNAL(textChanged()), this, SLOT(slotTextChanged()) );
151
 
    }
152
 
}
153
 
void AbbrevPart::load()
154
 
{
155
 
    KStandardDirs *dirs = AbbrevFactory::instance()->dirs();
156
 
    QString localTemplatesFile = locateLocal("codetemplates", "templates", AbbrevFactory::instance());
157
 
    QStringList files;
158
 
    if (QFileInfo(localTemplatesFile).exists())
159
 
        files << localTemplatesFile;
160
 
    else
161
 
        files = dirs->findAllResources("codetemplates", QString::null, false, true);
162
 
 
163
 
    QString localSourcesFile = locateLocal("sources", "sources", AbbrevFactory::instance());
164
 
    QStringList sourceFiles;
165
 
    if (QFileInfo(localSourcesFile).exists())
166
 
        sourceFiles << localSourcesFile;
167
 
    else
168
 
        sourceFiles = dirs->findAllResources("sources", QString::null, false, true);
169
 
    kdDebug(9028) << "=========> sourceFiles: " << sourceFiles.join(" ") << endl;
170
 
 
171
 
    this->m_completionFile = QString::null;
172
 
    for( QStringList::Iterator it=sourceFiles.begin(); it!=sourceFiles.end(); ++it ) {
173
 
        QString fn = *it;
174
 
        kdDebug(9028) << "===> load file: " << fn << endl;
175
 
        QFile f( fn );
176
 
        if ( f.open(IO_ReadOnly) ) {
177
 
                QTextStream stream( &f );
178
 
                m_completionFile += ( stream.read() + QString("\n") );
179
 
                f.close();
180
 
        }
181
 
    }
182
 
 
183
 
    QStringList::ConstIterator it;
184
 
    for (it = files.begin(); it != files.end(); ++it) {
185
 
        QString fn = *it;
186
 
        kdDebug(9028) << "fn = " << fn << endl;
187
 
        QFile f( fn );
188
 
        if ( f.open(IO_ReadOnly) ) {
189
 
            QDomDocument doc;
190
 
            doc.setContent( &f );
191
 
            QDomElement root = doc.firstChild().toElement();
192
 
            QDomElement e = root.firstChild().toElement();
193
 
            while ( !e.isNull() ){
194
 
                addTemplate( e.attribute("name"),
195
 
                             e.attribute("description"),
196
 
                             e.attribute("suffixes"),
197
 
                             e.attribute("code") );
198
 
                e = e.nextSibling().toElement();
199
 
            }
200
 
            f.close();
201
 
        }
202
 
    }
203
 
}
204
 
 
205
 
 
206
 
void AbbrevPart::save()
207
 
{
208
 
    QString fn = AbbrevFactory::instance()->dirs()->saveLocation("codetemplates", "", true);
209
 
    kdDebug(9028) << "fn = " << fn << endl;
210
 
 
211
 
    QDomDocument doc( "Templates" );
212
 
    QDomElement root = doc.createElement( "Templates" );
213
 
    doc.appendChild( root );
214
 
 
215
 
    QPtrList<CodeTemplate> templates = m_templates.allTemplates();
216
 
    CodeTemplate *templ;
217
 
    for (templ = templates.first(); templ; templ = templates.next())
218
 
    {
219
 
        QDomElement e = doc.createElement( "Template" );
220
 
        e.setAttribute( "name", templ->name );
221
 
        e.setAttribute( "description", templ->description );
222
 
        e.setAttribute( "suffixes", templ->suffixes );
223
 
        e.setAttribute( "code", templ->code );
224
 
        root.appendChild( e );
225
 
    }
226
 
 
227
 
    QFile f( fn + "templates" );
228
 
    if( f.open(IO_WriteOnly) ){
229
 
        QTextStream stream( &f );
230
 
        stream << doc.toString();
231
 
        f.close();
232
 
    }
233
 
}
234
 
 
235
 
 
236
 
QString AbbrevPart::currentWord() const
237
 
{
238
 
    return KDevEditorUtil::currentWord( dynamic_cast<KTextEditor::Document*>( partController()->activePart() ) );
239
 
}
240
 
 
241
 
 
242
 
void AbbrevPart::configWidget(KDialogBase *dlg)
243
 
{
244
 
        QVBox *vbox = dlg->addVBoxPage(i18n("Abbreviations"), i18n("Abbreviations"), BarIcon( info()->icon(), KIcon::SizeMedium) );
245
 
    AbbrevConfigWidget *w = new AbbrevConfigWidget(this, vbox, "abbrev config widget");
246
 
    connect(dlg, SIGNAL(okClicked()), w, SLOT(accept()));
247
 
}
248
 
 
249
 
 
250
 
void AbbrevPart::slotExpandText()
251
 
{
252
 
    if( !editIface || !completionIface || !viewCursorIface )
253
 
        return;
254
 
 
255
 
    QString word = currentWord();
256
 
    if (word.isEmpty())
257
 
        return;
258
 
 
259
 
    QValueList<KTextEditor::CompletionEntry> entries = findAllWords(editIface->text(), word);
260
 
    if (entries.count() == 0) {
261
 
        ; // some statusbar message?
262
 
//    } else if (entries.count() == 1) {
263
 
//        uint line, col;
264
 
//        viewCursorIface->cursorPositionReal(&line, &col);
265
 
//        QString txt = entries[0].text.mid(word.length());
266
 
//        editIface->insertText( line, col, txt );
267
 
//        viewCursorIface->setCursorPositionReal( line, col + txt.length() );
268
 
    } else {
269
 
        m_inCompletion = true;
270
 
        completionIface->showCompletionBox(entries, word.length());
271
 
    }
272
 
}
273
 
 
274
 
 
275
 
QValueList<KTextEditor::CompletionEntry> AbbrevPart::findAllWords(const QString &text, const QString &prefix)
276
 
{
277
 
    QValueList<KTextEditor::CompletionEntry> entries;
278
 
 
279
 
    KParts::ReadWritePart *part = dynamic_cast<KParts::ReadWritePart*>(partController()->activePart());
280
 
    QWidget *view = partController()->activeWidget();
281
 
    if (!part || !view) {
282
 
        kdDebug(9028) << "no rw part" << endl;
283
 
        return entries;
284
 
    }
285
 
 
286
 
    QString suffix = part->url().url();
287
 
    int pos = suffix.findRev('.');
288
 
    if (pos != -1)
289
 
        suffix.remove(0, pos+1);
290
 
    kdDebug(9028) << "AbbrevPart::findAllWords with suffix " << suffix << endl;
291
 
 
292
 
    QMap<QString, bool> map;
293
 
    QRegExp rx( QString("\\b") + prefix + "[a-zA-Z0-9_]+\\b" );
294
 
 
295
 
    int idx = 0;
296
 
    pos = 0;
297
 
    int len = 0;
298
 
    while ( (pos = rx.search(text, idx)) != -1 ) {
299
 
        len = rx.matchedLength();
300
 
        QString word = text.mid(pos, len);
301
 
        if (map.find(word) == map.end()) {
302
 
            KTextEditor::CompletionEntry e;
303
 
            e.text = word;
304
 
            entries << e;
305
 
            map[ word ] = TRUE;
306
 
        }
307
 
        idx = pos + len + 1;
308
 
    }
309
 
 
310
 
    idx = 0;
311
 
    pos = 0;
312
 
    len = 0;
313
 
    while ( (pos = rx.search(m_completionFile, idx)) != -1 ) {
314
 
        len = rx.matchedLength();
315
 
        QString word = m_completionFile.mid(pos, len);
316
 
        if (map.find(word) == map.end()) {
317
 
            KTextEditor::CompletionEntry e;
318
 
            e.text = word;
319
 
            entries << e;
320
 
            map[ word ] = TRUE;
321
 
        }
322
 
        idx = pos + len + 1;
323
 
    }
324
 
 
325
 
 
326
 
    QMap<QString, CodeTemplate*> m = m_templates[suffix];
327
 
    for (QMap<QString, CodeTemplate*>::const_iterator it = m.begin(); it != m.end() ; ++it) {
328
 
        KTextEditor::CompletionEntry e;
329
 
        e.text = it.data()->description + " <abbrev>";
330
 
        e.userdata = it.key();
331
 
        entries << e;
332
 
    }
333
 
 
334
 
    return entries;
335
 
}
336
 
 
337
 
 
338
 
void AbbrevPart::slotExpandAbbrev()
339
 
{
340
 
    KParts::ReadWritePart *part = dynamic_cast<KParts::ReadWritePart*>(partController()->activePart());
341
 
    QWidget *view = partController()->activeWidget();
342
 
    if (!part || !view) {
343
 
        kdDebug(9028) << "no rw part" << endl;
344
 
        return;
345
 
    }
346
 
 
347
 
    QString suffix = part->url().url();
348
 
    int pos = suffix.findRev('.');
349
 
    if (pos != -1)
350
 
        suffix.remove(0, pos+1);
351
 
 
352
 
    KTextEditor::EditInterface *editiface
353
 
        = dynamic_cast<KTextEditor::EditInterface*>(part);
354
 
    if (!editiface) {
355
 
        kdDebug(9028) << "no editiface" << endl;
356
 
        return;
357
 
    }
358
 
    KTextEditor::ViewCursorInterface *cursoriface
359
 
        = dynamic_cast<KTextEditor::ViewCursorInterface*>(view);
360
 
    if (!cursoriface) {
361
 
        kdDebug(9028) << "no viewcursoriface" << endl;
362
 
        return;
363
 
    }
364
 
 
365
 
    QString word = currentWord();
366
 
    kdDebug(9028) << "Expanding word " << word << " with suffix " << suffix << "." << endl;
367
 
 
368
 
    QMap<QString, CodeTemplate*> m = m_templates[suffix];
369
 
    for (QMap<QString, CodeTemplate*>::const_iterator it = m.begin(); it != m.end() ; ++it) {
370
 
        if (it.key() != word)
371
 
            continue;
372
 
 
373
 
        uint line, col;
374
 
        cursoriface->cursorPositionReal(&line, &col);
375
 
 
376
 
        QString linestr = editIface->textLine(line);
377
 
        int startPos = QMAX( QMIN( (int)col, (int)linestr.length()-1 ), 0 );
378
 
        int endPos = startPos;
379
 
        startPos--;
380
 
        while (startPos >= 0 && ( linestr[startPos].isLetterOrNumber() || linestr[startPos] == '_' || linestr[startPos] == '~') )
381
 
            startPos--;
382
 
        while (endPos < (int)linestr.length() && ( linestr[endPos].isLetterOrNumber() || linestr[endPos] == '_' ) )
383
 
            endPos++;
384
 
 
385
 
        editiface->removeText( line, startPos+1, line, endPos );
386
 
        insertChars(it.data()->code );
387
 
    }
388
 
}
389
 
 
390
 
 
391
 
void AbbrevPart::insertChars( const QString &chars )
392
 
{
393
 
    unsigned line=0, col=0;
394
 
    viewCursorIface->cursorPositionReal( &line, &col );
395
 
 
396
 
    unsigned int currentLine=line, currentCol=col;
397
 
 
398
 
    QString spaces;
399
 
    QString s = editIface->textLine( currentLine );
400
 
    uint i=0;
401
 
    while( i<s.length() && s[ i ].isSpace() ){
402
 
        spaces += s[ i ];
403
 
        ++i;
404
 
    }
405
 
 
406
 
    bool foundPipe = false;
407
 
    QString str;
408
 
    QTextStream stream( &str, IO_WriteOnly );
409
 
    QStringList lines = QStringList::split( "\n", chars );
410
 
    QStringList::Iterator it = lines.begin();
411
 
    line = currentLine;
412
 
    while( it != lines.end() ){
413
 
        QString lineText = *it;
414
 
        if( it != lines.begin() ){
415
 
            stream << spaces;
416
 
            if( !foundPipe )
417
 
                currentCol += spaces.length();
418
 
        }
419
 
 
420
 
        int idx = lineText.find( '|' );
421
 
        if( idx != -1 ){
422
 
            stream << lineText.left( idx ) << lineText.mid( idx+1 );
423
 
            if( !foundPipe ){
424
 
                foundPipe = true;
425
 
                currentCol += lineText.left( idx ).length();
426
 
                kdDebug(9007) << "found pipe at " << currentLine << ", " << currentCol << endl;
427
 
            }
428
 
        } else {
429
 
            stream << lineText;
430
 
        }
431
 
 
432
 
        ++it;
433
 
 
434
 
        if( it != lines.end() ){
435
 
            stream << "\n";
436
 
            if( !foundPipe ){
437
 
                ++currentLine;
438
 
                currentCol = 0;
439
 
            }
440
 
        }
441
 
    }
442
 
    editIface->insertText( line, col, str );
443
 
    kdDebug(9007) << "go to " << currentLine << ", " << currentCol << endl;
444
 
    viewCursorIface->setCursorPositionReal( currentLine, currentCol );
445
 
}
446
 
 
447
 
void AbbrevPart::addTemplate( const QString& templ,
448
 
                              const QString& descr,
449
 
                              const QString& suffixes,
450
 
                              const QString& code)
451
 
{
452
 
    m_templates.insert(templ, descr, code, suffixes);
453
 
}
454
 
 
455
 
 
456
 
void AbbrevPart::removeTemplate( const QString &suffixes, const QString &name )
457
 
{
458
 
    m_templates.remove( suffixes, name );
459
 
}
460
 
 
461
 
 
462
 
void AbbrevPart::clearTemplates()
463
 
{
464
 
    m_templates.clear();
465
 
}
466
 
 
467
 
CodeTemplateList AbbrevPart::templates() const
468
 
{
469
 
    return m_templates;
470
 
}
471
 
 
472
 
void AbbrevPart::slotActivePartChanged( KParts::Part* part )
473
 
{
474
 
    kdDebug(9028) << "AbbrevPart::slotActivePartChanged()" << endl;
475
 
    KTextEditor::Document* doc = dynamic_cast<KTextEditor::Document*>( part );
476
 
 
477
 
    if( !doc || !part->widget() || doc == docIface  )
478
 
    {
479
 
        actionCollection()->action( "edit_expandtext" )->setEnabled( false );
480
 
        actionCollection()->action( "edit_expandabbrev" )->setEnabled( false );
481
 
        return;
482
 
    }
483
 
 
484
 
    docIface = doc;
485
 
 
486
 
    if( !docIface ){
487
 
        docIface = 0;
488
 
        editIface = 0;
489
 
        viewCursorIface = 0;
490
 
        completionIface = 0;
491
 
    }
492
 
 
493
 
    editIface = dynamic_cast<KTextEditor::EditInterface*>( part );
494
 
    viewCursorIface = dynamic_cast<KTextEditor::ViewCursorInterface*>( part->widget() );
495
 
    completionIface = dynamic_cast<KTextEditor::CodeCompletionInterface*>( part->widget() );
496
 
 
497
 
    updateActions();
498
 
 
499
 
    if( !editIface || !viewCursorIface || !completionIface )
500
 
        return;
501
 
 
502
 
    disconnect( part->widget(), 0, this, 0 );
503
 
    disconnect( doc, 0, this, 0 );
504
 
 
505
 
    connect( part->widget(), SIGNAL(filterInsertString(KTextEditor::CompletionEntry*, QString*)),
506
 
             this, SLOT(slotFilterInsertString(KTextEditor::CompletionEntry*, QString*)) );
507
 
 
508
 
    if( autoWordCompletionEnabled() ){
509
 
        connect( part->widget(), SIGNAL(completionAborted()), this, SLOT(slotCompletionAborted()) );
510
 
        connect( part->widget(), SIGNAL(completionDone()), this, SLOT(slotCompletionDone()) );
511
 
        connect( part->widget(), SIGNAL(aboutToShowCompletionBox()), this, SLOT(slotAboutToShowCompletionBox()) );
512
 
        connect( doc, SIGNAL(textChanged()), this, SLOT(slotTextChanged()) );
513
 
    }
514
 
 
515
 
    m_prevLine = -1;
516
 
    m_prevColumn = -1;
517
 
    m_sequenceLength = 0;
518
 
    kdDebug(9028) << "AbbrevPart::slotActivePartChanged() -- OK" << endl;
519
 
}
520
 
 
521
 
void AbbrevPart::slotTextChanged()
522
 
{
523
 
    if( m_inCompletion )
524
 
        return;
525
 
 
526
 
    unsigned int line, col;
527
 
    viewCursorIface->cursorPositionReal( &line, &col );
528
 
 
529
 
    if( m_prevLine != int(line) || m_prevColumn+1 != int(col) || col == 0 ){
530
 
        m_prevLine = line;
531
 
        m_prevColumn = col;
532
 
        m_sequenceLength = 1;
533
 
        return;
534
 
    }
535
 
 
536
 
    QString textLine = editIface->textLine( line );
537
 
    QChar ch = textLine[ col-1 ];
538
 
    QChar currentChar = textLine[ col ];
539
 
 
540
 
    if( currentChar.isLetterOrNumber() || currentChar == QChar('_') || !(ch.isLetterOrNumber() || ch == QChar('_')) ){
541
 
        // reset
542
 
        m_prevLine = -1;
543
 
        return;
544
 
    }
545
 
 
546
 
    if( m_sequenceLength >= 3 )
547
 
        slotExpandText();
548
 
 
549
 
    ++m_sequenceLength;
550
 
    m_prevLine = line;
551
 
    m_prevColumn = col;
552
 
}
553
 
 
554
 
void AbbrevPart::slotFilterInsertString( KTextEditor::CompletionEntry* entry, QString* text )
555
 
{
556
 
    kdDebug(9028) << "AbbrevPart::slotFilterInsertString()" << endl;
557
 
    KParts::ReadWritePart *part = dynamic_cast<KParts::ReadWritePart*>(partController()->activePart());
558
 
    QWidget *view = partController()->activeWidget();
559
 
    if (!part || !view) {
560
 
        kdDebug(9028) << "no rw part" << endl;
561
 
        return;
562
 
    }
563
 
 
564
 
    QString suffix = part->url().url();
565
 
    int pos = suffix.findRev('.');
566
 
    if (pos != -1)
567
 
        suffix.remove(0, pos+1);
568
 
    kdDebug(9028) << "AbbrevPart::slotFilterInsertString with suffix " << suffix << endl;
569
 
 
570
 
    if( !entry || !text || !viewCursorIface || !editIface )
571
 
        return;
572
 
 
573
 
    QString expand( " <abbrev>" );
574
 
    if( !entry->userdata.isNull() && entry->text.endsWith(expand) ){
575
 
        QString macro = entry->text.left( entry->text.length() - expand.length() );
576
 
        *text = "";
577
 
        uint line, col;
578
 
        viewCursorIface->cursorPositionReal( &line, &col );
579
 
        editIface->removeText( line, col-currentWord().length(), line, col );
580
 
        insertChars( m_templates[suffix][entry->userdata]->code );
581
 
    }
582
 
}
583
 
 
584
 
void AbbrevPart::updateActions()
585
 
{
586
 
    actionCollection()->action( "edit_expandtext" )->setEnabled( docIface != 0 );
587
 
    actionCollection()->action( "edit_expandabbrev" )->setEnabled( docIface != 0 );
588
 
}
589
 
 
590
 
void AbbrevPart::slotCompletionAborted()
591
 
{
592
 
    kdDebug(9028) << "AbbrevPart::slotCompletionAborted()" << endl;
593
 
    m_inCompletion = false;
594
 
}
595
 
 
596
 
void AbbrevPart::slotCompletionDone()
597
 
{
598
 
    kdDebug(9028) << "AbbrevPart::slotCompletionDone()" << endl;
599
 
    m_inCompletion = false;
600
 
}
601
 
 
602
 
void AbbrevPart::slotAboutToShowCompletionBox()
603
 
{
604
 
    kdDebug(9028) << "AbbrevPart::slotAboutToShowCompletionBox()" << endl;
605
 
    m_inCompletion = true;
606
 
}
607
 
 
608
 
CodeTemplateList::CodeTemplateList( )
609
 
{
610
 
    allCodeTemplates.setAutoDelete(true);
611
 
}
612
 
 
613
 
CodeTemplateList::~ CodeTemplateList( )
614
 
{
615
 
}
616
 
 
617
 
QMap< QString, CodeTemplate * > CodeTemplateList::operator [ ]( QString suffix )
618
 
{
619
 
    kdDebug(9028) << "CodeTemplateList::operator []" << endl;
620
 
    QMap< QString, CodeTemplate * > selectedTemplates;
621
 
    for (QMap<QString, QMap<QString, CodeTemplate* > >::const_iterator it = templates.begin(); it != templates.end(); ++it)
622
 
    {
623
 
        kdDebug(9028) << "CodeTemplateList::operator [] - suffixes " << it.key() << endl;
624
 
        if (QStringList::split(",", it.key()).contains(suffix))
625
 
        {
626
 
            kdDebug(9028) << "CodeTemplateList::operator [] - suffixes " << it.key() << " contains " << suffix << endl;
627
 
 
628
 
            QMap<QString, CodeTemplate* > m = it.data();
629
 
            for (QMap<QString, CodeTemplate* >::const_iterator itt = m.begin(); itt != m.end(); ++itt)
630
 
            {
631
 
                kdDebug(9028) << "x" << endl;
632
 
                selectedTemplates[itt.key()] = itt.data();
633
 
            }
634
 
        }
635
 
    }
636
 
    return selectedTemplates;
637
 
}
638
 
 
639
 
void CodeTemplateList::insert( QString name, QString description, QString code, QString suffixes )
640
 
{
641
 
    QString origSuffixes = suffixes;
642
 
//    QStringList suffixList;
643
 
    int pos = suffixes.find('(');
644
 
    if (pos == -1)
645
 
        return;
646
 
    suffixes.remove(0, pos+1);
647
 
    pos = suffixes.find(')');
648
 
    if (pos == -1)
649
 
        return;
650
 
    suffixes.remove(pos, suffixes.length()-pos);
651
 
//    suffixList = QStringList::split(",", suffixes);
652
 
 
653
 
    CodeTemplate *t;
654
 
    if (templates.contains(suffixes) && templates[suffixes].contains(name))
655
 
    {
656
 
        kdDebug(9028) << "found template for suffixes " << suffixes << " and name " << name << endl;
657
 
        t = templates[suffixes][name];
658
 
    }
659
 
    else
660
 
    {
661
 
        kdDebug(9028) << "creating template for suffixes " << suffixes << " and name " << name << endl;
662
 
        t = new CodeTemplate();
663
 
        allCodeTemplates.append(t);
664
 
        templates[suffixes][name] = t;
665
 
    }
666
 
    t->name = name;
667
 
    t->description = description;
668
 
    t->code = code;
669
 
    t->suffixes = origSuffixes;
670
 
    if (!m_suffixes.contains(origSuffixes))
671
 
        m_suffixes.append(origSuffixes);
672
 
}
673
 
 
674
 
QPtrList< CodeTemplate > CodeTemplateList::allTemplates( ) const
675
 
{
676
 
    return allCodeTemplates;
677
 
}
678
 
 
679
 
void CodeTemplateList::remove( const QString & suffixes, const QString & name )
680
 
{
681
 
    allCodeTemplates.remove(templates[suffixes][name]);
682
 
    templates[suffixes].remove(name);
683
 
}
684
 
 
685
 
void CodeTemplateList::clear( )
686
 
{
687
 
    templates.clear();
688
 
    allCodeTemplates.clear();
689
 
}
690
 
 
691
 
QStringList CodeTemplateList::suffixes( )
692
 
{
693
 
    return m_suffixes;
694
 
}
695
 
 
696
 
#include "abbrevpart.moc"