aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins
diff options
context:
space:
mode:
authorLeandro Melo <[email protected]>2012-01-31 13:41:39 +0100
committerLeandro Melo <[email protected]>2012-01-31 16:44:38 +0100
commit5b847a66dfc762e3a8049860ca036be195faba89 (patch)
treedfa3743349d6e96c81cbf7ecb909ca497e35d4cc /src/plugins
parentac3093058c4ce620987e3c1c1dc67ccfbe040996 (diff)
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 <[email protected]>
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/cpptools/insertionpointlocator.cpp53
1 files changed, 35 insertions, 18 deletions
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<const Name *> 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<const Name *> 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<InsertionLocation> 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<InsertionLocation> 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;
}