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

« back to all changes in this revision

Viewing changes to formatters/indent_plugin.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
/* This file is part of KDevelop
 
2
*  Copyright (C) 2008 Cédric Pasteur <cedric.pasteur@free.fr>
 
3
 
 
4
   This program is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2 of the License, or (at your option) any later version.
 
8
 
 
9
   This program is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
    General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with this program; see the file COPYING.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
   Boston, MA 02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "indent_plugin.h"
 
21
 
 
22
#include <KPluginLoader>
 
23
#include <KPluginFactory>
 
24
#include <KAboutData>
 
25
#include <QTextStream>
 
26
#include <KDebug>
 
27
#include <KProcess>
 
28
#include <interfaces/icore.h>
 
29
#include <interfaces/isourceformattercontroller.h>
 
30
 
 
31
using namespace KDevelop;
 
32
 
 
33
K_PLUGIN_FACTORY(IndentFactory, registerPlugin<IndentPlugin>();)
 
34
K_EXPORT_PLUGIN(IndentFactory(KAboutData("kdevindent","kdevformatters", ki18n("Indent Formatter"), "0.1", ki18n("A formatter using indent"), KAboutData::License_GPL)))
 
35
 
 
36
IndentPlugin::IndentPlugin(QObject *parent, const QVariantList&)
 
37
                : IPlugin(IndentFactory::componentData(), parent)
 
38
{
 
39
        KDEV_USE_EXTENSION_INTERFACE(ISourceFormatter)
 
40
        m_currentStyle = predefinedStyles().at(0);
 
41
}
 
42
 
 
43
IndentPlugin::~IndentPlugin()
 
44
{
 
45
}
 
46
 
 
47
QString IndentPlugin::name()
 
48
{
 
49
        // This needs to match the X-KDE-PluginInfo-Name entry from the .desktop file!
 
50
        return "kdevindent";
 
51
}
 
52
 
 
53
QString IndentPlugin::caption()
 
54
{
 
55
        return "GNU Indent";
 
56
}
 
57
 
 
58
QString IndentPlugin::description()
 
59
{
 
60
        // check if indent is installed
 
61
        KProcess proc;
 
62
        QStringList args;
 
63
        args << "--version";
 
64
        proc.setProgram("indent", args);
 
65
        int res = proc.execute();
 
66
 
 
67
        if(res >= 0) //indent was found
 
68
                return i18n("<b>Indent and Format C Program Source.</b><br />"
 
69
                "The `indent' program can be used to make code easier to read."
 
70
                " It can also convert from one style of writing C to another.<br />"
 
71
                "<b>indent</b> understands a substantial amount about the syntax of C,"
 
72
                " but it also attempts to cope with incomplete and misformed syntax.<br />"
 
73
                "Home Page: <a href=\"http://www.gnu.org/software/indent/\">"
 
74
                "http://www.gnu.org/software/indent/</a>");
 
75
        else
 
76
                return ISourceFormatter::missingExecutableMessage("indent");
 
77
}
 
78
 
 
79
QString IndentPlugin::highlightModeForMime(const KMimeType::Ptr &mime)
 
80
{
 
81
        Q_UNUSED(mime);
 
82
        return "C++";
 
83
}
 
84
 
 
85
QString IndentPlugin::formatSourceWithStyle(SourceFormatterStyle style, const QString& text, const KMimeType::Ptr& mime, const QString& leftContext, const QString& rightContext)
 
86
{
 
87
 
 
88
        if (style.content().isEmpty()) {
 
89
                m_options.clear();
 
90
                if(style.name() == "KR")
 
91
                        m_options << "-kr";
 
92
                else if(style.name() == "orig")
 
93
                        m_options << "-orig";
 
94
        }
 
95
        else
 
96
                m_options = style.content().split(' ');
 
97
        KProcess proc;
 
98
        QTextStream ios(&proc);
 
99
        proc.setProgram("indent", m_options);
 
100
        proc.setOutputChannelMode(KProcess::MergedChannels);
 
101
        
 
102
        proc.start();
 
103
        if(!proc.waitForStarted()) {
 
104
                kDebug() << "Unable to start indent" << endl;
 
105
                return text;
 
106
        }
 
107
        
 
108
        QString useText = text;
 
109
        //We can only respect the context if it is in a separate line
 
110
        if(leftContext.endsWith("\n"))
 
111
                useText = leftContext + useText;
 
112
        
 
113
        if(rightContext.startsWith("\n"))
 
114
                useText = useText + rightContext;
 
115
        
 
116
        proc.write(useText.toLocal8Bit());
 
117
        proc.closeWriteChannel();
 
118
        if(!proc.waitForFinished()) {
 
119
                kDebug() << "Process doesn't finish" << endl;
 
120
                return text;
 
121
        }
 
122
        ///@todo Be able to detect errors in formatting, since indent returns the error as the output
 
123
        
 
124
        QStringList output = ios.readAll().split("\n");
 
125
        
 
126
        ///@todo Correctly remove the contexts, if the context originally had a different ammount of lines
 
127
        ///as it does after formatting, this will chop off the incorrect text portion
 
128
        //Remove right context
 
129
        if(rightContext.startsWith("\n"))
 
130
                output = output.mid(0, (leftContext + text).split("\n").count());
 
131
        
 
132
        //Remove left context
 
133
        if(leftContext.endsWith("\n"))
 
134
                output = output.mid(leftContext.split("\n").count());
 
135
        
 
136
        return output.join("\n");
 
137
}
 
138
 
 
139
QString IndentPlugin::formatSource(const QString& text, const KMimeType::Ptr& mime, const QString& leftContext, const QString& rightContext)
 
140
{
 
141
        return formatSourceWithStyle( KDevelop::ICore::self()->sourceFormatterController()->styleForMimeType( mime ), text, mime, leftContext, rightContext );
 
142
}
 
143
 
 
144
QList<KDevelop::SourceFormatterStyle> IndentPlugin::predefinedStyles()
 
145
{
 
146
        QList<KDevelop::SourceFormatterStyle> styles;
 
147
        KDevelop::SourceFormatterStyle st = KDevelop::SourceFormatterStyle( "GNU" );
 
148
        st.setCaption( "GNU" );
 
149
        styles << st;
 
150
        st = KDevelop::SourceFormatterStyle( "KR" );
 
151
        st.setCaption( "Kernighan & Ritchie" );
 
152
        styles << st;
 
153
        st = KDevelop::SourceFormatterStyle( "orig" );
 
154
        st.setCaption( "Original Berkeley indent style" );
 
155
        styles << st;
 
156
        return styles;
 
157
}
 
158
 
 
159
KDevelop::SettingsWidget* IndentPlugin::editStyleWidget(const KMimeType::Ptr &mime)
 
160
{
 
161
        Q_UNUSED(mime);
 
162
//      return new IndentPreferences();
 
163
        return 0;
 
164
}
 
165
 
 
166
QString IndentPlugin::previewText(const KMimeType::Ptr &)
 
167
{
 
168
        return "int foo (){puts(\"Hi\");}\n/* The procedure bar is even less interesting.  */\n"
 
169
        "char * bar () { puts(\"Hello\");}";
 
170
}
 
171
 
 
172
ISourceFormatter::IndentationType IndentPlugin::indentationType()
 
173
{
 
174
        if(m_options.contains("-nut"))
 
175
                return ISourceFormatter::IndentWithSpaces;
 
176
        else
 
177
                return ISourceFormatter::IndentWithTabs;
 
178
}
 
179
 
 
180
int IndentPlugin::indentationLength()
 
181
{
 
182
        int idx = m_options.indexOf("^-i\\d+");
 
183
        if(idx < 0)
 
184
                return 2;
 
185
        return m_options[idx].mid(2).toInt();
 
186
}
 
187
 
 
188
#include "indent_plugin.moc"
 
189
 
 
190
// kate: indent-mode cstyle; space-indent off; tab-width 4;