Skip to content
Snippets Groups Projects
Commit 03b443b8 authored by Christian Kamm's avatar Christian Kamm
Browse files

QuickFix: Simplify getting a Document for a C++ refactoring.

parent 538f6115
No related branches found
No related tags found
No related merge requests found
...@@ -84,12 +84,12 @@ public: ...@@ -84,12 +84,12 @@ public:
void performChanges(TextEditor::RefactoringFile *, CppRefactoringChanges *refactoring) 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); InsertionPointLocator locator(targetDoc);
const InsertionLocation loc = locator.methodDeclarationInClass(m_targetSymbol, m_xsSpec); const InsertionLocation loc = locator.methodDeclarationInClass(m_targetSymbol, m_xsSpec);
Q_ASSERT(loc.isValid()); Q_ASSERT(loc.isValid());
TextEditor::RefactoringFile targetFile = refactoring->file(m_targetFileName);
int targetPosition1 = targetFile.position(loc.line(), loc.column()); int targetPosition1 = targetFile.position(loc.line(), loc.column());
int targetPosition2 = qMax(0, targetFile.position(loc.line(), 1) - 1); int targetPosition2 = qMax(0, targetFile.position(loc.line(), 1) - 1);
......
...@@ -63,33 +63,13 @@ const LookupContext &CppRefactoringChanges::context() const ...@@ -63,33 +63,13 @@ const LookupContext &CppRefactoringChanges::context() const
return m_context; return m_context;
} }
Document::Ptr CppRefactoringChanges::document(const QString &fileName) const Document::Ptr CppRefactoringChanges::document(const TextEditor::RefactoringFile &file) const
{ {
QString source; QString source = file.document()->toPlainText();
unsigned editorRevision = 0; QString fileName = file.fileName();
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();
}
const QByteArray contents = m_snapshot.preprocessedCode(source, fileName); const QByteArray contents = m_snapshot.preprocessedCode(source, fileName);
Document::Ptr doc = m_snapshot.documentFromSource(contents, fileName); Document::Ptr doc = m_snapshot.documentFromSource(contents, fileName);
if (lastModified.isValid())
doc->setLastModified(lastModified);
else
doc->setEditorRevision(editorRevision);
doc->check(); doc->check();
return doc; return doc;
......
...@@ -48,9 +48,10 @@ public: ...@@ -48,9 +48,10 @@ public:
CPlusPlus::Document::Ptr thisDocument() const; CPlusPlus::Document::Ptr thisDocument() const;
const CPlusPlus::Snapshot &snapshot() const; const CPlusPlus::Snapshot &snapshot() const;
CPlusPlus::Document::Ptr document(const QString &fileName) const;
const CPlusPlus::LookupContext &context() const; const CPlusPlus::LookupContext &context() const;
CPlusPlus::Document::Ptr document(const TextEditor::RefactoringFile &file) const;
private: private:
virtual void indentSelection(const QTextCursor &selection) const; virtual void indentSelection(const QTextCursor &selection) const;
virtual void fileChanged(const QString &fileName); virtual void fileChanged(const QString &fileName);
......
...@@ -270,6 +270,11 @@ const QTextCursor RefactoringFile::cursor() const ...@@ -270,6 +270,11 @@ const QTextCursor RefactoringFile::cursor() const
return QTextCursor(); return QTextCursor();
} }
QString RefactoringFile::fileName() const
{
return m_fileName;
}
int RefactoringFile::position(unsigned line, unsigned column) const int RefactoringFile::position(unsigned line, unsigned column) const
{ {
Q_ASSERT(line != 0); Q_ASSERT(line != 0);
......
...@@ -57,6 +57,7 @@ public: ...@@ -57,6 +57,7 @@ public:
const QTextDocument *document() const; const QTextDocument *document() const;
const QTextCursor cursor() const; const QTextCursor cursor() const;
QString fileName() const;
// converts 1-based line and column into 0-based offset // converts 1-based line and column into 0-based offset
int position(unsigned line, unsigned column) const; int position(unsigned line, unsigned column) const;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment