ClangCodeCompleteResults CodeCompleter::completeSmartPointerCreation(uint line, uint column, int funcNameStartLine, int funcNameStartColumn) { if (column <= 1 || funcNameStartLine == -1) return ClangCodeCompleteResults(); UnsavedFile &file = unsavedFiles.unsavedFile(translationUnit.filePath()); if (!file.hasCharacterAt(line, column - 1, '(')) return ClangCodeCompleteResults(); bool ok; const uint startPos = file.toUtf8Position(funcNameStartLine, funcNameStartColumn, &ok); const uint endPos = file.toUtf8Position(line, column - 1, &ok); Utf8String content = file.fileContent(); const QString oldName = content.mid(startPos, endPos - startPos); const QString updatedName = tweakName(oldName); if (updatedName.isEmpty()) return ClangCodeCompleteResults(); column += updatedName.length(); file.replaceAt(endPos + 1, 0, updatedName); ClangCodeCompleteResults results = completeHelper(line, column); if (results.isEmpty()) { column -= updatedName.length(); file.replaceAt(endPos + 1, updatedName.length(), QString()); } return results; }
CodeCompletions CodeCompleter::complete(uint line, uint column) { neededCorrection_ = CompletionCorrection::NoCorrection; ClangCodeCompleteResults results = complete(line, column, translationUnit.cxUnsavedFiles(), translationUnit.unsavedFilesCount()); if (results.hasNoResultsForDotCompletion() && hasDotAt(line, column - 1)) results = completeWithArrowInsteadOfDot(line, column); return toCodeCompletions(results); }
ClangCodeCompleteResults CodeCompleter::completeWithArrowInsteadOfDot(uint line, uint column, uint dotPosition) { ClangCodeCompleteResults results; const bool replaced = unsavedFile().replaceAt(dotPosition, 1, Utf8StringLiteral("->")); if (replaced) { results = completeHelper(line, column + 1); if (results.hasResults()) neededCorrection_ = CompletionCorrection::DotToArrowCorrection; filterUnknownContextResults(results, unsavedFile(), line, column+1); } return results; }
ClangCodeCompleteResults CodeCompleter::completeWithArrowInsteadOfDot(uint line, uint column) { ClangCodeCompleteResults results; const SourceLocation location = translationUnit.sourceLocationAtWithoutReparsing(line, column - 1); const bool replaced = translationUnit.unsavedFile().replaceAt(location.offset(), 1, Utf8StringLiteral("->")); if (replaced) { results = complete(line, column + 1, translationUnit.cxUnsavedFiles(), translationUnit.unsavedFilesCount()); if (results.hasResults()) neededCorrection_ = CompletionCorrection::DotToArrowCorrection; } return results; }
CodeCompletions CodeCompleter::complete(uint line, uint column, int funcNameStartLine, int funcNameStartColumn) { neededCorrection_ = CompletionCorrection::NoCorrection; // Check if we have a smart pointer completion and get proper constructor signatures in results. // Results are empty when it's not a smart pointer or this completion failed. ClangCodeCompleteResults results = completeSmartPointerCreation(line, column, funcNameStartLine, funcNameStartColumn); if (results.isNull() || results.isEmpty()) results = completeHelper(line, column); filterUnknownContextResults(results, unsavedFile(), line, column); tryDotArrowCorrectionIfNoResults(results, line, column); return toCodeCompletions(results); }
void CodeCompleter::tryDotArrowCorrectionIfNoResults(ClangCodeCompleteResults &results, uint line, uint column) { if (results.hasNoResultsForDotCompletion()) { const UnsavedFile &theUnsavedFile = unsavedFile(); bool positionIsOk = false; const uint dotPosition = theUnsavedFile.toUtf8Position(line, column - 1, &positionIsOk); if (positionIsOk && theUnsavedFile.hasCharacterAt(dotPosition, '.')) results = completeWithArrowInsteadOfDot(line, column, dotPosition); } }