<commit_before>/*
 * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 */
package com.intellij.testGuiFramework.fixtures;

import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testGuiFramework.framework.Timeouts;
import org.fest.swing.core.Robot;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.edt.GuiTask;
import org.fest.swing.timing.Condition;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

import static org.fest.assertions.Assertions.assertThat;
import static org.fest.reflect.core.Reflection.method;
import static org.fest.swing.edt.GuiActionRunner.execute;
import static org.fest.swing.timing.Pause.pause;
import static org.fest.util.Strings.quote;
import static org.junit.Assert.*;

public class FileEditorFixture extends EditorFixture {

  private final FileEditorManager myManager;
  private final IdeFrameFixture myFrame;
  private final EditorTabsFixture tabs;

  public FileEditorFixture(Robot robot, IdeFrameFixture frame) {
    super(robot, null);
    myManager = FileEditorManager.getInstance(frame.getProject());
    execute(new GuiTask() {
      @Override
      protected void executeInEDT() {
        FileEditorFixture.this.setEditor(myManager.getSelectedTextEditor());
      }
    });
    myFrame = frame;
    tabs = new EditorTabsFixture(robot, frame);
  }

  /**
   * Returns the current file being shown in the editor, if there is a current
   * editor open and it's a file editor
   *
   * @return the currently edited file or null
   */
  @Nullable
  public VirtualFile getCurrentFile() {
    VirtualFile[] selectedFiles = myManager.getSelectedFiles();
    if (selectedFiles.length > 0) {

      // we should be sure that EditorComponent is already showing
      VirtualFile selectedFile = selectedFiles[0];
      if (myManager.getEditors(selectedFile).length == 0) {
        return null;
      }
      else {
        FileEditor editor = myManager.getEditors(selectedFile)[0];
        return editor.getComponent().isShowing() ? selectedFile : null;
      }
    }

    return null;
  }

  /**
   * Returns the name of the current file, if any. Convenience method
   * for {@link #getCurrentFile()}.getName().
   *
   * @return the current file name, or null
   */
  @Nullable
  public String getCurrentFileName() {
    return execute(new GuiQuery<String>() {
      @Override
      protected String executeInEDT() throws Throwable {
        VirtualFile currentFile = getCurrentFile();
        return currentFile != null ? currentFile.getName() : null;
      }
    });
  }

  /**
   * Selects the given tab in the current editor. Used to switch between
   * design mode and editor mode for example.
   *
   * @param tabName the label in the editor, or null for the default (first) tab
   */
  public EditorFixture selectEditorView(@Nullable final String tabName) {
    execute(new GuiTask() {
      @Override
      protected void executeInEDT() {<commit_msg>[gui-test] calculate getCurrentFile on EDT only<commit_after>/*
 * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 */
package com.intellij.testGuiFramework.fixtures;

import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testGuiFramework.framework.Timeouts;
import org.fest.swing.core.Robot;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.edt.GuiTask;
import org.fest.swing.timing.Condition;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

import static org.fest.assertions.Assertions.assertThat;
import static org.fest.reflect.core.Reflection.method;
import static org.fest.swing.edt.GuiActionRunner.execute;
import static org.fest.swing.timing.Pause.pause;
import static org.fest.util.Strings.quote;
import static org.junit.Assert.*;

public class FileEditorFixture extends EditorFixture {

  private final FileEditorManager myManager;
  private final IdeFrameFixture myFrame;
  private final EditorTabsFixture tabs;

  public FileEditorFixture(Robot robot, IdeFrameFixture frame) {
    super(robot, null);
    myManager = FileEditorManager.getInstance(frame.getProject());
    execute(new GuiTask() {
      @Override
      protected void executeInEDT() {
        FileEditorFixture.this.setEditor(myManager.getSelectedTextEditor());
      }
    });
    myFrame = frame;
    tabs = new EditorTabsFixture(robot, frame);
  }

  /**
   * Returns the current file being shown in the editor, if there is a current
   * editor open and it's a file editor
   *
   * @return the currently edited file or null
   */
  @Nullable
  public VirtualFile getCurrentFile() {
    return execute(new GuiQuery<VirtualFile>() {
      @javax.annotation.Nullable
      @Override
      protected VirtualFile executeInEDT() throws Throwable {
        VirtualFile[] selectedFiles = myManager.getSelectedFiles();
        if (selectedFiles.length > 0) {

          // we should be sure that EditorComponent is already showing
          VirtualFile selectedFile = selectedFiles[0];
          if (myManager.getEditors(selectedFile).length == 0) {
            return null;
          }
          else {
            FileEditor editor = myManager.getEditors(selectedFile)[0];
            return editor.getComponent().isShowing() ? selectedFile : null;
          }
        }

        return null;
      }
    });
  }

  /**
   * Returns the name of the current file, if any. Convenience method
   * for {@link #getCurrentFile()}.getName().
   *
   * @return the current file name, or null
   */
  @Nullable
  public String getCurrentFileName() {
    return execute(new GuiQuery<String>() {
      @Override
      protected String executeInEDT() throws Throwable {
        VirtualFile currentFile = getCurrentFile();
        return currentFile != null ? currentFile.getName() : null;
      }
    });
  }

  /**
   * Selects the given tab in the current editor. Used to switch between
   * design mode and editor mode for example.
   *
   * @param tabName the label in the editor, or null for the default (first) tab
   */
  public EditorFixture selectEditorView(@Nullable final String tabName) {
    execute(new GuiTask() {
      @Override
      protected void executeInEDT() {
{'fraction_non_alphanumeric': 0.0946761800219539, 'fraction_numerical': 0.0038419319429198683, 'mean_word_length': 3.6162127929069032, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4268797', 'n_tokens_mistral': 2153, 'n_tokens_neox': 2088, 'n_words': 563}