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

Method renderFileTemplate

kdevplatform/language/codegen/templaterenderer.cpp:236–282  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

234}
235
236DocumentChangeSet TemplateRenderer::renderFileTemplate(const SourceFileTemplate& fileTemplate,
237 const QUrl& baseUrl,
238 const QHash<QString, QUrl>& fileUrls)
239{
240 Q_ASSERT(fileTemplate.isValid());
241
242 DocumentChangeSet changes;
243 const QDir baseDir(baseUrl.path());
244
245 static const QRegularExpression nonAlphaNumeric(QStringLiteral("\\W"));
246 for (QHash<QString, QUrl>::const_iterator it = fileUrls.constBegin(); it != fileUrls.constEnd(); ++it) {
247 QString cleanName = it.key().toLower();
248 cleanName.replace(nonAlphaNumeric, QStringLiteral("_"));
249 const QString path = it.value().toLocalFile();
250 addVariable(QLatin1String("output_file_") + cleanName, baseDir.relativeFilePath(path));
251 addVariable(QLatin1String("output_file_") + cleanName + QLatin1String("_absolute"), path);
252 }
253
254 const KArchiveDirectory* directory = fileTemplate.directory();
255 ArchiveTemplateLocation location(directory);
256 const auto outputFiles = fileTemplate.outputFiles();
257 for (const SourceFileTemplate::OutputFile& outputFile : outputFiles) {
258 const KArchiveEntry* entry = directory->entry(outputFile.fileName);
259 if (!entry) {
260 qCWarning(LANGUAGE) << "Entry" << outputFile.fileName << "is mentioned in group" << outputFile.identifier <<
261 "but is not present in the archive";
262 continue;
263 }
264
265 const auto* file = dynamic_cast<const KArchiveFile*>(entry);
266 if (!file) {
267 qCWarning(LANGUAGE) << "Entry" << entry->name() << "is not a file";
268 continue;
269 }
270
271 QUrl url = fileUrls[outputFile.identifier];
272 IndexedString document(url);
273 KTextEditor::Range range(KTextEditor::Cursor(0, 0), 0);
274
275 DocumentChange change(document, range, QString(),
276 render(QString::fromUtf8(file->data()), outputFile.identifier));
277 changes.addChange(change);
278 qCDebug(LANGUAGE) << "Added change for file" << document.str();
279 }
280
281 return changes;
282}
283
284QString TemplateRenderer::errorString() const
285{

Callers 5

generateMethod · 0.80
previewTemplateMethod · 0.80
acceptMethod · 0.80
yamlTemplateMethod · 0.80
cppTemplateMethod · 0.80

Calls 15

constBeginMethod · 0.80
constEndMethod · 0.80
toLocalFileMethod · 0.80
outputFilesMethod · 0.80
entryMethod · 0.80
addChangeMethod · 0.80
strMethod · 0.80
CursorClass · 0.70
QStringClass · 0.70
isValidMethod · 0.45
pathMethod · 0.45
keyMethod · 0.45

Tested by 2

yamlTemplateMethod · 0.64
cppTemplateMethod · 0.64