File: signatureassistant.cpp

package info (click to toggle)
kdevelop 4%3A4.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 18,844 kB
  • sloc: cpp: 91,758; python: 1,095; lex: 422; ruby: 120; sh: 114; xml: 42; makefile: 38
file content (412 lines) | stat: -rw-r--r-- 16,675 bytes parent folder | download
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
   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 "signatureassistant.h"

#include <interfaces/icore.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/ilanguagecontroller.h>

#include <language/duchain/duchainlock.h>
#include <language/duchain/duchain.h>
#include <language/duchain/duchainutils.h>
#include <language/backgroundparser/backgroundparser.h>
#include <language/duchain/declaration.h>
#include <language/backgroundparser/parsejob.h>
#include <language/duchain/functiondefinition.h>
#include <language/codegen/documentchangeset.h>
#include <language/duchain/types/functiontype.h>
#include <language/duchain/parsingenvironment.h>
#include <language/duchain/types/arraytype.h>

#include <KTextEditor/Document>
#include <KTextEditor/View>
#include <KLocalizedString>
#include <KMessageBox>

#include "cppduchain.h"

using namespace  KDevelop;
using namespace Cpp;

AdaptDefinitionSignatureAssistant::AdaptDefinitionSignatureAssistant(KTextEditor::View* view,
                                                                     const KTextEditor::Range& inserted)
: m_editingDefinition(false)
, m_view(view)
{
  connect(KDevelop::ICore::self()->languageController()->backgroundParser(), SIGNAL(parseJobFinished(KDevelop::ParseJob*)), SLOT(parseJobFinished(KDevelop::ParseJob*)));
  m_document = KDevelop::IndexedString(view->document()->url());

  m_invocationRange = SimpleRange(inserted);

//   kDebug() << "checking";

  KDevelop::DUChainReadLocker lock(KDevelop::DUChain::lock(), 300);
  if(!lock.locked()) {
    kDebug() << "failed to lock duchain in time";
    return;
  }
  TopDUContext* top = DUChainUtils::standardContextForUrl(m_document.toUrl());
  if(!top)
    return;

  Declaration* funDecl = DUChainUtils::declarationInLine(m_invocationRange.start, top);

  if(!funDecl || !funDecl->type<KDevelop::FunctionType>()) {
//     kDebug() << "no declaration found in line";
    return;
  }

  m_declarationName = funDecl->identifier();
//   kDebug() << "found local function declaration:" << m_declarationName.toString();
  m_document = top->url();

  KDevelop::FunctionDefinition* definition = dynamic_cast<KDevelop::FunctionDefinition*>(funDecl);

  KDevelop::Declaration* otherSide = 0;

  if(definition) {
    m_editingDefinition = true;
    otherSide = definition->declaration(top);
    if(otherSide) {
      kDebug() << "found declaration" << otherSide->qualifiedIdentifier().toString();

      m_definitionId = otherSide->id();
      m_definitionContext = KDevelop::ReferencedTopDUContext(otherSide->topContext());
    }
  }else if((definition = KDevelop::FunctionDefinition::definition(funDecl))) {
    m_editingDefinition = false;

    otherSide = definition;

    kDebug() << "found definition" << m_definitionId.qualifiedIdentifier().toString();

    m_definitionId = definition->id();
    m_definitionContext = KDevelop::ReferencedTopDUContext(definition->topContext());
  }

  if(!otherSide) {
    kDebug() << "not found other side";
    return;
  }

  DUContext* otherSideFunctionContext = DUChainUtils::getFunctionContext(otherSide);
  AbstractFunctionDeclaration* otherFunDecl = dynamic_cast<AbstractFunctionDeclaration*>(otherSide);

  if(!otherSideFunctionContext || !otherFunDecl) {
    kDebug() << "no function-context for definition";
    return;
  }

  int pos = 0;

  foreach(Declaration* parameter, otherSideFunctionContext->localDeclarations()) {
    m_oldSignature.defaultParams << otherFunDecl->defaultParameterForArgument(pos).str();
    m_oldSignature.parameters << qMakePair(parameter->indexedType(), parameter->identifier().identifier().str());
    ++pos;
  }
  m_oldSignature.isConst = otherSide->abstractType() && otherSide->abstractType()->modifiers() & AbstractType::ConstModifier;

  KDevelop::FunctionType::Ptr funType = otherSide->type<KDevelop::FunctionType>();
  if(funType)
    m_oldSignature.returnType = funType->returnType()->indexed();

  //Schedule an update, to make sure the ranges match
  DUChain::self()->updateContextForUrl(m_definitionContext->url(), KDevelop::TopDUContext::AllDeclarationsAndContexts);

}

bool AdaptDefinitionSignatureAssistant::isUseful() {
  kDebug() << m_declarationName.toString() << m_definitionId.qualifiedIdentifier().toString();
  return !m_declarationName.isEmpty() && m_definitionId.isValid();
}

DUContext* AdaptDefinitionSignatureAssistant::findFunctionContext(const KUrl& url,
                                                                  const KDevelop::SimpleRange& _range) const {
  KDevelop::DUChainReadLocker lock(KDevelop::DUChain::lock());
  TopDUContext* top = DUChainUtils::standardContextForUrl(url);

  if(top) {
    RangeInRevision range = top->transformToLocalRevision(_range);
    DUContext* context = top->findContextAt(range.start, true);
    if(context == top)
      context = top->findContextAt(range.end, true);

    if(context && context->type() == DUContext::Function && context->owner()) {
      return context;
    }
  }
  return 0;
}

QString makeSignatureString(const Signature& signature, DUContext* visibilityFrom) {
  QString ret;
  int pos = 0;
  foreach(const ParameterItem& item, signature.parameters) {
    if(!ret.isEmpty())
      ret += ", ";

    ///TODO: merge common code with helpers.cpp::createArgumentList
    AbstractType::Ptr type = item.first.abstractType();

    QString arrayAppendix;
    ArrayType::Ptr arrayType;
    while ((arrayType = type.cast<ArrayType>())) {
      type = arrayType->elementType();
      //note: we have to prepend since we iterate from outside, i.e. from right to left.
      if (arrayType->dimension()) {
        arrayAppendix.prepend(QString("[%1]").arg(arrayType->dimension()));
      } else {
        // dimensionless
        arrayAppendix.prepend("[]");
      }
    }
    ret += Cpp::simplifiedTypeString(type,  visibilityFrom);

    if(!item.second.isEmpty())
      ret += " " + item.second;

    ret += arrayAppendix;

    if (signature.defaultParams.size() > pos && !signature.defaultParams[pos].isEmpty())
      ret += " = " + signature.defaultParams[pos];

    ++pos;
  }
  return ret;
}

class AdaptSignatureAction : public KDevelop::IAssistantAction {
  public:
    AdaptSignatureAction(const KDevelop::DeclarationId& definitionId,
                         KDevelop::ReferencedTopDUContext definitionContext,
                         const Signature& oldSignature,
                         const Signature& newSignature,
                         bool editingDefinition)
    : m_otherSideId(definitionId),
    m_otherSideContext(definitionContext),
    m_oldSignature(oldSignature),
    m_newSignature(newSignature),
    m_editingDefinition(editingDefinition) {
    }

    virtual QString description() const {
      return i18n("Update %1 signature", m_editingDefinition ? i18n("declaration") : i18n("definition"));
    }

    virtual QString toolTip() const {
      KDevelop::DUChainReadLocker lock(KDevelop::DUChain::lock());
      return i18n("Update %1\nfrom: %2(%3)%4\nto: %2(%5)%6",
                  m_editingDefinition ? i18n("declaration") : i18n("definition"),
                  m_otherSideId.qualifiedIdentifier().toString(),
                  makeSignatureString(m_oldSignature, m_otherSideContext.data()),
                  m_oldSignature.isConst ? " const" : "",
                  makeSignatureString(m_newSignature, m_otherSideContext.data()),
                  m_newSignature.isConst ? " const" : "");
    }

    virtual void execute() {
      KDevelop::DUChainReadLocker lock(KDevelop::DUChain::lock());
      IndexedString url = m_otherSideContext->url();
      lock.unlock();
      m_otherSideContext = DUChain::self()->waitForUpdate(url, TopDUContext::AllDeclarationsContextsAndUses);
      if(!m_otherSideContext) {
        kDebug() << "failed to update" << url.str();
        return;
      }

      lock.lock();

      Declaration* otherSide = m_otherSideId.getDeclaration(m_otherSideContext.data());
      if(!otherSide) {
        kDebug() << "could not find definition";
        return;
      }

      DUContext* functionContext = DUChainUtils::getFunctionContext(otherSide);
      if(!functionContext) {
        kDebug() << "no function context";
        return;
      }

      ///@todo Handle return-type
      ///@todo Eventually do real refactoring-like renaming of the function?
      if(!functionContext || functionContext->type() != DUContext::Function) {
        kDebug() << "no correct function context";
        return;
      }

      DocumentChangeSet changes;
      DocumentChange changeParameters(functionContext->url(), functionContext->rangeInCurrentRevision(), QString(), makeSignatureString(m_newSignature, m_otherSideContext.data()));
      changeParameters.m_ignoreOldText = true;
      changes.addChange( changeParameters );
      if (m_oldSignature.isConst != m_newSignature.isConst) {
        ///TODO: also use code representation here
        RangeInRevision range = functionContext->range();
        // go after closing paren
        range.end.column++;
        // start == end (default when not const before)
        range.start = range.end;
        QString oldText;
        QString newText;
        if (m_oldSignature.isConst) {
          range.end.column += 6;
          oldText = " const";
        } else {
          newText = " const";
        }
        DocumentChange changeConstness(functionContext->url(), range.castToSimpleRange(), oldText, newText);
        changes.addChange(changeConstness);
      }
      if (m_oldSignature.returnType != m_newSignature.returnType) {
        CodeRepresentation::Ptr document = createCodeRepresentation(functionContext->url());
        int l = functionContext->range().start.line;
        QString line = document->line(l);
        QRegExp exe( QString("^(\\s*)(.+)\\s+(?:\\w+::)*\\b%1\\s*\\(").arg(otherSide->identifier().toString()), Qt::CaseSensitive, QRegExp::RegExp2 );
        int pos = exe.indexIn(line);
        if (pos != -1) {
          QString oldText = exe.cap(2);
          SimpleRange range = SimpleRange(l, exe.cap(1).length(), l, exe.cap(1).length() + oldText.length());
          QString newText = Cpp::simplifiedTypeString(m_newSignature.returnType.abstractType(), functionContext->parentContext());
          DocumentChange changeRetType(functionContext->url(), range, oldText, newText);
          changes.addChange(changeRetType);
        }
      }
      changes.setReplacementPolicy(DocumentChangeSet::WarnOnFailedChange);
      DocumentChangeSet::ChangeResult result = changes.applyAllChanges();
      if(!result) {
        KMessageBox::error(0, i18n("Failed to apply changes: %1", result.m_failureReason));
      }
    }

    KDevelop::DeclarationId m_otherSideId;
    KDevelop::ReferencedTopDUContext m_otherSideContext;
    Signature m_oldSignature;
    Signature m_newSignature;
    bool m_editingDefinition;
};

/* -- only needed for return type updating, which isn't supported yet
static QString typeToString(KDevelop::IndexedType type) {
  KDevelop::AbstractType::Ptr t = type.abstractType();
  if(t)
    return t->toString();
  else
    return QString();
}*/

void AdaptDefinitionSignatureAssistant::parseJobFinished(KDevelop::ParseJob* job) {
  if(job->document() == m_document) {
    kDebug() << "parse job finshed for current document";
    clearActions();
    if(!m_view) {
      kDebug() << "breaking";
      return;
    }
    SimpleCursor currentPos(m_view->cursorPosition());
    KDevelop::DUChainReadLocker lock(KDevelop::DUChain::lock());
    KDevelop::ReferencedTopDUContext top(DUChainUtils::standardContextForUrl(m_document.toUrl()));
    if(!top)
      return;

    Declaration* decl = DUChainUtils::declarationInLine(currentPos, top.data());
    if(decl && decl->identifier() == m_declarationName) {
      DUContext* context = DUChainUtils::getFunctionContext(decl);
      if(context) {
        int pos = 0;
        Signature newSignature;
        AbstractFunctionDeclaration* contextFunction = dynamic_cast<AbstractFunctionDeclaration*>(decl);
        foreach(Declaration* parameter, context->localDeclarations()) {
//           kDebug() << "parameter:" << parameter->toString() << parameter->range().textRange();
          newSignature.defaultParams << contextFunction->defaultParameterForArgument(pos).str();
          newSignature.parameters << qMakePair(parameter->indexedType(), parameter->identifier().identifier().str());
          ++pos;
        }
        newSignature.isConst = decl->abstractType() && decl->abstractType()->modifiers() & AbstractType::ConstModifier;

        KDevelop::IndexedType newReturnType;
        FunctionType::Ptr funType = decl->type<FunctionType>();
        if(funType)
          newSignature.returnType = funType->returnType()->indexed();

        bool changed = false;
        bool canHaveDefault = false;
        for (int curNewParam = newSignature.parameters.size() - 1; curNewParam >= 0 ; --curNewParam)
        {//detect changes in parameters, assign default arguments as needed
          int foundAt = -1;
          canHaveDefault = canHaveDefault | (curNewParam == newSignature.parameters.size() - 1);

          for (int curOldParam = m_oldSignature.parameters.size() - 1; curOldParam >= 0 ; --curOldParam) {
            if (newSignature.parameters[curNewParam].first == m_oldSignature.parameters[curOldParam].first) {
               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;
          else if (!m_oldSignature.defaultParams[foundAt].isEmpty() &&
                    newSignature.defaultParams[curNewParam].isEmpty()) {
            //the other side's arg specified a default, this side didn't, preserve old default for this arg
            if (canHaveDefault)
              newSignature.defaultParams[curNewParam] = m_oldSignature.defaultParams[foundAt];
          } else {
            canHaveDefault = false; //This param didn't have a default, none that follow may either
          }
        }

        if(newSignature.parameters.size() != m_oldSignature.parameters.size())
          changed = true;

        if(newSignature.isConst != m_oldSignature.isConst)
          changed = true;

        //We only need to fiddle around with default parameters if we're updating the declaration
        if(!m_editingDefinition) {
          for(QList<QString>::iterator it = newSignature.defaultParams.begin(); it != newSignature.defaultParams.end(); ++it)
            *it = QString();
        }

        changed = changed || newSignature.returnType != m_oldSignature.returnType;

        if(changed) {
          kDebug() << "signature changed";
          addAction(IAssistantAction::Ptr(new AdaptSignatureAction(m_definitionId, m_definitionContext, m_oldSignature, newSignature, m_editingDefinition)));
        }else{
          kDebug() << "signature stayed equal";
        }
      }
    }else{
      kDebug() << "found no declaration in line" << currentPos.textCursor() << "in document" << job->duChain();
    }
    emit actionsChanged();
  }
}

#include "signatureassistant.moc"