mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-07 17:56:01 +02:00
FIXED - bug 264712: Refactor extract function deletes comments in header
https://bugs.eclipse.org/bugs/show_bug.cgi?id=264712
This commit is contained in:
parent
b749b3efe5
commit
251c37e865
2 changed files with 140 additions and 13 deletions
|
@ -85,24 +85,31 @@ public class ASTCommenter {
|
||||||
private static ArrayList<IASTComment> removeAllPreprocessorComments(IASTTranslationUnit tu, ArrayList<IASTComment> comments) {
|
private static ArrayList<IASTComment> removeAllPreprocessorComments(IASTTranslationUnit tu, ArrayList<IASTComment> comments) {
|
||||||
IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
|
IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
|
||||||
TreeMap<Integer,String> treeOfPreProcessorLines = new TreeMap<Integer,String>();
|
TreeMap<Integer,String> treeOfPreProcessorLines = new TreeMap<Integer,String>();
|
||||||
ArrayList<Integer> listOfPreProcessorOffset = new ArrayList<Integer>();
|
TreeMap<String, ArrayList<Integer>> ppOffsetForFiles = new TreeMap<String, ArrayList<Integer>>();
|
||||||
|
|
||||||
for (IASTPreprocessorStatement statement : preprocessorStatements) {
|
for (IASTPreprocessorStatement statement : preprocessorStatements) {
|
||||||
if (isInWorkspace(statement)) {
|
if (isInWorkspace(statement)) {
|
||||||
treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement), statement.getFileLocation().getFileName());
|
String fileName = statement.getFileLocation().getFileName();
|
||||||
listOfPreProcessorOffset.add(statement.getFileLocation().getNodeOffset());
|
treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement), fileName);
|
||||||
|
ArrayList<Integer> offsetList = ppOffsetForFiles.get(fileName);
|
||||||
|
if(offsetList == null) {
|
||||||
|
offsetList = new ArrayList<Integer>();
|
||||||
|
ppOffsetForFiles.put(fileName, offsetList);
|
||||||
|
}
|
||||||
|
offsetList.add(statement.getFileLocation().getNodeOffset());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<IASTComment> commentsInCode = new ArrayList<IASTComment>();
|
ArrayList<IASTComment> commentsInCode = new ArrayList<IASTComment>();
|
||||||
for (IASTComment comment : comments) {
|
for (IASTComment comment : comments) {
|
||||||
int comStartLineNumber = OffsetHelper.getStartingLineNumber(comment);
|
int comStartLineNumber = OffsetHelper.getStartingLineNumber(comment);
|
||||||
|
String fileName = comment.getFileLocation().getFileName();
|
||||||
if (treeOfPreProcessorLines.containsKey(comStartLineNumber)
|
if (treeOfPreProcessorLines.containsKey(comStartLineNumber)
|
||||||
&& treeOfPreProcessorLines.get(comStartLineNumber).equals(comment.getFileLocation().getFileName()
|
&& treeOfPreProcessorLines.get(comStartLineNumber).equals(fileName
|
||||||
)) {
|
)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(commentIsAtTheBeginningBeforePreprocessorStatements(comment, listOfPreProcessorOffset)) {
|
if(commentIsAtTheBeginningBeforePreprocessorStatements(comment, ppOffsetForFiles.get(fileName))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
commentsInCode.add(comment);
|
commentsInCode.add(comment);
|
||||||
|
@ -113,7 +120,7 @@ public class ASTCommenter {
|
||||||
private static boolean commentIsAtTheBeginningBeforePreprocessorStatements(
|
private static boolean commentIsAtTheBeginningBeforePreprocessorStatements(
|
||||||
IASTComment comment,
|
IASTComment comment,
|
||||||
ArrayList<Integer> listOfPreProcessorOffset) {
|
ArrayList<Integer> listOfPreProcessorOffset) {
|
||||||
if(listOfPreProcessorOffset.size() <1) {
|
if(listOfPreProcessorOffset == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,16 +128,22 @@ public class ASTCommenter {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
IASTDeclaration decl = comment.getTranslationUnit().getDeclarations()[0];
|
IASTDeclaration decl = comment.getTranslationUnit().getDeclarations()[0];
|
||||||
if(decl.getFileLocation().getNodeOffset() < comment.getFileLocation().getNodeOffset()) {
|
boolean sameFile = decl.getFileLocation().getFileName().equals(comment.getFileLocation().getFileName());
|
||||||
return false;
|
if(sameFile) {
|
||||||
|
if(decl.getFileLocation().getNodeOffset() < comment.getFileLocation().getNodeOffset()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Collections.sort(listOfPreProcessorOffset);
|
Collections.sort(listOfPreProcessorOffset);
|
||||||
if(listOfPreProcessorOffset.get(0) < comment.getFileLocation().getNodeOffset()) {
|
if(listOfPreProcessorOffset.get(0) < comment.getFileLocation().getNodeOffset()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(listOfPreProcessorOffset.get(0) < decl.getFileLocation().getNodeOffset()) {
|
if(sameFile) {
|
||||||
|
if(listOfPreProcessorOffset.get(0) < decl.getFileLocation().getNodeOffset()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}else {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2592,3 +2592,117 @@ int main(int argc, char **argv) {
|
||||||
exp();
|
exp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//!Bug#264712 Refactor extract function deletes comments in header
|
||||||
|
//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest
|
||||||
|
//@.config
|
||||||
|
filename=test.cpp
|
||||||
|
methodname=exp
|
||||||
|
//@classhelper.h
|
||||||
|
// Comment
|
||||||
|
//
|
||||||
|
// Comment
|
||||||
|
// Comment
|
||||||
|
#ifndef utils_classhelper_h_seen
|
||||||
|
#define utils_classhelper_h_seen
|
||||||
|
#define IMPORTANT_VALUE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//@test.h
|
||||||
|
/*
|
||||||
|
* Copyright 2009
|
||||||
|
*/
|
||||||
|
#ifndef test_h_seen
|
||||||
|
#define test_h_seen
|
||||||
|
|
||||||
|
#include "classhelper.h"
|
||||||
|
|
||||||
|
class Test
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Small class with some comments
|
||||||
|
*/
|
||||||
|
Test();
|
||||||
|
|
||||||
|
/** Calculate important things.
|
||||||
|
* @returns the result of the calculation
|
||||||
|
*/
|
||||||
|
int calculateStuff();
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Retain a value for something.
|
||||||
|
*/
|
||||||
|
int m_value;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//=
|
||||||
|
/*
|
||||||
|
* Copyright 2009
|
||||||
|
*/
|
||||||
|
#ifndef test_h_seen
|
||||||
|
#define test_h_seen
|
||||||
|
|
||||||
|
#include "classhelper.h"
|
||||||
|
|
||||||
|
class Test
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Small class with some comments
|
||||||
|
*/
|
||||||
|
Test();
|
||||||
|
|
||||||
|
/** Calculate important things.
|
||||||
|
* @returns the result of the calculation
|
||||||
|
*/
|
||||||
|
int calculateStuff();
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* Retain a value for something.
|
||||||
|
*/
|
||||||
|
int m_value;
|
||||||
|
int exp();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//@test.cpp
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
Test::Test() {}
|
||||||
|
|
||||||
|
int Test::calculateStuff() {
|
||||||
|
// refactor these lines to a new method:
|
||||||
|
/*$*/int result = m_value;
|
||||||
|
result += 10;
|
||||||
|
result *= 10;/*$$*/
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//=
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
Test::Test() {}
|
||||||
|
|
||||||
|
int Test::exp()
|
||||||
|
{
|
||||||
|
// refactor these lines to a new method:
|
||||||
|
int result = m_value;
|
||||||
|
result += 10;
|
||||||
|
result *= 10;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Test::calculateStuff() {
|
||||||
|
int result = exp();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue