From 9a00b8ba27f2bfbe0ce9e09b8585822d9843e9f7 Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Thu, 2 Sep 2010 15:17:57 +0200 Subject: C++: Renamed cppdeclfromdef.{cpp,h} to cppinsertdecldef.{cpp,h} --- src/plugins/cppeditor/cppdeclfromdef.cpp | 185 ----------------------------- src/plugins/cppeditor/cppdeclfromdef.h | 59 --------- src/plugins/cppeditor/cppeditor.pro | 4 +- src/plugins/cppeditor/cppinsertdecldef.cpp | 182 ++++++++++++++++++++++++++++ src/plugins/cppeditor/cppinsertdecldef.h | 59 +++++++++ src/plugins/cppeditor/cppquickfix.cpp | 1 - src/plugins/cppeditor/cppquickfixes.cpp | 2 +- 7 files changed, 244 insertions(+), 248 deletions(-) delete mode 100644 src/plugins/cppeditor/cppdeclfromdef.cpp delete mode 100644 src/plugins/cppeditor/cppdeclfromdef.h create mode 100644 src/plugins/cppeditor/cppinsertdecldef.cpp create mode 100644 src/plugins/cppeditor/cppinsertdecldef.h (limited to 'src/plugins/cppeditor') diff --git a/src/plugins/cppeditor/cppdeclfromdef.cpp b/src/plugins/cppeditor/cppdeclfromdef.cpp deleted file mode 100644 index f0ede56f5e..0000000000 --- a/src/plugins/cppeditor/cppdeclfromdef.cpp +++ /dev/null @@ -1,185 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** Commercial Usage -** -** Licensees holding valid Qt Commercial licenses may use this file in -** accordance with the Qt Commercial License Agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Nokia. -** -** GNU Lesser General Public License Usage -** -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at http://qt.nokia.com/contact. -** -**************************************************************************/ - -#include "cppdeclfromdef.h" - -#include //### remove -#include //###remove - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -using namespace CPlusPlus; -using namespace CppEditor; -using namespace CppEditor::Internal; -using namespace CppTools; - -using CppEditor::CppRefactoringChanges; - -namespace { - -class Operation: public CppQuickFixOperation -{ -public: - Operation(const CppQuickFixState &state, int priority, - const QString &targetFileName, const Class *targetSymbol, - InsertionPointLocator::AccessSpec xsSpec, - const QString &decl) - : CppQuickFixOperation(state, priority) - , m_targetFileName(targetFileName) - , m_targetSymbol(targetSymbol) - , m_xsSpec(xsSpec) - , m_decl(decl) - { - QString type; - switch (xsSpec) { - case InsertionPointLocator::Public: type = QLatin1String("public"); break; - case InsertionPointLocator::Protected: type = QLatin1String("protected"); break; - case InsertionPointLocator::Private: type = QLatin1String("private"); break; - case InsertionPointLocator::PublicSlot: type = QLatin1String("public slot"); break; - case InsertionPointLocator::ProtectedSlot: type = QLatin1String("protected slot"); break; - case InsertionPointLocator::PrivateSlot: type = QLatin1String("private slot"); break; - default: break; - } - - setDescription(QCoreApplication::tr("Create %1 Declaration from Definition", - "CppEditor::DeclFromDef").arg(type)); - } - - void performChanges(CppRefactoringFile *, CppRefactoringChanges *refactoring) - { - CppRefactoringFile targetFile = refactoring->file(m_targetFileName); - Document::Ptr targetDoc = targetFile.cppDocument(); - InsertionPointLocator locator(targetDoc); - const InsertionLocation loc = locator.methodDeclarationInClass(m_targetSymbol, m_xsSpec); - Q_ASSERT(loc.isValid()); - - int targetPosition1 = targetFile.position(loc.line(), loc.column()); - int targetPosition2 = qMax(0, targetFile.position(loc.line(), 1) - 1); - - Utils::ChangeSet target; - target.insert(targetPosition1, loc.prefix() + m_decl); - targetFile.change(target); - targetFile.indent(Utils::ChangeSet::Range(targetPosition2, targetPosition1)); - - refactoring->activateEditor(m_targetFileName, targetPosition1); - } - -private: - QString m_targetFileName; - const Class *m_targetSymbol; - InsertionPointLocator::AccessSpec m_xsSpec; - QString m_decl; -}; - -} // anonymous namespace - -QList DeclFromDef::match(const CppQuickFixState &state) -{ - const QList &path = state.path(); - const CppRefactoringFile &file = state.currentFile(); - - FunctionDefinitionAST *funDef = 0; - int idx = 0; - for (; idx < path.size(); ++idx) { - AST *node = path.at(idx); - if (FunctionDefinitionAST *candidate = node->asFunctionDefinition()) { - if (!funDef && file.isCursorOn(candidate) && !file.isCursorOn(candidate->function_body)) - funDef = candidate; - } else if (node->asClassSpecifier()) { - return noResult(); - } - } - - if (!funDef || !funDef->symbol) - return noResult(); - - Function *method = funDef->symbol; - - if (ClassOrNamespace *targetBinding = state.context().lookupParent(method)) { - foreach (Symbol *s, targetBinding->symbols()) { - if (Class *clazz = s->asClass()) { - QList results; - const QLatin1String fn(clazz->fileName()); - const QString decl = generateDeclaration(state, - method, - targetBinding); - results.append(singleResult(new Operation(state, idx, fn, clazz, - InsertionPointLocator::Public, - decl))); - results.append(singleResult(new Operation(state, idx, fn, clazz, - InsertionPointLocator::Protected, - decl))); - results.append(singleResult(new Operation(state, idx, fn, clazz, - InsertionPointLocator::Private, - decl))); - results.append(singleResult(new Operation(state, idx, fn, clazz, - InsertionPointLocator::PublicSlot, - decl))); - results.append(singleResult(new Operation(state, idx, fn, clazz, - InsertionPointLocator::ProtectedSlot, - decl))); - results.append(singleResult(new Operation(state, idx, fn, clazz, - InsertionPointLocator::PrivateSlot, - decl))); - return results; - } //! \todo support insertion into namespaces - } - } - - return noResult(); -} - -QString DeclFromDef::generateDeclaration(const CppQuickFixState &, - Function *method, - ClassOrNamespace *targetBinding) -{ - Q_UNUSED(targetBinding); - - Overview oo; - oo.setShowFunctionSignatures(true); - oo.setShowReturnTypes(true); - oo.setShowArgumentNames(true); - - QString decl; - decl += oo(method->type(), method->unqualifiedName()); - decl += QLatin1String(";\n"); - - return decl; -} diff --git a/src/plugins/cppeditor/cppdeclfromdef.h b/src/plugins/cppeditor/cppdeclfromdef.h deleted file mode 100644 index 8b71ef78c0..0000000000 --- a/src/plugins/cppeditor/cppdeclfromdef.h +++ /dev/null @@ -1,59 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** Commercial Usage -** -** Licensees holding valid Qt Commercial licenses may use this file in -** accordance with the Qt Commercial License Agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Nokia. -** -** GNU Lesser General Public License Usage -** -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** If you are unsure which license is appropriate for your use, please -** contact the sales department at http://qt.nokia.com/contact. -** -**************************************************************************/ - -#ifndef CPPDECLFROMDEF_H -#define CPPDECLFROMDEF_H - -#include "cppquickfix.h" - -#include - -namespace CPlusPlus { -class ClassOrNamespace; -} // namespace CPlusPlus - -namespace CppEditor { -namespace Internal { - -class DeclFromDef: public CppQuickFixFactory -{ -public: - virtual QList match(const CppQuickFixState &state); - -protected: - static QString generateDeclaration(const CppQuickFixState &state, - CPlusPlus::Function *method, - CPlusPlus::ClassOrNamespace *targetBinding); -}; - - -} // namespace Internal -} // namespace CppEditor - -#endif // CPPDECLFROMDEF_H diff --git a/src/plugins/cppeditor/cppeditor.pro b/src/plugins/cppeditor/cppeditor.pro index 4e37070300..0e86ffeb8e 100644 --- a/src/plugins/cppeditor/cppeditor.pro +++ b/src/plugins/cppeditor/cppeditor.pro @@ -19,7 +19,7 @@ HEADERS += cppplugin.h \ cppchecksymbols.h \ cppsemanticinfo.h \ cppoutline.h \ - cppdeclfromdef.h \ + cppinsertdecldef.h \ cpplocalsymbols.h \ cpptypehierarchy.h \ cppelementevaluator.h @@ -35,7 +35,7 @@ SOURCES += cppplugin.cpp \ cppchecksymbols.cpp \ cppsemanticinfo.cpp \ cppoutline.cpp \ - cppdeclfromdef.cpp \ + cppinsertdecldef.cpp \ cpplocalsymbols.cpp \ cpptypehierarchy.cpp \ cppelementevaluator.cpp diff --git a/src/plugins/cppeditor/cppinsertdecldef.cpp b/src/plugins/cppeditor/cppinsertdecldef.cpp new file mode 100644 index 0000000000..97ee79a640 --- /dev/null +++ b/src/plugins/cppeditor/cppinsertdecldef.cpp @@ -0,0 +1,182 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include "cppinsertdecldef.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace CPlusPlus; +using namespace CppEditor; +using namespace CppEditor::Internal; +using namespace CppTools; + +using CppEditor::CppRefactoringChanges; + +namespace { + +class Operation: public CppQuickFixOperation +{ +public: + Operation(const CppQuickFixState &state, int priority, + const QString &targetFileName, const Class *targetSymbol, + InsertionPointLocator::AccessSpec xsSpec, + const QString &decl) + : CppQuickFixOperation(state, priority) + , m_targetFileName(targetFileName) + , m_targetSymbol(targetSymbol) + , m_xsSpec(xsSpec) + , m_decl(decl) + { + QString type; + switch (xsSpec) { + case InsertionPointLocator::Public: type = QLatin1String("public"); break; + case InsertionPointLocator::Protected: type = QLatin1String("protected"); break; + case InsertionPointLocator::Private: type = QLatin1String("private"); break; + case InsertionPointLocator::PublicSlot: type = QLatin1String("public slot"); break; + case InsertionPointLocator::ProtectedSlot: type = QLatin1String("protected slot"); break; + case InsertionPointLocator::PrivateSlot: type = QLatin1String("private slot"); break; + default: break; + } + + setDescription(QCoreApplication::tr("Create %1 Declaration from Definition", + "CppEditor::DeclFromDef").arg(type)); + } + + void performChanges(CppRefactoringFile *, CppRefactoringChanges *refactoring) + { + CppRefactoringFile targetFile = refactoring->file(m_targetFileName); + Document::Ptr targetDoc = targetFile.cppDocument(); + InsertionPointLocator locator(targetDoc); + const InsertionLocation loc = locator.methodDeclarationInClass(m_targetSymbol, m_xsSpec); + Q_ASSERT(loc.isValid()); + + int targetPosition1 = targetFile.position(loc.line(), loc.column()); + int targetPosition2 = qMax(0, targetFile.position(loc.line(), 1) - 1); + + Utils::ChangeSet target; + target.insert(targetPosition1, loc.prefix() + m_decl); + targetFile.change(target); + targetFile.indent(Utils::ChangeSet::Range(targetPosition2, targetPosition1)); + + refactoring->activateEditor(m_targetFileName, targetPosition1); + } + +private: + QString m_targetFileName; + const Class *m_targetSymbol; + InsertionPointLocator::AccessSpec m_xsSpec; + QString m_decl; +}; + +} // anonymous namespace + +QList DeclFromDef::match(const CppQuickFixState &state) +{ + const QList &path = state.path(); + const CppRefactoringFile &file = state.currentFile(); + + FunctionDefinitionAST *funDef = 0; + int idx = 0; + for (; idx < path.size(); ++idx) { + AST *node = path.at(idx); + if (FunctionDefinitionAST *candidate = node->asFunctionDefinition()) { + if (!funDef && file.isCursorOn(candidate) && !file.isCursorOn(candidate->function_body)) + funDef = candidate; + } else if (node->asClassSpecifier()) { + return noResult(); + } + } + + if (!funDef || !funDef->symbol) + return noResult(); + + Function *method = funDef->symbol; + + if (ClassOrNamespace *targetBinding = state.context().lookupParent(method)) { + foreach (Symbol *s, targetBinding->symbols()) { + if (Class *clazz = s->asClass()) { + QList results; + const QLatin1String fn(clazz->fileName()); + const QString decl = generateDeclaration(state, + method, + targetBinding); + results.append(singleResult(new Operation(state, idx, fn, clazz, + InsertionPointLocator::Public, + decl))); + results.append(singleResult(new Operation(state, idx, fn, clazz, + InsertionPointLocator::Protected, + decl))); + results.append(singleResult(new Operation(state, idx, fn, clazz, + InsertionPointLocator::Private, + decl))); + results.append(singleResult(new Operation(state, idx, fn, clazz, + InsertionPointLocator::PublicSlot, + decl))); + results.append(singleResult(new Operation(state, idx, fn, clazz, + InsertionPointLocator::ProtectedSlot, + decl))); + results.append(singleResult(new Operation(state, idx, fn, clazz, + InsertionPointLocator::PrivateSlot, + decl))); + return results; + } //! \todo support insertion into namespaces + } + } + + return noResult(); +} + +QString DeclFromDef::generateDeclaration(const CppQuickFixState &, + Function *method, + ClassOrNamespace *targetBinding) +{ + Q_UNUSED(targetBinding); + + Overview oo; + oo.setShowFunctionSignatures(true); + oo.setShowReturnTypes(true); + oo.setShowArgumentNames(true); + + QString decl; + decl += oo(method->type(), method->unqualifiedName()); + decl += QLatin1String(";\n"); + + return decl; +} diff --git a/src/plugins/cppeditor/cppinsertdecldef.h b/src/plugins/cppeditor/cppinsertdecldef.h new file mode 100644 index 0000000000..024359d086 --- /dev/null +++ b/src/plugins/cppeditor/cppinsertdecldef.h @@ -0,0 +1,59 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#ifndef CPPINSERTDECLDEF_H +#define CPPINSERTDECLDEF_H + +#include "cppquickfix.h" + +#include + +namespace CPlusPlus { +class ClassOrNamespace; +} // namespace CPlusPlus + +namespace CppEditor { +namespace Internal { + +class DeclFromDef: public CppQuickFixFactory +{ +public: + virtual QList match(const CppQuickFixState &state); + +protected: + static QString generateDeclaration(const CppQuickFixState &state, + CPlusPlus::Function *method, + CPlusPlus::ClassOrNamespace *targetBinding); +}; + + +} // namespace Internal +} // namespace CppEditor + +#endif // CPPINSERTDECLDEF_H diff --git a/src/plugins/cppeditor/cppquickfix.cpp b/src/plugins/cppeditor/cppquickfix.cpp index 9d1c080609..eb5dce7a11 100644 --- a/src/plugins/cppeditor/cppquickfix.cpp +++ b/src/plugins/cppeditor/cppquickfix.cpp @@ -29,7 +29,6 @@ #include "cppquickfix.h" #include "cppeditor.h" -#include "cppdeclfromdef.h" #include #include diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 56709c1cf6..9434dcf055 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -29,7 +29,7 @@ #include "cppeditor.h" #include "cppquickfix.h" -#include "cppdeclfromdef.h" +#include "cppinsertdecldef.h" #include #include -- cgit v1.2.1