1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/*
Copyright 2009 David Nolden <david.nolden.kdevelop@art-master.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "adaptsignatureassistant.h"
#include <interfaces/icore.h>
#include <interfaces/ilanguagecontroller.h>
#include <language/assistant/renameaction.h>
#include <language/duchain/duchainutils.h>
#include <language/backgroundparser/backgroundparser.h>
#include <language/backgroundparser/parsejob.h>
#include <language/duchain/functiondefinition.h>
#include <language/duchain/types/functiontype.h>
#include <KTextEditor/Document>
#include <KTextEditor/View>
#include "cppduchain.h"
#include "qtfunctiondeclaration.h"
using namespace KDevelop;
using namespace Cpp;
namespace {
Declaration *getDeclarationAtCursor(const SimpleCursor &cursor, const KUrl &documentUrl)
{
ENSURE_CHAIN_READ_LOCKED
ReferencedTopDUContext top(DUChainUtils::standardContextForUrl(documentUrl));
if(!top)
return 0;
Declaration* functionDecl = DUChainUtils::declarationInLine(cursor, top.data());
return functionDecl;
}
Signature getDeclarationSignature(const Declaration *functionDecl, const DUContext *functionCtxt, bool includeDefaults)
{
ENSURE_CHAIN_READ_LOCKED
int pos = 0;
Signature signature;
const AbstractFunctionDeclaration* abstractFunDecl = dynamic_cast<const AbstractFunctionDeclaration*>(functionDecl);
foreach(Declaration* parameter, functionCtxt->localDeclarations()) {
signature.defaultParams << (includeDefaults ? abstractFunDecl->defaultParameterForArgument(pos).str() : "");
signature.parameters << qMakePair(parameter->indexedType(), parameter->identifier().identifier().str());
++pos;
}
signature.isConst = functionDecl->abstractType() && functionDecl->abstractType()->modifiers() & AbstractType::ConstModifier;
FunctionType::Ptr funType = functionDecl->type<FunctionType>();
if(funType)
signature.returnType = funType->returnType()->indexed();
return signature;
}
}
AdaptSignatureAssistant::AdaptSignatureAssistant(ILanguageSupport* supportedLanguage)
: StaticAssistant(supportedLanguage)
{
connect(ICore::self()->languageController()->backgroundParser(),
SIGNAL(parseJobFinished(KDevelop::ParseJob*)),
SLOT(parseJobFinished(KDevelop::ParseJob*)));
}
QString AdaptSignatureAssistant::title() const
{
return tr("Adapt Signature");
}
void AdaptSignatureAssistant::reset()
{
doHide();
clearActions();
m_editingDefinition = {};
m_declarationName = {};
m_otherSideId = {};
m_otherSideTopContext = {};
m_otherSideContext = {};
m_oldSignature = {};
m_document = {};
m_view.clear();
}
void AdaptSignatureAssistant::textChanged(KTextEditor::View* view, const KTextEditor::Range& invocationRange, const QString& removedText)
{
reset();
m_view = view;
//FIXME: update signature assistant to play well with the rename assistant
KTextEditor::Range sigAssistRange = invocationRange;
if (!removedText.isEmpty()) {
sigAssistRange.setRange(sigAssistRange.start(), sigAssistRange.start());
}
m_document = view->document()->url();
DUChainReadLocker lock(DUChain::lock(), 300);
if(!lock.locked()) {
kDebug() << "failed to lock duchain in time";
return;
}
SimpleRange simpleInvocationRange = SimpleRange(sigAssistRange);
Declaration* funDecl = getDeclarationAtCursor(simpleInvocationRange.start, m_document);
if(!funDecl || !funDecl->type<FunctionType>())
return;
if(QtFunctionDeclaration* classFun = dynamic_cast<QtFunctionDeclaration*>(funDecl)) {
if (classFun->isSignal()) {
// do not offer to change signature of a signal, as the implementation will be generated by moc
return;
}
}
Declaration* otherSide = 0;
FunctionDefinition* definition = dynamic_cast<FunctionDefinition*>(funDecl);
if (definition)
{
m_editingDefinition = true;
otherSide = definition->declaration();
}
else if ((definition = FunctionDefinition::definition(funDecl)))
{
m_editingDefinition = false;
otherSide = definition;
}
if (!otherSide)
return;
m_otherSideContext = DUContextPointer(DUChainUtils::getFunctionContext(otherSide));
if (!m_otherSideContext)
return;
m_declarationName = funDecl->identifier();
m_otherSideId = otherSide->id();
m_otherSideTopContext = ReferencedTopDUContext(otherSide->topContext());
m_oldSignature = getDeclarationSignature(otherSide, m_otherSideContext.data(), true);
//Schedule an update, to make sure the ranges match
DUChain::self()->updateContextForUrl(m_otherSideTopContext->url(), TopDUContext::AllDeclarationsAndContexts);
}
bool AdaptSignatureAssistant::isUseful() const
{
return !m_declarationName.isEmpty() && m_otherSideId.isValid();
}
bool AdaptSignatureAssistant::getSignatureChanges(const Signature& newSignature, QList< int >& oldPositions) const
{
bool changed = false;
for (int i = 0; i < newSignature.parameters.size(); ++i)
oldPositions.append(-1);
for (int curNewParam = newSignature.parameters.size() - 1; curNewParam >= 0 ; --curNewParam)
{
int foundAt = -1;
for (int curOldParam = m_oldSignature.parameters.size() - 1; curOldParam >= 0 ; --curOldParam)
{
if (newSignature.parameters[curNewParam].first != m_oldSignature.parameters[curOldParam].first)
continue; //Different type == different parameters
if (newSignature.parameters[curNewParam].second == m_oldSignature.parameters[curOldParam].second || curOldParam == curNewParam)
{
//given the same type and either the same position or the same name, it's (probably) the same argument
foundAt = curOldParam;
if (newSignature.parameters[curNewParam].second != m_oldSignature.parameters[curOldParam].second || curOldParam != curNewParam)
changed = true; //Either the name changed at this position, or position of this name has changed
if (newSignature.parameters[curNewParam].second == m_oldSignature.parameters[curOldParam].second)
break; //Found an argument with the same name and type, no need to look further
//else: position/type match, but name match will trump, allowing: (int i=0, int j=1) => (int j=1, int i=0)
}
}
if (foundAt < 0)
changed = true;
oldPositions[curNewParam] = foundAt;
}
if(newSignature.parameters.size() != m_oldSignature.parameters.size())
changed = true;
if(newSignature.isConst != m_oldSignature.isConst)
changed = true;
if(newSignature.returnType != m_oldSignature.returnType)
changed = true;
return changed;
}
void AdaptSignatureAssistant::setDefaultParams(Signature& newSignature, const QList< int >& oldPositions) const
{
for(int i = newSignature.parameters.size() - 1; i >= 0; --i)
{
if (oldPositions[i] == -1)
return; //this param is new, no further defaults possible
if (i == newSignature.defaultParams.size() - 1 || !newSignature.defaultParams[i+1].isEmpty())
newSignature.defaultParams[i] = m_oldSignature.defaultParams[oldPositions[i]];
}
}
QList< RenameAction* > AdaptSignatureAssistant::getRenameActions(const Signature &newSignature, const QList<int> &oldPositions) const
{
Q_ASSERT(DUChain::lock()->currentThreadHasReadLock());
QList<RenameAction*> renameActions;
if (!m_otherSideContext)
return renameActions;
for(int i = newSignature.parameters.size() - 1; i >= 0; --i)
{
if (oldPositions[i] == -1)
continue; //new parameter
Declaration *renamedDecl = m_otherSideContext->localDeclarations()[oldPositions[i]];
if (newSignature.parameters[i].second != m_oldSignature.parameters[oldPositions[i]].second) {
QMap< IndexedString, QList< RangeInRevision > > uses = renamedDecl->uses();
if (!uses.isEmpty()) {
renameActions << new RenameAction(renamedDecl->identifier(), newSignature.parameters[i].second,
RevisionedFileRanges::convert(uses));
}
}
}
return renameActions;
}
void AdaptSignatureAssistant::parseJobFinished(KDevelop::ParseJob* job)
{
if (job->document().toUrl() != m_document || !m_view)
return;
clearActions();
DUChainReadLocker lock;
Declaration *functionDecl = getDeclarationAtCursor(SimpleCursor(m_view.data()->cursorPosition()), m_document);
if (!functionDecl || functionDecl->identifier() != m_declarationName)
return;
DUContext *functionCtxt = DUChainUtils::getFunctionContext(functionDecl);
if (!functionCtxt)
return;
if(QtFunctionDeclaration* classFun = dynamic_cast<QtFunctionDeclaration*>(functionDecl)) {
if (classFun->isSignal()) {
// do not offer to change signature of a signal, as the implementation will be generated by moc
return;
}
}
//ParseJob having finished, get the signature that was modified
Signature newSignature = getDeclarationSignature(functionDecl, functionCtxt, false);
//Check for changes between m_oldSignature and newSignature, use oldPositions to store old<->new param index mapping
QList<int> oldPositions;
if (!getSignatureChanges(newSignature, oldPositions)) {
reset();
return; //No changes to signature
}
QList<RenameAction*> renameActions;
if (m_editingDefinition)
setDefaultParams(newSignature, oldPositions); //restore default parameters before updating the declarations
else
renameActions = getRenameActions(newSignature, oldPositions); //rename as needed when updating the definition
IAssistantAction::Ptr action(new AdaptSignatureAction(m_otherSideId, m_otherSideTopContext,
m_oldSignature, newSignature,
m_editingDefinition, renameActions));
connect(action.data(), SIGNAL(executed(IAssistantAction*)), SLOT(reset()));
addAction(action);
emit actionsChanged();
}
#include "adaptsignatureassistant.moc"
|