mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-06 17:26: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) {
|
||||
IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
|
||||
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) {
|
||||
if (isInWorkspace(statement)) {
|
||||
treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement), statement.getFileLocation().getFileName());
|
||||
listOfPreProcessorOffset.add(statement.getFileLocation().getNodeOffset());
|
||||
String fileName = statement.getFileLocation().getFileName();
|
||||
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>();
|
||||
for (IASTComment comment : comments) {
|
||||
int comStartLineNumber = OffsetHelper.getStartingLineNumber(comment);
|
||||
String fileName = comment.getFileLocation().getFileName();
|
||||
if (treeOfPreProcessorLines.containsKey(comStartLineNumber)
|
||||
&& treeOfPreProcessorLines.get(comStartLineNumber).equals(comment.getFileLocation().getFileName()
|
||||
&& treeOfPreProcessorLines.get(comStartLineNumber).equals(fileName
|
||||
)) {
|
||||
continue;
|
||||
}
|
||||
if(commentIsAtTheBeginningBeforePreprocessorStatements(comment, listOfPreProcessorOffset)) {
|
||||
if(commentIsAtTheBeginningBeforePreprocessorStatements(comment, ppOffsetForFiles.get(fileName))) {
|
||||
continue;
|
||||
}
|
||||
commentsInCode.add(comment);
|
||||
|
@ -113,27 +120,33 @@ public class ASTCommenter {
|
|||
private static boolean commentIsAtTheBeginningBeforePreprocessorStatements(
|
||||
IASTComment comment,
|
||||
ArrayList<Integer> listOfPreProcessorOffset) {
|
||||
if(listOfPreProcessorOffset.size() <1) {
|
||||
if(listOfPreProcessorOffset == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(comment.getTranslationUnit()==null || comment.getTranslationUnit().getDeclarations().length < 1) {
|
||||
return true;
|
||||
}
|
||||
IASTDeclaration decl = comment.getTranslationUnit().getDeclarations()[0];
|
||||
if(decl.getFileLocation().getNodeOffset() < comment.getFileLocation().getNodeOffset()) {
|
||||
return false;
|
||||
IASTDeclaration decl = comment.getTranslationUnit().getDeclarations()[0];
|
||||
boolean sameFile = decl.getFileLocation().getFileName().equals(comment.getFileLocation().getFileName());
|
||||
if(sameFile) {
|
||||
if(decl.getFileLocation().getNodeOffset() < comment.getFileLocation().getNodeOffset()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Collections.sort(listOfPreProcessorOffset);
|
||||
if(listOfPreProcessorOffset.get(0) < comment.getFileLocation().getNodeOffset()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(listOfPreProcessorOffset.get(0) < decl.getFileLocation().getNodeOffset()) {
|
||||
return true;
|
||||
if(sameFile) {
|
||||
if(listOfPreProcessorOffset.get(0) < decl.getFileLocation().getNodeOffset()) {
|
||||
return true;
|
||||
}
|
||||
}else {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -2592,3 +2592,117 @@ int main(int argc, char **argv) {
|
|||
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