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

Method insertFunctionDeclaration

plugins/clang/codegen/sourcemanipulation.cpp:195–240  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

193}
194
195bool SourceCodeInsertion::insertFunctionDeclaration(KDevelop::Declaration* declaration, const Identifier& id, const QString& body)
196{
197 if (!m_context) {
198 return false;
199 }
200
201 Signature signature;
202 const auto localDeclarations = declaration->internalContext()->localDeclarations();
203 signature.parameters.reserve(localDeclarations.count());
204 std::transform(localDeclarations.begin(), localDeclarations.end(),
205 std::back_inserter(signature.parameters),
206 [] (Declaration* argument) -> ParameterItem
207 { return {IndexedType(argument->indexedType()), argument->identifier().toString()}; });
208
209 auto funcType = declaration->type<FunctionType>();
210 auto returnType = funcType->returnType();
211 if (auto classFunDecl = dynamic_cast<const ClassFunctionDeclaration*>(declaration)) {
212 if (classFunDecl->isConstructor() || classFunDecl->isDestructor()) {
213 returnType = nullptr;
214 }
215 }
216 signature.returnType = IndexedType(returnType);
217 signature.isConst = funcType->modifiers() & AbstractType::ConstModifier;
218
219 QString decl = CodegenHelper::makeSignatureString(declaration, signature, true);
220 decl.replace(declaration->qualifiedIdentifier().toString(), id.toString());
221
222 if (body.isEmpty()) {
223 decl += QLatin1Char(';');
224 } else {
225 if (!body.startsWith(QLatin1Char(' ')) && !body.startsWith(QLatin1Char('\n'))) {
226 decl += QLatin1Char(' ');
227 }
228 decl += zeroIndentation(body);
229 }
230
231 int line = findInsertionPoint();
232
233 decl = QLatin1String("\n\n") + applySubScope(decl);
234 const auto formatter = ICore::self()->sourceFormatterController()->fileFormatter(declaration->url().toUrl());
235 if (formatter) {
236 decl = formatter->format(decl);
237 }
238
239 return m_changeSet.addChange(DocumentChange(m_context->url(), insertionRange(line), QString(), decl));
240}
241
242int SourceCodeInsertion::findInsertionPoint() const
243{

Callers 1

moveIntoSourceMethod · 0.80

Calls 15

transformFunction · 0.85
makeSignatureStringFunction · 0.85
zeroIndentationFunction · 0.85
localDeclarationsMethod · 0.80
indexedTypeMethod · 0.80
isConstructorMethod · 0.80
isDestructorMethod · 0.80
modifiersMethod · 0.80
fileFormatterMethod · 0.80
formatMethod · 0.80
addChangeMethod · 0.80

Tested by

no test coverage detected