1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-02 13:55:39 +02:00

Cosmetics.

This commit is contained in:
Sergey Prigogin 2010-09-21 01:45:17 +00:00
parent d1ee0b132a
commit 45f057367a
8 changed files with 244 additions and 269 deletions

View file

@ -10,6 +10,6 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.settings.model; package org.eclipse.cdt.core.settings.model;
public interface ICFileDescription extends ICResourceDescription{ public interface ICFileDescription extends ICResourceDescription {
ICLanguageSetting getLanguageSetting(); ICLanguageSetting getLanguageSetting();
} }

View file

@ -57,25 +57,24 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
/** /**
* The base class for all other refactorings, provides some common implementations for * The base class for all other refactorings, provides some common implementations for
* condition checking, change generating, selection handling and translation unit loading. * condition checking, change generating, selection handling and translation unit loading.
*
*/ */
public abstract class CRefactoring extends Refactoring { public abstract class CRefactoring extends Refactoring {
private static final int AST_STYLE = ITranslationUnit.AST_CONFIGURE_USING_SOURCE_CONTEXT | ITranslationUnit.AST_SKIP_INDEXED_HEADERS; private static final int AST_STYLE =
ITranslationUnit.AST_CONFIGURE_USING_SOURCE_CONTEXT | ITranslationUnit.AST_SKIP_INDEXED_HEADERS;
protected String name = Messages.Refactoring_name; protected String name = Messages.Refactoring_name;
protected IFile file; protected IFile file;
protected Region region; protected Region region;
protected RefactoringStatus initStatus; protected RefactoringStatus initStatus;
protected IASTTranslationUnit unit; protected IASTTranslationUnit unit;
private IIndex fIndex;
protected ICProject project; protected ICProject project;
private IIndex fIndex;
public CRefactoring(IFile file, ISelection selection, ICElement element, ICProject proj) { public CRefactoring(IFile file, ISelection selection, ICElement element, ICProject proj) {
project = proj; project = proj;
if (element instanceof ISourceReference) { if (element instanceof ISourceReference) {
ISourceReference sourceRef= (ISourceReference) element; ISourceReference sourceRef= (ISourceReference) element;
ITranslationUnit tu= sourceRef.getTranslationUnit(); ITranslationUnit tu = sourceRef.getTranslationUnit();
IResource res= tu.getResource(); IResource res= tu.getResource();
if (res instanceof IFile) if (res instanceof IFile)
this.file= (IFile) res; this.file= (IFile) res;
@ -91,7 +90,7 @@ public abstract class CRefactoring extends Refactoring {
this.region = SelectionHelper.getRegion(selection); this.region = SelectionHelper.getRegion(selection);
} }
this.initStatus=new RefactoringStatus(); this.initStatus= new RefactoringStatus();
if (this.file == null || region == null) { if (this.file == null || region == null) {
initStatus.addFatalError(Messages.Refactoring_SelectionNotValid); initStatus.addFatalError(Messages.Refactoring_SelectionNotValid);
} }
@ -196,10 +195,9 @@ public abstract class CRefactoring extends Refactoring {
return initStatus; return initStatus;
} }
protected boolean isProgressMonitorCanceld(IProgressMonitor sm, protected static boolean isProgressMonitorCanceld(IProgressMonitor sm, RefactoringStatus status) {
RefactoringStatus initStatus2) {
if (sm.isCanceled()) { if (sm.isCanceled()) {
initStatus2.addFatalError(Messages.Refactoring_CanceledByUser); status.addFatalError(Messages.Refactoring_CanceledByUser);
return true; return true;
} }
return false; return false;
@ -224,8 +222,7 @@ public abstract class CRefactoring extends Refactoring {
return name; return name;
} }
protected boolean loadTranslationUnit(RefactoringStatus status, protected boolean loadTranslationUnit(RefactoringStatus status, IProgressMonitor mon) {
IProgressMonitor mon) {
SubMonitor subMonitor = SubMonitor.convert(mon, 10); SubMonitor subMonitor = SubMonitor.convert(mon, 10);
if (file != null) { if (file != null) {
try { try {
@ -247,7 +244,6 @@ public abstract class CRefactoring extends Refactoring {
subMonitor.done(); subMonitor.done();
return false; return false;
} }
} else { } else {
status.addFatalError(Messages.NO_FILE); status.addFatalError(Messages.NO_FILE);
subMonitor.done(); subMonitor.done();

View file

@ -53,7 +53,7 @@ public abstract class CRefactoringDescription extends RefactoringDescriptor {
if (selectStrings.length >= 2) { if (selectStrings.length >= 2) {
int offset = Integer.parseInt(selectStrings[0]); int offset = Integer.parseInt(selectStrings[0]);
int length = Integer.parseInt(selectStrings[1]); int length = Integer.parseInt(selectStrings[1]);
selection = new TextSelection(offset,length); selection = new TextSelection(offset, length);
} else { } else {
throw new CoreException(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, "Illegal Selection")); //$NON-NLS-1$ throw new CoreException(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, "Illegal Selection")); //$NON-NLS-1$
} }

View file

@ -22,17 +22,16 @@ import org.eclipse.cdt.core.model.ICProject;
* Base class for all refactoring runners. * Base class for all refactoring runners.
* *
* @author Emanuel Graf * @author Emanuel Graf
*
*/ */
public abstract class RefactoringRunner { public abstract class RefactoringRunner {
protected IFile file; protected IFile file;
protected ISelection selection; protected ISelection selection;
protected ICElement celement; protected ICElement celement;
protected IShellProvider shellProvider; protected IShellProvider shellProvider;
protected ICProject project; protected ICProject project;
public RefactoringRunner(IFile file, ISelection selection, ICElement element, IShellProvider shellProvider, ICProject cProject) { public RefactoringRunner(IFile file, ISelection selection, ICElement element,
IShellProvider shellProvider, ICProject cProject) {
this.file = file; this.file = file;
this.selection = selection; this.selection = selection;
this.celement= element; this.celement= element;
@ -41,5 +40,4 @@ public abstract class RefactoringRunner {
} }
public abstract void run(); public abstract void run();
} }

View file

@ -74,15 +74,15 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
* @author Tom Ball * @author Tom Ball
*/ */
public class ExtractLocalVariableRefactoring extends CRefactoring { public class ExtractLocalVariableRefactoring extends CRefactoring {
public static final String ID =
"org.eclipse.cdt.internal.ui.refactoring.extractlocalvariable.ExtractLocalVariableRefactoring"; //$NON-NLS-1$
public static final String ID = "org.eclipse.cdt.internal.ui.refactoring.extractlocalvariable.ExtractLocalVariableRefactoring"; //$NON-NLS-1$ private IASTExpression target;
private IASTExpression target = null;
private final NameNVisibilityInformation info; private final NameNVisibilityInformation info;
private NodeContainer container; private NodeContainer container;
public ExtractLocalVariableRefactoring(IFile file, ISelection selection, public ExtractLocalVariableRefactoring(IFile file, ISelection selection, NameNVisibilityInformation info,
NameNVisibilityInformation info, ICProject project) { ICProject project) {
super(file, selection, null, project); super(file, selection, null, project);
this.info = info; this.info = info;
name = Messages.ExtractLocalVariable; name = Messages.ExtractLocalVariable;
@ -96,7 +96,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
lockIndex(); lockIndex();
try { try {
RefactoringStatus status = super.checkInitialConditions(sm.newChild(6)); RefactoringStatus status = super.checkInitialConditions(sm.newChild(6));
if(status.hasError()) { if (status.hasError()) {
return status; return status;
} }
@ -110,8 +110,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
if (isProgressMonitorCanceld(sm, initStatus)) if (isProgressMonitorCanceld(sm, initStatus))
return initStatus; return initStatus;
boolean oneMarked = region != null boolean oneMarked = region != null && isOneMarked(container.getNodesToWrite(), region);
&& isOneMarked(container.getNodesToWrite(), region);
if (!oneMarked) { if (!oneMarked) {
if (target == null) { if (target == null) {
initStatus.addFatalError(Messages.NoExpressionSelected); initStatus.addFatalError(Messages.NoExpressionSelected);
@ -132,13 +131,12 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
info.addNamesToUsedNames(findAllDeclaredNames()); info.addNamesToUsedNames(findAllDeclaredNames());
sm.worked(1); sm.worked(1);
NodeHelper.findMethodContext(container.getNodesToWrite().get(0), NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
getIndex());
sm.worked(1); sm.worked(1);
info.setName(guessTempName()); info.setName(guessTempName());
sm.done(); sm.done();
}finally { } finally {
unlockIndex(); unlockIndex();
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -149,10 +147,8 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
private ArrayList<String> findAllDeclaredNames() { private ArrayList<String> findAllDeclaredNames() {
ArrayList<String> names = new ArrayList<String>(); ArrayList<String> names = new ArrayList<String>();
IASTFunctionDefinition funcDef = NodeHelper IASTFunctionDefinition funcDef = NodeHelper.findFunctionDefinitionInAncestors(target);
.findFunctionDefinitionInAncestors(target); ICPPASTCompositeTypeSpecifier comTypeSpec = getCompositeTypeSpecifier(funcDef);
ICPPASTCompositeTypeSpecifier comTypeSpec =
getCompositeTypeSpecifier(funcDef);
if (comTypeSpec != null) { if (comTypeSpec != null) {
for (IASTDeclaration dec : comTypeSpec.getMembers()) { for (IASTDeclaration dec : comTypeSpec.getMembers()) {
if (dec instanceof IASTSimpleDeclaration) { if (dec instanceof IASTSimpleDeclaration) {
@ -166,21 +162,16 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
return names; return names;
} }
private ICPPASTCompositeTypeSpecifier getCompositeTypeSpecifier( private ICPPASTCompositeTypeSpecifier getCompositeTypeSpecifier(IASTFunctionDefinition funcDef) {
IASTFunctionDefinition funcDef) {
if (funcDef != null) { if (funcDef != null) {
IBinding binding = funcDef.getDeclarator().getName() IBinding binding = funcDef.getDeclarator().getName().resolveBinding();
.resolveBinding();
if (binding instanceof CPPFunction) { if (binding instanceof CPPFunction) {
CPPFunction function = (CPPFunction) binding; CPPFunction function = (CPPFunction) binding;
IASTNode[] decls = function.getDeclarations(); IASTNode[] decls = function.getDeclarations();
if (decls != null && decls.length > 0) { if (decls != null && decls.length > 0) {
IASTNode spec = decls[0].getParent().getParent(); IASTNode spec = decls[0].getParent().getParent();
if (spec instanceof ICPPASTCompositeTypeSpecifier) { if (spec instanceof ICPPASTCompositeTypeSpecifier) {
ICPPASTCompositeTypeSpecifier compTypeSpec = return (ICPPASTCompositeTypeSpecifier) spec;
(ICPPASTCompositeTypeSpecifier) spec;
return compTypeSpec;
} }
} }
} }
@ -188,16 +179,14 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
return null; return null;
} }
private boolean isOneMarked(List<IASTNode> selectedNodes, private boolean isOneMarked(List<IASTNode> selectedNodes, Region textSelection) {
Region textSelection) {
boolean oneMarked = false; boolean oneMarked = false;
for (IASTNode node : selectedNodes) { for (IASTNode node : selectedNodes) {
if (node instanceof IASTExpression) { if (node instanceof IASTExpression) {
IASTExpression expression = (IASTExpression) node; IASTExpression expression = (IASTExpression) node;
boolean isInSameFileSelection = SelectionHelper boolean isInSameFileSelection =
.isInSameFileSelection(textSelection, expression, file); SelectionHelper.isInSameFileSelection(textSelection, expression, file);
if (isInSameFileSelection if (isInSameFileSelection && isExpressionInSelection(expression, textSelection)) {
&& isExpressionInSelection(expression, textSelection)) {
if (target == null) { if (target == null) {
target = expression; target = expression;
oneMarked = true; oneMarked = true;
@ -210,8 +199,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
return oneMarked; return oneMarked;
} }
private boolean isExpressionInSelection(IASTExpression expression, private boolean isExpressionInSelection(IASTExpression expression, Region selection) {
Region selection) {
IASTFileLocation location = expression.getFileLocation(); IASTFileLocation location = expression.getFileLocation();
int e1 = location.getNodeOffset(); int e1 = location.getNodeOffset();
int e2 = location.getNodeOffset() + location.getNodeLength(); int e2 = location.getNodeOffset() + location.getNodeLength();
@ -255,29 +243,26 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
} }
@Override @Override
protected void collectModifications(IProgressMonitor pm, protected void collectModifications(IProgressMonitor pm, ModificationCollector collector)
ModificationCollector collector) throws CoreException, throws CoreException, OperationCanceledException {
OperationCanceledException {
try { try {
lockIndex(); lockIndex();
try { try {
String variableName = info.getName(); String variableName = info.getName();
TextEditGroup editGroup = new TextEditGroup( TextEditGroup editGroup = new TextEditGroup(Messages.CreateLocalVariable);
Messages.CreateLocalVariable);
// Define temporary variable declaration and insert it // Define temporary variable declaration and insert it
IASTStatement declInsertPoint = getParentStatement(target); IASTStatement declInsertPoint = getParentStatement(target);
IASTDeclarationStatement declaration = getVariableNodes(variableName); IASTDeclarationStatement declaration = getVariableNodes(variableName);
declaration.setParent(declInsertPoint.getParent()); declaration.setParent(declInsertPoint.getParent());
ASTRewrite rewriter = collector.rewriterForTranslationUnit(unit); ASTRewrite rewriter = collector.rewriterForTranslationUnit(unit);
rewriter.insertBefore(declInsertPoint.getParent(), declInsertPoint, rewriter.insertBefore(declInsertPoint.getParent(), declInsertPoint, declaration, editGroup);
declaration, editGroup);
// Replace target with reference to temporary variable // Replace target with reference to temporary variable
CPPASTIdExpression idExpression = new CPPASTIdExpression( CPPASTIdExpression idExpression =
new CPPASTName(variableName.toCharArray())); new CPPASTIdExpression(new CPPASTName(variableName.toCharArray()));
rewriter.replace(target, idExpression, editGroup); rewriter.replace(target, idExpression, editGroup);
}finally { } finally {
unlockIndex(); unlockIndex();
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -295,7 +280,6 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
} }
private IASTDeclarationStatement getVariableNodes(String newName) { private IASTDeclarationStatement getVariableNodes(String newName) {
INodeFactory factory = this.unit.getASTNodeFactory(); INodeFactory factory = this.unit.getASTNodeFactory();
IASTSimpleDeclaration simple = factory.newSimpleDeclaration(null); IASTSimpleDeclaration simple = factory.newSimpleDeclaration(null);
@ -306,7 +290,8 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
declSpec.setStorageClass(IASTDeclSpecifier.sc_unspecified); declSpec.setStorageClass(IASTDeclSpecifier.sc_unspecified);
simple.setDeclSpecifier(declSpec); simple.setDeclSpecifier(declSpec);
IASTDeclarator decl = generator.createDeclaratorFromType(target.getExpressionType(), newName.toCharArray()); IASTDeclarator decl = generator.createDeclaratorFromType(target.getExpressionType(),
newName.toCharArray());
IASTEqualsInitializer init = new CPPASTEqualsInitializer(); IASTEqualsInitializer init = new CPPASTEqualsInitializer();
init.setInitializerClause(deblock(target.copy())); init.setInitializerClause(deblock(target.copy()));
@ -345,11 +330,9 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
public String[] guessTempNames() { public String[] guessTempNames() {
final List<String> guessedTempNames = new ArrayList<String>(); final List<String> guessedTempNames = new ArrayList<String>();
final List<String> usedNames = new ArrayList<String>(); final List<String> usedNames = new ArrayList<String>();
IASTFunctionDefinition funcDef = NodeHelper IASTFunctionDefinition funcDef = NodeHelper.findFunctionDefinitionInAncestors(target);
.findFunctionDefinitionInAncestors(target);
final IScope scope; final IScope scope;
if (funcDef != null && if (funcDef != null && funcDef.getBody() instanceof IASTCompoundStatement) {
funcDef.getBody() instanceof IASTCompoundStatement) {
IASTCompoundStatement body = (IASTCompoundStatement)funcDef.getBody(); IASTCompoundStatement body = (IASTCompoundStatement)funcDef.getBody();
scope = body.getScope(); scope = body.getScope();
} else { } else {
@ -432,8 +415,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
} }
private boolean nameAvailable(String name, List<String> guessedNames, IScope scope) { private boolean nameAvailable(String name, List<String> guessedNames, IScope scope) {
if (guessedNames.contains(name) || if (guessedNames.contains(name) || info.getUsedNames().contains(name)) {
info.getUsedNames().contains(name)) {
return false; return false;
} }
if (scope != null) { if (scope != null) {
@ -459,7 +441,8 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
@Override @Override
protected RefactoringDescriptor getRefactoringDescriptor() { protected RefactoringDescriptor getRefactoringDescriptor() {
Map<String, String> arguments = getArgumentMap(); Map<String, String> arguments = getArgumentMap();
RefactoringDescriptor desc = new ExtractLocalVariableRefactoringDescription(project.getProject().getName(), "Extract Local Variable Refactoring", "Extract " + target.getRawSignature(), arguments); //$NON-NLS-1$//$NON-NLS-2$ RefactoringDescriptor desc = new ExtractLocalVariableRefactoringDescription(project.getProject().getName(),
"Extract Local Variable Refactoring", "Extract " + target.getRawSignature(), arguments); //$NON-NLS-1$//$NON-NLS-2$
return desc; return desc;
} }

View file

@ -19,7 +19,6 @@ import org.eclipse.cdt.internal.ui.refactoring.CRefactoringContribution;
/** /**
* @author Emanuel Graf IFS * @author Emanuel Graf IFS
*
*/ */
public class ExtractLocalVariableRefactoringContribution extends CRefactoringContribution { public class ExtractLocalVariableRefactoringContribution extends CRefactoringContribution {
@ -27,9 +26,9 @@ public class ExtractLocalVariableRefactoringContribution extends CRefactoringCon
@Override @Override
public RefactoringDescriptor createDescriptor(String id, String project, String description, public RefactoringDescriptor createDescriptor(String id, String project, String description,
String comment, Map arguments, int flags) throws IllegalArgumentException { String comment, Map arguments, int flags) throws IllegalArgumentException {
if(id.equals(ExtractLocalVariableRefactoring.ID)) { if (id.equals(ExtractLocalVariableRefactoring.ID)) {
return new ExtractLocalVariableRefactoringDescription(project, description, comment, arguments); return new ExtractLocalVariableRefactoringDescription(project, description, comment, arguments);
}else { } else {
return null; return null;
} }
} }

View file

@ -34,7 +34,8 @@ public class ExtractLocalVariableRefactoringDescription extends CRefactoringDesc
public ExtractLocalVariableRefactoringDescription(String project, String description, public ExtractLocalVariableRefactoringDescription(String project, String description,
String comment, Map<String, String> arguments) { String comment, Map<String, String> arguments) {
super(ExtractLocalVariableRefactoring.ID, project, description, comment, CRefactoringDescription.MULTI_CHANGE, arguments); super(ExtractLocalVariableRefactoring.ID, project, description, comment,
CRefactoringDescription.MULTI_CHANGE, arguments);
} }
@Override @Override
@ -52,5 +53,4 @@ public class ExtractLocalVariableRefactoringDescription extends CRefactoringDesc
ISelection selection = getSelection(); ISelection selection = getSelection();
return new ExtractLocalVariableRefactoring(file, selection, info, proj); return new ExtractLocalVariableRefactoring(file, selection, info, proj);
} }
} }

View file

@ -29,16 +29,15 @@ import org.eclipse.cdt.internal.ui.refactoring.RefactoringRunner;
*/ */
public class ExtractLocalVariableRefactoringRunner extends RefactoringRunner { public class ExtractLocalVariableRefactoringRunner extends RefactoringRunner {
public ExtractLocalVariableRefactoringRunner(IFile file, public ExtractLocalVariableRefactoringRunner(IFile file, ISelection selection,
ISelection selection, IShellProvider shellProvider, ICProject cProject) { IShellProvider shellProvider, ICProject cProject) {
super(file, selection, null, shellProvider, cProject); super(file, selection, null, shellProvider, cProject);
} }
@Override @Override
public void run() { public void run() {
NameNVisibilityInformation info = new NameNVisibilityInformation(); NameNVisibilityInformation info = new NameNVisibilityInformation();
CRefactoring refactoring = new ExtractLocalVariableRefactoring(file, CRefactoring refactoring = new ExtractLocalVariableRefactoring(file, selection, info, project);
selection, info, project);
ExtractLocalVariableRefactoringWizard wizard = new ExtractLocalVariableRefactoringWizard( ExtractLocalVariableRefactoringWizard wizard = new ExtractLocalVariableRefactoringWizard(
refactoring, info); refactoring, info);
RefactoringWizardOpenOperation operator = new RefactoringWizardOpenOperation(wizard); RefactoringWizardOpenOperation operator = new RefactoringWizardOpenOperation(wizard);