diff options
Diffstat (limited to 'src/plugins/cppeditor/cpprefactoringchanges.cpp')
-rw-r--r-- | src/plugins/cppeditor/cpprefactoringchanges.cpp | 137 |
1 files changed, 128 insertions, 9 deletions
diff --git a/src/plugins/cppeditor/cpprefactoringchanges.cpp b/src/plugins/cppeditor/cpprefactoringchanges.cpp index 6ed6657865..25050360ea 100644 --- a/src/plugins/cppeditor/cpprefactoringchanges.cpp +++ b/src/plugins/cppeditor/cpprefactoringchanges.cpp @@ -29,6 +29,8 @@ #include "cpprefactoringchanges.h" +#include <TranslationUnit.h> +#include <AST.h> #include <cpptools/cppcodeformatter.h> #include <texteditor/texteditorsettings.h> #include <texteditor/tabsettings.h> @@ -37,6 +39,7 @@ using namespace CppEditor; using namespace CPlusPlus; +using namespace Utils; CppRefactoringChanges::CppRefactoringChanges(const Document::Ptr &thisDocument, const Snapshot &snapshot) : m_thisDocument(thisDocument) @@ -63,16 +66,9 @@ const LookupContext &CppRefactoringChanges::context() const return m_context; } -Document::Ptr CppRefactoringChanges::document(const TextEditor::RefactoringFile &file) const +CppRefactoringFile CppRefactoringChanges::file(const QString &fileName) { - 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); - doc->check(); - - return doc; + return CppRefactoringFile(fileName, this); } void CppRefactoringChanges::indentSelection(const QTextCursor &selection) const @@ -98,3 +94,126 @@ void CppRefactoringChanges::fileChanged(const QString &fileName) { m_modelManager->updateSourceFiles(QStringList(fileName)); } + + +CppRefactoringFile::CppRefactoringFile() +{ } + +CppRefactoringFile::CppRefactoringFile(const QString &fileName, CppRefactoringChanges *refactoringChanges) + : RefactoringFile(fileName, refactoringChanges) +{ } + +Document::Ptr CppRefactoringFile::cppDocument() const +{ + if (!m_cppDocument) { + const QString source = document()->toPlainText(); + const QString name = fileName(); + const Snapshot &snapshot = refactoringChanges()->snapshot(); + + const QByteArray contents = snapshot.preprocessedCode(source, name); + m_cppDocument = snapshot.documentFromSource(contents, name); + m_cppDocument->check(); + } + + return m_cppDocument; +} + +Scope *CppRefactoringFile::scopeAt(unsigned index) const +{ + unsigned line, column; + cppDocument()->translationUnit()->getTokenStartPosition(index, &line, &column); + return cppDocument()->scopeAt(line, column); +} + +bool CppRefactoringFile::isCursorOn(unsigned tokenIndex) const +{ + QTextCursor tc = cursor(); + int cursorBegin = tc.selectionStart(); + + int start = startOf(tokenIndex); + int end = endOf(tokenIndex); + + if (cursorBegin >= start && cursorBegin <= end) + return true; + + return false; +} + +bool CppRefactoringFile::isCursorOn(const AST *ast) const +{ + QTextCursor tc = cursor(); + int cursorBegin = tc.selectionStart(); + + int start = startOf(ast); + int end = endOf(ast); + + if (cursorBegin >= start && cursorBegin <= end) + return true; + + return false; +} + +ChangeSet::Range CppRefactoringFile::range(unsigned tokenIndex) const +{ + const Token &token = tokenAt(tokenIndex); + unsigned line, column; + cppDocument()->translationUnit()->getPosition(token.begin(), &line, &column); + const int start = document()->findBlockByNumber(line - 1).position() + column - 1; + return ChangeSet::Range(start, start + token.length()); +} + +ChangeSet::Range CppRefactoringFile::range(AST *ast) const +{ + return ChangeSet::Range(startOf(ast), endOf(ast)); +} + +int CppRefactoringFile::startOf(unsigned index) const +{ + unsigned line, column; + cppDocument()->translationUnit()->getPosition(tokenAt(index).begin(), &line, &column); + return document()->findBlockByNumber(line - 1).position() + column - 1; +} + +int CppRefactoringFile::startOf(const AST *ast) const +{ + return startOf(ast->firstToken()); +} + +int CppRefactoringFile::endOf(unsigned index) const +{ + unsigned line, column; + cppDocument()->translationUnit()->getPosition(tokenAt(index).end(), &line, &column); + return document()->findBlockByNumber(line - 1).position() + column - 1; +} + +int CppRefactoringFile::endOf(const AST *ast) const +{ + if (unsigned end = ast->lastToken()) + return endOf(end - 1); + else + return 0; +} + +void CppRefactoringFile::startAndEndOf(unsigned index, int *start, int *end) const +{ + unsigned line, column; + Token token(tokenAt(index)); + cppDocument()->translationUnit()->getPosition(token.begin(), &line, &column); + *start = document()->findBlockByNumber(line - 1).position() + column - 1; + *end = *start + token.length(); +} + +QString CppRefactoringFile::textOf(const AST *ast) const +{ + return textOf(startOf(ast), endOf(ast)); +} + +const Token &CppRefactoringFile::tokenAt(unsigned index) const +{ + return cppDocument()->translationUnit()->tokenAt(index); +} + +CppRefactoringChanges *CppRefactoringFile::refactoringChanges() const +{ + return static_cast<CppRefactoringChanges *>(m_refactoringChanges); +} |