def run(self, edit): super(RubocopAutoCorrectCommand, self).run(edit) cancel_op = self.user_wants_to_cancel() if cancel_op: return view = self.view path = view.file_name() quoted_file_path = FileTools.quote(path) if view.is_read_only(): sublime.message_dialog('RuboCop: Unable to run auto correction on a read only buffer.') return # Inform user about unsaved contents of current buffer if view.is_dirty(): warn_msg = 'RuboCop: The curent buffer is modified. Save the file and continue?' cancel_op = not sublime.ok_cancel_dialog(warn_msg) if cancel_op: return else: view.run_command('save') RubocopEventListener.instance().clear_marks(view) quoted_file_path = FileTools.quote(path) # Run rubocop with auto-correction self.runner.run(quoted_file_path, '-a') sublime.status_message('RuboCop: Auto correction done.')
def run(self, edit): super(RubocopAutoCorrectCommand, self).run(edit) cancel_op = self.user_wants_to_cancel() if cancel_op: return view = self.view path = view.file_name() quoted_file_path = FileTools.quote(path) if view.is_read_only(): sublime.message_dialog( 'RuboCop: Unable to run auto correction on a read only buffer.' ) return # Inform user about unsaved contents of current buffer if view.is_dirty(): warn_msg = 'RuboCop: The curent buffer is modified. Save the file and continue?' cancel_op = not sublime.ok_cancel_dialog(warn_msg) if cancel_op: return else: view.run_command('save') RubocopEventListener.instance().clear_marks(view) quoted_file_path = FileTools.quote(path) # Run rubocop with auto-correction self.runner.run([quoted_file_path], ['-a']) sublime.status_message('RuboCop: Auto correction done.')
def run(self, edit): super(RubocopAutoCorrectCommand, self).run(edit) cancel_op = self.warning_msg() if cancel_op: return view = self.view path = view.file_name() quoted_file_path = FileTools.quote(path) if view.is_read_only(): sublime.message_dialog( 'RuboCop: Unable to run auto correction on a read only buffer.' ) return # Inform user about unsaved contents of current buffer if view.is_dirty(): warn_msg = 'RuboCop: The curent buffer is modified. Save the file and continue?' cancel_op = not sublime.ok_cancel_dialog(warn_msg) if cancel_op: return else: view.run_command('save') RubocopEventListener.instance().clear_marks(view) # Copy the current file to a temp file content = view.substr(sublime.Region(0, view.size())) f = tempfile.NamedTemporaryFile() try: self.write_to_file(f, content, view) f.flush() # Create path for possible config file in the source directory quoted_file_path = FileTools.quote(path) config_opt = '-c ' + os.path.dirname( quoted_file_path) + '/.rubocop.yml' print(config_opt) # Run rubocop with auto-correction on temp file self.runner.run(f.name, '-a ' + config_opt) # Read contents of file f.seek(0) content = self.read_from_file(f, view) # Overwrite buffer contents rgn = sublime.Region(0, view.size()) view.replace(edit, rgn, content) finally: # TempFile will be deleted here f.close() sublime.status_message('RuboCop: Auto correction done.')
def run(self, edit): super(RubocopAutoCorrectCommand, self).run(edit) cancel_op = self.warning_msg() if cancel_op: return view = self.view path = view.file_name() quoted_file_path = FileTools.quote(path) if view.is_read_only(): sublime.message_dialog('RuboCop: Unable to run auto correction on a read only buffer.') return # Inform user about unsaved contents of current buffer if view.is_dirty(): warn_msg = 'RuboCop: The curent buffer is modified. Save the file and continue?' cancel_op = not sublime.ok_cancel_dialog(warn_msg) if cancel_op: return else: view.run_command('save') RubocopEventListener.instance().clear_marks(view) # Copy the current file to a temp file content = view.substr(sublime.Region(0, view.size())) f = tempfile.NamedTemporaryFile() try: self.write_to_file(f, content, view) f.flush() # Create path for possible config file in the source directory quoted_file_path = FileTools.quote(path) config_opt = '-c ' + os.path.dirname(quoted_file_path) + '/.rubocop.yml' print(config_opt) # Run rubocop with auto-correction on temp file self.runner.run(f.name, '-a ' + config_opt) # Read contents of file f.seek(0) content = self.read_from_file(f, view) # Overwrite buffer contents rgn = sublime.Region(0, view.size()) view.replace(edit, rgn, content) finally: # TempFile will be deleted here f.close() sublime.status_message('RuboCop: Auto correction done.')
def run_rubocop(self, view): s = sublime.load_settings(SETTINGS_FILE) use_rvm = view.settings().get('check_for_rvm', s.get('check_for_rvm')) use_rbenv = view.settings().get('check_for_rbenv', s.get('check_for_rbenv')) cmd = view.settings().get('rubocop_command', s.get('rubocop_command')) rvm_path = view.settings().get('rvm_auto_ruby_path', s.get('rvm_auto_ruby_path')) rbenv_path = view.settings().get('rbenv_path', s.get('rbenv_path')) cfg_file = view.settings().get('rubocop_config_file', s.get('rubocop_config_file')) chdir = view.settings().get('rubocop_chdir', s.get('rubocop_chdir')) if cfg_file: cfg_file = FileTools.quote(cfg_file) runner = RubocopRunner( { 'use_rbenv': use_rbenv, 'use_rvm': use_rvm, 'custom_rubocop_cmd': cmd, 'rvm_auto_ruby_path': rvm_path, 'rbenv_path': rbenv_path, 'on_windows': sublime.platform() == 'windows', 'rubocop_config_file': cfg_file, 'chdir': chdir, 'is_st2': sublime.version() < '3000' } ) output = runner.run([view.file_name()], ['--format', 'emacs']).splitlines() return output
def run_rubocop(self, view): s = sublime.load_settings(SETTINGS_FILE) use_rvm = view.settings().get('check_for_rvm', s.get('check_for_rvm')) use_rbenv = view.settings().get('check_for_rbenv', s.get('check_for_rbenv')) cmd = view.settings().get('rubocop_command', s.get('rubocop_command')) rvm_path = view.settings().get('rvm_auto_ruby_path', s.get('rvm_auto_ruby_path')) rbenv_path = view.settings().get('rbenv_path', s.get('rbenv_path')) cfg_file = view.settings().get('rubocop_config_file', s.get('rubocop_config_file')) chdir = view.settings().get('rubocop_chdir', s.get('rubocop_chdir')) if cfg_file: cfg_file = FileTools.quote(cfg_file) runner = RubocopRunner({ 'use_rbenv': use_rbenv, 'use_rvm': use_rvm, 'custom_rubocop_cmd': cmd, 'rvm_auto_ruby_path': rvm_path, 'rbenv_path': rbenv_path, 'on_windows': sublime.platform() == 'windows', 'rubocop_config_file': cfg_file, 'chdir': chdir, 'is_st2': sublime.version() < '3000' }) output = runner.run([view.file_name()], ['--format', 'emacs']).splitlines() return output
def open_ruby_files(self): files = [] views = self.view.window().views() for vw in views: if FileTools.is_ruby_file(vw): files.append(vw.file_name()) return files
def open_ruby_files(self): files = [] views = self.view.window().views() for vw in views: file_path = vw.file_name() if FileTools.is_ruby_file(file_path): files.append(file_path) return files
def do_post_save_check(self, view): self.clear_marks(view) if not sublime.load_settings(SETTINGS_FILE).get('check_on_save'): return if not FileTools.is_ruby_file(view.file_name()): return results = self.run_rubocop(view.file_name()) self.set_marks_by_results(view, results)
def run_rubocop_on(self, path, file_list=False): if not path: return if not file_list: # Single item to check. quoted_file_path = FileTools.quote(path) working_dir = os.path.dirname(path) else: # Multiple files to check. working_dir = '.' quoted_file_path = '' for file in path: quoted_file_path += FileTools.quote(file) + ' ' rubocop_cmd = self.runner.command_string( quoted_file_path, self.used_options() ) self.run_shell_command(rubocop_cmd, working_dir)
def run_rubocop_on(self, path, file_list=False): if not path: return if not file_list: # Single item to check. quoted_file_path = FileTools.quote(path) working_dir = os.path.dirname(quoted_file_path) else: # Multiple files to check. working_dir = '.' quoted_file_path = '' for file in path: quoted_file_path += FileTools.quote(file) + ' ' cop_command = self.command_with_options() rubocop_cmd = cop_command.replace('{path}', quoted_file_path) self.run_shell_command(rubocop_cmd, working_dir)
def run_rubocop_on(self, pathlist): if len(pathlist) == 0: return working_dir = self.current_project_folder() quoted_paths = [] for path in pathlist: quoted_paths.append(FileTools.quote(path)) rubocop_cmd = self.runner.command_string(quoted_paths, self.used_options()) self.run_shell_command(rubocop_cmd, working_dir)
def do_in_file_check(self, view): if not FileTools.is_ruby_file(view): return settings = sublime.load_settings(SETTINGS_FILE) mark_issues_in_view = settings.get('mark_issues_in_view') auto_correct_on_save = settings.get('auto_correct_on_save') cmd_args = ['--format', 'emacs'] if auto_correct_on_save: cmd_args.append('-a') if mark_issues_in_view: self.clear_marks(view) if mark_issues_in_view or auto_correct_on_save: results = self.run_rubocop(view, cmd_args) if mark_issues_in_view: self.set_marks_by_results(view, results)
def load_config(self): s = sublime.load_settings(SETTINGS_FILE) cfg_file = s.get('rubocop_config_file') if cfg_file: cfg_file = FileTools.quote(cfg_file) self.runner = RubocopRunner( { 'use_rbenv': s.get('check_for_rbenv'), 'use_rvm': s.get('check_for_rvm'), 'custom_rubocop_cmd': s.get('rubocop_command'), 'rvm_auto_ruby_path': s.get('rvm_auto_ruby_path'), 'rbenv_path': s.get('rbenv_path'), 'on_windows': (sublime.platform() == 'windows'), 'rubocop_config_file': cfg_file } )
def load_config(self): s = sublime.load_settings(SETTINGS_FILE) cfg_file = s.get("rubocop_config_file") if cfg_file: cfg_file = FileTools.quote(cfg_file) self.runner = RubocopRunner( { "use_rbenv": s.get("check_for_rbenv"), "use_rvm": s.get("check_for_rvm"), "custom_rubocop_cmd": s.get("rubocop_command"), "rvm_auto_ruby_path": s.get("rvm_auto_ruby_path"), "rbenv_path": s.get("rbenv_path"), "on_windows": self.on_windows(), "rubocop_config_file": cfg_file, "is_st2": self.is_st2(), } )
def run(self, edit): super(RubocopOpenAllOffensiveFilesCommand, self).run(edit) folders = self.view.window().folders() if len(folders) <= 0: sublime.status_message('RuboCop: No project folder available.') return folder = FileTools.quote(folders[0]) # Run rubocop with file formatter file_list = self.runner.run([folder], ['--format files']).splitlines() for path in file_list: self.view.window().open_file(path.decode(locale.getpreferredencoding())) sublime.status_message('RuboCop: Opened ' + str(len(file_list)) + ' files.')
def load_config(self): s = sublime.load_settings(SETTINGS_FILE) cfg_file = s.get('rubocop_config_file') if cfg_file: cfg_file = FileTools.quote(cfg_file) self.runner = RubocopRunner( { 'use_rbenv': s.get('check_for_rbenv'), 'use_rvm': s.get('check_for_rvm'), 'custom_rubocop_cmd': s.get('rubocop_command'), 'rvm_auto_ruby_path': s.get('rvm_auto_ruby_path'), 'rbenv_path': s.get('rbenv_path'), 'on_windows': self.on_windows(), 'rubocop_config_file': cfg_file, 'is_st2': self.is_st2() } )
def run_rubocop(self, path): s = sublime.load_settings(SETTINGS_FILE) use_rvm = s.get('check_for_rvm') use_rbenv = s.get('check_for_rbenv') cmd = s.get('rubocop_command') rvm_path = s.get('rvm_auto_ruby_path') rbenv_path = s.get('rbenv_path') cfg_file = s.get('rubocop_config_file') if cfg_file: cfg_file = FileTools.quote(cfg_file) runner = RubocopRunner( { 'use_rbenv': use_rbenv, 'use_rvm': use_rvm, 'custom_rubocop_cmd': cmd, 'rvm_auto_ruby_path': rvm_path, 'rbenv_path': rbenv_path, 'on_windows': sublime.platform() == 'windows', 'rubocop_config_file': cfg_file } ) output = runner.run(path, '--format emacs').splitlines() return output
def do_in_file_check(self, view): if not FileTools.is_ruby_file(view.file_name()): return mark = sublime.load_settings(SETTINGS_FILE).get('mark_issues_in_view') self.mark_issues(view, mark)