forked from qt-creator/qt-creator
Cpp{Tools,Editor}: Respect multi-QChar code points when handling identifiers
* Consolidate code dealing with C++ identifiers into cpptoolsreuse.h * Handle code points that are represented with two QChars Change-Id: I4fb4435aa539f65d88598cac0b50629f33f32440 Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
#include "cppsnapshotupdater.h"
|
||||
#include "cpptoolsconstants.h"
|
||||
#include "cpptoolseditorsupport.h"
|
||||
#include "cpptoolsreuse.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
#include <cppeditor/cppeditorconstants.h>
|
||||
@@ -314,8 +315,7 @@ void CppAssistProposalItem::applyContextualContent(TextEditor::BaseTextEditor *e
|
||||
while (preserveLength > 0) {
|
||||
if (inEditor.startsWith(toInsert.right(preserveLength))
|
||||
&& (inEditorLength == preserveLength
|
||||
|| (!inEditor.at(preserveLength).isLetterOrNumber()
|
||||
&& inEditor.at(preserveLength) != QLatin1Char('_')))) {
|
||||
|| !CppTools::isValidIdentifierChar(inEditor.at(preserveLength)))) {
|
||||
break;
|
||||
}
|
||||
--preserveLength;
|
||||
@@ -671,11 +671,12 @@ bool CppCompletionAssistProcessor::accepts() const
|
||||
} else {
|
||||
// Trigger completion after three characters of a name have been typed, when not editing an existing name
|
||||
QChar characterUnderCursor = m_interface->characterAt(pos);
|
||||
if (!characterUnderCursor.isLetterOrNumber() && characterUnderCursor != QLatin1Char('_')) {
|
||||
|
||||
if (!isValidIdentifierChar(characterUnderCursor)) {
|
||||
const int startOfName = findStartOfName(pos);
|
||||
if (pos - startOfName >= 3) {
|
||||
const QChar firstCharacter = m_interface->characterAt(startOfName);
|
||||
if (firstCharacter.isLetter() || firstCharacter == QLatin1Char('_')) {
|
||||
if (isValidFirstIdentifierChar(firstCharacter)) {
|
||||
// Finally check that we're not inside a comment or string (code copied from startOfOperator)
|
||||
QTextCursor tc(m_interface->textDocument());
|
||||
tc.setPosition(pos);
|
||||
@@ -875,7 +876,7 @@ int CppCompletionAssistProcessor::findStartOfName(int pos) const
|
||||
// Skip to the start of a name
|
||||
do {
|
||||
chr = m_interface->characterAt(--pos);
|
||||
} while (chr.isLetterOrNumber() || chr == QLatin1Char('_'));
|
||||
} while (CppTools::isValidIdentifierChar(chr));
|
||||
|
||||
return pos + 1;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
|
||||
#include "cppcompletionassistprovider.h"
|
||||
|
||||
#include "cpptoolsreuse.h"
|
||||
|
||||
#include <cppeditor/cppeditorconstants.h>
|
||||
|
||||
#include <cplusplus/Token.h>
|
||||
@@ -59,6 +61,11 @@ bool CppCompletionAssistProvider::isActivationCharSequence(const QString &sequen
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CppCompletionAssistProvider::isContinuationChar(const QChar &c) const
|
||||
{
|
||||
return isValidIdentifierChar(c);
|
||||
}
|
||||
|
||||
int CppCompletionAssistProvider::activationSequenceChar(const QChar &ch,
|
||||
const QChar &ch2,
|
||||
const QChar &ch3,
|
||||
|
||||
@@ -58,6 +58,7 @@ public:
|
||||
bool supportsEditor(const Core::Id &editorId) const QTC_OVERRIDE;
|
||||
int activationCharSequenceLength() const QTC_OVERRIDE;
|
||||
bool isActivationCharSequence(const QString &sequence) const QTC_OVERRIDE;
|
||||
bool isContinuationChar(const QChar &c) const QTC_OVERRIDE;
|
||||
|
||||
virtual TextEditor::IAssistInterface *createAssistInterface(
|
||||
ProjectExplorer::Project *project, TextEditor::BaseTextEditor *editor,
|
||||
|
||||
@@ -50,7 +50,7 @@ static void moveCursorToStartOrEndOfIdentifier(QTextCursor *tc,
|
||||
return;
|
||||
|
||||
QChar ch = doc->characterAt(tc->position() - posDiff);
|
||||
while (ch.isLetterOrNumber() || ch == QLatin1Char('_')) {
|
||||
while (isValidIdentifierChar(ch)) {
|
||||
tc->movePosition(op);
|
||||
ch = doc->characterAt(tc->position() - posDiff);
|
||||
}
|
||||
@@ -111,16 +111,31 @@ bool isOwnershipRAIIType(CPlusPlus::Symbol *symbol, const LookupContext &context
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isValidAsciiIdentifierChar(const QChar &ch)
|
||||
{
|
||||
return ch.isLetterOrNumber() || ch == QLatin1Char(' ');
|
||||
}
|
||||
|
||||
bool isValidFirstIdentifierChar(const QChar &ch)
|
||||
{
|
||||
return ch.isLetter() || ch == QLatin1Char('_') || ch.isHighSurrogate() || ch.isLowSurrogate();
|
||||
}
|
||||
|
||||
bool isValidIdentifierChar(const QChar &ch)
|
||||
{
|
||||
return isValidFirstIdentifierChar(ch) || ch.isNumber();
|
||||
}
|
||||
|
||||
bool isValidIdentifier(const QString &s)
|
||||
{
|
||||
const int length = s.length();
|
||||
for (int i = 0; i < length; ++i) {
|
||||
const QChar &c = s.at(i);
|
||||
if (i == 0) {
|
||||
if (!c.isLetter() && c != QLatin1Char('_'))
|
||||
if (!isValidFirstIdentifierChar(c))
|
||||
return false;
|
||||
} else {
|
||||
if (!c.isLetterOrNumber() && c != QLatin1Char('_'))
|
||||
if (!isValidIdentifierChar(c))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
#include "cpptools_global.h"
|
||||
|
||||
QT_FORWARD_DECLARE_CLASS(QChar)
|
||||
QT_FORWARD_DECLARE_CLASS(QTextCursor)
|
||||
QT_FORWARD_DECLARE_CLASS(QStringRef)
|
||||
|
||||
@@ -48,6 +49,9 @@ void CPPTOOLS_EXPORT moveCursorToStartOfIdentifier(QTextCursor *tc);
|
||||
bool CPPTOOLS_EXPORT isOwnershipRAIIType(CPlusPlus::Symbol *symbol,
|
||||
const CPlusPlus::LookupContext &context);
|
||||
|
||||
bool CPPTOOLS_EXPORT isValidAsciiIdentifierChar(const QChar &ch);
|
||||
bool CPPTOOLS_EXPORT isValidFirstIdentifierChar(const QChar &ch);
|
||||
bool CPPTOOLS_EXPORT isValidIdentifierChar(const QChar &ch);
|
||||
bool CPPTOOLS_EXPORT isValidIdentifier(const QString &s);
|
||||
|
||||
bool CPPTOOLS_EXPORT isQtKeyword(const QStringRef &text);
|
||||
|
||||
Reference in New Issue
Block a user