diff --git a/src/plugins/cppeditor/cppdeclfromdef.cpp b/src/plugins/cppeditor/cppdeclfromdef.cpp index 034c478b1125cb88e5220c8ca7f7cc69b14084df..8fb3d3278b8c1a940ede6bc3d081abcac5b7ccdc 100644 --- a/src/plugins/cppeditor/cppdeclfromdef.cpp +++ b/src/plugins/cppeditor/cppdeclfromdef.cpp @@ -93,9 +93,10 @@ public: Document::Ptr targetDoc = changes->document(m_targetFileName); InsertionPointLocator locator(targetDoc); const InsertionLocation loc = locator.methodDeclarationInClass(m_targetSymbol, InsertionPointLocator::Public); + Q_ASSERT(loc.isValid()); - int targetPosition1 = changes->positionInFile(m_targetFileName, loc.line() - 1, loc.column() - 1); - int targetPosition2 = changes->positionInFile(m_targetFileName, loc.line(), 0) - 1; + int targetPosition1 = changes->positionInFile(m_targetFileName, loc.line(), loc.column()); + int targetPosition2 = qMax(0, changes->positionInFile(m_targetFileName, loc.line(), 1) - 1); Utils::ChangeSet target; target.insert(targetPosition1, loc.prefix() + m_decl); @@ -104,7 +105,7 @@ public: changes->reindent(m_targetFileName, Utils::ChangeSet::Range(targetPosition1, targetPosition2)); - changes->openEditor(m_targetFileName, loc.line() - 1, loc.column() - 1); + changes->openEditor(m_targetFileName, loc.line(), loc.column()); } private: diff --git a/src/plugins/texteditor/refactoringchanges.cpp b/src/plugins/texteditor/refactoringchanges.cpp index 7bbb8b5c5ba3a5d75451e07cd1fe23da2ee6afd7..35209a947545a9eb324d9c9f4f742eef2d3f013e 100644 --- a/src/plugins/texteditor/refactoringchanges.cpp +++ b/src/plugins/texteditor/refactoringchanges.cpp @@ -158,17 +158,20 @@ QStringList RefactoringChanges::apply() Core::EditorManager *editorManager = Core::EditorManager::instance(); BaseTextEditor *editor = editorForFile(m_fileNameToShow); editorManager->activateEditor(editor->editableInterface()); - if (m_lineToShow != -1) - editor->gotoLine(m_lineToShow + 1, m_columnToShow); + if (m_lineToShow != 0) { + editor->gotoLine(m_lineToShow, qMax(0, int(m_columnToShow) - 1)); + } } return changed.toList(); } -int RefactoringChanges::positionInFile(const QString &fileName, int line, int column) const +int RefactoringChanges::positionInFile(const QString &fileName, unsigned line, unsigned column) const { if (BaseTextEditor *editor = editorForFile(fileName, true)) { - return editor->document()->findBlockByNumber(line).position() + column; + Q_ASSERT(line != 0); + Q_ASSERT(column != 0); + return editor->document()->findBlockByNumber(line - 1).position() + column - 1; } else { return -1; } @@ -205,12 +208,7 @@ BaseTextEditor *RefactoringChanges::editorForNewFile(const QString &fileName) return editorForFile(fileName, true); } -/** - * \param fileName the file to open - * \param line the line to focus on, 0-based - * \param column the column to focus on, 0-based - */ -void RefactoringChanges::openEditor(const QString &fileName, int line, int column) +void RefactoringChanges::openEditor(const QString &fileName, unsigned line, unsigned column) { m_fileNameToShow = fileName; m_lineToShow = line; diff --git a/src/plugins/texteditor/refactoringchanges.h b/src/plugins/texteditor/refactoringchanges.h index e4a5cd6eb8c0e1ec44d37d777f088be542d5d97f..2dccf2d11fefae58e666454e9b15e863a3b767c3 100644 --- a/src/plugins/texteditor/refactoringchanges.h +++ b/src/plugins/texteditor/refactoringchanges.h @@ -67,22 +67,27 @@ public: */ virtual QStringList apply(); - int positionInFile(const QString &fileName, int line, int column = 0) const; + // 1-based + int positionInFile(const QString &fileName, unsigned line, unsigned column) const; static BaseTextEditor *editorForFile(const QString &fileName, bool openIfClosed = false); static BaseTextEditor *editorForNewFile(const QString &fileName); - /** line and column are zero-based */ - void openEditor(const QString &fileName, int line, int column); + /** + * \param fileName the file to open + * \param line the line to focus on, 1-based + * \param column the column to focus on, 1-based + */ + void openEditor(const QString &fileName, unsigned line, unsigned column); private: QMap<QString, QString> m_contentsByCreatedFile; QMap<QString, Utils::ChangeSet> m_changesByFile; QMap<QString, QList<Range> > m_indentRangesByFile; QString m_fileNameToShow; - int m_lineToShow; - int m_columnToShow; + unsigned m_lineToShow; + unsigned m_columnToShow; }; } // namespace TextEditor