def run(self, edit): try: code = self.view.substr(sublime.Region(0, self.view.size())) row, col = self.view.rowcol(self.view.sel()[0].begin()) racer = get_settings(self.view, 'racer_binary_path', 'racer') if racer == '': racer = 'racer' data = { 'vid': self.view.id(), 'filename': self.view.file_name(), 'settings': { 'racer_binary_path': racer, 'rust_src_path': get_settings(self.view, 'rust_src_path'), 'row': row, 'col': col, 'source': code }, 'method': 'goto', 'handler': 'racer' } Worker().execute( Callback(on_success=partial(JediUsages(self).process, False), on_failure=self._on_failure, on_timeout=self._on_timeout), **data) except: pass
def run_linter(view=None): """Run the linter for the given view """ if view is None: view = active_view() if not get_settings(view, 'anaconda_rust_linting', False): return if view.file_name() in anaconda_sublime.ANACONDA['DISABLED']: anaconda_sublime.erase_lint_marks(view) return rustc = get_settings(view, 'rustc_binary_path', 'rustc') if rustc == '': rustc = 'rustc' data = { 'vid': view.id(), 'code': '', 'settings': {'rustc_binary_path': rustc}, 'filename': view.file_name(), 'method': 'lint', 'handler': 'rust_linter' } callback = partial(anaconda_sublime.parse_results, **dict(code='rust')) Worker().execute(Callback(on_success=callback), **data)
def run(self, edit): if self.documentation is not None: return self.print_doc(edit) try: code = self.view.substr(sublime.Region(0, self.view.size())) row, col = self.view.rowcol(self.view.sel()[0].begin()) racer = get_settings(self.view, 'racer_binary_path', 'racer') if racer == '': racer = 'racer' data = { 'vid': self.view.id(), 'filename': self.view.file_name(), 'settings': { 'racer_binary_path': racer, 'rust_src_path': get_settings(self.view, 'rust_src_path'), 'row': row, 'col': col, 'source': code }, 'method': 'doc', 'handler': 'racer' } Worker().execute( Callback(on_success=self.prepare_data, on_failure=self._on_failure, on_timeout=self._on_timeout), **data) except Exception as error: print(error)
def run(self, edit): try: code = self.view.substr(sublime.Region(0, self.view.size())) row, col = self.view.rowcol(self.view.sel()[0].begin()) racer = get_settings(self.view, 'racer_binary_path', 'racer') if racer == '': racer = 'racer' data = { 'vid': self.view.id(), 'filename': self.view.file_name(), 'settings': { 'racer_binary_path': racer, 'rust_src_path': get_settings(self.view, 'rust_src_path'), 'row': row, 'col': col, 'source': code }, 'method': 'goto', 'handler': 'racer' } Worker().execute( Callback( on_success=self._on_success, on_failure=self._on_failure, on_timeout=self._on_timeout ), **data ) except: pass
def run(self, edit): if self.data is not None: self.update_buffer(edit) return try: messages = { 'start': 'Auto formatting file...', 'end': 'done!', 'fail': 'The auto formatting failed!', 'timeout': 'The auto formatiing timed out!' } self.pbar = ProgressBar(messages) self.pbar.start() self.view.set_read_only(True) rustfmt = get_settings(self.view, 'rustfmt_binary_path', 'rustfmt') if rustfmt == '': rustfmt = 'rustfmt' self.code = self.view.substr(sublime.Region(0, self.view.size())) # the JonServer deletes the temp file so we don't worry fd, path = tempfile.mkstemp(suffix=".rs", dir=file_directory()) try: with os.fdopen(fd, "wb") as tmp: tmp.write(self.code.encode('utf-8')) except Exception as e: self.clean_tmp_file(path) raise e config_path = get_settings(self.view, 'rust_rustfmt_config_path') if config_path is None or config_path == '': config_path = self._get_working_directory() data = { 'vid': self.view.id(), 'filename': path, 'settings': { 'rustfmt_binary_path': rustfmt, 'config_path': config_path }, 'method': 'format', 'handler': 'rustfmt' } timeout = get_settings(self.view, 'rust_rustfmt_timeout', 1) callback = Callback(timeout=timeout) callback.on(success=self.prepare_data) callback.on(error=self.on_failure) callback.on(timeout=partial(self.clean_tmp_file, path)) Worker().execute(callback, **data) except: logging.error(traceback.format_exc())
def run(self, edit): if self.data is not None: self.update_buffer(edit) return try: messages = { 'start': 'Auto formatting file...', 'end': 'done!', 'fail': 'The auto formatting failed!', 'timeout': 'The auto formatiing timed out!' } self.pbar = ProgressBar(messages) self.pbar.start() self.view.set_read_only(True) rustfmt = get_settings( self.view, 'rustfmt_binary_path', 'rustfmt' ) if rustfmt == '': rustfmt = 'rustfmt' self.code = self.view.substr( sublime.Region(0, self.view.size()) ) # the JonServer deletes the temp file so we don't worry fd, path = tempfile.mkstemp(suffix=".rs", dir=file_directory()) with os.fdopen(fd, "w", encoding="utf-8") as tmp: tmp.write(self.code) config_path = get_settings(self.view, 'rust_rustfmt_config_path', '') data = { 'vid': self.view.id(), 'filename': path, 'settings': { 'rustfmt_binary_path': rustfmt, 'config_path': config_path }, 'method': 'format', 'handler': 'rustfmt' } timeout = get_settings(self.view, 'rust_rustfmt_timeout', 1) callback = Callback(timeout=timeout) callback.on(success=self.prepare_data) callback.on(error=self.on_failure) callback.on(timeout=partial(self.clean_tmp_file, path)) Worker().execute(callback, **data) except: logging.error(traceback.format_exc())
def on_query_completions(self, view, prefix, locations): """Sublime Text autocompletion event handler """ if not is_code(view, lang='rust'): return if self.ready_from_defer is True: completion_flags = 0 if ags(view, 'suppress_word_completions', False): completion_flags = sublime.INHIBIT_WORD_COMPLETIONS if ags(view, 'suppress_explicit_completions', False): completion_flags = sublime.INHIBIT_EXPLICIT_COMPLETIONS cpl = self.completions self.completions = [] self.ready_from_defer = False return (cpl, completion_flags) code = view.substr(sublime.Region(0, view.size())) row, col = view.rowcol(locations[0]) racer = get_settings(view, 'racer_binary_path', 'racer') if racer == '': racer = 'racer' data = { 'vid': view.id(), 'filename': view.file_name(), 'settings': { 'racer_binary_path': racer, 'rust_src_path': get_settings(view, 'rust_src_path'), 'row': row, 'col': col, 'source': code, }, 'method': 'autocomplete', 'handler': 'racer' } Worker().execute( Callback( on_success=self._complete, on_failure=self._on_failure, on_timeout=self._on_timeout ), **data )
def on_pre_save(self, view): """Called just before the file is goign to be saved """ if is_code(view, lang='rust', ignore_comments=True): if get_settings(view, 'rust_format_on_save', False): view.run_command('anaconda_rust_fmt')
def on_modified(self, view): """Rustc can only work in files not in buffers """ if check_linting(view, 0, code=self.lang.lower()): # remove prvious linting marks if configured to do so if not get_settings(view, 'anaconda_linter_persistent', False): linting.erase_lint_marks(view) else: self._erase_marks_if_no_linting(view)
def update_buffer(self, edit): """Update and reload the buffer """ view = get_window_view(self.data['vid']) if self.sanitize(self.code) != self.sanitize(self.data.get('output')): region = sublime.Region(0, view.size()) view.replace(edit, region, self.data.get('output')) if get_settings(view, 'rust_format_on_save'): sublime.set_timeout(lambda: view.run_command('save'), 0) self.data = None self.code = None
def check_racer_version(): """Check the installed racer version """ global RACER_VERSION view = active_view() racer = get_settings(view, 'racer_binary_path', 'racer') if racer == '': racer = 'racer' env = os.environ.copy() rust_src_path = get_settings(view, 'rust_src_path') if rust_src_path is None or rust_src_path == '': rust_src_path = os.environ.get('RUST_SRC_PATH', '') env['RUST_SRC_PATH'] = rust_src_path try: data = subprocess.check_output([racer, '-V'], env=env) RACER_VERSION = tuple(int(i) for i in data.split()[1].split(b'.')) except Exception as error: print('Can\'t determine racer version: {}'.format(error))
def update_buffer(self, edit): """Update and reload the buffer """ view = get_window_view(self.data['vid']) output = self.sanitize(self.data.get('output')) if output and self.sanitize(self.code) != output: region = sublime.Region(0, view.size()) view.replace(edit, region, self.data.get('output')) if get_settings(view, 'rust_format_on_save'): sublime.set_timeout(lambda: view.run_command('save'), 0) self.data = None self.code = None