| 227 | } |
| 228 | |
| 229 | void execute(KTextEditor::View* view, const KTextEditor::Range& word) override |
| 230 | { |
| 231 | QString repl = m_replacement; |
| 232 | DUChainReadLocker lock; |
| 233 | |
| 234 | if(!m_declaration){ |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | if(m_declaration->isFunctionDeclaration()) { |
| 239 | const auto functionType = m_declaration->type<FunctionType>(); |
| 240 | |
| 241 | // protect against buggy code that created the m_declaration, |
| 242 | // to mark it as a function but not assign a function type |
| 243 | if (!functionType) |
| 244 | return; |
| 245 | |
| 246 | auto doc = view->document(); |
| 247 | |
| 248 | // Function pointer? |
| 249 | bool funcptr = false; |
| 250 | const auto line = doc->line(word.start().line()); |
| 251 | auto pos = word.end().column() - 1; |
| 252 | while ( pos > 0 && (line.at(pos).isLetterOrNumber() || line.at(pos) == QLatin1Char(':')) ) { |
| 253 | pos--; |
| 254 | if ( line.at(pos) == QLatin1Char('&') ) { |
| 255 | funcptr = true; |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | auto restEmpty = doc->characterAt(word.end() + KTextEditor::Cursor{0, 1}) == QChar(); |
| 261 | |
| 262 | bool didAddParentheses = false; |
| 263 | if ( !funcptr && doc->characterAt(word.end()) != QLatin1Char('(') ) { |
| 264 | repl += QLatin1String("()"); |
| 265 | didAddParentheses = true; |
| 266 | } |
| 267 | view->document()->replaceText(word, repl); |
| 268 | if (functionType->indexedArgumentsSize() && didAddParentheses) { |
| 269 | view->setCursorPosition(word.start() + KTextEditor::Cursor(0, repl.size() - 1)); |
| 270 | } |
| 271 | auto returnTypeIntegral = functionType->returnType().dynamicCast<IntegralType>(); |
| 272 | if ( restEmpty && !funcptr && returnTypeIntegral && returnTypeIntegral->dataType() == IntegralType::TypeVoid ) { |
| 273 | // function returns void and rest of line is empty -- nothing can be done with the result |
| 274 | if (functionType->indexedArgumentsSize() ) { |
| 275 | // we placed the cursor inside the () |
| 276 | view->document()->insertText(view->cursorPosition() + KTextEditor::Cursor(0, 1), QStringLiteral(";")); |
| 277 | } |
| 278 | else { |
| 279 | // we placed the cursor after the () |
| 280 | view->document()->insertText(view->cursorPosition(), QStringLiteral(";")); |
| 281 | view->setCursorPosition(view->cursorPosition() + KTextEditor::Cursor{0, 1}); |
| 282 | } |
| 283 | } |
| 284 | } else { |
| 285 | view->document()->replaceText(word, repl); |
| 286 | } |
nothing calls this directly
no test coverage detected