diff options
author | David Schulz <david.schulz@qt.io> | 2022-11-15 14:19:06 +0100 |
---|---|---|
committer | David Schulz <david.schulz@qt.io> | 2022-11-17 13:23:55 +0000 |
commit | 0e4b0a26d34c523463ea68b27caf69cbb89083f2 (patch) | |
tree | 51a8efe27b419a613a88b6f2497e7a701526b47a /src/plugins/glsleditor | |
parent | aa6633ca2157b686d5398e8a09f538c15089fd36 (diff) |
Editor: move ownership of assist interface to processor
This way the base class can manage the lifetime of the interface object
and it doesn't need to be done in each implementation of perform.
Change-Id: Ie1ce742e31b688a337533ee6c57d376146e25ace
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'src/plugins/glsleditor')
-rw-r--r-- | src/plugins/glsleditor/glslcompletionassist.cpp | 42 | ||||
-rw-r--r-- | src/plugins/glsleditor/glslcompletionassist.h | 3 | ||||
-rw-r--r-- | src/plugins/glsleditor/glsleditor.cpp | 20 |
3 files changed, 33 insertions, 32 deletions
diff --git a/src/plugins/glsleditor/glslcompletionassist.cpp b/src/plugins/glsleditor/glslcompletionassist.cpp index cc170b1b0e..86c3e21d75 100644 --- a/src/plugins/glsleditor/glslcompletionassist.cpp +++ b/src/plugins/glsleditor/glslcompletionassist.cpp @@ -290,17 +290,17 @@ static AssistProposalItem *createCompletionItem(const QString &text, const QIcon return item; } -IAssistProposal *GlslCompletionAssistProcessor::performAsync(AssistInterface *interface) +IAssistProposal *GlslCompletionAssistProcessor::performAsync() { - m_interface.reset(static_cast<const GlslCompletionAssistInterface *>(interface)); + auto interface = static_cast<const GlslCompletionAssistInterface *>(this->interface()); if (interface->reason() == IdleEditor && !acceptsIdleEditor()) return nullptr; - int pos = m_interface->position() - 1; - QChar ch = m_interface->characterAt(pos); + int pos = interface->position() - 1; + QChar ch = interface->characterAt(pos); while (ch.isLetterOrNumber() || ch == QLatin1Char('_')) - ch = m_interface->characterAt(--pos); + ch = interface->characterAt(--pos); CPlusPlus::ExpressionUnderCursor expressionUnderCursor( CPlusPlus::LanguageFeatures::defaultFeatures()); @@ -310,16 +310,16 @@ IAssistProposal *GlslCompletionAssistProcessor::performAsync(AssistInterface *in QStringList specialMembers; QList<AssistProposalItemInterface *> m_completions; - bool functionCall = (ch == QLatin1Char('(') && pos == m_interface->position() - 1); + bool functionCall = (ch == QLatin1Char('(') && pos == interface->position() - 1); if (ch == QLatin1Char(',')) { - QTextCursor tc(m_interface->textDocument()); + QTextCursor tc(interface->textDocument()); tc.setPosition(pos); const int start = expressionUnderCursor.startOfFunctionCall(tc); if (start == -1) return nullptr; - if (m_interface->characterAt(start) == QLatin1Char('(')) { + if (interface->characterAt(start) == QLatin1Char('(')) { pos = start; ch = QLatin1Char('('); functionCall = true; @@ -328,7 +328,7 @@ IAssistProposal *GlslCompletionAssistProcessor::performAsync(AssistInterface *in if (ch == QLatin1Char('.') || functionCall) { const bool memberCompletion = ! functionCall; - QTextCursor tc(m_interface->textDocument()); + QTextCursor tc(interface->textDocument()); tc.setPosition(pos); // get the expression under cursor @@ -337,7 +337,7 @@ IAssistProposal *GlslCompletionAssistProcessor::performAsync(AssistInterface *in // parse the expression GLSL::Engine engine; - GLSL::Parser parser(&engine, code, code.size(), languageVariant(m_interface->mimeType())); + GLSL::Parser parser(&engine, code, code.size(), languageVariant(interface->mimeType())); GLSL::ExpressionAST *expr = parser.parseExpression(); #if 0 @@ -347,7 +347,7 @@ IAssistProposal *GlslCompletionAssistProcessor::performAsync(AssistInterface *in dump(expr); #endif - if (Document::Ptr doc = m_interface->glslDocument()) { + if (Document::Ptr doc = interface->glslDocument()) { GLSL::Scope *currentScope = doc->scopeAt(pos); GLSL::Semantic sem; @@ -396,7 +396,7 @@ IAssistProposal *GlslCompletionAssistProcessor::performAsync(AssistInterface *in } else { // it's a global completion - if (Document::Ptr doc = m_interface->glslDocument()) { + if (Document::Ptr doc = interface->glslDocument()) { GLSL::Scope *currentScope = doc->scopeAt(pos); bool isGlobal = !currentScope || !currentScope->scope(); @@ -435,12 +435,12 @@ IAssistProposal *GlslCompletionAssistProcessor::performAsync(AssistInterface *in } } - // if (m_keywordVariant != languageVariant(m_interface->mimeType())) { - QStringList keywords = GLSL::Lexer::keywords(languageVariant(m_interface->mimeType())); + // if (m_keywordVariant != languageVariant(interface->mimeType())) { + QStringList keywords = GLSL::Lexer::keywords(languageVariant(interface->mimeType())); // m_keywordCompletions.clear(); for (int index = 0; index < keywords.size(); ++index) m_completions << createCompletionItem(keywords.at(index), glslIcon(IconTypeKeyword)); -// m_keywordVariant = languageVariant(m_interface->mimeType()); +// m_keywordVariant = languageVariant(interface->mimeType()); // } // m_completions += m_keywordCompletions; @@ -491,22 +491,22 @@ IAssistProposal *GlslCompletionAssistProcessor::createHintProposal( bool GlslCompletionAssistProcessor::acceptsIdleEditor() const { - const int cursorPosition = m_interface->position(); - const QChar ch = m_interface->characterAt(cursorPosition - 1); + const int cursorPosition = interface()->position(); + const QChar ch = interface()->characterAt(cursorPosition - 1); - const QChar characterUnderCursor = m_interface->characterAt(cursorPosition); + const QChar characterUnderCursor = interface()->characterAt(cursorPosition); if (isIdentifierChar(ch) && (characterUnderCursor.isSpace() || characterUnderCursor.isNull() || isDelimiter(characterUnderCursor))) { - int pos = m_interface->position() - 1; + int pos = interface()->position() - 1; for (; pos != -1; --pos) { - if (! isIdentifierChar(m_interface->characterAt(pos))) + if (! isIdentifierChar(interface()->characterAt(pos))) break; } ++pos; - const QString word = m_interface->textAt(pos, cursorPosition - pos); + const QString word = interface()->textAt(pos, cursorPosition - pos); if (word.length() >= TextEditorSettings::completionSettings().m_characterThreshold && checkStartOfIdentifier(word)) { for (auto character : word) { diff --git a/src/plugins/glsleditor/glslcompletionassist.h b/src/plugins/glsleditor/glslcompletionassist.h index ea7c4561d5..5496e7046e 100644 --- a/src/plugins/glsleditor/glslcompletionassist.h +++ b/src/plugins/glsleditor/glslcompletionassist.h @@ -72,14 +72,13 @@ class GlslCompletionAssistProcessor : public TextEditor::AsyncProcessor public: ~GlslCompletionAssistProcessor() override; - TextEditor::IAssistProposal *performAsync(TextEditor::AssistInterface *interface) override; + TextEditor::IAssistProposal *performAsync() override; private: TextEditor::IAssistProposal *createHintProposal(const QVector<GLSL::Function *> &symbols); bool acceptsIdleEditor() const; int m_startPosition = 0; - QScopedPointer<const GlslCompletionAssistInterface> m_interface; }; class GlslCompletionAssistInterface : public TextEditor::AssistInterface diff --git a/src/plugins/glsleditor/glsleditor.cpp b/src/plugins/glsleditor/glsleditor.cpp index f506831373..fa24256ecc 100644 --- a/src/plugins/glsleditor/glsleditor.cpp +++ b/src/plugins/glsleditor/glsleditor.cpp @@ -143,7 +143,8 @@ public: QSet<QString> identifiers() const; - AssistInterface *createAssistInterface(AssistKind assistKind, AssistReason reason) const override; + std::unique_ptr<AssistInterface> createAssistInterface(AssistKind assistKind, + AssistReason reason) const override; private: void updateDocumentNow(); @@ -343,16 +344,17 @@ int languageVariant(const QString &type) return variant; } -AssistInterface *GlslEditorWidget::createAssistInterface( +std::unique_ptr<AssistInterface> GlslEditorWidget::createAssistInterface( AssistKind kind, AssistReason reason) const { - if (kind == Completion) - return new GlslCompletionAssistInterface(textCursor(), - textDocument()->filePath(), - reason, - textDocument()->mimeType(), - m_glslDocument); - return TextEditorWidget::createAssistInterface(kind, reason); + if (kind != Completion) + return TextEditorWidget::createAssistInterface(kind, reason); + + return std::make_unique<GlslCompletionAssistInterface>(textCursor(), + textDocument()->filePath(), + reason, + textDocument()->mimeType(), + m_glslDocument); } |