Commit 32c8bbeb authored by Igor Kushnir's avatar Igor Kushnir
Browse files

ISourceFormatter::formatSourceWithStyle: take style by reference

This should be faster than copying SourceFormatterStyle at each call of
this function.

Introduce a QString styleContent variable in
CustomScriptPlugin::formatSourceWithStyle() for these reasons:
* style is now a reference to const and cannot be assigned to;
* the name of an incomplete style matches the name of the corresponding
  predefined style, so using unmodified style.name() below is correct;
* QString content() is the only other member of style used below.

Reformat the lines touched by this change.

Don't refer to the previous name of kdevcustomscript plugin "indent" in
the warning message touched by this change.
parent 5ee9b9fe
Pipeline #96155 passed with stage
in 34 minutes and 45 seconds
......@@ -162,7 +162,7 @@ class KDEVPLATFORMINTERFACES_EXPORT ISourceFormatter
* Its a bit of a hassle that this needs to be public API, but I can't find a better way
* to do this.
*/
virtual QString formatSourceWithStyle( SourceFormatterStyle,
virtual QString formatSourceWithStyle( const SourceFormatterStyle& style,
const QString& text,
const QUrl& url,
const QMimeType &mime,
......
......@@ -232,7 +232,12 @@ QString AStylePlugin::description() const
"Home Page: <a href=\"http://astyle.sourceforge.net/\">http://astyle.sourceforge.net</a>");
}
QString AStylePlugin::formatSourceWithStyle( SourceFormatterStyle s, const QString& text, const QUrl& /*url*/, const QMimeType& mime, const QString& leftContext, const QString& rightContext ) const
QString AStylePlugin::formatSourceWithStyle(const SourceFormatterStyle& style,
const QString& text,
const QUrl& /*url*/,
const QMimeType& mime,
const QString& leftContext,
const QString& rightContext) const
{
if(mime.inherits(QStringLiteral("text/x-java")))
m_formatter->setJavaStyle();
......@@ -241,14 +246,12 @@ QString AStylePlugin::formatSourceWithStyle( SourceFormatterStyle s, const QStri
else
m_formatter->setCStyle();
if( s.content().isEmpty() )
{
m_formatter->predefinedStyle( s.name() );
} else
{
m_formatter->loadStyle( s.content() );
if (style.content().isEmpty()) {
m_formatter->predefinedStyle(style.name());
} else {
m_formatter->loadStyle(style.content());
}
return m_formatter->formatSource(text, leftContext, rightContext);
}
......
......@@ -40,11 +40,12 @@ public:
*/
KDevelop::SettingsWidget* editStyleWidget(const QMimeType& mime) const override;
QString formatSourceWithStyle(KDevelop::SourceFormatterStyle, const QString& text,
const QUrl &url,
const QMimeType& mime,
const QString& leftContext = QString(),
const QString& rightContext = QString()) const override;
QString formatSourceWithStyle(const KDevelop::SourceFormatterStyle& style,
const QString& text,
const QUrl &url,
const QMimeType& mime,
const QString& leftContext = QString(),
const QString& rightContext = QString()) const override;
/** \return The text used in the config dialog to preview the current style.
*/
......
......@@ -99,20 +99,28 @@ QString CustomScriptPlugin::usageHint() const
"to be installed. Otherwise, code will not be formatted.");
}
QString CustomScriptPlugin::formatSourceWithStyle(SourceFormatterStyle style, const QString& text, const QUrl& url, const QMimeType& /*mime*/, const QString& leftContext, const QString& rightContext) const
QString CustomScriptPlugin::formatSourceWithStyle(const SourceFormatterStyle& style,
const QString& text,
const QUrl& url,
const QMimeType& /*mime*/,
const QString& leftContext,
const QString& rightContext) const
{
KProcess proc;
QTextStream ios(&proc);
std::unique_ptr<QTemporaryFile> tmpFile;
if (style.content().isEmpty()) {
style = predefinedStyle(style.name());
if (style.content().isEmpty()) {
QString styleContent = style.content();
if (styleContent.isEmpty()) {
styleContent = predefinedStyle(style.name()).content();
if (styleContent.isEmpty()) {
qCWarning(CUSTOMSCRIPT) << "Empty contents for style" << style.name() << "for indent plugin";
return text;
}
}
// NOTE: from now on, only one member function of @p style may be called: name(), because only the
// name of an incomplete style is guaranteed to match that of the corresponding predefined style.
QString useText = text;
useText = leftContext + useText + rightContext;
......@@ -123,7 +131,7 @@ QString CustomScriptPlugin::formatSourceWithStyle(SourceFormatterStyle style, co
projectVariables[project->name()] = project->path().toUrl().toLocalFile();
}
QString command = style.content();
QString command = styleContent;
// Replace ${<project name>} with the project path
command = replaceVariables(command, projectVariables);
......@@ -180,7 +188,7 @@ QString CustomScriptPlugin::formatSourceWithStyle(SourceFormatterStyle style, co
output = ios.readAll();
}
if (output.isEmpty()) {
qCWarning(CUSTOMSCRIPT) << "indent returned empty text for style" << style.name() << style.content();
qCWarning(CUSTOMSCRIPT) << styleContent << "command returned empty text for style" << style.name();
return text;
}
......
......@@ -36,7 +36,8 @@ public:
*/
QString formatSource(const QString& text, const QUrl& url, const QMimeType& mime, const QString& leftContext, const QString& rightContext) const override;
QString formatSourceWithStyle(KDevelop::SourceFormatterStyle, const QString& text,
QString formatSourceWithStyle(const KDevelop::SourceFormatterStyle& style,
const QString& text,
const QUrl& url,
const QMimeType& mime,
const QString& leftContext = QString(),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment