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:
Daniel Teske
2015-10-05 15:00:45 +02:00
committed by Orgad Shaneh
parent 36c8227caf
commit c7c61ce633
2 changed files with 39 additions and 66 deletions

View File

@@ -168,36 +168,52 @@ KeywordsCompletionAssistProcessor::KeywordsCompletionAssistProcessor(Keywords ke
IAssistProposal *KeywordsCompletionAssistProcessor::perform(const AssistInterface *interface)
{
m_interface.reset(interface);
if (isInComment())
QScopedPointer<const AssistInterface> assistInterface(interface);
if (isInComment(interface))
return nullptr;
if (interface->reason() == IdleEditor && !acceptsIdleEditor())
return nullptr;
int pos = interface->position();
if (m_startPosition == -1)
m_startPosition = findStartOfName();
// 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 == '_');
int nextCharPos = m_startPosition + m_word.length();
if (m_keywords.isFunction(m_word)
&& m_interface->characterAt(nextCharPos) == QLatin1Char('(')) {
QStringList functionSymbols = m_keywords.argsForFunction(m_word);
++pos;
int startPosition = pos;
if (interface->reason() == IdleEditor) {
QChar characterUnderCursor = interface->characterAt(interface->position());
if (characterUnderCursor.isLetterOrNumber())
return nullptr;
if (interface->position() - startPosition < 3)
return 0;
}
// extract word
QString word;
do {
word += interface->characterAt(pos);
chr = interface->characterAt(++pos);
} while ((chr.isLetterOrNumber() || chr == '_') && chr != '(');
if (m_keywords.isFunction(word) && interface->characterAt(pos) == '(') {
QStringList functionSymbols = m_keywords.argsForFunction(word);
IFunctionHintProposalModel *model = new KeywordsFunctionHintModel(functionSymbols);
return new FunctionHintProposal(m_startPosition, model);
return new FunctionHintProposal(startPosition, model);
} else {
QList<AssistProposalItemInterface *> items = m_snippetCollector.collect();
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)
{
m_snippetCollector.setGroupId(id);
@@ -208,49 +224,12 @@ void KeywordsCompletionAssistProcessor::setKeywords(Keywords keywords)
m_keywords = keywords;
}
bool KeywordsCompletionAssistProcessor::acceptsIdleEditor()
bool KeywordsCompletionAssistProcessor::isInComment(const AssistInterface *interface) const
{
const int pos = m_interface->position();
const QChar characterUnderCursor = m_interface->characterAt(pos);
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());
QTextCursor tc(interface->textDocument());
tc.setPosition(interface->position());
tc.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
const QString &lineBeginning = tc.selectedText();
return lineBeginning.contains(startOfCommentChar());
return tc.selectedText().contains('#');
}
QList<AssistProposalItemInterface *>

View File

@@ -86,7 +86,6 @@ public:
~KeywordsCompletionAssistProcessor() override = default;
IAssistProposal *perform(const AssistInterface *interface) override;
QChar startOfCommentChar() const;
void setSnippetGroup(const QString &id);
@@ -94,15 +93,10 @@ protected:
void setKeywords (Keywords keywords);
private:
bool acceptsIdleEditor();
int findStartOfName(int pos = -1);
bool isInComment() const;
bool isInComment(const AssistInterface *interface) const;
QList<AssistProposalItemInterface *> generateProposalList(const QStringList &words, const QIcon &icon);
int m_startPosition = -1;
TextEditor::SnippetAssistCollector m_snippetCollector;
QString m_word;
QScopedPointer<const AssistInterface> m_interface;
const QIcon m_variableIcon;
const QIcon m_functionIcon;
Keywords m_keywords;