Community
Participate
Working Groups
20051123 It is currently quite difficult to apply a text edit to a compilation unit. Text edits are the result of the code formatter or the import rewriter. Currently edits can only be applied to IDocument. Having a compilation unit in hand, it is a lot of work or inefficient to get the corresponding document: - If the compilation unit is a primary working copy, you go the FileBuffers to get the document: ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager(); IPath path= compilationUnit.getPath(); bufferManager.connect(path, monitor); document= bufferManager.getTextFileBuffer(path).getDocument(); ... textEdit.apply(document).... bufferManager.disconnect(path, monitor); - If the compilation unit is a non-primary working copy then you have to build a document yourself, which is very inefficient (copying content): Document document= new Document(compilationUnit.getSource()) ... textEdit.apply(document).... compilationUnit.getBuffer().setContents(document.get()) We need an easier way to apply edits on documents, to offer a nicer story to clients. Here are some suggestions: a.) Get a document from the compilation unit: openable.getDocument(). The compilation builds a wrapper that implements IDocument but redirects to IBuffer. + simple API - Users must understand that the returned document is not the document managed by filebuffers: Some of the API would be 'not supported', e.g. you don't get positions and have no partitions. Regarding of funtionality, this is not a problem (to apply a change you don't need these features), but could be confusing. b.) Add a method apply(textEdit) to IBuffer. Problem is IBuffer in implemented by clients, so it can not be extended. A new 'extension' could be added, IRichBuffer or abstract class RichBuffer, where clients can check for with a cast. Implementors of WorkingCopyOwner would return such 'rich buffers' and can e.g. forward a call apply(textEdit) directly to the underlying document. + most efficient, no additional indirection + at the same time line position API could be added to the rich buffers, solving the biggest usability problem with buffers. - Extra cast is not so nice. Maybe IOpenable should offer getRichBuffer that replaces getBuffer
IOpenable#getDocument() would also be interesting for clients of ASTRewrite#rewriteAST(IDocument, Map). Currently, they also have to copy the document when they create an AST on a non-primary working copy.
Moving to 3.3M6 since Martin is back from vacations after M5 is out.
did not find the time to look at this for M6
I think the "plan" keyword should be removed here. That keyword is only intended for bugs on the 3.3 release plan (http://www.eclipse.org/eclipse/development/eclipse_project_plan_3_3.html)
removing the 'plan' keyword
Need more discussion (with Jerome) to convince we want to go into this direction for 3.4 (looks like cleanup only on the surface; i.e cosmetical).
One idea is to introduce ITextEditBuffer (or another name) that extends IBuffer. The api on ICompilationUnit would apply the edits to the buffer if it is an ITextEditBuffer, or it would apply the edits in memory and use IBuffer.setContents() if it is a regular buffer. We should then encourage clients to implement ITextEditBuffer (especially in WorkingCopyOwner.createBuffer(). Time permitting though.
Created attachment 90081 [details] patch Patch containing changes in jdt.core and tests.
Created attachment 90082 [details] patch for jdt-ui Patch for JDT-UI to support the API and (some first) usages.
(In reply to comment #8) This looks very good. A few remarks though: 1. ICompilationUnit#applyTextEdit(...) is missing a @since 3.4 tag 2. It should say that it applies the text edit to the compilation unit's buffer (not to the compilation unit itself) since a reconcile (in working copy mode) or a save (in compilation unit mode) will be necessary to reflect the changes on the ICompilationUnit 3. The "@throws JavaModelException" tag should also say that this is thrown if the compilation unit doesn't exist 4. We usually don't use Assert, so if the buffer is null (which should never happen for a compilation unit), it should just do nothing
Created attachment 90839 [details] updated patch
(In reply to comment #11) +1
patch released, build notes updated > 20080227
I also released the JDT/UI patch (support for applyTextEdits our IBuffer implementation). Filed bug 220565 to update code in JDT/Core to use the new API.
this got released for 3.4 M6
Verified in the code for 3.4M6 using build I20080324-1300