2015-06-01 18:51:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:14 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:14 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
2016-01-15 14:57:14 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "codecompleter.h"
|
|
|
|
|
|
2018-06-15 14:35:58 +02:00
|
|
|
#include "clangbackend_global.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
#include "clangcodecompleteresults.h"
|
2018-06-15 14:35:58 +02:00
|
|
|
#include "clangdocument.h"
|
2016-09-09 14:25:59 +02:00
|
|
|
#include "clangexceptions.h"
|
2018-06-15 14:35:58 +02:00
|
|
|
#include "clangfilepath.h"
|
|
|
|
|
#include "clangstring.h"
|
|
|
|
|
#include "clangtranslationunitupdater.h"
|
|
|
|
|
#include "clangunsavedfilesshallowarguments.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
#include "codecompletionsextractor.h"
|
2018-06-15 14:35:58 +02:00
|
|
|
#include "cursor.h"
|
2016-01-11 15:20:04 +01:00
|
|
|
#include "sourcelocation.h"
|
2018-06-15 14:35:58 +02:00
|
|
|
#include "sourcerange.h"
|
2016-01-28 18:31:05 +01:00
|
|
|
#include "unsavedfile.h"
|
2016-05-30 10:25:52 +02:00
|
|
|
#include "unsavedfiles.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
#include <clang-c/Index.h>
|
|
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
namespace ClangBackEnd {
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2016-01-11 15:20:04 +01:00
|
|
|
namespace {
|
|
|
|
|
|
2018-06-15 14:35:58 +02:00
|
|
|
CodeCompletions toCodeCompletions(const TranslationUnit &translationUnit,
|
|
|
|
|
const ClangCodeCompleteResults &results)
|
2016-01-11 15:20:04 +01:00
|
|
|
{
|
|
|
|
|
if (results.isNull())
|
|
|
|
|
return CodeCompletions();
|
|
|
|
|
|
2018-06-15 14:35:58 +02:00
|
|
|
CodeCompletionsExtractor extractor(translationUnit.cxTranslationUnit(), results.data());
|
2016-01-11 15:20:04 +01:00
|
|
|
CodeCompletions codeCompletions = extractor.extractAll();
|
|
|
|
|
|
|
|
|
|
return codeCompletions;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 15:23:18 +02:00
|
|
|
void filterUnknownContextResults(ClangCodeCompleteResults &results,
|
|
|
|
|
const UnsavedFile &theUnsavedFile,
|
|
|
|
|
uint line,
|
|
|
|
|
uint column)
|
|
|
|
|
{
|
|
|
|
|
if (results.hasUnknownContext()) {
|
|
|
|
|
bool positionIsOk = false;
|
|
|
|
|
const uint position = theUnsavedFile.toUtf8Position(line, column - 1, &positionIsOk);
|
|
|
|
|
if (positionIsOk && (theUnsavedFile.hasCharacterAt(position, '.')
|
|
|
|
|
|| (theUnsavedFile.hasCharacterAt(position - 1, '-')
|
|
|
|
|
&& theUnsavedFile.hasCharacterAt(position, '>')))) {
|
|
|
|
|
results = {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 15:20:04 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
CodeCompleter::CodeCompleter(const TranslationUnit &translationUnit,
|
2016-05-30 10:25:52 +02:00
|
|
|
const UnsavedFiles &unsavedFiles)
|
2016-09-07 14:50:58 +02:00
|
|
|
: translationUnit(translationUnit)
|
2016-05-30 10:25:52 +02:00
|
|
|
, unsavedFiles(unsavedFiles)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-10 11:05:48 +02:00
|
|
|
CodeCompletions CodeCompleter::complete(uint line, uint column,
|
|
|
|
|
int funcNameStartLine,
|
|
|
|
|
int funcNameStartColumn)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-08-10 11:05:48 +02:00
|
|
|
// 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);
|
|
|
|
|
|
2017-06-02 15:23:18 +02:00
|
|
|
filterUnknownContextResults(results, unsavedFile(), line, column);
|
2016-01-11 15:20:04 +01:00
|
|
|
|
2018-06-15 14:35:58 +02:00
|
|
|
return toCodeCompletions(translationUnit, results);
|
2016-01-11 15:20:04 +01:00
|
|
|
}
|
|
|
|
|
|
2017-08-10 11:05:48 +02:00
|
|
|
// For given "make_unique<T>" / "make_shared<T>" / "QSharedPointer<T>::create" return "new T("
|
|
|
|
|
// Otherwize return empty QString
|
|
|
|
|
static QString tweakName(const QString &oldName)
|
|
|
|
|
{
|
|
|
|
|
QString fullName = oldName.trimmed();
|
|
|
|
|
if (!fullName.contains('>'))
|
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
if (!fullName.endsWith('>')) {
|
|
|
|
|
// This is the class<type>::method case - remove ::method part
|
|
|
|
|
if (!fullName.endsWith("create") || !fullName.contains("QSharedPointer"))
|
|
|
|
|
return QString();
|
|
|
|
|
fullName = fullName.mid(0, fullName.lastIndexOf(':') - 1);
|
|
|
|
|
} else if (!fullName.contains("make_unique") && !fullName.contains("make_shared")) {
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
int templateStart = fullName.indexOf('<');
|
|
|
|
|
QString templatePart = fullName.mid(templateStart + 1,
|
|
|
|
|
fullName.length() - templateStart - 2);
|
|
|
|
|
return "new " + templatePart + "(";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-30 10:25:52 +02:00
|
|
|
ClangCodeCompleteResults CodeCompleter::completeHelper(uint line, uint column)
|
2016-01-11 15:20:04 +01:00
|
|
|
{
|
2016-09-07 14:50:58 +02:00
|
|
|
const Utf8String nativeFilePath = FilePath::toNativeSeparators(translationUnit.filePath());
|
2016-05-30 10:25:52 +02:00
|
|
|
UnsavedFilesShallowArguments unsaved = unsavedFiles.shallowArguments();
|
2016-07-01 15:07:32 +02:00
|
|
|
|
2016-09-07 14:50:58 +02:00
|
|
|
return clang_codeCompleteAt(translationUnit.cxTranslationUnit(),
|
2016-07-01 15:07:32 +02:00
|
|
|
nativeFilePath.constData(),
|
2016-01-11 15:20:04 +01:00
|
|
|
line,
|
|
|
|
|
column,
|
2016-05-30 10:25:52 +02:00
|
|
|
unsaved.data(),
|
|
|
|
|
unsaved.count(),
|
2016-03-07 16:20:50 +01:00
|
|
|
defaultOptions());
|
2016-01-11 15:20:04 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 16:20:50 +01:00
|
|
|
uint CodeCompleter::defaultOptions() const
|
|
|
|
|
{
|
|
|
|
|
uint options = CXCodeComplete_IncludeMacros
|
2018-06-15 14:35:58 +02:00
|
|
|
#ifdef IS_COMPLETION_FIXITS_BACKPORTED
|
|
|
|
|
| CXCodeComplete_IncludeCompletionsWithFixIts
|
|
|
|
|
#endif
|
|
|
|
|
| CXCodeComplete_IncludeCodePatterns;
|
2016-03-07 16:20:50 +01:00
|
|
|
|
2016-06-05 19:59:23 +02:00
|
|
|
if (TranslationUnitUpdater::defaultParseOptions()
|
2016-07-15 12:30:25 +02:00
|
|
|
& CXTranslationUnit_IncludeBriefCommentsInCodeCompletion) {
|
2016-03-07 16:20:50 +01:00
|
|
|
options |= CXCodeComplete_IncludeBriefComments;
|
2016-07-15 12:30:25 +02:00
|
|
|
}
|
2016-03-07 16:20:50 +01:00
|
|
|
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-30 10:25:52 +02:00
|
|
|
UnsavedFile &CodeCompleter::unsavedFile()
|
|
|
|
|
{
|
2016-09-07 14:50:58 +02:00
|
|
|
return unsavedFiles.unsavedFile(translationUnit.filePath());
|
2016-05-30 10:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
} // namespace ClangBackEnd
|
2015-06-01 18:51:55 +02:00
|
|
|
|