From 251c37e865dff4b6aacbf9c7a2ca591e1fcbe23d Mon Sep 17 00:00:00 2001 From: Emanuel Graf Date: Tue, 17 Feb 2009 10:37:22 +0000 Subject: [PATCH] FIXED - bug 264712: Refactor extract function deletes comments in header https://bugs.eclipse.org/bugs/show_bug.cgi?id=264712 --- .../rewrite/commenthandler/ASTCommenter.java | 39 ++++-- .../resources/refactoring/ExtractMethod.rts | 114 ++++++++++++++++++ 2 files changed, 140 insertions(+), 13 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java index bb01cae2dee..f07aeafd018 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java @@ -85,24 +85,31 @@ public class ASTCommenter { private static ArrayList removeAllPreprocessorComments(IASTTranslationUnit tu, ArrayList comments) { IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements(); TreeMap treeOfPreProcessorLines = new TreeMap(); - ArrayList listOfPreProcessorOffset = new ArrayList(); + TreeMap> ppOffsetForFiles = new TreeMap>(); 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 offsetList = ppOffsetForFiles.get(fileName); + if(offsetList == null) { + offsetList = new ArrayList(); + ppOffsetForFiles.put(fileName, offsetList); + } + offsetList.add(statement.getFileLocation().getNodeOffset()); } } ArrayList commentsInCode = new ArrayList(); 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 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; } diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts index d68aa090f12..171658a9b1e 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts @@ -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; +} +