void AStylePreferences::load(const SourceFormatterStyle &style) { if(!style.content().isEmpty()) m_formatter->loadStyle(style.content()); else m_formatter->predefinedStyle(style.name()); updateWidgets(); updatePreviewText(); }
QString CustomScriptPlugin::formatSourceWithStyle(SourceFormatterStyle style, const QString& text, const QUrl &url, const QMimeType& /*mime*/, const QString& leftContext, const QString& rightContext) { KProcess proc; QTextStream ios(&proc); std::unique_ptr<QTemporaryFile> tmpFile; if (style.content().isEmpty()) { style = predefinedStyle(style.name()); if (style.content().isEmpty()) { qWarning() << "Empty contents for style" << style.name() << "for indent plugin"; return text; } } QString useText = text; useText = leftContext + useText + rightContext; QMap<QString, QString> projectVariables; foreach(IProject* project, ICore::self()->projectController()->projects()) projectVariables[project->name()] = project->folder().toLocalFile(); QString command = style.content(); // Replace ${Project} with the project path command = replaceVariables( command, projectVariables ); command.replace("$FILE", url.toLocalFile()); if(command.contains("$TMPFILE")) { tmpFile.reset(new QTemporaryFile(QDir::tempPath() + "/code")); tmpFile->setAutoRemove(false); if(tmpFile->open()) { qCDebug(CUSTOMSCRIPT) << "using temporary file" << tmpFile->fileName(); command.replace("$TMPFILE", tmpFile->fileName()); QByteArray useTextArray = useText.toLocal8Bit(); if( tmpFile->write(useTextArray) != useTextArray.size() ) { qWarning() << "failed to write text to temporary file"; return text; } }else{ qWarning() << "Failed to create a temporary file"; return text; } tmpFile->close(); } qCDebug(CUSTOMSCRIPT) << "using shell command for indentation: " << command; proc.setShellCommand(command); proc.setOutputChannelMode(KProcess::OnlyStdoutChannel); proc.start(); if(!proc.waitForStarted()) { qCDebug(CUSTOMSCRIPT) << "Unable to start indent" << endl; return text; } if(!tmpFile.get()) proc.write(useText.toLocal8Bit()); proc.closeWriteChannel(); if(!proc.waitForFinished()) { qCDebug(CUSTOMSCRIPT) << "Process doesn't finish" << endl; return text; } QString output; if(tmpFile.get()) { QFile f(tmpFile->fileName()); if( f.open(QIODevice::ReadOnly) ) { output = QString::fromLocal8Bit(f.readAll()); }else{ qWarning() << "Failed opening the temporary file for reading"; return text; } }else{ output = ios.readAll(); } if (output.isEmpty()) { qWarning() << "indent returned empty text for style" << style.name() << style.content(); return text; } int tabWidth = 4; if((!leftContext.isEmpty() || !rightContext.isEmpty()) && (text.contains(' ') || output.contains(' '))) { // If we have to do contex-matching with tabs, determine the correct tab-width so that the context // can be matched correctly Indentation indent = indentation(url); if(indent.indentationTabWidth > 0) tabWidth = indent.indentationTabWidth; } return KDevelop::extractFormattedTextFromContext(output, text, leftContext, rightContext, tabWidth); }