diff --git a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp index db95be93d45..0f3ffd3efe8 100644 --- a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp +++ b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp @@ -616,18 +616,13 @@ void addFixItsActionsToMenu(QMenu *menu, const TextEditor::QuickFixOperations &f } } -static int lineToPosition(const QTextDocument *textDocument, int lineNumber) -{ - QTC_ASSERT(textDocument, return 0); - const QTextBlock textBlock = textDocument->findBlockByLineNumber(lineNumber); - return textBlock.isValid() ? textBlock.position() - 1 : 0; -} - static TextEditor::AssistInterface createAssistInterface(TextEditor::TextEditorWidget *widget, int lineNumber) { - return TextEditor::AssistInterface(widget->document(), - lineToPosition(widget->document(), lineNumber), + QTextCursor cursor(widget->document()->findBlockByLineNumber(lineNumber)); + if (!cursor.atStart()) + cursor.movePosition(QTextCursor::PreviousCharacter); + return TextEditor::AssistInterface(cursor, widget->textDocument()->filePath(), TextEditor::IdleEditor); } diff --git a/src/plugins/cppeditor/cppcompletion_test.cpp b/src/plugins/cppeditor/cppcompletion_test.cpp index 683056f6b3e..d03c4b8390f 100644 --- a/src/plugins/cppeditor/cppcompletion_test.cpp +++ b/src/plugins/cppeditor/cppcompletion_test.cpp @@ -105,9 +105,12 @@ public: QStringList completions; LanguageFeatures languageFeatures = LanguageFeatures::defaultFeatures(); languageFeatures.objCEnabled = false; + QTextCursor textCursor = m_editorWidget->textCursor(); + textCursor.setPosition(m_position); + m_editorWidget->setTextCursor(textCursor); CppCompletionAssistInterface *ai = new CppCompletionAssistInterface(m_editorWidget->textDocument()->filePath(), - m_textDocument, m_position, + m_editorWidget, ExplicitlyInvoked, m_snapshot, ProjectExplorer::HeaderPaths(), languageFeatures); diff --git a/src/plugins/cppeditor/cppcompletionassist.cpp b/src/plugins/cppeditor/cppcompletionassist.cpp index 19fbab8972f..5ce93504483 100644 --- a/src/plugins/cppeditor/cppcompletionassist.cpp +++ b/src/plugins/cppeditor/cppcompletionassist.cpp @@ -426,7 +426,6 @@ AssistInterface *InternalCompletionAssistProvider::createAssistInterface( const Utils::FilePath &filePath, const TextEditorWidget *textEditorWidget, const LanguageFeatures &languageFeatures, - int position, AssistReason reason) const { QTC_ASSERT(textEditorWidget, return nullptr); @@ -435,7 +434,6 @@ AssistInterface *InternalCompletionAssistProvider::createAssistInterface( textEditorWidget, BuiltinEditorDocumentParser::get(filePath.toString()), languageFeatures, - position, reason, CppModelManager::instance()->workingCopy()); } diff --git a/src/plugins/cppeditor/cppcompletionassist.h b/src/plugins/cppeditor/cppcompletionassist.h index 1d477cff792..adcdcaa798a 100644 --- a/src/plugins/cppeditor/cppcompletionassist.h +++ b/src/plugins/cppeditor/cppcompletionassist.h @@ -87,7 +87,6 @@ public: const Utils::FilePath &filePath, const TextEditor::TextEditorWidget *textEditorWidget, const CPlusPlus::LanguageFeatures &languageFeatures, - int position, TextEditor::AssistReason reason) const override; }; @@ -168,10 +167,9 @@ public: const TextEditor::TextEditorWidget *textEditorWidget, BuiltinEditorDocumentParser::Ptr parser, const CPlusPlus::LanguageFeatures &languageFeatures, - int position, TextEditor::AssistReason reason, const WorkingCopy &workingCopy) - : TextEditor::AssistInterface(textEditorWidget->document(), position, filePath, reason) + : TextEditor::AssistInterface(textEditorWidget->textCursor(), filePath, reason) , m_parser(parser) , m_gotCppSpecifics(false) , m_workingCopy(workingCopy) @@ -179,13 +177,12 @@ public: {} CppCompletionAssistInterface(const Utils::FilePath &filePath, - QTextDocument *textDocument, - int position, + const TextEditor::TextEditorWidget *textEditorWidget, TextEditor::AssistReason reason, const CPlusPlus::Snapshot &snapshot, const ProjectExplorer::HeaderPaths &headerPaths, const CPlusPlus::LanguageFeatures &features) - : TextEditor::AssistInterface(textDocument, position, filePath, reason) + : TextEditor::AssistInterface(textEditorWidget->textCursor(), filePath, reason) , m_gotCppSpecifics(true) , m_snapshot(snapshot) , m_headerPaths(headerPaths) diff --git a/src/plugins/cppeditor/cppcompletionassistprovider.h b/src/plugins/cppeditor/cppcompletionassistprovider.h index eb9410973a2..bd851b0d02c 100644 --- a/src/plugins/cppeditor/cppcompletionassistprovider.h +++ b/src/plugins/cppeditor/cppcompletionassistprovider.h @@ -59,7 +59,6 @@ public: const Utils::FilePath &filePath, const TextEditor::TextEditorWidget *textEditorWidget, const CPlusPlus::LanguageFeatures &languageFeatures, - int position, TextEditor::AssistReason reason) const = 0; static int activationSequenceChar(const QChar &ch, const QChar &ch2, diff --git a/src/plugins/cppeditor/cppeditorwidget.cpp b/src/plugins/cppeditor/cppeditorwidget.cpp index 62da35451aa..7c00340fd5c 100644 --- a/src/plugins/cppeditor/cppeditorwidget.cpp +++ b/src/plugins/cppeditor/cppeditorwidget.cpp @@ -1312,7 +1312,6 @@ AssistInterface *CppEditorWidget::createAssistInterface(AssistKind kind, AssistR return cap->createAssistInterface(textDocument()->filePath(), this, features, - position(), reason); } else { return TextEditorWidget::createAssistInterface(kind, reason); diff --git a/src/plugins/cppeditor/cppquickfixassistant.cpp b/src/plugins/cppeditor/cppquickfixassistant.cpp index 9d5d2271a6c..5310ef7f7a7 100644 --- a/src/plugins/cppeditor/cppquickfixassistant.cpp +++ b/src/plugins/cppeditor/cppquickfixassistant.cpp @@ -74,10 +74,8 @@ IAssistProcessor *CppQuickFixAssistProvider::createProcessor(const AssistInterfa // -------------------------- // CppQuickFixAssistInterface // -------------------------- -CppQuickFixInterface::CppQuickFixInterface(CppEditorWidget *editor, - AssistReason reason) - : AssistInterface(editor->document(), editor->position(), - editor->textDocument()->filePath(), reason) +CppQuickFixInterface::CppQuickFixInterface(CppEditorWidget *editor, AssistReason reason) + : AssistInterface(editor->textCursor(), editor->textDocument()->filePath(), reason) , m_editor(editor) , m_semanticInfo(editor->semanticInfo()) , m_snapshot(CppModelManager::instance()->snapshot()) diff --git a/src/plugins/glsleditor/glslcompletionassist.cpp b/src/plugins/glsleditor/glslcompletionassist.cpp index 60ee6c8214a..853209f392d 100644 --- a/src/plugins/glsleditor/glslcompletionassist.cpp +++ b/src/plugins/glsleditor/glslcompletionassist.cpp @@ -545,13 +545,12 @@ bool GlslCompletionAssistProcessor::acceptsIdleEditor() const // ----------------------------- // GlslCompletionAssistInterface // ----------------------------- -GlslCompletionAssistInterface::GlslCompletionAssistInterface(QTextDocument *textDocument, - int position, +GlslCompletionAssistInterface::GlslCompletionAssistInterface(const QTextCursor &cursor, const Utils::FilePath &fileName, AssistReason reason, const QString &mimeType, const Document::Ptr &glslDoc) - : AssistInterface(textDocument, position, fileName, reason) + : AssistInterface(cursor, fileName, reason) , m_mimeType(mimeType) , m_glslDoc(glslDoc) { diff --git a/src/plugins/glsleditor/glslcompletionassist.h b/src/plugins/glsleditor/glslcompletionassist.h index 596ddac25ff..c54ffe6e368 100644 --- a/src/plugins/glsleditor/glslcompletionassist.h +++ b/src/plugins/glsleditor/glslcompletionassist.h @@ -107,8 +107,7 @@ private: class GlslCompletionAssistInterface : public TextEditor::AssistInterface { public: - GlslCompletionAssistInterface(QTextDocument *textDocument, - int position, const Utils::FilePath &fileName, + GlslCompletionAssistInterface(const QTextCursor &cursor, const Utils::FilePath &fileName, TextEditor::AssistReason reason, const QString &mimeType, const Document::Ptr &glslDoc); diff --git a/src/plugins/glsleditor/glsleditor.cpp b/src/plugins/glsleditor/glsleditor.cpp index 1f76e997eb2..85d4c15ae7d 100644 --- a/src/plugins/glsleditor/glsleditor.cpp +++ b/src/plugins/glsleditor/glsleditor.cpp @@ -369,8 +369,7 @@ AssistInterface *GlslEditorWidget::createAssistInterface( AssistKind kind, AssistReason reason) const { if (kind == Completion) - return new GlslCompletionAssistInterface(document(), - position(), + return new GlslCompletionAssistInterface(textCursor(), textDocument()->filePath(), reason, textDocument()->mimeType(), diff --git a/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp b/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp index 5b9c54b4d41..6cf747e9d5d 100644 --- a/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp +++ b/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp @@ -103,7 +103,7 @@ TextEditor::AssistInterface *BindingEditorWidget::createAssistInterface( { Q_UNUSED(assistKind) return new QmlJSEditor::QmlJSCompletionAssistInterface( - document(), position(), Utils::FilePath(), + textCursor(), Utils::FilePath(), assistReason, qmljsdocument->semanticInfo()); } diff --git a/src/plugins/qmljseditor/qmljscompletionassist.cpp b/src/plugins/qmljseditor/qmljscompletionassist.cpp index 5fc1e3d7041..21c23274e2e 100644 --- a/src/plugins/qmljseditor/qmljscompletionassist.cpp +++ b/src/plugins/qmljseditor/qmljscompletionassist.cpp @@ -334,10 +334,11 @@ QStringList qmlJSAutoComplete(QTextDocument *textDocument, { QStringList list; QmlJSCompletionAssistProcessor processor; + QTextCursor cursor(textDocument); + cursor.setPosition(position); QScopedPointer proposal(processor.perform( /* The processor takes ownership. */ new QmlJSCompletionAssistInterface( - textDocument, - position, + cursor, fileName, reason, info))); @@ -978,12 +979,11 @@ bool QmlJSCompletionAssistProcessor::completeUrl(const QString &relativeBasePath // ------------------------------ // QmlJSCompletionAssistInterface // ------------------------------ -QmlJSCompletionAssistInterface::QmlJSCompletionAssistInterface(QTextDocument *textDocument, - int position, +QmlJSCompletionAssistInterface::QmlJSCompletionAssistInterface(const QTextCursor &cursor, const Utils::FilePath &fileName, AssistReason reason, const SemanticInfo &info) - : AssistInterface(textDocument, position, fileName, reason) + : AssistInterface(cursor, fileName, reason) , m_semanticInfo(info) {} diff --git a/src/plugins/qmljseditor/qmljscompletionassist.h b/src/plugins/qmljseditor/qmljscompletionassist.h index 4c8a5eb53db..e9f0218c28a 100644 --- a/src/plugins/qmljseditor/qmljscompletionassist.h +++ b/src/plugins/qmljseditor/qmljscompletionassist.h @@ -105,8 +105,7 @@ private: class QMLJSEDITOR_EXPORT QmlJSCompletionAssistInterface : public TextEditor::AssistInterface { public: - QmlJSCompletionAssistInterface(QTextDocument *textDocument, - int position, + QmlJSCompletionAssistInterface(const QTextCursor &cursor, const Utils::FilePath &fileName, TextEditor::AssistReason reason, const QmlJSTools::SemanticInfo &info); diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp index 265b59ca1a7..32ff7895600 100644 --- a/src/plugins/qmljseditor/qmljseditor.cpp +++ b/src/plugins/qmljseditor/qmljseditor.cpp @@ -1021,11 +1021,8 @@ AssistInterface *QmlJSEditorWidget::createAssistInterface( AssistReason reason) const { if (assistKind == Completion) { - return new QmlJSCompletionAssistInterface(document(), - position(), - textDocument()->filePath(), - reason, - m_qmlJsEditorDocument->semanticInfo()); + return new QmlJSCompletionAssistInterface(textCursor(), textDocument()->filePath(), + reason, m_qmlJsEditorDocument->semanticInfo()); } else if (assistKind == QuickFix) { return new Internal::QmlJSQuickFixAssistInterface(const_cast(this), reason); } diff --git a/src/plugins/qmljseditor/qmljsquickfixassist.cpp b/src/plugins/qmljseditor/qmljsquickfixassist.cpp index 15feed0cfd5..482bcc34aa8 100644 --- a/src/plugins/qmljseditor/qmljsquickfixassist.cpp +++ b/src/plugins/qmljseditor/qmljsquickfixassist.cpp @@ -48,8 +48,7 @@ using namespace Internal; // ----------------------- QmlJSQuickFixAssistInterface::QmlJSQuickFixAssistInterface(QmlJSEditorWidget *editor, AssistReason reason) - : AssistInterface(editor->document(), editor->position(), - editor->textDocument()->filePath(), reason) + : AssistInterface(editor->textCursor(), editor->textDocument()->filePath(), reason) , m_semanticInfo(editor->qmlJsEditorDocument()->semanticInfo()) , m_currentFile(QmlJSRefactoringChanges::file(editor, m_semanticInfo.document)) {} diff --git a/src/plugins/texteditor/codeassist/assistinterface.cpp b/src/plugins/texteditor/codeassist/assistinterface.cpp index 0a25e1ff34f..43edc19196a 100644 --- a/src/plugins/texteditor/codeassist/assistinterface.cpp +++ b/src/plugins/texteditor/codeassist/assistinterface.cpp @@ -101,13 +101,14 @@ using namespace TextEditor; namespace TextEditor { -AssistInterface::AssistInterface(QTextDocument *textDocument, - int position, - const Utils::FilePath &filePath, - AssistReason reason) - : m_textDocument(textDocument) +AssistInterface::AssistInterface(const QTextCursor &cursor, + const Utils::FilePath &filePath, + AssistReason reason) + : m_textDocument(cursor.document()) + , m_cursor(cursor) , m_isAsync(false) - , m_position(position) + , m_position(cursor.position()) + , m_anchor(cursor.anchor()) , m_filePath(filePath) , m_reason(reason) {} @@ -141,6 +142,9 @@ void AssistInterface::prepareForAsyncUse() void AssistInterface::recreateTextDocument() { m_textDocument = new QTextDocument(m_text); + m_cursor = QTextCursor(m_textDocument); + m_cursor.setPosition(m_anchor); + m_cursor.setPosition(m_position, QTextCursor::KeepAnchor); m_text.clear(); QTC_CHECK(m_textDocument->blockCount() == m_userStates.count()); diff --git a/src/plugins/texteditor/codeassist/assistinterface.h b/src/plugins/texteditor/codeassist/assistinterface.h index 85a738309ad..abe1b396382 100644 --- a/src/plugins/texteditor/codeassist/assistinterface.h +++ b/src/plugins/texteditor/codeassist/assistinterface.h @@ -30,21 +30,17 @@ #include #include +#include #include #include -QT_BEGIN_NAMESPACE -class QTextDocument; -QT_END_NAMESPACE - namespace TextEditor { class TEXTEDITOR_EXPORT AssistInterface { public: - AssistInterface(QTextDocument *textDocument, - int position, + AssistInterface(const QTextCursor &cursor, const Utils::FilePath &filePath, AssistReason reason); virtual ~AssistInterface(); @@ -52,6 +48,7 @@ public: virtual int position() const { return m_position; } virtual QChar characterAt(int position) const; virtual QString textAt(int position, int length) const; + QTextCursor cursor() const { return m_cursor; } virtual Utils::FilePath filePath() const { return m_filePath; } virtual QTextDocument *textDocument() const { return m_textDocument; } virtual void prepareForAsyncUse(); @@ -60,8 +57,10 @@ public: private: QTextDocument *m_textDocument; + QTextCursor m_cursor; bool m_isAsync; int m_position; + int m_anchor; Utils::FilePath m_filePath; AssistReason m_reason; QString m_text; diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 333f5f37aa5..d2eb9e43e3b 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -8169,10 +8169,10 @@ void TextEditorWidget::invokeAssist(AssistKind kind, IAssistProvider *provider) } AssistInterface *TextEditorWidget::createAssistInterface(AssistKind kind, - AssistReason reason) const + AssistReason reason) const { Q_UNUSED(kind) - return new AssistInterface(document(), position(), d->m_document->filePath(), reason); + return new AssistInterface(textCursor(), d->m_document->filePath(), reason); } QString TextEditorWidget::foldReplacementText(const QTextBlock &) const