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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
/*
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 "sourcemanipulation.h"
#include <language/codegen/coderepresentation.h>
#include "qtfunctiondeclaration.h"
#include "declarationbuilder.h"
#include "templateparameterdeclaration.h"
#include <language/duchain/parsingenvironment.h>
#include <language/duchain/stringhelpers.h>
using namespace KDevelop;
///Makes sure the line is not in a comment, moving it behind if needed. Just does very simple matching, should be ok for header copyright-notices and such.
int KDevelop::SourceCodeInsertion::firstValidCodeLineBefore(int lineNumber) const {
if(lineNumber == -1)
lineNumber = 1000000;
if(lineNumber > 300)
lineNumber = 300; //Don't do too much processing
int checkLines = m_codeRepresentation->lines() < lineNumber ? m_codeRepresentation->lines() : lineNumber;
int chosen = -1;
//Add some whitespace so we always have some comment clearing done, in every line
QString allText = " \n";
// we've added a line, hence increase our limit as well
checkLines += 1;
for(int a = 0; a < checkLines; ++a)
allText += m_codeRepresentation->line(a) + " \n";
allText = KDevelop::clearComments(allText, '$');
QStringList lines = allText.split('\n');
if(lines.count() < checkLines)
checkLines = lines.count();
int lastDefine = -1;
int lastComment = -1;
for(int a = 0; a < checkLines; ++a) {
if(lines.at(a).startsWith('$')) {
chosen = -1;
lastComment = a;
continue;
}
QString trimmedLine = lines.at(a).trimmed();
if(trimmedLine.startsWith('#')) {
lastDefine = a;
chosen = -1;
continue;
}
if(trimmedLine.isEmpty() && chosen == -1)
chosen = a;
if(!trimmedLine.isEmpty())
break;
}
if(chosen == -1 && lastDefine != -1) {
chosen = lastDefine + 1;
}
if (chosen == -1 && lastComment != -1) {
chosen = lastComment + 1;
}
if(chosen != -1) {
// if chosen == 0 we can only add at the start of the document
// NOTE: chosen must be reduced by one since we add one line
// at the beginning, see above (allText = ...)
return qMax(0, chosen - 1);
} else
return lineNumber;
}
//Re-indents the code so the leftmost line starts at zero
QString zeroIndentation(QString str, int fromLine = 0) {
QStringList lines = str.split('\n');
QStringList ret;
if(fromLine < lines.size()) {
ret = lines.mid(0, fromLine);
lines = lines.mid(fromLine);
}
QRegExp nonWhiteSpace("\\S");
int minLineStart = 10000;
foreach(const QString& line, lines) {
int lineStart = line.indexOf(nonWhiteSpace);
if(lineStart < minLineStart)
minLineStart = lineStart;
}
foreach(const QString& line, lines)
ret << line.mid(minLineStart);
return ret.join("\n");
}
KDevelop::DocumentChangeSet& KDevelop::SourceCodeInsertion::changes() {
return m_changeSet;
}
void KDevelop::SourceCodeInsertion::setInsertBefore(KDevelop::SimpleCursor position) {
m_insertBefore = position;
}
void KDevelop::SourceCodeInsertion::setContext(KDevelop::DUContext* context) {
m_context = context;
}
void KDevelop::SourceCodeInsertion::setSubScope(KDevelop::QualifiedIdentifier scope) {
m_scope = scope;
DUContext* context = m_topContext;
if(m_context)
context = m_context;
if(!context)
return;
QStringList needNamespace = m_scope.toStringList();
bool foundChild = true;
while(!needNamespace.isEmpty() && foundChild) {
foundChild = false;
foreach(DUContext* child, context->childContexts()) {
kDebug() << "checking child" << child->localScopeIdentifier().toString() << "against" << needNamespace.first();
if(child->localScopeIdentifier().toString() == needNamespace.first() && child->type() == DUContext::Namespace && (child->rangeInCurrentRevision().start < m_insertBefore || !m_insertBefore.isValid())) {
kDebug() << "taking";
context = child;
foundChild = true;
needNamespace.pop_front();
break;
}
}
}
m_context = context;
m_scope = Cpp::stripPrefixes(context, QualifiedIdentifier(needNamespace.join("::")));
}
QString KDevelop::SourceCodeInsertion::applySubScope(QString decl) const {
QString ret;
QString scopeType = "namespace";
QString scopeClose;
if(m_context && m_context->type() == DUContext::Class) {
scopeType = "struct";
scopeClose = ";";
}
foreach(const QString& scope, m_scope.toStringList())
ret += scopeType + " " + scope + " {\n";
ret += decl;
ret += QString("}" + scopeClose + "\n").repeated(m_scope.count());
return ret;
}
void KDevelop::SourceCodeInsertion::setAccess(KDevelop::Declaration::AccessPolicy access) {
m_access = access;
}
KDevelop::SourceCodeInsertion::SourceCodeInsertion(KDevelop::TopDUContext* topContext)
: m_context(topContext), m_access(Declaration::Public), m_topContext(topContext),
m_codeRepresentation(KDevelop::createCodeRepresentation(m_topContext->url()))
{
if(m_topContext->parsingEnvironmentFile() && m_topContext->parsingEnvironmentFile()->isProxyContext()) {
kWarning() << "source-code manipulation on proxy-context is wrong!!!" << m_topContext->url().toUrl();
}
m_insertBefore = SimpleCursor::invalid();
}
KDevelop::SourceCodeInsertion::~SourceCodeInsertion() {
}
QString KDevelop::SourceCodeInsertion::accessString() const {
switch(m_access) {
case KDevelop::Declaration::Public:
return "public";
case KDevelop::Declaration::Protected:
return "protected";
case KDevelop::Declaration::Private:
return "private";
default:
return QString();
}
}
QString KDevelop::SourceCodeInsertion::indentation() const {
if(!m_codeRepresentation || !m_context || m_context->localDeclarations(m_topContext).size() == 0) {
kDebug() << "cannot do indentation";
return QString();
}
foreach(Declaration* decl, m_context->localDeclarations(m_topContext)) {
if(decl->range().isEmpty() || decl->range().start.column == 0)
continue; //Skip declarations with empty range, that were expanded from macros
int spaces = 0;
QString textLine = m_codeRepresentation->line(decl->range().start.line);
for(int a = 0; a < textLine.size(); ++a) {
if(textLine.at(a).isSpace())
++spaces;
else
break;
}
return textLine.left(spaces);
}
return QString();
}
QString KDevelop::SourceCodeInsertion::applyIndentation(QString decl) const {
QStringList lines = decl.split('\n');
QString ind = indentation();
QStringList ret;
foreach(const QString& line, lines) {
if(!line.isEmpty())
ret << ind + line;
else
ret << line;
}
return ret.join("\n");;
}
QString makeSignatureString(QList<SourceCodeInsertion::SignatureItem> signature, DUContext* context) {
QString ret;
foreach(const SourceCodeInsertion::SignatureItem& item, signature) {
if(!ret.isEmpty())
ret += ", ";
AbstractType::Ptr type = TypeUtils::removeConstants(item.type, context->topContext());
ret += Cpp::simplifiedTypeString(type, context);
if(!item.name.isEmpty())
ret += " " + item.name;
}
return ret;
}
SimpleRange SourceCodeInsertion::insertionRange(int line)
{
if(line == 0 || !m_codeRepresentation)
return SimpleRange(line, 0, line, 0);
else
{
SimpleRange range(line-1, m_codeRepresentation->line(line-1).size(), line-1, m_codeRepresentation->line(line-1).size());
//If the context finishes on that line, then this will need adjusting
if(!m_context->rangeInCurrentRevision().textRange().contains(range.textRange()))
{
range.start = m_context->rangeInCurrentRevision().end;
if(range.start.column > 0)
range.start.column -= 1;
range.end = range.start;
}
return range;
}
}
bool KDevelop::SourceCodeInsertion::insertFunctionDeclaration(KDevelop::Identifier name, AbstractType::Ptr returnType, QList<SignatureItem> signature, bool isConstant, QString body) {
if(!m_context)
return false;
returnType = TypeUtils::removeConstants(returnType, m_topContext);
QString decl = (returnType ? (Cpp::simplifiedTypeString(returnType, m_context) + " ") : QString()) + name.toString() + "(" + makeSignatureString(signature, m_context) + ")";
if(isConstant)
decl += " const";
if(body.isEmpty())
decl += ";";
else {
if (!body.startsWith(' ') && !body.startsWith('\n')) {
decl += " ";
}
decl += zeroIndentation(body);
}
InsertionPoint insertion = findInsertionPoint(m_access, Function);
decl = "\n" + applyIndentation(applySubScope(insertion.prefix +decl));
return m_changeSet.addChange(DocumentChange(m_context->url(), insertionRange(insertion.line), QString(), decl));
}
bool KDevelop::SourceCodeInsertion::insertVariableDeclaration(KDevelop::Identifier name, KDevelop::AbstractType::Ptr type) {
if(!m_context)
return false;
type = TypeUtils::removeConstants(type, m_topContext);
QString decl = Cpp::simplifiedTypeString(type, m_context) + " " + name.toString() + ";";
InsertionPoint insertion = findInsertionPoint(m_access, Variable);
decl = "\n" + applyIndentation(applySubScope(insertion.prefix + decl));
return m_changeSet.addChange(DocumentChange(m_context->url(), insertionRange(insertion.line), QString(), decl));
}
SimpleCursor SourceCodeInsertion::end() const
{
SimpleCursor ret = m_context->rangeInCurrentRevision().end;
if(m_codeRepresentation && m_codeRepresentation->lines() && dynamic_cast<TopDUContext*>(m_context)) {
ret.line = m_codeRepresentation->lines()-1;
ret.column = m_codeRepresentation->line(ret.line).size();
}
return ret;
}
SourceCodeInsertion::InsertionPoint SourceCodeInsertion::findInsertionPoint(KDevelop::Declaration::AccessPolicy policy, InsertionKind kind) const {
Q_UNUSED(policy);
InsertionPoint ret;
ret.line = end().line;
bool behindExistingItem = false;
//Try twice, in the second run, only match the "access"
for(int anyMatch = 0; anyMatch <= 1 && !behindExistingItem; ++anyMatch) {
foreach(Declaration* decl, m_context->localDeclarations()) {
ClassMemberDeclaration* classMem = dynamic_cast<ClassMemberDeclaration*>(decl);
if(m_context->type() != DUContext::Class || (classMem && classMem->accessPolicy() == m_access)) {
Cpp::QtFunctionDeclaration* qtFunction = dynamic_cast<Cpp::QtFunctionDeclaration*>(decl);
if( (kind != Slot && anyMatch) || //Only allow anyMatch if not searching a slot, since else it may end up in a wrong section, not being a slot at all
(kind == Slot && qtFunction && qtFunction->isSlot()) ||
(kind == Function && dynamic_cast<AbstractFunctionDeclaration*>(decl)) ||
(kind == Variable && decl->kind() == Declaration::Instance && !dynamic_cast<AbstractFunctionDeclaration*>(decl)) ) {
behindExistingItem = true;
ret.line = decl->range().end.line+1;
if(decl->internalContext())
ret.line = decl->internalContext()->range().end.line+1;
}
}
}
}
kDebug() << ret.line << m_context->scopeIdentifier(true) << m_context->rangeInCurrentRevision().textRange() << behindExistingItem << m_context->url().toUrl() << m_context->parentContext();
kDebug() << "is proxy:" << m_context->topContext()->parsingEnvironmentFile()->isProxyContext() << "count of declarations:" << m_context->topContext()->localDeclarations().size();
if(!behindExistingItem) {
ClassDeclaration* classDecl = dynamic_cast<ClassDeclaration*>(m_context->owner());
if(kind != Slot && m_access == Declaration::Public && classDecl && classDecl->classType() == ClassDeclarationData::Struct) {
//Nothing to do, we can just insert into a struct if it should be public
}else if(m_context->type() == DUContext::Class) {
ret.prefix = accessString();
if(kind == Slot)
ret.prefix += " slots";
ret.prefix += ":\n";
}
}
return ret;
}
bool Cpp::SourceCodeInsertion::insertSlot(QString name, QString normalizedSignature) {
if(!m_context || !m_codeRepresentation)
return false;
InsertionPoint insertion = findInsertionPoint(m_access, Slot);
QString add = insertion.prefix;
QString sig;
add += "void " + name + "(" + normalizedSignature + ");";
if(insertion.line > m_codeRepresentation->lines())
return false;
add = "\n" + applyIndentation(add);
return m_changeSet.addChange(DocumentChange(m_context->url(), insertionRange(insertion.line), QString(), add));
}
bool Cpp::SourceCodeInsertion::insertForwardDeclaration(KDevelop::Declaration* decl) {
setSubScope(decl->context()->scopeIdentifier(true));
if(!m_context) {
kDebug() << "no context";
return false;
}
QString forwardDeclaration;
if(decl->kind() == Declaration::Instance) {
//A simple variable declaration
forwardDeclaration = "extern " + Cpp::simplifiedTypeString(decl->abstractType(), m_context) + " " + decl->identifier().toString() + ";";
}else if(decl->type<KDevelop::EnumerationType>()) {
forwardDeclaration = "enum " + decl->identifier().toString() + ";";
}else if(decl->isTypeAlias()) {
if(!decl->abstractType()) {
kDebug() << "no type";
return false;
}
forwardDeclaration = "typedef " + Cpp::simplifiedTypeString(decl->abstractType(), m_context) + " " + decl->identifier().toString() + ";";
}else{
DUContext* templateContext = getTemplateContext(decl);
if(templateContext) {
forwardDeclaration += "template<";
bool first = true;
foreach(Declaration* _paramDecl, templateContext->localDeclarations()) {
TemplateParameterDeclaration* paramDecl = dynamic_cast<TemplateParameterDeclaration*>(_paramDecl);
if(!paramDecl)
continue;
if(!first) {
forwardDeclaration += ", ";
}else{
first = false;
}
CppTemplateParameterType::Ptr templParamType = paramDecl->type<CppTemplateParameterType>();
if(templParamType) {
forwardDeclaration += "class ";
}else if(paramDecl->abstractType()) {
forwardDeclaration += Cpp::simplifiedTypeString(paramDecl->abstractType(), m_context) + " ";
}
forwardDeclaration += paramDecl->identifier().toString();
if(!paramDecl->defaultParameter().isEmpty()) {
forwardDeclaration += " = " + paramDecl->defaultParameter().toString();
}
}
forwardDeclaration += " >\n";
}
ClassDeclaration * classDecl = dynamic_cast<ClassDeclaration *>(decl);
if(classDecl) {
forwardDeclaration += classDecl->toString() + ";";
}else{
forwardDeclaration += "class " + decl->identifier().toString() + ";";
}
}
//Put declarations to the end, and namespaces to the begin
KTextEditor::Cursor position;
bool needNewLine = true;
if(!m_scope.isEmpty() || (m_insertBefore.isValid() && m_context->rangeInCurrentRevision().end > m_insertBefore)) {
//To the begin
position = m_context->rangeInCurrentRevision().start.textCursor();
if(m_context->type() == DUContext::Namespace) {
position += KTextEditor::Cursor(0, 1); //Skip over the opening '{' paren
//Put the newline to the beginning instead of the end
forwardDeclaration = "\n" + forwardDeclaration;
if(forwardDeclaration.endsWith("\n"))
forwardDeclaration = forwardDeclaration.left(forwardDeclaration.length()-1);
}
} else{
//To the end
position = end().textCursor();
if(position.column() != 0 && m_context->type() == DUContext::Namespace) {
position -= KTextEditor::Cursor(0, 1);
forwardDeclaration += "\n";
needNewLine = false;
}
}
int firstValidLine = firstValidCodeLineBefore(m_insertBefore.line);
if(firstValidLine > position.line() && m_context == m_topContext && (!m_insertBefore.isValid() || firstValidLine < m_insertBefore.line)) {
position.setLine(firstValidLine);
position.setColumn(0);
}
forwardDeclaration = applySubScope(forwardDeclaration);
if(needNewLine)
forwardDeclaration = "\n" + forwardDeclaration;
kDebug() << "inserting at" << position << forwardDeclaration;
return m_changeSet.addChange(DocumentChange(m_context->url(), SimpleRange(position.line(), position.column(), position.line(), position.column()), QString(), forwardDeclaration));
}
Cpp::SourceCodeInsertion::SourceCodeInsertion(TopDUContext* topContext) : KDevelop::SourceCodeInsertion(topContext){
m_changeSet.setFormatPolicy(KDevelop::DocumentChangeSet::AutoFormatChanges);
}
|