1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 10:16:03 +02:00

Code maintenance for refactoring, by Emanuel Graf, bug 226645.

This commit is contained in:
Markus Schorn 2008-04-11 10:00:05 +00:00
parent fed2e66552
commit fa32e66c65
9 changed files with 49 additions and 206 deletions

View file

@ -62,29 +62,29 @@ public abstract class RefactoringBaseTest extends BaseTestFramework implements I
} }
protected void assertEquals(TestSourceFile file, IFile file2) throws Exception { protected void assertEquals(TestSourceFile file, IFile file2) throws Exception {
StringBuffer code = getCodeFromIFile(file2); String code = getCodeFromIFile(file2);
assertEquals(file.getExpectedSource(), TestHelper.unifyNewLines(code.toString())); assertEquals(file.getExpectedSource(), TestHelper.unifyNewLines(code));
} }
protected void compareFiles(Map<String,TestSourceFile> testResourceFiles) throws Exception { protected void compareFiles(Map<String,TestSourceFile> testResourceFiles) throws Exception {
for (String fileName : testResourceFiles.keySet()) { for (String fileName : testResourceFiles.keySet()) {
TestSourceFile file = testResourceFiles.get(fileName); String expectedSource = testResourceFiles.get(fileName).getExpectedSource();
IFile iFile = project.getFile(new Path(fileName)); IFile iFile = project.getFile(new Path(fileName));
StringBuffer code = getCodeFromIFile(iFile); String code = getCodeFromIFile(iFile);
assertEquals(TestHelper.unifyNewLines(file.getExpectedSource()), TestHelper.unifyNewLines(code.toString())); assertEquals(TestHelper.unifyNewLines(expectedSource), TestHelper.unifyNewLines(code));
} }
} }
protected StringBuffer getCodeFromIFile(IFile file) throws Exception { protected String getCodeFromIFile(IFile file) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(file.getContents())); BufferedReader br = new BufferedReader(new InputStreamReader(file.getContents()));
StringBuffer code = new StringBuffer(); StringBuilder code = new StringBuilder();
String line; String line;
while((line = br.readLine()) != null) { while((line = br.readLine()) != null) {
code.append(line); code.append(line);
code.append('\n'); code.append('\n');
} }
br.close(); br.close();
return code; return code.toString();
} }
@Override @Override

View file

@ -68,11 +68,10 @@ public abstract class CRefactoring extends Refactoring {
protected String name = Messages.HSRRefactoring_name; protected String name = Messages.HSRRefactoring_name;
protected IFile file; protected IFile file;
protected ISelection selection; private ISelection selection;
protected RefactoringStatus initStatus; protected RefactoringStatus initStatus;
protected IASTTranslationUnit unit; protected IASTTranslationUnit unit;
private IIndex fIndex; private IIndex fIndex;
public static final String NEWLINE = System.getProperty("line.separator"); //$NON-NLS-1$
public CRefactoring(IFile file, ISelection selection) { public CRefactoring(IFile file, ISelection selection) {
this.file = file; this.file = file;
@ -240,23 +239,25 @@ public abstract class CRefactoring extends Refactoring {
return collector.createFinalChange(); return collector.createFinalChange();
} }
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) abstract protected void collectModifications(IProgressMonitor pm, ModificationCollector collector)
throws CoreException, OperationCanceledException { throws CoreException, OperationCanceledException;
}
@Override @Override
public String getName() { public String getName() {
return name; return name;
} }
protected boolean loadTranslationUnit(RefactoringStatus status, IProgressMonitor mon) { protected ITextSelection getTextSelection() {
return (ITextSelection) selection;
}
private boolean loadTranslationUnit(RefactoringStatus status,
IProgressMonitor mon) {
SubMonitor subMonitor = SubMonitor.convert(mon, 10); SubMonitor subMonitor = SubMonitor.convert(mon, 10);
if (file != null) { if (file != null) {
try { try {
subMonitor.subTask(Messages.HSRRefactoring_PM_ParseTU); subMonitor.subTask(Messages.HSRRefactoring_PM_ParseTU);
ITranslationUnit tu = (ITranslationUnit) CCorePlugin unit = loadTranslationUnit(file);
.getDefault().getCoreModel().create(file);
unit = tu.getAST(fIndex, AST_STYLE);
subMonitor.worked(2); subMonitor.worked(2);
if(isProgressMonitorCanceld(subMonitor, initStatus)) { if(isProgressMonitorCanceld(subMonitor, initStatus)) {
return true; return true;
@ -279,6 +280,11 @@ public abstract class CRefactoring extends Refactoring {
return true; return true;
} }
protected IASTTranslationUnit loadTranslationUnit(IFile file) throws CoreException {
ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(file);
return tu.getAST(fIndex, AST_STYLE);
}
private static class ExpressionPosition { private static class ExpressionPosition {
public int start; public int start;
public int end; public int end;

View file

@ -13,7 +13,7 @@ package org.eclipse.cdt.internal.ui.refactoring;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI; import org.eclipse.ui.PlatformUI;
/** /**
@ -26,17 +26,12 @@ public abstract class RefactoringRunner {
protected IFile file; protected IFile file;
protected ISelection selection; protected ISelection selection;
protected IWorkbenchWindow window; protected Shell shell;
public RefactoringRunner(IFile file, ISelection selection, IWorkbenchWindow window) { public RefactoringRunner(IFile file, ISelection selection) {
super();
this.file = file; this.file = file;
this.selection = selection; this.selection = selection;
if(window != null) { shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
this.window = window;
}else {
this.window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
} }
public abstract void run(); public abstract void run();

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007, 2008 Institute for Software, HSR Hochschule fuer Technik * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others * Rapperswil, University of applied sciences 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
@ -99,13 +99,7 @@ public class ExtractConstantRefactoring extends CRefactoring {
if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; if(isProgressMonitorCanceld(sm, initStatus)) return initStatus;
ITextSelection textSelection = null; ITextSelection textSelection = getTextSelection();
if (selection instanceof ITextSelection) {
textSelection = (ITextSelection) selection;
} else {
initStatus.addFatalError(Messages.ExtractConstantRefactoring_LiteralMustBeSelected);
return initStatus;
}
sm.worked(1); sm.worked(1);
if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; if(isProgressMonitorCanceld(sm, initStatus)) return initStatus;

View file

@ -15,7 +15,6 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation; import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
@ -29,8 +28,8 @@ import org.eclipse.cdt.internal.ui.refactoring.RefactoringRunner;
*/ */
public class ExtractConstantRefactoringRunner extends RefactoringRunner { public class ExtractConstantRefactoringRunner extends RefactoringRunner {
public ExtractConstantRefactoringRunner(IFile file, ISelection selection, IWorkbenchWindow window) { public ExtractConstantRefactoringRunner(IFile file, ISelection selection) {
super(file, selection, window); super(file, selection);
} }
@Override @Override
@ -43,7 +42,7 @@ public class ExtractConstantRefactoringRunner extends RefactoringRunner {
try { try {
refactoring.lockIndex(); refactoring.lockIndex();
try { try {
operator.run(window.getShell(), refactoring.getName()); operator.run(shell, refactoring.getName());
} }
finally { finally {
refactoring.unlockIndex(); refactoring.unlockIndex();

View file

@ -22,7 +22,7 @@ import org.eclipse.core.runtime.CoreException;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
/** /**
* Some helper methods to access part of the content of an ifile * Some helper methods to access part of the content of an iFile
* *
* @author Emanuel Graf * @author Emanuel Graf
* *
@ -32,12 +32,9 @@ public class FileContentHelper {
private static final int bufferSize = 512; private static final int bufferSize = 512;
public static String getContent(IFile file, int start) throws CoreException, IOException{ public static String getContent(IFile file, int start) throws CoreException, IOException{
InputStreamReader reader = getReaderForFile(file); InputStreamReader reader = getReaderForFile(file);
skip(start, reader); skip(start, reader);
return readRest(reader); return readRest(reader);
} }
public static String getContent(IFile file, int start, int length) { public static String getContent(IFile file, int start, int length) {
@ -72,8 +69,6 @@ public class FileContentHelper {
while((bytesRead = reader.read(buffer)) >= 0){ while((bytesRead = reader.read(buffer)) >= 0){
content.append(buffer, 0, bytesRead); content.append(buffer, 0, bytesRead);
} }
return content.toString(); return content.toString();
} }
@ -90,14 +85,13 @@ public class FileContentHelper {
} }
} }
private static void skip(int start, InputStreamReader r) throws IOException { private static void skip(int count, InputStreamReader r) throws IOException {
long skipped = 0; long skipped = 0;
while(skipped >= 0 && start > 0 && r.ready()){ while(skipped >= 0 && count > 0 && r.ready()){
skipped = r.skip(start); skipped = r.skip(count);
if(skipped > 0){ if(skipped > 0){
start -= skipped; count -= skipped;
} }
} }
} }
} }

View file

@ -11,67 +11,23 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.utils; package org.eclipse.cdt.internal.ui.refactoring.utils;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
/**
* Helper class concerning files.
*
* @author Lukas Felber
*
*/
public class FileHelper { public class FileHelper {
private static final String DEFAULT_LINE_DELIMITTER = "\n"; //$NON-NLS-1$
public static IFile getIFilefromIASTNode(IASTNode node) { public static IFile getIFilefromIASTNode(IASTNode node) {
IPath implPath = new Path(node.getContainingFilename()); IPath implPath = new Path(node.getContainingFilename());
return ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(implPath); return ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(implPath);
} }
public static boolean isFirstWithinSecondLocation(IASTFileLocation loc1, IASTFileLocation loc2){
boolean isEquals = true;
isEquals &= loc1.getFileName().equals(loc2.getFileName());
isEquals &= loc1.getNodeOffset() >= loc2.getNodeOffset();
isEquals &= loc1.getNodeOffset()+loc1.getNodeLength() <= loc2.getNodeOffset() + loc2.getNodeLength();
return isEquals;
}
public static String determineLineDelimiter(IFile file) {
StringBuilder fileContent = new StringBuilder();
try {
InputStream fis = file.getContents();
byte[] buffer = new byte[1024];
int read;
while ((read = fis.read(buffer)) >= 0)
fileContent.append(new String(buffer, 0, read));
} catch (CoreException e) {
} catch (IOException e) {
} catch (NullPointerException e){
}
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject();
IScopeContext[] scopeContext;
if(project != null){
scopeContext = new IScopeContext[] { new ProjectScope(project)};
}
else{
scopeContext = new IScopeContext[] { new InstanceScope()};
}
String platformDefaultLineDelimiter = System.getProperty("line.separator", DEFAULT_LINE_DELIMITTER); //$NON-NLS-1$
String defaultLineDelimiter = Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, platformDefaultLineDelimiter, scopeContext);
return TextUtilities.determineLineDelimiter(fileContent.toString(), defaultLineDelimiter);
}
} }

View file

@ -1,100 +0,0 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Institute for Software - initial API and implementation
******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.utils;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
/**
* Some helper methods that calculate offsets from nodes.
*
* @author Emanuel Graf
*
*/
public class OffsetHelper {
public static int getOffsetIncludingComment(IASTNode node) {
int nodeStart = Integer.MAX_VALUE;
IASTNodeLocation[] nodeLocations = node.getNodeLocations();
if (nodeLocations.length != 1) {
int offset;
for (IASTNodeLocation location : nodeLocations) {
if (location instanceof IASTMacroExpansionLocation) {
IASTMacroExpansionLocation macroLoc = (IASTMacroExpansionLocation) location;
offset = macroLoc.asFileLocation().getNodeOffset();
}else {
offset = location.asFileLocation().getNodeOffset();
}
if(offset < nodeStart) nodeStart = offset;
}
} else {
nodeStart = node.getFileLocation().getNodeOffset();
}
return nodeStart;
}
public static int getEndOffsetIncludingComments(IASTNode node) {
int fileOffset = 0;
int length = 0;
IASTNodeLocation[] nodeLocations = node.getNodeLocations();
if (nodeLocations.length != 1) {
for (IASTNodeLocation location : nodeLocations) {
if (location instanceof IASTMacroExpansionLocation) {
IASTMacroExpansionLocation macroLoc = (IASTMacroExpansionLocation) location;
fileOffset = macroLoc.asFileLocation().getNodeOffset();
length = macroLoc.asFileLocation().getNodeLength();
}else {
fileOffset = location.asFileLocation().getNodeOffset();
length = location.asFileLocation().getNodeLength();
}
}
} else {
IASTFileLocation loc = node.getFileLocation();
fileOffset = loc.getNodeOffset();
length = loc.getNodeLength();
}
return fileOffset + length;
}
public static int getEndOffsetWithoutComments(IASTNode node) {
return node.getFileLocation().getNodeOffset() + node.getFileLocation().getNodeLength();
}
public static int getLengthIncludingComment(IASTNode node) {
return OffsetHelper.getEndOffsetIncludingComments(node) - OffsetHelper.getOffsetIncludingComment(node);
}
public static int getNodeOffset(ASTNode node) {
return node.getOffset();
}
public static int getNodeEndPoint(ASTNode node) {
return node.getOffset() + node.getLength();
}
public static int getStartingLineNumber(IASTNode node) {
return node.getFileLocation().getStartingLineNumber();
}
public static int getEndingLineNumber(IASTNode node) {
return node.getFileLocation().getEndingLineNumber();
}
}

View file

@ -11,6 +11,7 @@
package org.eclipse.cdt.ui.refactoring.actions; package org.eclipse.cdt.ui.refactoring.actions;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.ITextSelection;
@ -22,7 +23,7 @@ import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.ui.refactoring.extractconstant.ExtractConstantRefactoringRunner; import org.eclipse.cdt.internal.ui.refactoring.extractconstant.ExtractConstantRefactoringRunner;
/** /**
* Launches a rename refactoring. * Launches a extract constant refactoring.
*/ */
public class ExtractConstantAction extends RefactoringAction { public class ExtractConstantAction extends RefactoringAction {
@ -35,12 +36,10 @@ public class ExtractConstantAction extends RefactoringAction {
} }
@Override @Override
public void run(IShellProvider shellProvider, IWorkingCopy wc, ITextSelection s) { public void run(IShellProvider shellProvider, IWorkingCopy wc, ITextSelection selection) {
IResource res= wc.getResource(); IResource res= wc.getResource();
if (res instanceof IFile) { if (res instanceof IFile) {
new ExtractConstantRefactoringRunner((IFile) res, new ExtractConstantRefactoringRunner((IFile) res, selection).run();
fEditor.getSelectionProvider().getSelection(),
fEditor.getSite().getWorkbenchWindow()).run();
} }
} }