forked from qt-creator/qt-creator
KeyWordsCompletion: Simplify code
Make the perform method stand alone by removing various members of the class and inlining various functions. Change-Id: I57cc561f011aa813fbf548837ef0834fdb517118 Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
committed by
Orgad Shaneh
parent
36c8227caf
commit
c7c61ce633
@@ -168,36 +168,52 @@ KeywordsCompletionAssistProcessor::KeywordsCompletionAssistProcessor(Keywords ke
|
|||||||
|
|
||||||
IAssistProposal *KeywordsCompletionAssistProcessor::perform(const AssistInterface *interface)
|
IAssistProposal *KeywordsCompletionAssistProcessor::perform(const AssistInterface *interface)
|
||||||
{
|
{
|
||||||
m_interface.reset(interface);
|
QScopedPointer<const AssistInterface> assistInterface(interface);
|
||||||
|
if (isInComment(interface))
|
||||||
if (isInComment())
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
if (interface->reason() == IdleEditor && !acceptsIdleEditor())
|
int pos = interface->position();
|
||||||
|
|
||||||
|
// Find start position
|
||||||
|
QChar chr = interface->characterAt(pos - 1);
|
||||||
|
if (chr == '(')
|
||||||
|
--pos;
|
||||||
|
// Skip to the start of a name
|
||||||
|
do {
|
||||||
|
chr = interface->characterAt(--pos);
|
||||||
|
} while (chr.isLetterOrNumber() || chr == '_');
|
||||||
|
|
||||||
|
++pos;
|
||||||
|
|
||||||
|
int startPosition = pos;
|
||||||
|
|
||||||
|
if (interface->reason() == IdleEditor) {
|
||||||
|
QChar characterUnderCursor = interface->characterAt(interface->position());
|
||||||
|
if (characterUnderCursor.isLetterOrNumber())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
if (interface->position() - startPosition < 3)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (m_startPosition == -1)
|
// extract word
|
||||||
m_startPosition = findStartOfName();
|
QString word;
|
||||||
|
do {
|
||||||
|
word += interface->characterAt(pos);
|
||||||
|
chr = interface->characterAt(++pos);
|
||||||
|
} while ((chr.isLetterOrNumber() || chr == '_') && chr != '(');
|
||||||
|
|
||||||
int nextCharPos = m_startPosition + m_word.length();
|
if (m_keywords.isFunction(word) && interface->characterAt(pos) == '(') {
|
||||||
if (m_keywords.isFunction(m_word)
|
QStringList functionSymbols = m_keywords.argsForFunction(word);
|
||||||
&& m_interface->characterAt(nextCharPos) == QLatin1Char('(')) {
|
|
||||||
QStringList functionSymbols = m_keywords.argsForFunction(m_word);
|
|
||||||
IFunctionHintProposalModel *model = new KeywordsFunctionHintModel(functionSymbols);
|
IFunctionHintProposalModel *model = new KeywordsFunctionHintModel(functionSymbols);
|
||||||
return new FunctionHintProposal(m_startPosition, model);
|
return new FunctionHintProposal(startPosition, model);
|
||||||
} else {
|
} else {
|
||||||
QList<AssistProposalItemInterface *> items = m_snippetCollector.collect();
|
QList<AssistProposalItemInterface *> items = m_snippetCollector.collect();
|
||||||
items.append(generateProposalList(m_keywords.variables(), m_variableIcon));
|
items.append(generateProposalList(m_keywords.variables(), m_variableIcon));
|
||||||
items.append(generateProposalList(m_keywords.variables(), m_variableIcon));
|
items.append(generateProposalList(m_keywords.variables(), m_variableIcon));
|
||||||
return new GenericProposal(m_startPosition, items);
|
return new GenericProposal(startPosition, items);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QChar KeywordsCompletionAssistProcessor::startOfCommentChar() const
|
|
||||||
{
|
|
||||||
return QLatin1Char('#');
|
|
||||||
}
|
|
||||||
|
|
||||||
void KeywordsCompletionAssistProcessor::setSnippetGroup(const QString &id)
|
void KeywordsCompletionAssistProcessor::setSnippetGroup(const QString &id)
|
||||||
{
|
{
|
||||||
m_snippetCollector.setGroupId(id);
|
m_snippetCollector.setGroupId(id);
|
||||||
@@ -208,49 +224,12 @@ void KeywordsCompletionAssistProcessor::setKeywords(Keywords keywords)
|
|||||||
m_keywords = keywords;
|
m_keywords = keywords;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool KeywordsCompletionAssistProcessor::acceptsIdleEditor()
|
bool KeywordsCompletionAssistProcessor::isInComment(const AssistInterface *interface) const
|
||||||
{
|
{
|
||||||
const int pos = m_interface->position();
|
QTextCursor tc(interface->textDocument());
|
||||||
const QChar characterUnderCursor = m_interface->characterAt(pos);
|
tc.setPosition(interface->position());
|
||||||
if (!characterUnderCursor.isLetterOrNumber()) {
|
|
||||||
m_startPosition = findStartOfName();
|
|
||||||
if (pos - m_startPosition >= 3 && !isInComment())
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int KeywordsCompletionAssistProcessor::findStartOfName(int pos)
|
|
||||||
{
|
|
||||||
if (pos == -1)
|
|
||||||
pos = m_interface->position();
|
|
||||||
|
|
||||||
QChar chr = m_interface->characterAt(pos-1);
|
|
||||||
if (chr == QLatin1Char('('))
|
|
||||||
--pos;
|
|
||||||
// Skip to the start of a name
|
|
||||||
do {
|
|
||||||
chr = m_interface->characterAt(--pos);
|
|
||||||
} while (chr.isLetterOrNumber() || chr == QLatin1Char('_'));
|
|
||||||
|
|
||||||
const int start = ++pos;
|
|
||||||
m_word.clear();
|
|
||||||
do {
|
|
||||||
m_word += m_interface->characterAt(pos);
|
|
||||||
chr = m_interface->characterAt(++pos);
|
|
||||||
} while ((chr.isLetterOrNumber() || chr == QLatin1Char('_'))
|
|
||||||
&& chr != QLatin1Char('('));
|
|
||||||
|
|
||||||
return start;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool KeywordsCompletionAssistProcessor::isInComment() const
|
|
||||||
{
|
|
||||||
QTextCursor tc(m_interface->textDocument());
|
|
||||||
tc.setPosition(m_interface->position());
|
|
||||||
tc.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
|
tc.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
|
||||||
const QString &lineBeginning = tc.selectedText();
|
return tc.selectedText().contains('#');
|
||||||
return lineBeginning.contains(startOfCommentChar());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<AssistProposalItemInterface *>
|
QList<AssistProposalItemInterface *>
|
||||||
|
@@ -86,7 +86,6 @@ public:
|
|||||||
~KeywordsCompletionAssistProcessor() override = default;
|
~KeywordsCompletionAssistProcessor() override = default;
|
||||||
|
|
||||||
IAssistProposal *perform(const AssistInterface *interface) override;
|
IAssistProposal *perform(const AssistInterface *interface) override;
|
||||||
QChar startOfCommentChar() const;
|
|
||||||
|
|
||||||
void setSnippetGroup(const QString &id);
|
void setSnippetGroup(const QString &id);
|
||||||
|
|
||||||
@@ -94,15 +93,10 @@ protected:
|
|||||||
void setKeywords (Keywords keywords);
|
void setKeywords (Keywords keywords);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool acceptsIdleEditor();
|
bool isInComment(const AssistInterface *interface) const;
|
||||||
int findStartOfName(int pos = -1);
|
|
||||||
bool isInComment() const;
|
|
||||||
QList<AssistProposalItemInterface *> generateProposalList(const QStringList &words, const QIcon &icon);
|
QList<AssistProposalItemInterface *> generateProposalList(const QStringList &words, const QIcon &icon);
|
||||||
|
|
||||||
int m_startPosition = -1;
|
|
||||||
TextEditor::SnippetAssistCollector m_snippetCollector;
|
TextEditor::SnippetAssistCollector m_snippetCollector;
|
||||||
QString m_word;
|
|
||||||
QScopedPointer<const AssistInterface> m_interface;
|
|
||||||
const QIcon m_variableIcon;
|
const QIcon m_variableIcon;
|
||||||
const QIcon m_functionIcon;
|
const QIcon m_functionIcon;
|
||||||
Keywords m_keywords;
|
Keywords m_keywords;
|
||||||
|
Reference in New Issue
Block a user