summaryrefslogtreecommitdiffstats
path: root/parts/regexptest/regexptestdlg.cpp
blob: a211a7977952d6b268f6feee25be64e9d8124ea8 (plain)
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
/***************************************************************************
 *   Copyright (C) 2001 by Bernd Gehrmann                                  *
 *   bernd@kdevelop.org                                                    *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "regexptestdlg.h"

#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
#include <qlabel.h>
#include <qlistview.h>
#include <qradiobutton.h>
#include <qpushbutton.h>
#include <qregexp.h>
#include <klineedit.h>
#include <kdeversion.h>
#include <kregexp.h>
#include <kdebug.h>
#include <kglobalsettings.h>
#include <klocale.h>
#include <kparts/part.h>
#include <ktexteditor/viewcursorinterface.h>
#include <ktexteditor/editinterface.h>
#include <kparts/componentfactory.h>
#include <kregexpeditorinterface.h>

#include "kdevplugin.h"
#include "kdevpartcontroller.h"


RegexpTestDialog::RegexpTestDialog(KDevPlugin *part)
    : RegexpTestDialogBase(0, "regexp test dialog", false), _regexp_dialog(0)
{
    pattern_edit->setFocus();
    pattern_edit->setFont(KGlobalSettings::fixedFont());
    teststring_edit->setFont(KGlobalSettings::fixedFont());
    subgroups_listview->setSorting(0);

    m_part = part;
}


RegexpTestDialog::~RegexpTestDialog()
{}


void RegexpTestDialog::showEvent(QShowEvent *)
{
    KParts::ReadWritePart *rwpart = dynamic_cast<KParts::ReadWritePart*>
        (m_part->partController()->activePart());
    insertbutton->setEnabled(rwpart);
}


void RegexpTestDialog::somethingChanged()
{
    success_label->clear();
    subgroups_listview->clear();

    if ( qregexp_button->isChecked() || qregexp_min_button->isChecked() )
        checkQRegExp();
    else if ( kregexp_button->isChecked() )
	checkKRegExp();
    else
        checkPOSIX();
}

void RegexpTestDialog::checkQRegExp()
{
    QRegExp rx( pattern_edit->text() );
    rx.setMinimal( qregexp_min_button->isChecked() );
    if ( !rx.isValid() ) {
#if QT_VERSION >= 0x030100
	success_label->setText( rx.errorString() );
#else
	success_label->setText( i18n("Error compiling the regular expression.") );
#endif
	return;
    }
    if ( rx.search( teststring_edit->text() ) < 0 ) {
	success_label->setText( i18n( "No match" ) );
	return;
    }
    success_label->setText( i18n("Successfully matched") );
#if QT_VERSION >= 0x030100
    int numCaptures = rx.numCaptures() + 1;
#else
    int numCaptures = 10;
#endif
    for ( int i = 0; i < numCaptures; ++i ) {
	new QListViewItem( subgroups_listview, QString::number( i ), rx.cap( i ) );
    }
}

void RegexpTestDialog::checkKRegExp()
{
    KRegExp rx;
    if ( !rx.compile( pattern_edit->text().latin1() ) ) {
	success_label->setText( i18n( "Compile error, your regexp is invalid" ) );
	return;
    }
    if ( !rx.match( teststring_edit->text().latin1() ) ) {
        success_label->setText( i18n( "No match" ) );
        return;
    }
    success_label->setText( i18n("Successfully matched") );
    for ( int i = 0; i <= 9; ++i ) {
	const char* grp = rx.group( i );
	if ( grp )
	    new QListViewItem( subgroups_listview, QString::number( i ), QString( grp ) );
    }
}

void RegexpTestDialog::checkPOSIX()
{
    regex_t compiledPattern;
    regmatch_t matches[20];
    int cflags = extendedposix_button->isChecked()? REG_EXTENDED : 0;
    QCString regexp = pattern_edit->text().local8Bit();
    int res = regcomp(&compiledPattern, regexp, cflags);
    if (res != 0) {
        QString regcompMessage;
        switch (res)
            {
            case REG_BADRPT:
                regcompMessage = i18n("Repetition operators must not appear as first character");
                break;
            case REG_BADBR:
                regcompMessage = i18n("Invalid use of back reference operator");
                break;
            case REG_EBRACE:
                regcompMessage = i18n("Unmatched brace interval operators");
                break;
            case REG_EBRACK:
                regcompMessage = i18n("Unmatched bracket list operators");
                break;
            case REG_ERANGE:
                regcompMessage = i18n("Invalid use of range operator");
                break;
            case REG_ECTYPE:
                regcompMessage = i18n("Unknown character class");
                break;
            case REG_ECOLLATE:
                regcompMessage = i18n("Invalid collating element");
                break;
            case REG_EPAREN:
                regcompMessage = i18n("Unmatched parenthesis group operators");
                break;
            case REG_ESUBREG:
                regcompMessage = i18n("Invalid back reference to subexpression");
                break;
            case REG_EESCAPE:
                regcompMessage = i18n("Trailing backslash");
                break;
            case REG_BADPAT:
                regcompMessage = i18n("Invalid use of pattern operators");
                break;
#ifdef REG_ESIZE
            case REG_ESIZE:
                regcompMessage = i18n("Regular expression too large");
                break;
#endif
            default:
                regcompMessage = i18n("Unknown error");
                break;
            }
        success_label->setText(regcompMessage);
        return;
    }

    for (int i = 0; i < 20; ++i) {
        matches[i].rm_so = -1;
        matches[i].rm_eo = -1;
    }

    QCString testString = teststring_edit->text().local8Bit();
    res = regexec(&compiledPattern, testString, 20, matches, 0);
    if (res != 0) {
        success_label->setText(i18n("No match"));
        return;
    }

    success_label->setText(i18n("Successfully matched"));
    int len = testString.length();
    for (int i = 0; i < 20; ++i) {
        if (matches[i].rm_so >= 0 && matches[i].rm_so <= len &&
            matches[i].rm_eo >= 0 && matches[i].rm_eo <= len &&
            matches[i].rm_so <= matches[i].rm_eo) {
            QCString subGroup = testString.mid(matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so);
            new QListViewItem(subgroups_listview, QString::number(i), subGroup);
        }
    }
    regfree(&compiledPattern);
}


void RegexpTestDialog::insertQuoted()
{
    QString rawstr = pattern_edit->text();

    QString str;

    int len = rawstr.length();
    for (int i=0; i < len; ++i) {
        QChar ch = rawstr[i];
        if (ch == '"')
            str += "\\\"";
        else if (ch == '\\')
            str += "\\\\";
        else
            str += ch;
    }

    KParts::ReadWritePart *rwpart
        = dynamic_cast<KParts::ReadWritePart*>(m_part->partController()->activePart());
    QWidget *view = m_part->partController()->activeWidget();

    KTextEditor::EditInterface *editiface
        = dynamic_cast<KTextEditor::EditInterface*>(rwpart);
    if (!editiface) {
        kdDebug() << "no edit" << endl;
        return;
    }
    KTextEditor::ViewCursorInterface *cursoriface
        = dynamic_cast<KTextEditor::ViewCursorInterface*>(view);
    if (!cursoriface) {
        kdDebug() << "no viewcursor" << endl;
        return;
    }

    uint line, col;
    cursoriface->cursorPositionReal(&line, &col);
    editiface->insertText(line, col, str);
    reject();
}

void RegexpTestDialog::showRegExpEditor( )
{
    _regexp_dialog = KParts::ComponentFactory::createInstanceFromQuery<QDialog>( "KRegExpEditor/KRegExpEditor" );

    if ( _regexp_dialog )
    {
        KRegExpEditorInterface *editor =
            static_cast<KRegExpEditorInterface *>( _regexp_dialog->qt_cast( "KRegExpEditorInterface" ) );

        editor->setRegExp( pattern_edit->text() );

        if ( _regexp_dialog->exec() == QDialog::Accepted )
        {
            pattern_edit->setText( editor->regExp() );
        }
    }
}

#include "regexptestdlg.moc"