CppEditor: Add quickfix for converting comments from C++ to C style

... and vice versa.

Fixes: QTCREATORBUG-27501
Change-Id: I8584cc1e86718b3fe0f0ead2a3436495303ca3c8
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2023-07-10 12:58:26 +02:00
parent d201899a0a
commit f93836b25d
8 changed files with 567 additions and 8 deletions

View File

@@ -16,6 +16,8 @@
#include <QTextDocument>
#include <utility>
using namespace CPlusPlus;
using namespace Utils;
@@ -138,6 +140,31 @@ bool CppRefactoringFile::isCursorOn(const AST *ast) const
return cursorBegin >= start && cursorBegin <= end;
}
QList<Token> CppRefactoringFile::tokensForCursor() const
{
QTextCursor c = cursor();
int pos = c.selectionStart();
int endPos = c.selectionEnd();
if (pos > endPos)
std::swap(pos, endPos);
const std::vector<Token> &allTokens = m_cppDocument->translationUnit()->allTokens();
const int firstIndex = tokenIndexForPosition(allTokens, pos, 0);
if (firstIndex == -1)
return {};
const int lastIndex = pos == endPos
? firstIndex
: tokenIndexForPosition(allTokens, endPos, firstIndex);
if (lastIndex == -1)
return {};
QTC_ASSERT(lastIndex >= firstIndex, return {});
QList<Token> result;
for (int i = firstIndex; i <= lastIndex; ++i)
result.push_back(allTokens.at(i));
return result;
}
ChangeSet::Range CppRefactoringFile::range(unsigned tokenIndex) const
{
const Token &token = tokenAt(tokenIndex);
@@ -215,6 +242,29 @@ void CppRefactoringFile::fileChanged()
RefactoringFile::fileChanged();
}
int CppRefactoringFile::tokenIndexForPosition(const std::vector<CPlusPlus::Token> &tokens,
int pos, int startIndex) const
{
const TranslationUnit * const tu = m_cppDocument->translationUnit();
// Binary search
for (int l = startIndex, u = int(tokens.size()) - 1; l <= u; ) {
const int i = (l + u) / 2;
const int tokenPos = tu->getTokenPositionInDocument(tokens.at(i), document());
if (pos < tokenPos) {
u = i - 1;
continue;
}
const int tokenEndPos = tu->getTokenEndPositionInDocument(tokens.at(i), document());
if (pos > tokenEndPos) {
l = i + 1;
continue;
}
return i;
}
return -1;
}
CppRefactoringChangesData::CppRefactoringChangesData(const Snapshot &snapshot)
: m_snapshot(snapshot)
, m_workingCopy(CppModelManager::workingCopy())