Commit 3ebc3284 authored by Erik Verbruggen's avatar Erik Verbruggen

C++: added quickfix to insert a method definition.

Reviewed-by: Christian Kamm
parent 44d87bf8
...@@ -48,8 +48,7 @@ HEADERS += \ ...@@ -48,8 +48,7 @@ HEADERS += \
$$PWD/pp-cctype.h \ $$PWD/pp-cctype.h \
$$PWD/pp-engine.h \ $$PWD/pp-engine.h \
$$PWD/pp-macro-expander.h \ $$PWD/pp-macro-expander.h \
$$PWD/pp-scanner.h \ $$PWD/pp-scanner.h
$$PWD/InsertionPointLocator.h
SOURCES += \ SOURCES += \
$$PWD/SimpleLexer.cpp \ $$PWD/SimpleLexer.cpp \
...@@ -73,7 +72,6 @@ SOURCES += \ ...@@ -73,7 +72,6 @@ SOURCES += \
$$PWD/Macro.cpp \ $$PWD/Macro.cpp \
$$PWD/pp-engine.cpp \ $$PWD/pp-engine.cpp \
$$PWD/pp-macro-expander.cpp \ $$PWD/pp-macro-expander.cpp \
$$PWD/pp-scanner.cpp \ $$PWD/pp-scanner.cpp
$$PWD/InsertionPointLocator.cpp
RESOURCES += $$PWD/cplusplus.qrc RESOURCES += $$PWD/cplusplus.qrc
...@@ -15,7 +15,6 @@ HEADERS += cppplugin.h \ ...@@ -15,7 +15,6 @@ HEADERS += cppplugin.h \
cppeditor_global.h \ cppeditor_global.h \
cppclasswizard.h \ cppclasswizard.h \
cppquickfix.h \ cppquickfix.h \
cpprefactoringchanges.h \
cppchecksymbols.h \ cppchecksymbols.h \
cppsemanticinfo.h \ cppsemanticinfo.h \
cppoutline.h \ cppoutline.h \
...@@ -31,7 +30,6 @@ SOURCES += cppplugin.cpp \ ...@@ -31,7 +30,6 @@ SOURCES += cppplugin.cpp \
cppclasswizard.cpp \ cppclasswizard.cpp \
cppquickfix.cpp \ cppquickfix.cpp \
cppquickfixes.cpp \ cppquickfixes.cpp \
cpprefactoringchanges.cpp \
cppchecksymbols.cpp \ cppchecksymbols.cpp \
cppsemanticinfo.cpp \ cppsemanticinfo.cpp \
cppoutline.cpp \ cppoutline.cpp \
......
This diff is collapsed.
...@@ -56,11 +56,6 @@ class DefFromDecl: public CppQuickFixFactory ...@@ -56,11 +56,6 @@ class DefFromDecl: public CppQuickFixFactory
{ {
public: public:
virtual QList<CppQuickFixOperation::Ptr> match(const CppQuickFixState &state); virtual QList<CppQuickFixOperation::Ptr> match(const CppQuickFixState &state);
protected:
static QString generateDefinition(const CppQuickFixState &state,
CPlusPlus::Function *method,
CPlusPlus::ClassOrNamespace *targetBinding);
}; };
} // namespace Internal } // namespace Internal
......
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
#include <cplusplus/CppRewriter.h> #include <cplusplus/CppRewriter.h>
#include <cppeditor/cppeditor.h> #include <cppeditor/cppeditor.h>
#include <cppeditor/cpprefactoringchanges.h> #include <cpptools/cpprefactoringchanges.h>
#include <cpptools/cpptoolsconstants.h> #include <cpptools/cpptoolsconstants.h>
#include <cpptools/cppmodelmanagerinterface.h> #include <cpptools/cppmodelmanagerinterface.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
...@@ -52,6 +52,7 @@ ...@@ -52,6 +52,7 @@
using namespace CppEditor; using namespace CppEditor;
using namespace CppEditor::Internal; using namespace CppEditor::Internal;
using namespace CppTools;
using namespace TextEditor; using namespace TextEditor;
using namespace CPlusPlus; using namespace CPlusPlus;
using namespace Utils; using namespace Utils;
......
...@@ -30,11 +30,12 @@ ...@@ -30,11 +30,12 @@
#ifndef CPPQUICKFIX_H #ifndef CPPQUICKFIX_H
#define CPPQUICKFIX_H #define CPPQUICKFIX_H
#include "cpprefactoringchanges.h" #include "cppeditor_global.h"
#include "cppsemanticinfo.h" #include "cppsemanticinfo.h"
#include <ASTfwd.h> #include <ASTfwd.h>
#include <cplusplus/CppDocument.h> #include <cplusplus/CppDocument.h>
#include <cpptools/cpprefactoringchanges.h>
#include <texteditor/icompletioncollector.h> #include <texteditor/icompletioncollector.h>
#include <texteditor/quickfix.h> #include <texteditor/quickfix.h>
#include <utils/changeset.h> #include <utils/changeset.h>
...@@ -69,7 +70,7 @@ public: ...@@ -69,7 +70,7 @@ public:
CppEditor::Internal::SemanticInfo semanticInfo() const; CppEditor::Internal::SemanticInfo semanticInfo() const;
const CPlusPlus::LookupContext &context() const; const CPlusPlus::LookupContext &context() const;
const CppRefactoringFile currentFile() const; const CppTools::CppRefactoringFile currentFile() const;
bool isCursorOn(unsigned tokenIndex) const bool isCursorOn(unsigned tokenIndex) const
{ return currentFile().isCursorOn(tokenIndex); } { return currentFile().isCursorOn(tokenIndex); }
...@@ -94,7 +95,8 @@ public: ...@@ -94,7 +95,8 @@ public:
virtual void perform(); virtual void perform();
protected: protected:
virtual void performChanges(CppRefactoringFile *currentFile, CppRefactoringChanges *refactoring) = 0; virtual void performChanges(CppTools::CppRefactoringFile *currentFile,
CppTools::CppRefactoringChanges *refactoring) = 0;
QString fileName() const; QString fileName() const;
......
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
using namespace CppEditor; using namespace CppEditor;
using namespace CppEditor::Internal; using namespace CppEditor::Internal;
using namespace CppTools;
using namespace TextEditor; using namespace TextEditor;
using namespace CPlusPlus; using namespace CPlusPlus;
using namespace Utils; using namespace Utils;
......
...@@ -37,8 +37,8 @@ ...@@ -37,8 +37,8 @@
#include <QtGui/QTextBlock> #include <QtGui/QTextBlock>
using namespace CppEditor;
using namespace CPlusPlus; using namespace CPlusPlus;
using namespace CppTools;
using namespace Utils; using namespace Utils;
CppRefactoringChanges::CppRefactoringChanges(const Snapshot &snapshot) CppRefactoringChanges::CppRefactoringChanges(const Snapshot &snapshot)
......
...@@ -35,15 +35,15 @@ ...@@ -35,15 +35,15 @@
#include <cplusplus/LookupContext.h> #include <cplusplus/LookupContext.h>
#include <cpptools/cppmodelmanagerinterface.h> #include <cpptools/cppmodelmanagerinterface.h>
#include <cppeditor/cppeditor_global.h> #include <cpptools/cpptools_global.h>
#include <texteditor/refactoringchanges.h> #include <texteditor/refactoringchanges.h>
namespace CppEditor { namespace CppTools {
class CppRefactoringChanges; class CppRefactoringChanges;
class CPPEDITOR_EXPORT CppRefactoringFile: public TextEditor::RefactoringFile class CPPTOOLS_EXPORT CppRefactoringFile: public TextEditor::RefactoringFile
{ {
public: public:
CppRefactoringFile(); CppRefactoringFile();
...@@ -79,7 +79,7 @@ private: ...@@ -79,7 +79,7 @@ private:
mutable CPlusPlus::Document::Ptr m_cppDocument; mutable CPlusPlus::Document::Ptr m_cppDocument;
}; };
class CPPEDITOR_EXPORT CppRefactoringChanges: public TextEditor::RefactoringChanges class CPPTOOLS_EXPORT CppRefactoringChanges: public TextEditor::RefactoringChanges
{ {
public: public:
CppRefactoringChanges(const CPlusPlus::Snapshot &snapshot); CppRefactoringChanges(const CPlusPlus::Snapshot &snapshot);
...@@ -99,6 +99,6 @@ private: ...@@ -99,6 +99,6 @@ private:
CppTools::CppModelManagerInterface::WorkingCopy m_workingCopy; CppTools::CppModelManagerInterface::WorkingCopy m_workingCopy;
}; };
} // namespace CppEditor } // namespace CppTools
#endif // CPPREFACTORINGCHANGES_H #endif // CPPREFACTORINGCHANGES_H
...@@ -25,7 +25,9 @@ HEADERS += completionsettingspage.h \ ...@@ -25,7 +25,9 @@ HEADERS += completionsettingspage.h \
cppfilesettingspage.h \ cppfilesettingspage.h \
cppfindreferences.h \ cppfindreferences.h \
cppcodeformatter.h \ cppcodeformatter.h \
symbolsfindfilter.h symbolsfindfilter.h \
insertionpointlocator.h \
cpprefactoringchanges.h
SOURCES += completionsettingspage.cpp \ SOURCES += completionsettingspage.cpp \
cppclassesfilter.cpp \ cppclassesfilter.cpp \
...@@ -42,7 +44,9 @@ SOURCES += completionsettingspage.cpp \ ...@@ -42,7 +44,9 @@ SOURCES += completionsettingspage.cpp \
abstracteditorsupport.cpp \ abstracteditorsupport.cpp \
cppfindreferences.cpp \ cppfindreferences.cpp \
cppcodeformatter.cpp \ cppcodeformatter.cpp \
symbolsfindfilter.cpp symbolsfindfilter.cpp \
insertionpointlocator.cpp \
cpprefactoringchanges.cpp
FORMS += completionsettingspage.ui \ FORMS += completionsettingspage.ui \
cppfilesettingspage.ui cppfilesettingspage.ui
......
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
** **
**************************************************************************/ **************************************************************************/
#include "CppToolsPlugin.h"
#include "cpprefactoringchanges.h"
#include "InsertionPointLocator.h" #include "InsertionPointLocator.h"
#include <AST.h> #include <AST.h>
...@@ -34,6 +36,7 @@ ...@@ -34,6 +36,7 @@
#include <TranslationUnit.h> #include <TranslationUnit.h>
using namespace CPlusPlus; using namespace CPlusPlus;
using namespace CppTools;
namespace { namespace {
...@@ -150,7 +153,8 @@ protected: ...@@ -150,7 +153,8 @@ protected:
if (needsSuffix) if (needsSuffix)
suffix = QLatin1Char('\n'); suffix = QLatin1Char('\n');
_result = InsertionLocation(prefix, suffix, line, column); _result = InsertionLocation(_doc->fileName(), prefix, suffix,
line, column);
return false; return false;
} }
...@@ -264,16 +268,19 @@ InsertionLocation::InsertionLocation() ...@@ -264,16 +268,19 @@ InsertionLocation::InsertionLocation()
, m_column(0) , m_column(0)
{} {}
InsertionLocation::InsertionLocation(const QString &prefix, const QString &suffix, InsertionLocation::InsertionLocation(const QString &fileName,
const QString &prefix,
const QString &suffix,
unsigned line, unsigned column) unsigned line, unsigned column)
: m_prefix(prefix) : m_fileName(fileName)
, m_prefix(prefix)
, m_suffix(suffix) , m_suffix(suffix)
, m_line(line) , m_line(line)
, m_column(column) , m_column(column)
{} {}
InsertionPointLocator::InsertionPointLocator(const Snapshot &snapshot) InsertionPointLocator::InsertionPointLocator(CppRefactoringChanges *refactoringChanges)
: m_snapshot(snapshot) : m_refactoringChanges(refactoringChanges)
{ {
} }
...@@ -282,7 +289,7 @@ InsertionLocation InsertionPointLocator::methodDeclarationInClass( ...@@ -282,7 +289,7 @@ InsertionLocation InsertionPointLocator::methodDeclarationInClass(
const Class *clazz, const Class *clazz,
AccessSpec xsSpec) const AccessSpec xsSpec) const
{ {
const Document::Ptr doc = m_snapshot.document(fileName); const Document::Ptr doc = m_refactoringChanges->file(fileName).cppDocument();
if (doc) { if (doc) {
FindInClass find(doc, clazz, xsSpec); FindInClass find(doc, clazz, xsSpec);
return find(); return find();
...@@ -293,11 +300,30 @@ InsertionLocation InsertionPointLocator::methodDeclarationInClass( ...@@ -293,11 +300,30 @@ InsertionLocation InsertionPointLocator::methodDeclarationInClass(
/// Currently, we return the end of fileName.cpp /// Currently, we return the end of fileName.cpp
QList<InsertionLocation> InsertionPointLocator::methodDefinition( QList<InsertionLocation> InsertionPointLocator::methodDefinition(
const QString &/*fileName*/) const Declaration *declaration) const
{ {
QList<InsertionLocation> result; QList<InsertionLocation> result;
if (!declaration)
return result;
Internal::CppToolsPlugin *cpptools = Internal::CppToolsPlugin::instance();
const QString declFileName = QLatin1String(declaration->fileName());
QString target = cpptools->correspondingHeaderOrSource(declFileName);
Document::Ptr doc = m_refactoringChanges->file(target).cppDocument();
if (doc.isNull())
return result;
TranslationUnit *xUnit = doc->translationUnit();
unsigned tokenCount = xUnit->tokenCount();
if (tokenCount < 2) // no tokens available
return result;
unsigned line = 0, column = 0;
xUnit->getTokenEndPosition(xUnit->tokenCount() - 2, &line, &column);
const QLatin1String prefix("\n\n");
result.append(InsertionLocation(target, prefix, QString(), line, column));
return result; return result;
} }
...@@ -30,19 +30,27 @@ ...@@ -30,19 +30,27 @@
#ifndef INSERTIONPOINTLOCATOR_H #ifndef INSERTIONPOINTLOCATOR_H
#define INSERTIONPOINTLOCATOR_H #define INSERTIONPOINTLOCATOR_H
#include "cpptools_global.h"
#include <ASTfwd.h> #include <ASTfwd.h>
#include <CPlusPlusForwardDeclarations.h> #include <CPlusPlusForwardDeclarations.h>
#include <Symbols.h> #include <Symbols.h>
#include "CppDocument.h" #include <cplusplus/CppDocument.h>
namespace CppTools {
namespace CPlusPlus { class CppRefactoringChanges;
class CPLUSPLUS_EXPORT InsertionLocation class CPPTOOLS_EXPORT InsertionLocation
{ {
public: public:
InsertionLocation(); InsertionLocation();
InsertionLocation(const QString &prefix, const QString &suffix, unsigned line, unsigned column); InsertionLocation(const QString &fileName, const QString &prefix,
const QString &suffix, unsigned line, unsigned column);
QString fileName() const
{ return m_fileName; }
/// \returns The prefix to insert before any other text. /// \returns The prefix to insert before any other text.
QString prefix() const QString prefix() const
...@@ -61,16 +69,17 @@ public: ...@@ -61,16 +69,17 @@ public:
{ return m_column; } { return m_column; }
bool isValid() const bool isValid() const
{ return m_line > 0 && m_column > 0; } { return !m_fileName.isEmpty() && m_line > 0 && m_column > 0; }
private: private:
QString m_fileName;
QString m_prefix; QString m_prefix;
QString m_suffix; QString m_suffix;
unsigned m_line; unsigned m_line;
unsigned m_column; unsigned m_column;
}; };
class CPLUSPLUS_EXPORT InsertionPointLocator class CPPTOOLS_EXPORT InsertionPointLocator
{ {
public: public:
enum AccessSpec { enum AccessSpec {
...@@ -89,18 +98,18 @@ public: ...@@ -89,18 +98,18 @@ public:
}; };
public: public:
InsertionPointLocator(const Snapshot &snapshot); InsertionPointLocator(CppRefactoringChanges *refactoringChanges);
InsertionLocation methodDeclarationInClass(const QString &fileName, InsertionLocation methodDeclarationInClass(const QString &fileName,
const Class *clazz, const CPlusPlus::Class *clazz,
AccessSpec xsSpec) const; AccessSpec xsSpec) const;
QList<InsertionLocation> methodDefinition(const QString &fileName) const; QList<InsertionLocation> methodDefinition(CPlusPlus::Declaration *declaration) const;
private: private:
Snapshot m_snapshot; CppRefactoringChanges *m_refactoringChanges;
}; };
} // namespace CPlusPlus } // namespace CppTools
#endif // INSERTIONPOINTLOCATOR_H #endif // INSERTIONPOINTLOCATOR_H
...@@ -37,8 +37,9 @@ ...@@ -37,8 +37,9 @@
#include <widgethost.h> #include <widgethost.h>
#include <cpptools/cppmodelmanagerinterface.h> #include <cpptools/cppmodelmanagerinterface.h>
#include <cpptools/cpprefactoringchanges.h>
#include <cpptools/cpptoolsconstants.h> #include <cpptools/cpptoolsconstants.h>
#include <cplusplus/InsertionPointLocator.h> #include <cpptools/insertionpointlocator.h>
#include <cplusplus/Symbols.h> #include <cplusplus/Symbols.h>
#include <cplusplus/Overview.h> #include <cplusplus/Overview.h>
#include <cplusplus/CoreTypes.h> #include <cplusplus/CoreTypes.h>
...@@ -282,9 +283,10 @@ static void addDeclaration(const Snapshot &snapshot, ...@@ -282,9 +283,10 @@ static void addDeclaration(const Snapshot &snapshot,
declaration += functionName; declaration += functionName;
declaration += QLatin1String(";\n"); declaration += QLatin1String(";\n");
InsertionPointLocator find(snapshot); CppTools::CppRefactoringChanges refactoring(snapshot);
const InsertionLocation loc = find.methodDeclarationInClass( CppTools::InsertionPointLocator find(&refactoring);
fileName, cl, InsertionPointLocator::PrivateSlot); const CppTools::InsertionLocation loc = find.methodDeclarationInClass(
fileName, cl, CppTools::InsertionPointLocator::PrivateSlot);
// //
//! \todo change this to use the Refactoring changes. //! \todo change this to use the Refactoring changes.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment