问题
I'm developing a DSL using Eclipse's Xtext framework.
For the content assist/code completion, I have an expensive process which generates me a list of strings.
How do I cache the result of that process?
Long story: My DSL interfaces with Groovy scripts. The scripts provide methods which I offer in certain places in my DSL. This is pretty slow, even when I use a regexp to parse the methods of the scripts. So I'd like to cache the results of the script analysis.
From my analysis, the analysis code is called during validation (so I don't always have an editor) and when the user opens a DSL file.
There is no way to tell when the validation is over (the code is in a private method and the Xtext developers refuse to change that). But I figure that this must be a common problem when writing editors/compilers for Eclipse. How do other people solve this problem? Is there some caching service in the Eclipse framework?
回答1:
You could make use of the JVM model Xtext provides. As long as you have the groovy plugin installed, its types and methods should be available through it.
Caching:
On the resource there is a cache which is automatically evicted if there's a change in it.
((XtextResource)x.eResource()).getCache().get(myKey, x.eResource(),
new Provider<List<String>>(){
public List<String> get() {
return computeGroovyMethodNames();
}
})
The cache can also be injected :
@Inject
private IResourceScopeCache cache
回答2:
I'm not sure, but I think you can reuse org.eclipse.xtext.util.SimpleCache for such case.
来源:https://stackoverflow.com/questions/8309909/how-do-i-attach-some-cached-information-to-an-eclipse-editor-or-resource