| 193 | } |
| 194 | |
| 195 | bool 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 | |
| 242 | int SourceCodeInsertion::findInsertionPoint() const |
| 243 | { |
no test coverage detected