<commit_before>RailsRSpecFinder = require './rails-rspec-finder'
RSpecAnalyzerCommand = require './rspec-analyzer-command'
RSpecLauncherCommand = require './rspec-launcher-command'
{Emitter} = require 'event-kit'

module.exports =
class PluginState
  constructor: (
  emitter = new Emitter,
  railsRSpecFinder = new RailsRSpecFinder,
  rspecAnalyzerCommand = new RSpecAnalyzerCommand,
  rspecLauncherCommand = new RSpecLauncherCommand) ->
    @emitter = emitter
    @railsRSpecFinder = railsRSpecFinder
    @rspecAnalyzerCommand = rspecAnalyzerCommand
    @rspecLauncherCommand = rspecLauncherCommand

  set: (editor) ->
    if ((!editor) || (!editor.buffer))
      @setNullState()
    else
      @currentFilePath = editor.buffer.file.path

      @currentFilePathExtension = @currentFilePath.split('.').pop();

      @currentCorrespondingFilePath = @railsRSpecFinder.toggleSpecFile(@currentFilePath)

      if !@currentCorrespondingFilePath?
        @setNullState()
        return

      if @railsRSpecFinder.isSpec(@currentFilePath)
        @specFileToAnalyze = @currentFilePath
      else
        @specFileToAnalyze = @currentCorrespondingFilePath

      @currentFileName = @specFileToAnalyze.split("/").pop();

      @specFileExists = @railsRSpecFinder.fileExists(@specFileToAnalyze)

      @specFileToAnalyzeWithoutProjectRoot = @railsRSpecFinder.getFileWithoutProjectRoot(@specFileToAnalyze) if @specFileToAnalyze?

      @analyze(@specFileToAnalyze) if (@specFileToAnalyze? and @specFileExists)

      @rspecAnalyzerCommand.onDataParsed (asTree) =>
        @asTree = asTree
        @emitter.emit 'onTreeBuilt', { asTree: asTree, summary: undefined, stdErrorData: undefined }

      @rspecLauncherCommand.onResultReceived (testsResults) =>
        @updateTreeWithTests(testsResults.result, testsResults.stdErrorData)

  setNullState: ->
    @currentFilePath = null
    @currentCorrespondingFilePath = null
    @specFileToAnalyze = null
    @specFileExists = null
    @specFileToAnalyzeWithoutProjectRoot = null

  analyze: (file) -><commit_msg>Perf: Analysis avoided when toggle<commit_after>RailsRSpecFinder = require './rails-rspec-finder'
RSpecAnalyzerCommand = require './rspec-analyzer-command'
RSpecLauncherCommand = require './rspec-launcher-command'
{Emitter} = require 'event-kit'

module.exports =
class PluginState
  constructor: (
  emitter = new Emitter,
  railsRSpecFinder = new RailsRSpecFinder,
  rspecAnalyzerCommand = new RSpecAnalyzerCommand,
  rspecLauncherCommand = new RSpecLauncherCommand) ->
    @emitter = emitter
    @railsRSpecFinder = railsRSpecFinder
    @rspecAnalyzerCommand = rspecAnalyzerCommand
    @rspecLauncherCommand = rspecLauncherCommand

  set: (editor) ->
    if ((!editor) || (!editor.buffer))
      @setNullState()
    else
      @currentFilePath = editor.buffer.file.path

      @currentFilePathExtension = @currentFilePath.split('.').pop();

      isNavigatingToCorresponding = @prevCorrespondingFilePath? and @prevCorrespondingFilePath == @currentFilePath

      @currentCorrespondingFilePath = @railsRSpecFinder.toggleSpecFile(@currentFilePath)

      @prevCorrespondingFilePath = @currentCorrespondingFilePath

      if !@currentCorrespondingFilePath?
        @setNullState()
        return

      if @railsRSpecFinder.isSpec(@currentFilePath)
        @specFileToAnalyze = @currentFilePath
      else
        @specFileToAnalyze = @currentCorrespondingFilePath

      @currentFileName = @specFileToAnalyze.split("/").pop();

      @specFileExists = @railsRSpecFinder.fileExists(@specFileToAnalyze)

      @specFileToAnalyzeWithoutProjectRoot = @railsRSpecFinder.getFileWithoutProjectRoot(@specFileToAnalyze) if @specFileToAnalyze?

      shouldAnalyze = @specFileToAnalyze? and @specFileExists and !isNavigatingToCorresponding

      @analyze(@specFileToAnalyze) if (shouldAnalyze)

      @rspecAnalyzerCommand.onDataParsed (asTree) =>
        @asTree = asTree
        @emitter.emit 'onTreeBuilt', { asTree: asTree, summary: undefined, stdErrorData: undefined }

      @rspecLauncherCommand.onResultReceived (testsResults) =>
        @updateTreeWithTests(testsResults.result, testsResults.stdErrorData)

  setNullState: ->
    @currentFilePath = null
    @currentCorrespondingFilePath = null
    @specFileToAnalyze = null
    @specFileExists = null
    @specFileToAnalyzeWithoutProjectRoot = null

  analyze: (file) ->
{'fraction_non_alphanumeric': 0.09494394875314573, 'fraction_numerical': 0.0, 'mean_word_length': 4.417596034696406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '1323291', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1252, 'n_words': 219}