<commit_before>import os.path
from os import popen
import sublime, sublime_plugin, sys, re

class BeautifyRubyCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    self.settings = sublime.load_settings('BeautifyRuby.sublime-settings')
    self.filename = self.view.window().active_view().file_name()
    fname         = os.path.basename(self.filename)

    if self.is_ruby_file(fname):
      self.get_selection_position()
      self.save_document_if_dirty()

      beautified   = os.popen(self.cmd()).read()

      self.update_view(beautified.decode('utf8'))
      self.view.run_command('save')
      self.reset_selection_position()
    else:
      sublime.error_message("This is not a Ruby file.")

  def ruby_script(self):
    return os.path.join(sublime.packages_path(), 'BeautifyRuby', 'ruby', 'beautifier.rb')

  def update_view(self, contents):
    active_view = self.view.window().active_view()
    body = active_view.substr(sublime.Region(0, active_view.size()))
    edit = active_view.begin_edit()
    active_view.erase(edit, sublime.Region(0, active_view.size()))
    active_view.insert(edit, 0, contents)
    active_view.end_edit(edit)

  def reset_selection_position(self):
    self.view.sel().clear()
    self.view.sel().add(self.region)
    self.view.show_at_center(self.region)

  def get_selection_position(self):
    sel         = self.view.sel()[0].begin()
    pos         = self.view.rowcol(sel)
    target      = self.view.text_point(pos[0], 0)
    self.region = sublime.Region(target)


  def save_document_if_dirty(self):
    if self.view.is_dirty():
      self.view.run_command('save')

  def tab_or_space_setting(self):
    tab_or_space = self.settings.get('tab_or_space')
    if tab_or_space == '':
      return 'space'
    else:
      return tab_or_space

  def get_ruby_interpreter(self):
    return self.settings.get('ruby')

  def cmd(self):
    ruby_script  = self.ruby_script()
    tab_or_space = self.tab_or_space_setting()
    ruby_interpreter = self.get_ruby_interpreter()
    ruby_interpreter = ruby_interpreter or "/usr/bin/env ruby"
    command = ruby_interpreter + " '" + ruby_script + "' '" + tab_or_space + "'" + " '" + unicode(self.filename) + "'"
    return command

  def is_ruby_file(self, fname):
    patterns = re.compile(r'\.rb|\.rake')
    if patterns.search(fname):
      return True
    else:
      return False<commit_msg>Command modified to use new formatter<commit_after>import os.path
from os import popen
import sublime, sublime_plugin, sys, re

class BeautifyRubyCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    self.settings = sublime.load_settings('BeautifyRuby.sublime-settings')
    self.filename = self.view.window().active_view().file_name()
    fname         = os.path.basename(self.filename)

    if self.is_ruby_file(fname):
      self.get_selection_position()
      self.save_document_if_dirty()

      sublime.error_message(self.cmd())

      beautified   = os.popen(self.cmd()).read()

      self.update_view(beautified.decode('utf8'))
      self.view.run_command('save')
      self.reset_selection_position()
    else:
      sublime.error_message("This is not a Ruby file.")

  def update_view(self, contents):
    active_view = self.view.window().active_view()
    body = active_view.substr(sublime.Region(0, active_view.size()))
    edit = active_view.begin_edit()
    active_view.erase(edit, sublime.Region(0, active_view.size()))
    active_view.insert(edit, 0, contents)
    active_view.end_edit(edit)

  def reset_selection_position(self):
    self.view.sel().clear()
    self.view.sel().add(self.region)
    self.view.show_at_center(self.region)

  def get_selection_position(self):
    sel         = self.view.sel()[0].begin()
    pos         = self.view.rowcol(sel)
    target      = self.view.text_point(pos[0], 0)
    self.region = sublime.Region(target)

  def save_document_if_dirty(self):
    if self.view.is_dirty():
      self.view.run_command('save')

  def tab_or_space_setting(self):
    tab_or_space = self.settings.get('tab_or_space')
    if tab_or_space == '':
      return 'space'
    else:
      return tab_or_space

  def cmd(self):
    ruby_script  = os.path.join(sublime.packages_path(), 'BeautifyRubyNew', 'lib', 'rbeautify.rb')
    tab_or_space = self.tab_or_space_setting()
    ruby_interpreter = self.settings.get('ruby') or "/usr/bin/env ruby"
    command = ruby_interpreter + " '" + ruby_script + "' '" + tab_or_space + "'" + " '" + unicode(self.filename) + "'"
    return command

  def is_ruby_file(self, fname):
    patterns = re.compile(r'\.rb|\.rake')
    if patterns.search(fname):
      return True
    else:
      return False
{'fraction_non_alphanumeric': 0.134091396695988, 'fraction_numerical': 0.003003647285990131, 'mean_word_length': 3.570588235294118, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1598182', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1581, 'n_words': 255}