Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 90839 Details for
Bug 117694
[api] Applying edits to a ICompilationUnit
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
updated patch
117694.patch (text/plain), 9.54 KB, created by
Martin Aeschlimann
on 2008-02-27 05:39:27 EST
(
hide
)
Description:
updated patch
Filename:
MIME Type:
Creator:
Martin Aeschlimann
Created:
2008-02-27 05:39:27 EST
Size:
9.54 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.jdt.core >Index: model/org/eclipse/jdt/internal/core/CompilationUnit.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java,v >retrieving revision 1.245 >diff -u -r1.245 CompilationUnit.java >--- model/org/eclipse/jdt/internal/core/CompilationUnit.java 26 Feb 2008 10:11:46 -0000 1.245 >+++ model/org/eclipse/jdt/internal/core/CompilationUnit.java 27 Feb 2008 10:34:34 -0000 >@@ -31,6 +31,12 @@ > import org.eclipse.jdt.internal.core.util.Messages; > import org.eclipse.jdt.internal.core.util.Util; > >+import org.eclipse.jface.text.BadLocationException; >+ >+import org.eclipse.text.edits.MalformedTreeException; >+import org.eclipse.text.edits.TextEdit; >+import org.eclipse.text.edits.UndoEdit; >+ > /** > * @see ICompilationUnit > */ >@@ -56,6 +62,30 @@ > this.name = name; > this.owner = owner; > } >+ >+ >+/* >+ * @see ICompilationUnit#applyTextEdit(TextEdit, IProgressMonitor) >+ */ >+public UndoEdit applyTextEdit(TextEdit edit, IProgressMonitor monitor) throws JavaModelException { >+ IBuffer buffer= getBuffer(); >+ if (buffer instanceof IBuffer.ITextEditCapability) { >+ return ((IBuffer.ITextEditCapability) buffer).applyTextEdit(edit, monitor); >+ } else if (buffer != null) { >+ DocumentAdapter document= new DocumentAdapter(buffer); >+ try { >+ UndoEdit undoEdit= edit.apply(document); >+ buffer.setContents(document.get()); >+ return undoEdit; >+ } catch (MalformedTreeException e) { >+ throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION); >+ } catch (BadLocationException e) { >+ throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION); >+ } >+ } >+ return null; // can not happen, there are no compilation units without buffer >+} >+ > /* > * @see ICompilationUnit#becomeWorkingCopy(IProblemRequestor, IProgressMonitor) > */ >Index: model/org/eclipse/jdt/core/IJavaModelStatusConstants.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/IJavaModelStatusConstants.java,v >retrieving revision 1.38 >diff -u -r1.38 IJavaModelStatusConstants.java >--- model/org/eclipse/jdt/core/IJavaModelStatusConstants.java 19 Jan 2007 17:13:27 -0000 1.38 >+++ model/org/eclipse/jdt/core/IJavaModelStatusConstants.java 27 Feb 2008 10:34:34 -0000 >@@ -326,4 +326,12 @@ > * @since 3.3 > */ > public static final int DEPRECATED_VARIABLE = 1010; >+ >+ /** >+ * <p>Status constant indicating that a text edit can not be applied as there >+ * is a problem with the text edit location.</p> >+ * >+ * @since 3.4 >+ */ >+ public static final int BAD_TEXT_EDIT_LOCATION = 1011; > } >Index: model/org/eclipse/jdt/core/IBuffer.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/IBuffer.java,v >retrieving revision 1.20 >diff -u -r1.20 IBuffer.java >--- model/org/eclipse/jdt/core/IBuffer.java 10 May 2006 18:03:42 -0000 1.20 >+++ model/org/eclipse/jdt/core/IBuffer.java 27 Feb 2008 10:34:34 -0000 >@@ -12,6 +12,8 @@ > > import org.eclipse.core.resources.IResource; > import org.eclipse.core.runtime.IProgressMonitor; >+import org.eclipse.text.edits.TextEdit; >+import org.eclipse.text.edits.UndoEdit; > > /** > * A buffer contains the text contents of a resource. It is not language-specific. >@@ -34,6 +36,32 @@ > public interface IBuffer { > > /** >+ * Implementors of {@link IBuffer} can additionally implement {@link IBuffer.ITextEditCapability}. >+ * This adds the capability to apply text edits to the buffer and will be used by >+ * {@link ICompilationUnit#applyTextEdit(TextEdit, IProgressMonitor)}. >+ * >+ * <p> >+ * This interface may be implemented by clients. >+ * </p> >+ * @since 3.4 >+ */ >+public interface ITextEditCapability { >+ /** >+ * Applies a text edit to this underlying buffer. >+ * >+ * @param edit the edit to apply >+ * @param monitor the progress monitor to use or <code>null</code> if no progress should be reported >+ * @return the undo edit >+ * @throws JavaModelException if this edit can not be applied to the buffer. Reasons include: >+ * <ul> >+ * <li>The provided edit can not be applied as there is a problem with the text edit locations ({@link IJavaModelStatusConstants#BAD_TEXT_EDIT_LOCATION})}.</li> >+ * </ul> >+ */ >+ public UndoEdit applyTextEdit(TextEdit edit, IProgressMonitor monitor) throws JavaModelException; >+} >+ >+ >+/** > * Adds the given listener for changes to this buffer. > * Has no effect if an identical listener is already registered or if the buffer > * is closed. >Index: model/org/eclipse/jdt/core/ICompilationUnit.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/ICompilationUnit.java,v >retrieving revision 1.64 >diff -u -r1.64 ICompilationUnit.java >--- model/org/eclipse/jdt/core/ICompilationUnit.java 17 Mar 2007 12:21:48 -0000 1.64 >+++ model/org/eclipse/jdt/core/ICompilationUnit.java 27 Feb 2008 10:34:34 -0000 >@@ -16,6 +16,8 @@ > import org.eclipse.jdt.core.dom.CompilationUnit; > import org.eclipse.jdt.core.dom.AST; > import org.eclipse.jdt.core.dom.IBinding; >+import org.eclipse.text.edits.TextEdit; >+import org.eclipse.text.edits.UndoEdit; > > > /** >@@ -62,6 +64,22 @@ > public static final int ENABLE_BINDINGS_RECOVERY = 0x04; > > /** >+ * Applies a text edit to the compilation unit's buffer. >+ * >+ * @param edit the edit to apply >+ * @param monitor the progress monitor to use or <code>null</code> if no progress should be reported >+ * @return the undo edit >+ * @throws JavaModelException if this edit can not be applied to the compilation unit's buffer. Reasons include: >+ * <ul> >+ * <li>This compilation unit does not exist ({@link IJavaModelStatusConstants#ELEMENT_DOES_NOT_EXIST}).</li> >+ * <li>The provided edit can not be applied as there is a problem with the text edit locations ({@link IJavaModelStatusConstants#BAD_TEXT_EDIT_LOCATION}).</li> >+ * </ul> >+ * >+ * @since 3.4 >+ */ >+public UndoEdit applyTextEdit(TextEdit edit, IProgressMonitor monitor) throws JavaModelException; >+ >+/** > * Changes this compilation unit handle into a working copy. A new {@link IBuffer} is > * created using this compilation unit handle's owner. Uses the primary owner if none was > * specified when this compilation unit handle was created. >#P org.eclipse.jdt.core.tests.model >Index: src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java,v >retrieving revision 1.59 >diff -u -r1.59 CompilationUnitTests.java >--- src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java 16 Jan 2008 11:52:47 -0000 1.59 >+++ src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java 27 Feb 2008 10:34:38 -0000 >@@ -14,6 +14,8 @@ > import java.net.URI; > import java.net.URISyntaxException; > >+import junit.framework.Test; >+ > import org.eclipse.core.resources.IProject; > import org.eclipse.core.resources.IProjectDescription; > import org.eclipse.core.resources.IWorkspace; >@@ -22,10 +24,12 @@ > import org.eclipse.core.runtime.ILogListener; > import org.eclipse.core.runtime.IStatus; > import org.eclipse.jdt.core.*; >-import org.eclipse.jdt.internal.core.*; >+import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; >+import org.eclipse.jdt.internal.core.CompilationUnit; > import org.eclipse.jdt.internal.core.util.Util; >- >-import junit.framework.Test; >+import org.eclipse.text.edits.ReplaceEdit; >+import org.eclipse.text.edits.TextEdit; >+import org.eclipse.text.edits.UndoEdit; > > public class CompilationUnitTests extends ModifyingResourceTests { > ICompilationUnit cu; >@@ -2031,4 +2035,65 @@ > deleteFile("/P/src/X.java"); > } > } >+ >+public void testApplyEdit() throws CoreException { >+ try { >+ String source = >+ "public class X {\n" + >+ "}\n"; >+ createFile("/P/src/X.java", source); >+ ICompilationUnit compilationUnit = getCompilationUnit("/P/src/X.java"); >+ >+ ReplaceEdit edit= new ReplaceEdit(0, 6, "private"); >+ >+ UndoEdit undoEdit= compilationUnit.applyTextEdit(edit, null); >+ >+ String newSource = >+ "private class X {\n" + >+ "}\n"; >+ >+ assertEquals(newSource, compilationUnit.getSource()); >+ >+ compilationUnit.applyTextEdit(undoEdit, null); >+ >+ assertEquals(source, compilationUnit.getSource()); >+ } finally { >+ deleteFile("/P/src/X.java"); >+ } >+} >+ >+public void testApplyEdit2() throws CoreException { >+ try { >+ String source = >+ "public class X {\n" + >+ "}\n"; >+ createFile("/P/src/X.java", source); >+ ICompilationUnit compilationUnit = getCompilationUnit("/P/src/X.java"); >+ >+ ImportRewrite importRewrite= ImportRewrite.create(compilationUnit, true); >+ importRewrite.addImport("java.util.Vector"); >+ importRewrite.addImport("java.util.ArrayList"); >+ >+ TextEdit edit= importRewrite.rewriteImports(null); >+ >+ UndoEdit undoEdit= compilationUnit.applyTextEdit(edit, null); >+ >+ String newSource = >+ "import java.util.ArrayList;\n" + >+ "import java.util.Vector;\n" + >+ "\n" + >+ "public class X {\n" + >+ "}\n"; >+ >+ assertEquals(newSource, compilationUnit.getSource()); >+ >+ compilationUnit.applyTextEdit(undoEdit, null); >+ >+ assertEquals(source, compilationUnit.getSource()); >+ } finally { >+ deleteFile("/P/src/X.java"); >+ } >+} >+ >+ > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 117694
:
90081
|
90082
| 90839