Bug 117694 - [api] Applying edits to a ICompilationUnit
Summary: [api] Applying edits to a ICompilationUnit
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.4 M6   Edit
Assignee: Martin Aeschlimann CLA
QA Contact:
URL:
Whiteboard:
Keywords: api
Depends on:
Blocks:
 
Reported: 2005-11-23 05:36 EST by Martin Aeschlimann CLA
Modified: 2008-03-26 02:57 EDT (History)
9 users (show)

See Also:


Attachments
patch (10.42 KB, patch)
2008-02-19 11:58 EST, Martin Aeschlimann CLA
no flags Details | Diff
patch for jdt-ui (8.47 KB, patch)
2008-02-19 12:00 EST, Martin Aeschlimann CLA
no flags Details | Diff
updated patch (9.54 KB, patch)
2008-02-27 05:39 EST, Martin Aeschlimann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Aeschlimann CLA 2005-11-23 05:36:22 EST
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
Comment 1 Markus Keller CLA 2005-11-23 11:39:28 EST
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.
Comment 2 Olivier Thomann CLA 2007-01-29 12:17:04 EST
Moving to 3.3M6 since Martin is back from vacations after M5 is out.
Comment 3 Martin Aeschlimann CLA 2007-03-22 04:36:00 EDT
did not find the time to look at this for M6
Comment 4 John Arthorne CLA 2007-03-28 13:44:21 EDT
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)
Comment 5 Martin Aeschlimann CLA 2007-03-29 04:26:54 EDT
removing the 'plan' keyword
Comment 6 Philipe Mulet CLA 2007-08-31 12:33:21 EDT
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).
Comment 7 Jerome Lanneluc CLA 2008-02-13 11:00:04 EST
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.
Comment 8 Martin Aeschlimann CLA 2008-02-19 11:58:29 EST
Created attachment 90081 [details]
patch

Patch containing changes in jdt.core and tests.
Comment 9 Martin Aeschlimann CLA 2008-02-19 12:00:18 EST
Created attachment 90082 [details]
patch for jdt-ui

Patch for JDT-UI to support the API and (some first) usages.
Comment 10 Jerome Lanneluc CLA 2008-02-27 04:24:27 EST
(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
Comment 11 Martin Aeschlimann CLA 2008-02-27 05:39:27 EST
Created attachment 90839 [details]
updated patch
Comment 12 Jerome Lanneluc CLA 2008-02-27 09:27:34 EST
(In reply to comment #11)
+1

Comment 13 Martin Aeschlimann CLA 2008-02-27 09:50:55 EST
patch released, build notes updated > 20080227
Comment 14 Martin Aeschlimann CLA 2008-02-27 10:08:23 EST
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.
Comment 15 Martin Aeschlimann CLA 2008-02-27 10:13:15 EST
this got released for 3.4 M6
Comment 16 Eric Jodet CLA 2008-03-26 02:57:09 EDT
Verified in the code for 3.4M6 using build I20080324-1300