Commit 46f537e5 authored by ck's avatar ck

Quickfix: Convert underscore notation to camel case.

Reviewed-by: Roberto Raggi
parent 89cc8993
......@@ -660,7 +660,7 @@ void CPPEditor::hideRenameNotification()
Core::EditorManager::instance()->hideEditorInfoBar(QLatin1String("CppEditor.Rename"));
}
void CPPEditor::renameUsagesNow()
void CPPEditor::renameUsagesNow(const QString &replacement)
{
const SemanticInfo info = m_lastSemanticInfo;
......@@ -674,7 +674,7 @@ void CPPEditor::renameUsagesNow()
this, SLOT(hideRenameNotification()));
}
m_modelManager->renameUsages(canonicalSymbol, cs.context());
m_modelManager->renameUsages(canonicalSymbol, cs.context(), replacement);
}
}
}
......
......@@ -192,7 +192,7 @@ public Q_SLOTS:
void renameSymbolUnderCursor();
void renameUsages();
void findUsages();
void renameUsagesNow();
void renameUsagesNow(const QString &replacement = QString());
void hideRenameNotification();
protected:
......
......@@ -53,6 +53,7 @@
#include <Names.h>
#include <Literals.h>
#include <cppeditor/cppeditor.h>
#include <cppeditor/cpprefactoringchanges.h>
#include <cpptools/cpptoolsconstants.h>
#include <cpptools/cppmodelmanagerinterface.h>
......@@ -1576,6 +1577,75 @@ private:
BinaryExpressionAST *binaryAST;
};
/*
* Turns "an_example_symbol" into "anExampleSymbol" and
* "AN_EXAMPLE_SYMBOL" into "AnExampleSymbol".
*/
class ToCamelCaseConverter : public CppQuickFixOperation
{
public:
ToCamelCaseConverter(TextEditor::BaseTextEditor *editor)
: CppQuickFixOperation(editor)
{}
virtual QString description() const
{
return QApplication::translate("CppTools::QuickFix", "Convert to Camel Case ...");
}
virtual int match(const QList<AST *> &path)
{
if (path.isEmpty())
return -1;
AST * const ast = path.last();
const Name *name = 0;
if (const NameAST * const nameAst = ast->asName()) {
if (nameAst->name && nameAst->name->asNameId())
name = nameAst->name;
} else if (const NamespaceAST * const namespaceAst = ast->asNamespace()) {
name = namespaceAst->symbol->name();
}
if (!name)
return -1;
m_name = QString::fromUtf8(name->identifier()->chars());
if (m_name.length() < 3)
return -1;
for (int i = 1; i < m_name.length() - 1; ++i) {
if (isConvertibleUnderscore(i))
return path.size() - 1;
}
return -1;
}
virtual void createChanges()
{
for (int i = 1; i < m_name.length(); ++i) {
QCharRef c = m_name[i];
if (c.isUpper()) {
c = c.toLower();
} else if (i < m_name.length() - 1
&& isConvertibleUnderscore(i)) {
m_name.remove(i, 1);
m_name[i] = m_name.at(i).toUpper();
}
}
static_cast<CppEditor::Internal::CPPEditor*>(editor())->renameUsagesNow(m_name);
}
private:
bool isConvertibleUnderscore(int pos) const
{
return m_name.at(pos) == QLatin1Char('_') && m_name.at(pos+1).isLetter()
&& !(pos == 1 && m_name.at(0) == QLatin1Char('m'));
}
QString m_name;
};
} // end of anonymous namespace
......@@ -1785,6 +1855,7 @@ QList<TextEditor::QuickFixOperation::Ptr> CppQuickFixFactory::quickFixOperations
QSharedPointer<CompleteSwitchCaseStatement> completeSwitchCaseStatement(new CompleteSwitchCaseStatement(editor));
QSharedPointer<FixForwardDeclarationOp> fixForwardDeclarationOp(new FixForwardDeclarationOp(editor));
QSharedPointer<AddLocalDeclarationOp> addLocalDeclarationOp(new AddLocalDeclarationOp(editor));
QSharedPointer<ToCamelCaseConverter> toCamelCase(new ToCamelCaseConverter(editor));
QSharedPointer<DeclFromDef> declFromDef(new DeclFromDef(editor));
quickFixOperations.append(rewriteLogicalAndOp);
......@@ -1803,6 +1874,7 @@ QList<TextEditor::QuickFixOperation::Ptr> CppQuickFixFactory::quickFixOperations
quickFixOperations.append(completeSwitchCaseStatement);
quickFixOperations.append(fixForwardDeclarationOp);
quickFixOperations.append(addLocalDeclarationOp);
quickFixOperations.append(toCamelCase);
#if 0
quickFixOperations.append(declFromDef);
......
......@@ -236,10 +236,12 @@ void CppFindReferences::findUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::L
findAll_helper(symbol, context);
}
void CppFindReferences::renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context)
void CppFindReferences::renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
const QString &replacement)
{
if (const Identifier *id = symbol->identifier()) {
const QString textToReplace = QString::fromUtf8(id->chars(), id->size());
const QString textToReplace = replacement.isEmpty()
? QString::fromUtf8(id->chars(), id->size()) : replacement;
Find::SearchResult *search = _resultWindow->startNewSearch(Find::SearchResultWindow::SearchAndReplace);
_resultWindow->setTextToReplace(textToReplace);
......
......@@ -66,7 +66,8 @@ Q_SIGNALS:
public:
void findUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
const QString &replacement = QString());
void findMacroUses(const CPlusPlus::Macro &macro);
......
......@@ -794,10 +794,11 @@ void CppModelManager::findUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::Loo
m_findReferences->findUsages(symbol, context);
}
void CppModelManager::renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context)
void CppModelManager::renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
const QString &replacement)
{
if (symbol->identifier())
m_findReferences->renameUsages(symbol, context);
m_findReferences->renameUsages(symbol, context, replacement);
}
void CppModelManager::findMacroUsages(const CPlusPlus::Macro &macro)
......
......@@ -118,7 +118,8 @@ public:
virtual QList<int> references(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
virtual void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
virtual void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
const QString &replacement = QString());
virtual void findUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
virtual void findMacroUsages(const CPlusPlus::Macro &macro);
......
......@@ -139,7 +139,8 @@ public:
virtual QList<int> references(CPlusPlus::Symbol *symbol,
const CPlusPlus::LookupContext &context) = 0;
virtual void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context) = 0;
virtual void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
const QString &replacement = QString()) = 0;
virtual void findUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context) = 0;
virtual void findMacroUsages(const CPlusPlus::Macro &macro) = 0;
......
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