mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-22 14:12:10 +02:00
Bug 435340 - Add Include inserts an extra blank line
This commit is contained in:
parent
7e64f6a132
commit
dc63d60efd
11 changed files with 68 additions and 10 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2013 Google, Inc and others.
|
* Copyright (c) 2013, 2014 Google, Inc and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -41,7 +41,26 @@ public class TextUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns {@code true} the line prior to the line corresponding to the given {@code offset}
|
* Returns {@code true} if the line corresponding to the given {@code offset} does not contain
|
||||||
|
* non-whitespace characters.
|
||||||
|
*/
|
||||||
|
public static boolean isLineBlank(String text, int offset) {
|
||||||
|
while (--offset >= 0) {
|
||||||
|
if (text.charAt(offset) == '\n')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
while (++offset < text.length()) {
|
||||||
|
char c = text.charAt(offset);
|
||||||
|
if (c == '\n')
|
||||||
|
return true;
|
||||||
|
if (!Character.isWhitespace(c))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} if the line prior to the line corresponding to the given {@code offset}
|
||||||
* does not contain non-whitespace characters.
|
* does not contain non-whitespace characters.
|
||||||
*/
|
*/
|
||||||
public static boolean isPreviousLineBlank(String text, int offset) {
|
public static boolean isPreviousLineBlank(String text, int offset) {
|
||||||
|
@ -49,6 +68,9 @@ public class TextUtil {
|
||||||
if (text.charAt(offset) == '\n')
|
if (text.charAt(offset) == '\n')
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (offset < 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
while (--offset >= 0) {
|
while (--offset >= 0) {
|
||||||
char c = text.charAt(offset);
|
char c = text.charAt(offset);
|
||||||
if (c == '\n')
|
if (c == '\n')
|
||||||
|
@ -56,6 +78,6 @@ public class TextUtil {
|
||||||
if (!Character.isWhitespace(c))
|
if (!Character.isWhitespace(c))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "Enumerator_307738.h"
|
#include "Enumerator_307738.h"
|
||||||
|
|
||||||
void test() {
|
void test() {
|
||||||
int i = Ns::ENUM_VALUE;
|
int i = Ns::ENUM_VALUE;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "InsertionPoint_435340b.h"
|
||||||
|
|
||||||
|
A435340 a;
|
||||||
|
B435340 b;
|
|
@ -0,0 +1,7 @@
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "InsertionPoint_435340a.h"
|
||||||
|
#include "InsertionPoint_435340b.h"
|
||||||
|
|
||||||
|
A435340 a;
|
||||||
|
B435340 b;
|
|
@ -0,0 +1 @@
|
||||||
|
class A435340 {};
|
|
@ -0,0 +1 @@
|
||||||
|
class B435340 {};
|
|
@ -1,2 +1,3 @@
|
||||||
#include "Macro.h"
|
#include "Macro.h"
|
||||||
|
|
||||||
int x = ONE;
|
int x = ONE;
|
|
@ -1,4 +1,5 @@
|
||||||
#include "ResolvedName.h"
|
#include "ResolvedName.h"
|
||||||
|
|
||||||
namespace ns4 {
|
namespace ns4 {
|
||||||
|
|
||||||
A a;
|
A a;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "Template_306670.h"
|
#include "Template_306670.h"
|
||||||
|
|
||||||
void test() {
|
void test() {
|
||||||
func306670(1);
|
func306670(1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2009, 2010 Google, Inc and others.
|
* Copyright (c) 2009, 2014 Google, Inc and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -158,6 +158,11 @@ public class AddIncludeTest extends BaseTestCase {
|
||||||
assertAddIncludeResult();
|
assertAddIncludeResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testInsertionPoint_435340() throws Exception {
|
||||||
|
select("A435340");
|
||||||
|
assertAddIncludeResult();
|
||||||
|
}
|
||||||
|
|
||||||
public void testTemplate_306670() throws Exception {
|
public void testTemplate_306670() throws Exception {
|
||||||
select("func306670");
|
select("func306670");
|
||||||
assertAddIncludeResult();
|
assertAddIncludeResult();
|
||||||
|
|
|
@ -83,6 +83,7 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
|
||||||
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.ASTCommenter;
|
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.ASTCommenter;
|
||||||
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
|
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
|
||||||
import org.eclipse.cdt.internal.core.dom.rewrite.util.ASTNodes;
|
import org.eclipse.cdt.internal.core.dom.rewrite.util.ASTNodes;
|
||||||
|
import org.eclipse.cdt.internal.core.dom.rewrite.util.TextUtil;
|
||||||
import org.eclipse.cdt.internal.core.model.ASTStringUtil;
|
import org.eclipse.cdt.internal.core.model.ASTStringUtil;
|
||||||
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
|
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
|
||||||
import org.eclipse.cdt.internal.corext.codemanipulation.IncludeInfo;
|
import org.eclipse.cdt.internal.corext.codemanipulation.IncludeInfo;
|
||||||
|
@ -275,8 +276,8 @@ public class IncludeCreator {
|
||||||
|
|
||||||
if (preferences.allowReordering) {
|
if (preferences.allowReordering) {
|
||||||
// Since the order of existing include statements may not match the include order
|
// Since the order of existing include statements may not match the include order
|
||||||
// preferences, we find positions for the new include statements by pushing them from
|
// preferences, we find positions for the new include statements by pushing them up
|
||||||
// them up from the bottom of the include insertion region.
|
// from the bottom of the include insertion region.
|
||||||
for (StyledInclude include : styledIncludes) {
|
for (StyledInclude include : styledIncludes) {
|
||||||
int i = mergedIncludes.size();
|
int i = mergedIncludes.size();
|
||||||
while (--i >= 0 && preferences.compare(include, mergedIncludes.get(i)) < 0) {}
|
while (--i >= 0 && preferences.compare(include, mergedIncludes.get(i)) < 0) {}
|
||||||
|
@ -290,7 +291,8 @@ public class IncludeCreator {
|
||||||
StringBuilder text = new StringBuilder();
|
StringBuilder text = new StringBuilder();
|
||||||
StyledInclude previousInclude = null;
|
StyledInclude previousInclude = null;
|
||||||
for (StyledInclude include : mergedIncludes) {
|
for (StyledInclude include : mergedIncludes) {
|
||||||
if (include.getExistingInclude() == null) {
|
IASTPreprocessorIncludeStatement existingInclude = include.getExistingInclude();
|
||||||
|
if (existingInclude == null) {
|
||||||
if (previousInclude != null) {
|
if (previousInclude != null) {
|
||||||
IASTNode previousNode = previousInclude.getExistingInclude();
|
IASTNode previousNode = previousInclude.getExistingInclude();
|
||||||
if (previousNode != null) {
|
if (previousNode != null) {
|
||||||
|
@ -299,20 +301,30 @@ public class IncludeCreator {
|
||||||
if (contents.charAt(offset - 1) != '\n')
|
if (contents.charAt(offset - 1) != '\n')
|
||||||
text.append(fLineDelimiter);
|
text.append(fLineDelimiter);
|
||||||
}
|
}
|
||||||
if (include.getStyle().isBlankLineNeededAfter(previousInclude.getStyle(), preferences.includeStyles))
|
if (include.getStyle().isBlankLineNeededAfter(previousInclude.getStyle(), preferences.includeStyles)) {
|
||||||
|
if (TextUtil.isLineBlank(contents, offset)) {
|
||||||
|
offset = TextUtil.skipToNextLine(contents, offset);
|
||||||
|
} else {
|
||||||
text.append(fLineDelimiter);
|
text.append(fLineDelimiter);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
text.append(include.getIncludeInfo().composeIncludeStatement());
|
text.append(include.getIncludeInfo().composeIncludeStatement());
|
||||||
text.append(fLineDelimiter);
|
text.append(fLineDelimiter);
|
||||||
} else {
|
} else {
|
||||||
if (previousInclude != null && previousInclude.getExistingInclude() == null &&
|
if (previousInclude != null && previousInclude.getExistingInclude() == null &&
|
||||||
include.getStyle().isBlankLineNeededAfter(previousInclude.getStyle(), preferences.includeStyles)) {
|
include.getStyle().isBlankLineNeededAfter(previousInclude.getStyle(), preferences.includeStyles) &&
|
||||||
|
!TextUtil.isPreviousLineBlank(contents, ASTNodes.offset(existingInclude))) {
|
||||||
text.append(fLineDelimiter);
|
text.append(fLineDelimiter);
|
||||||
}
|
}
|
||||||
flushEditBuffer(offset, text, rootEdit);
|
flushEditBuffer(offset, text, rootEdit);
|
||||||
}
|
}
|
||||||
previousInclude = include;
|
previousInclude = include;
|
||||||
}
|
}
|
||||||
|
if (includeRegion.getLength() == 0 && !TextUtil.isLineBlank(contents, includeRegion.getOffset()) &&
|
||||||
|
!includes.isEmpty()) {
|
||||||
|
text.append(fLineDelimiter);
|
||||||
|
}
|
||||||
flushEditBuffer(offset, text, rootEdit);
|
flushEditBuffer(offset, text, rootEdit);
|
||||||
|
|
||||||
List<UsingDeclaration> mergedUsingDeclarations = getUsingDeclarations(ast);
|
List<UsingDeclaration> mergedUsingDeclarations = getUsingDeclarations(ast);
|
||||||
|
|
Loading…
Add table
Reference in a new issue