private void selectViewableElement() {
   PsiDocumentManager.getInstance(myProject).commitAllDocuments();
   final Object currentEditorElement = myTreeModel.getCurrentEditorElement();
   if (currentEditorElement != null) {
     select(currentEditorElement, false);
   }
 }
  @Nullable
  public PsiElement getCurrentElement(@Nullable final PsiFile psiFile) {
    if (psiFile == null) return null;

    PsiDocumentManager.getInstance(myProject).commitAllDocuments();

    Object elementAtCursor = myTreeModel.getCurrentEditorElement();
    if (elementAtCursor instanceof PsiElement) {
      return (PsiElement) elementAtCursor;
    }

    return null;
  }
  @Override
  public void invoke(
      @NotNull final Project project, @NotNull final Editor editor, @NotNull PsiFile file) {
    PsiDocumentManager.getInstance(project).commitAllDocuments();

    PsiElement container = null;
    WeakReference<LightweightHint> ref = editor.getUserData(MY_LAST_HINT_KEY);
    if (ref != null) {
      LightweightHint hint = ref.get();
      if (hint != null && hint.isVisible()) {
        hint.hide();
        container = hint.getUserData(CONTAINER_KEY);
        if (container != null && !container.isValid()) {
          container = null;
        }
      }
    }

    StructureViewBuilder builder =
        LanguageStructureViewBuilder.INSTANCE.getStructureViewBuilder(file);
    if (builder instanceof TreeBasedStructureViewBuilder) {
      StructureViewModel model =
          ((TreeBasedStructureViewBuilder) builder).createStructureViewModel();
      boolean goOneLevelUp = true;
      if (container == null) {
        goOneLevelUp = false;
        Object element = model.getCurrentEditorElement();
        if (element instanceof PsiElement) {
          container = (PsiElement) element;
        }
      }
      while (true) {
        if (container == null || container instanceof PsiFile) {
          return;
        }
        if (goOneLevelUp) {
          goOneLevelUp = false;
        } else {
          if (!isDeclarationVisible(container, editor)) {
            break;
          }
        }

        container = container.getParent();
        while (container != null
            && DeclarationRangeUtil.getPossibleDeclarationAtRange(container) == null) {
          container = container.getParent();
          if (container instanceof PsiFile) return;
        }
      }
    }
    if (container == null) {
      return;
    }

    final TextRange range = DeclarationRangeUtil.getPossibleDeclarationAtRange(container);
    if (range == null) {
      return;
    }
    final PsiElement _container = container;
    ApplicationManager.getApplication()
        .invokeLater(
            new Runnable() {
              @Override
              public void run() {
                LightweightHint hint =
                    EditorFragmentComponent.showEditorFragmentHint(editor, range, true, true);
                if (hint != null) {
                  hint.putUserData(CONTAINER_KEY, _container);
                  editor.putUserData(MY_LAST_HINT_KEY, new WeakReference<LightweightHint>(hint));
                }
              }
            });
  }
 public boolean navigateToSelectedElement(boolean requestFocus) {
   return select(myTreeModel.getCurrentEditorElement(), requestFocus);
 }
 @Override
 public Object getCurrentEditorElement() {
   return myStructureViewModel.getCurrentEditorElement();
 }