1
/* This file is part of KDevelop
2
* Copyright (C) 2008 Cédric Pasteur <cedric.pasteur@free.fr>
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.
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.
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.
20
#include "indent_plugin.h"
22
#include <KPluginLoader>
23
#include <KPluginFactory>
25
#include <QTextStream>
28
#include <interfaces/icore.h>
29
#include <interfaces/isourceformattercontroller.h>
31
using namespace KDevelop;
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)))
36
IndentPlugin::IndentPlugin(QObject *parent, const QVariantList&)
37
: IPlugin(IndentFactory::componentData(), parent)
39
KDEV_USE_EXTENSION_INTERFACE(ISourceFormatter)
40
m_currentStyle = predefinedStyles().at(0);
43
IndentPlugin::~IndentPlugin()
47
QString IndentPlugin::name()
49
// This needs to match the X-KDE-PluginInfo-Name entry from the .desktop file!
53
QString IndentPlugin::caption()
58
QString IndentPlugin::description()
60
// check if indent is installed
64
proc.setProgram("indent", args);
65
int res = proc.execute();
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>");
76
return ISourceFormatter::missingExecutableMessage("indent");
79
QString IndentPlugin::highlightModeForMime(const KMimeType::Ptr &mime)
85
QString IndentPlugin::formatSourceWithStyle(SourceFormatterStyle style, const QString& text, const KMimeType::Ptr& mime, const QString& leftContext, const QString& rightContext)
88
if (style.content().isEmpty()) {
90
if(style.name() == "KR")
92
else if(style.name() == "orig")
96
m_options = style.content().split(' ');
98
QTextStream ios(&proc);
99
proc.setProgram("indent", m_options);
100
proc.setOutputChannelMode(KProcess::MergedChannels);
103
if(!proc.waitForStarted()) {
104
kDebug() << "Unable to start indent" << endl;
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;
113
if(rightContext.startsWith("\n"))
114
useText = useText + rightContext;
116
proc.write(useText.toLocal8Bit());
117
proc.closeWriteChannel();
118
if(!proc.waitForFinished()) {
119
kDebug() << "Process doesn't finish" << endl;
122
///@todo Be able to detect errors in formatting, since indent returns the error as the output
124
QStringList output = ios.readAll().split("\n");
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());
132
//Remove left context
133
if(leftContext.endsWith("\n"))
134
output = output.mid(leftContext.split("\n").count());
136
return output.join("\n");
139
QString IndentPlugin::formatSource(const QString& text, const KMimeType::Ptr& mime, const QString& leftContext, const QString& rightContext)
141
return formatSourceWithStyle( KDevelop::ICore::self()->sourceFormatterController()->styleForMimeType( mime ), text, mime, leftContext, rightContext );
144
QList<KDevelop::SourceFormatterStyle> IndentPlugin::predefinedStyles()
146
QList<KDevelop::SourceFormatterStyle> styles;
147
KDevelop::SourceFormatterStyle st = KDevelop::SourceFormatterStyle( "GNU" );
148
st.setCaption( "GNU" );
150
st = KDevelop::SourceFormatterStyle( "KR" );
151
st.setCaption( "Kernighan & Ritchie" );
153
st = KDevelop::SourceFormatterStyle( "orig" );
154
st.setCaption( "Original Berkeley indent style" );
159
KDevelop::SettingsWidget* IndentPlugin::editStyleWidget(const KMimeType::Ptr &mime)
162
// return new IndentPreferences();
166
QString IndentPlugin::previewText(const KMimeType::Ptr &)
168
return "int foo (){puts(\"Hi\");}\n/* The procedure bar is even less interesting. */\n"
169
"char * bar () { puts(\"Hello\");}";
172
ISourceFormatter::IndentationType IndentPlugin::indentationType()
174
if(m_options.contains("-nut"))
175
return ISourceFormatter::IndentWithSpaces;
177
return ISourceFormatter::IndentWithTabs;
180
int IndentPlugin::indentationLength()
182
int idx = m_options.indexOf("^-i\\d+");
185
return m_options[idx].mid(2).toInt();
188
#include "indent_plugin.moc"
190
// kate: indent-mode cstyle; space-indent off; tab-width 4;