From 5b847a66dfc762e3a8049860ca036be195faba89 Mon Sep 17 00:00:00 2001 From: Leandro Melo Date: Tue, 31 Jan 2012 13:41:39 +0100 Subject: C++: Handle add definition on empty files Now in the case of a matching empty file we still select a best token so the insertion happens *at* the end of the file. The patch also fixes line breaks after the definition to be inserted for situations in which there's no line break yet. Example: namespace Foo {} namespace { } Another thing for correctness. Member _key from HighestValue is now value initialized instead of default initialized. Task-number: QTCREATORBUG-6696 Change-Id: I5c0303675429c3da5cb88825272c9611be022f6a Reviewed-by: Roberto Raggi --- src/plugins/cpptools/insertionpointlocator.cpp | 53 +++++++++++++++++--------- 1 file changed, 35 insertions(+), 18 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/cpptools/insertionpointlocator.cpp b/src/plugins/cpptools/insertionpointlocator.cpp index 587d983b9ea..0ff86a925fe 100644 --- a/src/plugins/cpptools/insertionpointlocator.cpp +++ b/src/plugins/cpptools/insertionpointlocator.cpp @@ -331,7 +331,7 @@ class HighestValue bool _set; public: HighestValue() - : _set(false) + : _key(), _set(false) {} HighestValue(const Key &initialKey, const Value &initialValue) @@ -369,22 +369,22 @@ public: void operator()(Declaration *decl, unsigned *line, unsigned *column) { - *line = *column = 0; - if (translationUnit()->ast()->lastToken() < 2) - return; - - QList names = LookupContext::fullyQualifiedName(decl); - foreach (const Name *name, names) { - const Identifier *id = name->asNameId(); - if (!id) - break; - _namespaceNames += id; - } - _currentDepth = 0; - // default to end of file - _bestToken.maybeSet(-1, translationUnit()->ast()->lastToken() - 1); - accept(translationUnit()->ast()); + _bestToken.maybeSet(-1, translationUnit()->ast()->lastToken()); + + if (translationUnit()->ast()->lastToken() >= 2) { + + QList names = LookupContext::fullyQualifiedName(decl); + foreach (const Name *name, names) { + const Identifier *id = name->asNameId(); + if (!id) + break; + _namespaceNames += id; + } + _currentDepth = 0; + + accept(translationUnit()->ast()); + } translationUnit()->getTokenEndPosition(_bestToken.get(), line, column); } @@ -586,7 +586,8 @@ QList InsertionPointLocator::methodDefinition( target = candidate; } - Document::Ptr doc = m_refactoringChanges.file(target)->cppDocument(); + CppRefactoringFilePtr targetFile = m_refactoringChanges.file(target); + Document::Ptr doc = targetFile->cppDocument(); if (doc.isNull()) return result; @@ -594,8 +595,24 @@ QList InsertionPointLocator::methodDefinition( FindMethodDefinitionInsertPoint finder(doc->translationUnit()); finder(declaration, &line, &column); + // Make sure we have a line before and after the new definition. const QLatin1String prefix("\n\n"); - result.append(InsertionLocation(target, prefix, QString(), line, column)); + QString suffix; + int firstNonSpace = targetFile->position(line, column); + QChar c = targetFile->charAt(firstNonSpace); + while (c == QLatin1Char(' ') || c == QLatin1Char('\t')) { + ++firstNonSpace; + c = targetFile->charAt(firstNonSpace); + } + if (targetFile->charAt(firstNonSpace) != QChar::ParagraphSeparator) { + suffix.append(QLatin1String("\n\n")); + } else { + ++firstNonSpace; + if (targetFile->charAt(firstNonSpace) != QChar::ParagraphSeparator) + suffix.append(QLatin1Char('\n')); + } + + result += InsertionLocation(target, prefix, suffix, line, column); return result; } -- cgit v1.2.3