MCPcopy Create free account
hub / github.com/KDE/kdevelop / formatSourceWithStyle

Method formatSourceWithStyle

plugins/customscript/customscript_plugin.cpp:103–204  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

101}
102
103QString CustomScriptPlugin::formatSourceWithStyle(const SourceFormatterStyle& style, const QString& text,
104 const QUrl& url, const QMimeType& mime, const QString& leftContext,
105 const QString& rightContext) const
106{
107 KProcess proc;
108 QTextStream ios(&proc);
109
110 std::unique_ptr<QTemporaryFile> tmpFile;
111
112 QString styleContent = style.content();
113 if (styleContent.isEmpty()) {
114 styleContent = predefinedStyle(style.name()).content();
115 if (styleContent.isEmpty()) {
116 qCWarning(CUSTOMSCRIPT) << "Empty contents for style" << style.name() << "for indent plugin";
117 return text;
118 }
119 }
120 // NOTE: from now on, only one member function of @p style may be called: name(), because only the
121 // name of an incomplete style is guaranteed to match that of the corresponding predefined style.
122
123 QString useText = text;
124 useText = leftContext + useText + rightContext;
125
126 QMap<QString, QString> projectVariables;
127 const auto projects = ICore::self()->projectController()->projects();
128 for (IProject* project : projects) {
129 projectVariables[project->name()] = project->path().toUrl().toLocalFile();
130 }
131
132 QString command = styleContent;
133
134 // Replace ${<project name>} with the project path
135 command = replaceVariables(command, projectVariables);
136 command.replace(QLatin1String("$FILE"), url.toLocalFile());
137
138 if (command.contains(QLatin1String("$TMPFILE"))) {
139 tmpFile.reset(new QTemporaryFile(QDir::tempPath() + QLatin1String("/code")));
140 if (tmpFile->open()) {
141 qCDebug(CUSTOMSCRIPT) << "using temporary file" << tmpFile->fileName();
142 command.replace(QLatin1String("$TMPFILE"), tmpFile->fileName());
143 QByteArray useTextArray = useText.toLocal8Bit();
144 if (tmpFile->write(useTextArray) != useTextArray.size()) {
145 qCWarning(CUSTOMSCRIPT) << "failed to write text to temporary file";
146 return text;
147 }
148 } else {
149 qCWarning(CUSTOMSCRIPT) << "Failed to create a temporary file";
150 return text;
151 }
152 tmpFile->close();
153 }
154
155 qCDebug(CUSTOMSCRIPT) << "using shell command for indentation: " << command;
156 proc.setShellCommand(command);
157 proc.setOutputChannelMode(KProcess::OnlyStdoutChannel);
158
159 proc.start();
160 if (!proc.waitForStarted()) {

Callers

nothing calls this directly

Calls 15

replaceVariablesFunction · 0.85
projectsMethod · 0.80
projectControllerMethod · 0.80
toLocalFileMethod · 0.80
contentMethod · 0.45
isEmptyMethod · 0.45
nameMethod · 0.45
toUrlMethod · 0.45
pathMethod · 0.45
replaceMethod · 0.45
containsMethod · 0.45
resetMethod · 0.45

Tested by

no test coverage detected