5151import org .eclipse .jdt .internal .compiler .parser .RecoveryScanner ;
5252
5353class DOMCompletionContext extends CompletionContext {
54+
55+ private final CompilationUnit domUnit ;
56+ private final ITypeRoot modelUnit ;
5457 private final int offset ;
5558 private final char [] token ;
56- private final IJavaElement enclosingElement ;
59+
60+ private IJavaElement enclosingElement ;
61+ private boolean enclosingElementComputed ;
5762 private final Supplier <Stream <IBinding >> bindingsAcquirer ;
5863 final ExpectedTypes expectedTypes ;
5964 private boolean inJavadoc = false ;
@@ -62,6 +67,8 @@ class DOMCompletionContext extends CompletionContext {
6267 private boolean isJustAfterStringLiteral ;
6368
6469 DOMCompletionContext (CompilationUnit domUnit , ITypeRoot modelUnit , String textContent , int offset , AssistOptions assistOptions , Bindings bindings ) {
70+ this .domUnit = domUnit ;
71+ this .modelUnit = modelUnit ;
6572 this .textContent = textContent ;
6673 this .offset = offset ;
6774 // Use the raw text to walk back the offset to the first non-whitespace spot
@@ -88,7 +95,6 @@ class DOMCompletionContext extends CompletionContext {
8895 : previousNodeBeforeWhitespaces ; // use previous node
8996 this .expectedTypes = new ExpectedTypes (assistOptions , this .node , offset );
9097 this .token = tokenBefore (this .textContent ).toCharArray ();
91- this .enclosingElement = computeEnclosingElement (domUnit , modelUnit );
9298 this .bindingsAcquirer = bindings ::all ;
9399 this .isJustAfterStringLiteral = this .node instanceof StringLiteral && this .node .getLength () > 1 && this .offset >= this .node .getStartPosition () + this .node .getLength () && textContent .charAt (this .offset - 1 ) == '"' ;
94100 }
@@ -175,6 +181,10 @@ public void setInJavadoc(boolean inJavadoc) {
175181
176182 @ Override
177183 public IJavaElement getEnclosingElement () {
184+ if (!this .enclosingElementComputed ) {
185+ this .enclosingElement = computeEnclosingElement (domUnit , modelUnit );
186+ this .enclosingElementComputed = true ;
187+ }
178188 return this .enclosingElement ;
179189 }
180190
0 commit comments