qtoutputformatter.cpp 6.98 KB
Newer Older
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

dt's avatar
dt committed
30
#include "qtoutputformatter.h"
31
32

#include <texteditor/basetexteditor.h>
dt's avatar
dt committed
33
#include <qt4projectmanager/qt4project.h>
34

dt's avatar
dt committed
35
#include <QtCore/QFileInfo>
36
#include <QtCore/QUrl>
37
38
39
#include <QtGui/QPlainTextEdit>

using namespace ProjectExplorer;
dt's avatar
dt committed
40
using namespace Qt4ProjectManager;
41

dt's avatar
dt committed
42
43
QtOutputFormatter::QtOutputFormatter(Qt4Project *project)
    : OutputFormatter()
44
    , m_qmlError(QLatin1String("(file:///.+:\\d+:\\d+):"))
dt's avatar
dt committed
45
    , m_qtError(QLatin1String("Object::.*in (.*:\\d+)"))
46
    , m_qtAssert(QLatin1String("^ASSERT: .* in file (.+, line \\d+)$"))
dt's avatar
dt committed
47
    , m_project(project)
48
49
50
{
}

51
LinkResult QtOutputFormatter::matchLine(const QString &line) const
52
{
53
54
55
56
57
58
59
60
61
62
63
    LinkResult lr;
    lr.start = -1;
    lr.end = -1;
    if (m_qmlError.indexIn(line) != -1) {
        lr.href = m_qmlError.cap(1);
        lr.start = m_qmlError.pos(1);
        lr.end = lr.start + lr.href.length();
    } else if (m_qtError.indexIn(line) != -1) {
        lr.href = m_qtError.cap(1);
        lr.start = m_qtError.pos(1);
        lr.end = lr.start + lr.href.length();
64
65
66
67
    } else if (m_qtAssert.indexIn(line) != -1) {
        lr.href = m_qtAssert.cap(1);
        lr.start = m_qtAssert.pos(1);
        lr.end = lr.start + lr.href.length();
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
    return lr;
}

void QtOutputFormatter::appendApplicationOutput(const QString &txt, bool onStdErr)
{
    // Do the initialization lazily, as we don't have a plaintext edit
    // in the ctor
    if (!m_linkFormat.isValid()) {
        m_linkFormat.setForeground(plainTextEdit()->palette().link().color());
        m_linkFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline);
        m_linkFormat.setAnchor(true);
    }

    QString text = txt;
    text.remove(QLatin1Char('\r'));

    int start = 0;
    int pos = txt.indexOf(QLatin1Char('\n'));
    while (pos != -1) {
        // Line identified
        if (!m_lastLine.isEmpty()) {
            // Line continuation
            const QString newPart = txt.mid(start, pos - start + 1);
            const QString line = m_lastLine + newPart;
            LinkResult lr = matchLine(line);
            if (!lr.href.isEmpty()) {
                // Found something && line continuation
                clearLastLine();
                appendLine(lr, line, onStdErr);
            } else {
                // Found nothing, just emit the new part
                append(newPart, onStdErr ? StdErrFormat : StdOutFormat);
            }
            // Handled line continuation
            m_lastLine.clear();
        } else {
            const QString line = txt.mid(start, pos - start + 1);
            LinkResult lr = matchLine(line);
            if (!lr.href.isEmpty()) {
                appendLine(lr, line, onStdErr);
            } else {
                append(line, onStdErr ? StdErrFormat : StdOutFormat);
            }

        }
        start = pos + 1;
        pos = txt.indexOf(QLatin1Char('\n'), start);
    }

    // Handle left over stuff
    if (start < txt.length()) {
        if (!m_lastLine.isEmpty()) {
            // Line continuation
            const QString newPart = txt.mid(start);
            m_lastLine.append(newPart);
            LinkResult lr = matchLine(m_lastLine);
            if (!lr.href.isEmpty()) {
                // Found something && line continuation
                clearLastLine();
                appendLine(lr, m_lastLine, onStdErr);
            } else {
                // Found nothing, just emit the new part
                append(newPart, onStdErr ? StdErrFormat : StdOutFormat);
            }
        } else {
            m_lastLine = txt.mid(start);
            LinkResult lr = matchLine(m_lastLine);
            if (!lr.href.isEmpty()) {
                appendLine(lr, m_lastLine, onStdErr);
            } else {
                append(m_lastLine, onStdErr ? StdErrFormat : StdOutFormat);
            }
        }
    }
}

void QtOutputFormatter::appendLine(LinkResult lr, const QString &line, bool onStdErr)
{
    append(line.left(lr.start), onStdErr ? StdErrFormat : StdOutFormat);
    m_linkFormat.setAnchorHref(lr.href);
    append(line.mid(lr.start, lr.end - lr.start), m_linkFormat);
    append(line.mid(lr.end), onStdErr ? StdErrFormat : StdOutFormat);
dt's avatar
dt committed
151
}
152

dt's avatar
dt committed
153
154
void QtOutputFormatter::handleLink(const QString &href)
{
155
    if (!href.isEmpty()) {
156
        const QRegExp qmlErrorLink(QLatin1String("^(file:///.+):(\\d+):(\\d+)"));
157
158

        if (qmlErrorLink.indexIn(href) != -1) {
159
            const QString fileName = QUrl(qmlErrorLink.cap(1)).toLocalFile();
160
161
162
            const int line = qmlErrorLink.cap(2).toInt();
            const int column = qmlErrorLink.cap(3).toInt();
            TextEditor::BaseTextEditor::openEditorAt(fileName, line, column - 1);
dt's avatar
dt committed
163
            return;
164
165
        }

166
167
168
        QString fileName;
        int line = -1;

dt's avatar
dt committed
169
        QRegExp qtErrorLink(QLatin1String("^(.*):(\\d+)$"));
170
171
172
173
174
175
176
177
178
179
180
181
        if (qtErrorLink.indexIn(href) != -1) {
            fileName = qtErrorLink.cap(1);
            line = qtErrorLink.cap(2).toInt();
        }

        QRegExp qtAssertLink(QLatin1String("^(.+), line (\\d+)$"));
        if (qtAssertLink.indexIn(href) != -1) {
            fileName = qtAssertLink.cap(1);
            line = qtAssertLink.cap(2).toInt();
        }

        if (!fileName.isEmpty()) {
dt's avatar
dt committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
            QFileInfo fi(fileName);
            if (fi.isRelative()) {
                // Yeah fileName is relative, no suprise
                Qt4Project *pro = m_project.data();
                if (pro) {
                    QString baseName = fi.fileName();
                    foreach (const QString &file, pro->files(Project::AllFiles)) {
                        if (file.endsWith(baseName)) {
                            // pick the first one...
                            fileName = file;
                            break;
                        }
                    }
                }
            }
            TextEditor::BaseTextEditor::openEditorAt(fileName, line, 0);
            return;
        }
    }
201
}