The secret behind OutlineView in Eclipse - Delta

(All is under the scope of JAVA, the provided example is applied in plugin development)

Everyone uses eclipse must know what is OutlineView. If you could not find it, choose "Window->show view->outline". This view shows the skeleton of the currently opened java file. The most interesting thing is not what it shows, but the synchronization functionality. How the view reflect what you are editing so fast? Activate the outline view, press "shift+alt+F1", wow! a plugin-spy appears. Let's choose the JavaOutlinePage and continue our adventure.

 

From our experience, we could know

1. Caculating this change is much lighter than buiding a full model; Otherwise, the view won't change simultaneously.

2. The synchronzation is triggered by some kind of event. Because everytime you stop typing for some seconds, then the view start to change. Lucky we, we can find a "ElementChangedListener" at the top of body. It sounds like what we are looking for and actually it is. 

 

 

This listener caputres a "ElementChangedEvent", and extract a "delta" from the event by the code "e.getDelta()". This delta is the key for the secret.

 

In math, delta means difference. It stands for the distance between two discrete points. Here, delta is the changes between two versions of code. Let me give you a intuition that what it looks like. Add the following code fragment.

 

 

During the runtime, I change a function name from "getZero" to "getZer". Then I see such infomation in console

 

[Working copy] T.java[*]: {CHILDREN | FINE GRAINED | AST AFFECTED}
 T[*]: {CHILDREN | FINE GRAINED}
  getZer()[+]: {}
  getZero()[-]: {}

 

It tells what has been changed in the previous version. From top to bottom, it's a path of changes. "T.java" is a file, "T" is the class in "T.java", then "getZer()" is added and "getZero()" is deleted which can be read from "[+]" and "[-]". Then let's see what is delta and how it is created?

 

The type of delta is "JavaElementDelta ", it extends SimpleDelta and implements IJavaElementDelta . From the constructor

 

we could assume it's nothing but a decorator of IJavaElement . Acctually, JavaElementDelta is "changed IJavaElement".

 

To build a "JavaElementDelta"[code ], we have to use "JavaElementDeltaBuilder"[code ], I spent several days reading it and finally had a clear view of the approximate work flow.

1. Old JavaElement information is recorded in Map infos. Start to find addtions.

2. Read the current code, if corresponding element exists in infos , that means this element is not changed. Then delete the element from Map. If it doesn't exist, that means it's a new element and a delta should be created.

3. During the creation of delta, actually it's a delta tree. The process traces back and records all its parents. Then wrap every JavaElement into JavaElementDelta and insert it into some appropriate place in the tree.

4. Find additions is finised and start to find deletions. All elements left in infos are deleted from the orignal code.

5. If a deleted element is found, a new delta should be inserted to the current delta tree. The place where it should be added is where it existed (which is its dierct parent).

6. Checking position ordering changes.

 

This is how the delta is created. Actually, it's a descriptive information showing changes. The OutlineView apply this delta by "reconcile(delta)", old model changes accordingly so new one doesn't need to be created fully.

 

But somehow, deleting a letter inside the method body could not be sensed by delta. For answering this question, I'll introduce JavaElement in the next article. Briefly speaking, as I said, delta is a changed Java element. If the change is not Java element, of course it couldn't be a delta.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值