package no.uio.ifi.refaktor.analyze.analyzers; import java.util.HashSet; import java.util.List; import java.util.Set; import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.dom.Statement; class TextSelectionsGeneratorHelper { private final Set textSelections; private final ICompilationUnit compilationUnit; private TextSelectionsGeneratorHelper(ICompilationUnit compilationUnit) { this.compilationUnit = compilationUnit; textSelections = new HashSet(); } static Set generateSelectionsFromList(List statements, ICompilationUnit compilationUnit) { return new TextSelectionsGeneratorHelper(compilationUnit).generateSelectionsFromList(statements); } private Set generateSelectionsFromList(List statements) { for (Statement statement: statements) generateSelectionsFor(statement); return textSelections; } private void generateSelectionsFor(Statement statement) { Set newSelections = new HashSet(); CompilationUnitTextSelection textSelectionFromStatement = createTextSelectionFromStatement(statement); newSelections.add(textSelectionFromStatement); for (CompilationUnitTextSelection textSelection: textSelections) newSelections.add(addTextSelections(textSelection, textSelectionFromStatement)); textSelections.addAll(newSelections); } private CompilationUnitTextSelection createTextSelectionFromStatement(Statement statement) { return new CompilationUnitTextSelection(compilationUnit, statement.getStartPosition(), statement.getLength()); } private static CompilationUnitTextSelection addTextSelections( CompilationUnitTextSelection textSelectionOne, CompilationUnitTextSelection textSelectionTwo) { int offset = Math.min(textSelectionOne.getOffset(), textSelectionTwo.getOffset()); int length = Math.max(textSelectionOne.getEnd(), textSelectionTwo.getEnd()) - offset; return new CompilationUnitTextSelection(textSelectionOne.getCompilationUnit(), offset, length); } }