private void addDefinition( ModificationCollector collector, List<IASTFunctionDefinition> definitions, IProgressMonitor pm) throws CoreException { findDefinitionInsertLocation(pm); IASTNode parent = definitionInsertLocation.getParentOfNodeToInsertBefore(); IASTTranslationUnit ast = parent.getTranslationUnit(); ASTRewrite rewrite = collector.rewriterForTranslationUnit(ast); IASTNode nodeToInsertBefore = definitionInsertLocation.getNodeToInsertBefore(); ContainerNode cont = new ContainerNode(); for (IASTFunctionDefinition functionDefinition : definitions) { cont.addNode(functionDefinition); } rewrite = rewrite.insertBefore(parent, nodeToInsertBefore, cont, null); }
@Override protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) throws CoreException, OperationCanceledException { List<IASTNode> getterAndSetters = new ArrayList<IASTNode>(); List<IASTFunctionDefinition> definitions = new ArrayList<IASTFunctionDefinition>(); for (GetterSetterInsertEditProvider currentProvider : context.selectedFunctions) { if (context.isDefinitionSeparate()) { getterAndSetters.add(currentProvider.getFunctionDeclaration()); IASTFunctionDefinition functionDefinition = currentProvider.getFunctionDefinition(true); // Standalone definitions in a header file have to be declared inline. if (definitionInsertLocation.getTranslationUnit().isHeaderUnit()) { functionDefinition.getDeclSpecifier().setInline(true); } definitions.add(functionDefinition); } else { getterAndSetters.add(currentProvider.getFunctionDefinition(false)); } } if (context.isDefinitionSeparate()) { addDefinition(collector, definitions, pm); } ICPPASTCompositeTypeSpecifier classDefinition = (ICPPASTCompositeTypeSpecifier) context.existingFields.get(context.existingFields.size() - 1).getParent(); AddDeclarationNodeToClassChange.createChange( classDefinition, VisibilityEnum.v_public, getterAndSetters, false, collector); }
private void findDefinitionInsertLocation(IProgressMonitor pm) throws CoreException { if (definitionInsertLocation != null) { return; } IASTSimpleDeclaration decl = context.existingFields.get(0); MethodDefinitionInsertLocationFinder locationFinder = new MethodDefinitionInsertLocationFinder(); InsertLocation location = locationFinder.find(tu, decl.getFileLocation(), decl.getParent(), astCache, pm); if (location.getFile() == null || NodeHelper.isContainedInTemplateDeclaration(decl)) { location.setNodeToInsertAfter(NodeHelper.findTopLevelParent(decl), tu); } definitionInsertLocation = location; }
private IFile[] getAllFilesToModify() { List<IFile> files = new ArrayList<IFile>(2); IFile file = (IFile) tu.getResource(); if (file != null) { files.add(file); } if (definitionInsertLocation != null) { file = definitionInsertLocation.getFile(); if (file != null) { files.add(file); } } return files.toArray(new IFile[files.size()]); }
@Override public RefactoringStatus checkFinalConditions( IProgressMonitor pm, CheckConditionsContext checkContext) throws CoreException, OperationCanceledException { RefactoringStatus result = new RefactoringStatus(); if (context.isDefinitionSeparate()) { findDefinitionInsertLocation(pm); if (definitionInsertLocation == null || definitionInsertLocation.getTranslationUnit() == null) { result.addInfo(Messages.GenerateGettersAndSettersRefactoring_NoImplFile); } } Checks.addModifiedFilesToChecker(getAllFilesToModify(), checkContext); return result; }