package com.karateca;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.popup.AbstractPopup;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Font;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Andres Dominguez.
 */
public class DDescirberAction extends AnAction {

  protected Project project;
  protected EditorImpl editor;
  protected AbstractPopup popup;
  protected VirtualFile virtualFile;
  protected DocumentImpl document;

  private Font font;
  private EditorColorsScheme scheme;

  private AceFinder aceFinder;

  private HashMap<String, Integer> textAndOffsetHash = new HashMap<String, Integer>();


  @Override
  public void update(AnActionEvent e) {
    e.getPresentation().setEnabled(e.getData(PlatformDataKeys.EDITOR) != null);
  }

  public void actionPerformed(AnActionEvent actionEvent) {
    project = actionEvent.getData(PlatformDataKeys.PROJECT);
    editor = (EditorImpl) actionEvent.getData(PlatformDataKeys.EDITOR);
    virtualFile = actionEvent.getData(PlatformDataKeys.VIRTUAL_FILE);
    document = (DocumentImpl) editor.getDocument();

    scheme = EditorColorsManager.getInstance().getGlobalScheme();
    font = new Font(scheme.getEditorFontName(), Font.BOLD, scheme.getEditorFontSize());

    aceFinder = new AceFinder(project, document, editor, virtualFile);

    aceFinder.addResultsReadyListener(new ChangeListener() {
      @Override
      public void stateChanged(ChangeEvent changeEvent) {
        if (changeEvent.getSource().equals("LinesFound")) {
          showResultsFound();

        }
      }
    });
    aceFinder.findText("it\\(|describe\\(", true);
  }

  private void showResultsFound() {
    // Filter the hierarchy.
    int currentIndentation = Integer.MAX_VALUE;
    List<LineFindResult> hierarchy = new ArrayList<LineFindResult>();

    // Find all the parents from the current scope.
    for (LineFindResult line : aceFinder.getLineFindResults()) {
      if (line.getIndentation() < currentIndentation) {
        currentIndentation = line.getIndentation();
        hierarchy.add(line);
      }
    }

    System.out.println("");
  }
}

package com.karateca;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Andres Dominguez.
 */
public class DDescirberAction extends AnAction {

  protected Project project;
  protected EditorImpl editor;
  protected VirtualFile virtualFile;
  protected DocumentImpl document;

  private AceFinder aceFinder;


  @Override
  public void update(AnActionEvent e) {
    e.getPresentation().setEnabled(e.getData(PlatformDataKeys.EDITOR) != null);
  }

  public void actionPerformed(AnActionEvent actionEvent) {
    project = actionEvent.getData(PlatformDataKeys.PROJECT);
    editor = (EditorImpl) actionEvent.getData(PlatformDataKeys.EDITOR);
    virtualFile = actionEvent.getData(PlatformDataKeys.VIRTUAL_FILE);
    document = (DocumentImpl) editor.getDocument();

    aceFinder = new AceFinder(project, document, editor, virtualFile);

    aceFinder.addResultsReadyListener(new ChangeListener() {
      @Override
      public void stateChanged(ChangeEvent changeEvent) {
        if (changeEvent.getSource().equals("LinesFound")) {
          showResultsFound();

        }
      }
    });
    aceFinder.findText("it\\(|describe\\(", true);
  }

  private void showResultsFound() {
    // Filter the hierarchy.
    int currentIndentation = Integer.MAX_VALUE;
    List<LineFindResult> hierarchy = new ArrayList<LineFindResult>();

    // Find all the parents from the current scope.
    for (LineFindResult line : aceFinder.getLineFindResults()) {
      if (line.getIndentation() < currentIndentation) {
        currentIndentation = line.getIndentation();
        hierarchy.add(line);
      }
    }

    System.out.println("");
  }
}

remove unused