ExpressionContext (final int start) { return new ExpressionContext() { @Override public Project getProject() { return myProject; } @Override public Editor getEditor() { return TemplateState.this.getEditor(); } @Override public int getStartOffset() { return start; } @Override public int getTemplateStartOffset() { if (myTemplateRange == null) { return -1; } return myTemplateRange.getStartOffset(); } @Override public int getTemplateEndOffset() { if (myTemplateRange == null) { return -1; } return myTemplateRange.getEndOffset(); } @Override public <T> T getProperty(Key<T> key) { //noinspection unchecked return (T)getProperties().get(key); } @Override public @Nullable PsiElement getPsiElementAtStartOffset() { Project project = getProject(); int templateStartOffset = getTemplateStartOffset(); int offset = templateStartOffset > 0 ? getTemplateStartOffset() - 1 : getTemplateStartOffset(); Editor editor = getEditor(); if (editor == null) { return null; } PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); return file == null ? null : file.findElementAt(offset); } @Override public TextResult getVariableValue(String variableName) { return TemplateState.this.getVariableValue(variableName); } }; }