From 03b443b86b51b31f907b2d4e565ae66144753539 Mon Sep 17 00:00:00 2001 From: Christian Kamm <christian.d.kamm@nokia.com> Date: Thu, 12 Aug 2010 14:18:36 +0200 Subject: [PATCH] QuickFix: Simplify getting a Document for a C++ refactoring. --- src/plugins/cppeditor/cppdeclfromdef.cpp | 4 +-- .../cppeditor/cpprefactoringchanges.cpp | 26 +++---------------- src/plugins/cppeditor/cpprefactoringchanges.h | 3 ++- src/plugins/texteditor/refactoringchanges.cpp | 5 ++++ src/plugins/texteditor/refactoringchanges.h | 1 + 5 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/plugins/cppeditor/cppdeclfromdef.cpp b/src/plugins/cppeditor/cppdeclfromdef.cpp index 8608da134ee..c3f312a60c5 100644 --- a/src/plugins/cppeditor/cppdeclfromdef.cpp +++ b/src/plugins/cppeditor/cppdeclfromdef.cpp @@ -84,12 +84,12 @@ public: void performChanges(TextEditor::RefactoringFile *, CppRefactoringChanges *refactoring) { - Document::Ptr targetDoc = refactoring->document(m_targetFileName); + TextEditor::RefactoringFile targetFile = refactoring->file(m_targetFileName); + Document::Ptr targetDoc = refactoring->document(targetFile); InsertionPointLocator locator(targetDoc); const InsertionLocation loc = locator.methodDeclarationInClass(m_targetSymbol, m_xsSpec); Q_ASSERT(loc.isValid()); - TextEditor::RefactoringFile targetFile = refactoring->file(m_targetFileName); int targetPosition1 = targetFile.position(loc.line(), loc.column()); int targetPosition2 = qMax(0, targetFile.position(loc.line(), 1) - 1); diff --git a/src/plugins/cppeditor/cpprefactoringchanges.cpp b/src/plugins/cppeditor/cpprefactoringchanges.cpp index 6b500a73f04..6ed66578652 100644 --- a/src/plugins/cppeditor/cpprefactoringchanges.cpp +++ b/src/plugins/cppeditor/cpprefactoringchanges.cpp @@ -63,33 +63,13 @@ const LookupContext &CppRefactoringChanges::context() const return m_context; } -Document::Ptr CppRefactoringChanges::document(const QString &fileName) const +Document::Ptr CppRefactoringChanges::document(const TextEditor::RefactoringFile &file) const { - QString source; - unsigned editorRevision = 0; - QDateTime lastModified; - - if (m_workingCopy.contains(fileName)) { - const QPair<QString, unsigned> workingCopy = m_workingCopy.get(fileName); - source = workingCopy.first; - editorRevision = workingCopy.second; - } else { - QFile file(fileName); - if (! file.open(QFile::ReadOnly)) - return Document::Ptr(); - - lastModified = QFileInfo(file).lastModified(); - source = QTextStream(&file).readAll(); // ### FIXME read bytes, and remove the convert below - file.close(); - } + QString source = file.document()->toPlainText(); + QString fileName = file.fileName(); const QByteArray contents = m_snapshot.preprocessedCode(source, fileName); Document::Ptr doc = m_snapshot.documentFromSource(contents, fileName); - - if (lastModified.isValid()) - doc->setLastModified(lastModified); - else - doc->setEditorRevision(editorRevision); doc->check(); return doc; diff --git a/src/plugins/cppeditor/cpprefactoringchanges.h b/src/plugins/cppeditor/cpprefactoringchanges.h index cedb68bef20..7c8d1368796 100644 --- a/src/plugins/cppeditor/cpprefactoringchanges.h +++ b/src/plugins/cppeditor/cpprefactoringchanges.h @@ -48,9 +48,10 @@ public: CPlusPlus::Document::Ptr thisDocument() const; const CPlusPlus::Snapshot &snapshot() const; - CPlusPlus::Document::Ptr document(const QString &fileName) const; const CPlusPlus::LookupContext &context() const; + CPlusPlus::Document::Ptr document(const TextEditor::RefactoringFile &file) const; + private: virtual void indentSelection(const QTextCursor &selection) const; virtual void fileChanged(const QString &fileName); diff --git a/src/plugins/texteditor/refactoringchanges.cpp b/src/plugins/texteditor/refactoringchanges.cpp index 9f0575b8afe..0d2d3a458c2 100644 --- a/src/plugins/texteditor/refactoringchanges.cpp +++ b/src/plugins/texteditor/refactoringchanges.cpp @@ -270,6 +270,11 @@ const QTextCursor RefactoringFile::cursor() const return QTextCursor(); } +QString RefactoringFile::fileName() const +{ + return m_fileName; +} + int RefactoringFile::position(unsigned line, unsigned column) const { Q_ASSERT(line != 0); diff --git a/src/plugins/texteditor/refactoringchanges.h b/src/plugins/texteditor/refactoringchanges.h index 267ed2e6e20..fc67d43f851 100644 --- a/src/plugins/texteditor/refactoringchanges.h +++ b/src/plugins/texteditor/refactoringchanges.h @@ -57,6 +57,7 @@ public: const QTextDocument *document() const; const QTextCursor cursor() const; + QString fileName() const; // converts 1-based line and column into 0-based offset int position(unsigned line, unsigned column) const; -- GitLab