Java Code Examples for org.eclipse.jdt.core.refactoring.CompilationUnitChange#setSaveMode()

The following examples show how to use org.eclipse.jdt.core.refactoring.CompilationUnitChange#setSaveMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChangeUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Destructively clones a {@link CompilationUnitChange} where the cloned
 * change will have a different compilation unit. This does not update text
 * regions or anything more than setting the change properties and moving text
 * edits from the old to new change.
 * 
 * @param originalChange the original change, this change's internal state
 *          will likely become invalid (its text edits will be moved to the
 *          new change)
 * @param cu the compilation unit to be used for the new
 *          {@link CompilationUnitChange}
 * @return the cloned {@link CompilationUnitChange}
 */
public static CompilationUnitChange cloneCompilationUnitChangeWithDifferentCu(
    TextFileChange originalChange, ICompilationUnit cu) {
  CompilationUnitChange newChange = new CompilationUnitChange(
      originalChange.getName(), cu);

  newChange.setEdit(originalChange.getEdit());
  newChange.setEnabledShallow(originalChange.isEnabled());
  newChange.setKeepPreviewEdits(originalChange.getKeepPreviewEdits());
  newChange.setSaveMode(originalChange.getSaveMode());
  newChange.setTextType(originalChange.getTextType());

  // Copy the changes over
  TextEditUtilities.moveTextEditGroupsIntoChange(
      originalChange.getChangeGroups(), newChange);

  return newChange;
}
 
Example 2
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected static CompilationUnitChange createCompilationUnitChange(CompilationUnitRewrite rewrite) throws CoreException {
	CompilationUnitChange change= rewrite.createChange(true);
	if (change != null) {
		change.setSaveMode(TextFileChange.KEEP_SAVE_STATE);
	}

	return change;
}
 
Example 3
Source File: FixCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected TextChange createTextChange() throws CoreException {
	CompilationUnitChange createChange = fFix.createChange(null);
	createChange.setSaveMode(TextFileChange.LEAVE_DIRTY);

	//			if (fFix instanceof ILinkedFix) {
	//				setLinkedProposalModel(((ILinkedFix) fFix).getLinkedPositions());
	//			}

	return createChange;
}
 
Example 4
Source File: AddResourcesToClientBundleAction.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
public void run(IProgressMonitor monitor) throws CoreException {
  ICompilationUnit icu = clientBundle.getCompilationUnit();
  CompilationUnit cu = JavaASTUtils.parseCompilationUnit(icu);
  ImportRewrite importRewrite = StubUtility.createImportRewrite(cu, true);

  // Find the target type declaration
  TypeDeclaration typeDecl = JavaASTUtils.findTypeDeclaration(cu,
      clientBundle.getFullyQualifiedName());
  if (typeDecl == null) {
    throw new CoreException(
        StatusUtilities.newErrorStatus("Missing ClientBundle type "
            + clientBundle.getFullyQualifiedName(), GWTPlugin.PLUGIN_ID));
  }

  // We need to rewrite the AST of the ClientBundle type declaration
  ASTRewrite astRewrite = ASTRewrite.create(cu.getAST());
  ChildListPropertyDescriptor property = ASTNodes.getBodyDeclarationsProperty(typeDecl);
  ListRewrite listRewriter = astRewrite.getListRewrite(typeDecl, property);

  // Add the new resource methods
  boolean addComments = StubUtility.doAddComments(icu.getJavaProject());
  for (ClientBundleResource resource : resources) {
    listRewriter.insertLast(resource.createMethodDeclaration(clientBundle,
        astRewrite, importRewrite, addComments), null);
  }

  // Create the edit to add the methods and update the imports
  TextEdit rootEdit = new MultiTextEdit();
  rootEdit.addChild(astRewrite.rewriteAST());
  rootEdit.addChild(importRewrite.rewriteImports(null));

  // Apply the change to the compilation unit
  CompilationUnitChange cuChange = new CompilationUnitChange(
      "Update ClientBundle", icu);
  cuChange.setSaveMode(TextFileChange.KEEP_SAVE_STATE);
  cuChange.setEdit(rootEdit);
  cuChange.perform(new NullProgressMonitor());
}
 
Example 5
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected static CompilationUnitChange createCompilationUnitChange(CompilationUnitRewrite rewrite) throws CoreException {
	CompilationUnitChange change= rewrite.createChange(true);
	if (change != null)
		change.setSaveMode(TextFileChange.KEEP_SAVE_STATE);

	return change;
}
 
Example 6
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
	ASTParser p= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
	p.setSource(getDestinationCu());
	CompilationUnit cuNode= (CompilationUnit) p.createAST(pm);
	ASTRewrite rewrite= ASTRewrite.create(cuNode.getAST());
	TypedSource source= null;
	for (int i= fSources.length - 1; i >= 0; i--) {
		source= fSources[i];
		final ASTNode destination= getDestinationNodeForSourceElement(fDestination, source.getType(), cuNode);
		if (destination != null) {
			if (destination instanceof CompilationUnit)
				insertToCu(rewrite, createNewNodeToInsertToCu(source, rewrite), (CompilationUnit) destination);
			else if (destination instanceof AbstractTypeDeclaration)
				insertToType(rewrite, createNewNodeToInsertToType(source, rewrite), (AbstractTypeDeclaration) destination);
		}
	}
	final CompilationUnitChange result= new CompilationUnitChange(ReorgMessages.PasteAction_change_name, getDestinationCu());
	try {
		ITextFileBuffer buffer= RefactoringFileBuffers.acquire(getDestinationCu());
		TextEdit rootEdit= rewrite.rewriteAST(buffer.getDocument(), fDestination.getJavaProject().getOptions(true));
		if (getDestinationCu().isWorkingCopy())
			result.setSaveMode(TextFileChange.LEAVE_DIRTY);
		TextChangeCompatibility.addTextEdit(result, ReorgMessages.PasteAction_edit_name, rootEdit);
	} finally {
		RefactoringFileBuffers.release(getDestinationCu());
	}
	return result;
}
 
Example 7
Source File: FixCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected TextChange createTextChange() throws CoreException {
	CompilationUnitChange createChange= fFix.createChange(null);
	createChange.setSaveMode(TextFileChange.LEAVE_DIRTY);

	if (fFix instanceof ILinkedFix) {
		setLinkedProposalModel(((ILinkedFix) fFix).getLinkedPositions());
	}

	return createChange;
}