def run(self, edit): class Worker(threading.Thread): def __init__(self, view, context, changes): self.view = view self.context = context self.changes = changes self.handler = TaskHandle(name='organizer_imports_handler') self.handler.add_observer(self.finish) threading.Thread.__init__(self) def run(self): self.context.project.do(self.changes, task_handle=self.handler) def finish(self): percent_done = self.handler.current_jobset().get_percent_done() if percent_done == 100: sublime.set_timeout(self.view.run_command('revert'), 10) with ropemate.context_for(self.view) as context: self.view.run_command("save") organizer = ImportOrganizer(context.project) changes = organizer.organize_imports(context.resource) if changes is not None: Worker(self.view, context, changes).start()
def on_query_completions(self, view, prefix, locations): if not view.match_selector(locations[0], "source.python"): return [] with ropemate.context_for(view) as context: loc = locations[0] try: raw_proposals = codeassist.code_assist( context.project, context.input, loc, context.resource, maxfixes=3, later_locals=False) except ModuleSyntaxError: raw_proposals = [] if len(raw_proposals) <= 0 and self.use_simple_completion: # try the simple hackish completion line = view.substr(view.line(loc)) identifier = line[:view.rowcol(loc)[1]].strip(' .') if ' ' in identifier: identifier = identifier.split(' ')[-1] raw_proposals = self.simple_module_completion(view, identifier) proposals = codeassist.sorted_proposals(raw_proposals) proposals = [ (proposal_string(p), p.name) for p in proposals if p.name != 'self=' ] completion_flags = 0 if self.suppress_word_completions: completion_flags = sublime.INHIBIT_WORD_COMPLETIONS if self.suppress_explicit_completions: completion_flags |= sublime.INHIBIT_EXPLICIT_COMPLETIONS return (proposals, completion_flags)
def process_args(self, input_str): if input_str in self.defaults: sublime.status_message( 'You have to provide valid arguments' ' for this restructure. Cancelling...') return try: self.args.append(ast.literal_eval(input_str)) except: sublime.error_message( 'Malformed string detected in Args.\n\n' 'The Args value must be a Python dictionary') return with ropemate.context_for(self.view) as context: self.refactoring = Restructure( context.project, self.args[0], self.args[1], self.args[2]) self.changes = self.refactoring.get_changes() try: context.project.do(self.changes) # sublime.error_message(self.changes.get_description()) print "RESTRUCTURING CHANGES PERFORMED" print "-------------------------------" print self.changes.get_description() print "-------------------------------" except ModuleNotFoundError, e: sublime.error_message(e)
def run(self, edit): ctx = ropemate.context_for(self.view) ctx.building = True # we have to enter on main, but build on worker thread ctx.__enter__() thread = PythonRegenerateCache.RegenerateCacheThread(ctx) thread.start()
def on_select_global(self, choice): if choice is not -1: name, module = self.candidates[choice] with ropemate.context_for(self.view) as context: # check whether adding an import is necessary, and where all_lines = self.view.lines( sublime.Region(0, self.view.size()) ) line_no = context.importer.find_insertion_line(context.input) insert_import_str = "from %s import %s\n" % (module, name) existing_imports_str = self.view.substr( sublime.Region(all_lines[0].a, all_lines[line_no - 1].b)) do_insert_import = \ insert_import_str not in existing_imports_str insert_import_point = all_lines[line_no].a # the word prefix that is replaced original_word = self.view.word(self.offset) # replace the prefix, add the import if necessary e = self.view.begin_edit() self.view.replace(e, original_word, name) if do_insert_import: self.view.insert( e, insert_import_point, insert_import_str) self.view.end_edit(e)
def run(self, edit): view = self.view row, col = view.rowcol(view.sel()[0].a) offset = view.text_point(row, col) if view.substr(offset) in [u'(', u')']: offset = view.text_point(row, col - 1) with ropemate.context_for(view) as context: try: doc = codeassist.get_doc(context.project, context.input, offset, context.resource, maxfixes=3) if not doc: raise rope.base.exceptions.BadIdentifierError self.output(doc) except rope.base.exceptions.BadIdentifierError: word = self.view.substr(self.view.word(offset)) self.view.set_status("rope_documentation_error", "No documentation found for %s" % word) def clear_status_callback(): self.view.erase_status("rope_documentation_error") sublime.set_timeout(clear_status_callback, 5000)
def process_args(self, input_str): if input_str in self.defaults: sublime.status_message('You have to provide valid arguments' ' for this restructure. Cancelling...') return try: self.args.append(ast.literal_eval(input_str)) except: sublime.error_message('Malformed string detected in Args.\n\n' 'The Args value must be a Python dictionary') return with ropemate.context_for(self.view) as context: self.refactoring = Restructure(context.project, self.args[0], self.args[1], self.args[2]) self.changes = self.refactoring.get_changes() try: context.project.do(self.changes) # sublime.error_message(self.changes.get_description()) print "RESTRUCTURING CHANGES PERFORMED" print "-------------------------------" print self.changes.get_description() print "-------------------------------" except ModuleNotFoundError, e: sublime.error_message(e)
def run(self, edit): ctx = ropemate.context_for(self.view) ctx.building = True # we have to enter on main, but build on worker thread ctx.__enter__() ctx.importer.class_methods = get_setting('include_classmethods_in_globals', False) thread = PythonRegenerateCache.RegenerateCacheThread(ctx) thread.start()
def on_select_location(self, choice): loc = self.locs[choice] with ropemate.context_for(self.view) as context: path, line = loc.split(":") if not os.path.isabs(path): path = context.project._get_resource_path(path) self.view.window().open_file("%s:%s" % (path, line), sublime.ENCODED_POSITION)
def on_select_location(self, choice): if choice != -1: loc = self.locs[choice] with ropemate.context_for(self.view) as context: path, line = loc.split(":") if not os.path.isabs(path): path = context.project._get_resource_path(path) self.view.window().open_file("%s:%s" % (path, line), sublime.ENCODED_POSITION)
def input_callback(self, input_str): with ropemate.context_for(self.view) as context: if input_str is None: return changes = self.get_changes(input_str) self.handle = TaskHandle(name="refactoring_handle") self.handle.add_observer(self.refactoring_done) context.project.do(changes, task_handle=self.handle)
def run(self, edit): ctx = ropemate.context_for(self.view) ctx.building = True # we have to enter on main, but build on worker thread ctx.__enter__() ctx.importer.class_methods = get_setting( 'include_classmethods_in_globals', False) thread = PythonRegenerateCache.RegenerateCacheThread(ctx) thread.start()
def run(self, edit): view = self.view row, col = view.rowcol(view.sel()[0].a) self.offset = view.text_point(row, col) with ropemate.context_for(view) as context: word = self.view.substr(self.view.word(self.offset)) self.candidates = list(context.importer.import_assist(word)) self.view.window().show_quick_panel( map(lambda c: [c[0], c[1]], self.candidates), self.on_select_global, sublime.MONOSPACE_FONT)
def run(self, edit, block=False): self.view.run_command("save") self.original_loc = self.view.rowcol(self.view.sel()[0].a) with ropemate.context_for(self.view) as context: self.sel = self.view.sel()[0] self.refactoring = self.create_refactoring_operation( context.project, context.resource, self.sel.a, self.sel.b ) self.view.window().show_input_panel(self.message, self.default_input(), self.input_callback, None, None)
def run(self, edit): modules = get_setting("autoimport_modules", []) if modules: sublime.status_message("Generating modules cache {0}...".format(" ".join(modules))) ctx = ropemate.context_for(self.view) ctx.building = True ctx.__enter__() ctx.importer.class_methods = get_setting("include_classmethods_in_globals", False) thread = PythonGenerateModulesCache.GenerateModulesCache(ctx, modules) thread.start() else: sublime.error_message("Missing modules in configuration file")
def run(self, edit, block=False): self.view.run_command("save") self.original_loc = self.view.rowcol(self.view.sel()[0].a) with ropemate.context_for(self.view) as context: self.sel = self.view.sel()[0] self.refactoring = self.create_refactoring_operation( context.project, context.resource, self.sel.a, self.sel.b) self.view.window().show_input_panel(self.message, self.default_input(), self.input_callback, None, None)
def run(self, edit, block=False): with ropemate.context_for(self.view) as context: offset = self.view.sel()[0].a found_resource, line = None, None try: found_resource, line = codeassist.get_definition_location( context.project, context.input, offset, context.resource) except rope.base.exceptions.BadIdentifierError, e: # fail silently -> the user selected empty space etc pass except Exception, e: print e
def on_query_completions(self, view, prefix, locations): if ( not view.match_selector(locations[0], 'source.python') or not (self.complete_as_you_type) or SublimeRopeListener.user_requested ): return [] SublimeRopeListener.user_requested = False with ropemate.context_for(view) as context: loc = locations[0] try: raw_proposals = codeassist.code_assist( context.project, context.input, loc, context.resource, maxfixes=3, later_locals=False, case_sensitive=self.case_sensitive_completion ) except ModuleSyntaxError: raw_proposals = [] if not raw_proposals and self.use_simple_completion: # try the simple hackish completion line = view.substr(view.line(loc)) identifier = line[:view.rowcol(loc)[1]].strip(' .') if ' ' in identifier: identifier = identifier.split(' ')[-1] raw_proposals = self.simple_module_completion(view, identifier) # do not use rope's own sorting for large results, it is very slow! # simple sort-by-name is good enough if len(raw_proposals) <= 20: sorted_proposals = codeassist.sorted_proposals(raw_proposals) else: sorted_proposals = sorted(raw_proposals, key=lambda p: p.name) proposals = [ (self.proposal_string(p), self.insert_string(p)) for p in sorted_proposals if p.name != 'self=' ] completion_flags = 0 if self.suppress_word_completions: completion_flags = sublime.INHIBIT_WORD_COMPLETIONS if self.suppress_explicit_completions: completion_flags |= sublime.INHIBIT_EXPLICIT_COMPLETIONS return (proposals, completion_flags)
def run(self, edit): modules = get_setting('autoimport_modules', []) if modules: sublime.status_message('Generating modules cache {0}...'.format( ' '.join(modules))) ctx = ropemate.context_for(self.view) ctx.building = True ctx.__enter__() thread = PythonGenerateModulesCache.GenerateModulesCache( ctx, modules) thread.start() else: sublime.error_message("Missing modules in configuration file")
def run(self, edit): settings = sublime.load_settings("SublimeRope.sublime-settings") modules = settings.get('autoimport_modules', []) if modules: sublime.status_message('Generating modules cache ...') ctx = ropemate.context_for(self.view) ctx.building = True ctx.__enter__() thread = PythonGenerateModulesCache.GenerateModulesCache(ctx, modules) thread.start() else: sublime.error_message("Missing modules in configuration file")
def run(self, edit): modules = get_setting('autoimport_modules', []) if modules: sublime.status_message('Generating modules cache {0}...'.format( ' '.join(modules) )) ctx = ropemate.context_for(self.view) ctx.building = True ctx.__enter__() thread = PythonGenerateModulesCache.GenerateModulesCache( ctx, modules) thread.start() else: sublime.error_message("Missing modules in configuration file")
def __init__(self, view, word=None): self.view = view if word is not None: self.word = word else: row, col = self.view.rowcol(view.sel()[0].a) offset = self.view.text_point(row, col) self.word = self.view.substr(self.view.word(offset)) threading.Thread.__init__(self) self.candidates = None self.ctx = ropemate.context_for(self.view) self.ctx.__enter__()
def on_query_completions(self, view, prefix, locations): if (not view.match_selector(locations[0], 'source.python') or not (self.complete_as_you_type) or SublimeRopeListener.user_requested): return [] SublimeRopeListener.user_requested = False with ropemate.context_for(view) as context: loc = locations[0] try: raw_proposals = codeassist.code_assist( context.project, context.input, loc, context.resource, maxfixes=3, later_locals=False, case_sensitive=self.case_sensitive_completion) except ModuleSyntaxError: raw_proposals = [] if not raw_proposals and self.use_simple_completion: # try the simple hackish completion line = view.substr(view.line(loc)) identifier = line[:view.rowcol(loc)[1]].strip(' .') if ' ' in identifier: identifier = identifier.split(' ')[-1] raw_proposals = self.simple_module_completion(view, identifier) # do not use rope's own sorting for large results, it is very slow! # simple sort-by-name is good enough if len(raw_proposals) <= 20: sorted_proposals = codeassist.sorted_proposals(raw_proposals) else: sorted_proposals = sorted(raw_proposals, key=lambda p: p.name) proposals = [(self.proposal_string(p), self.insert_string(p)) for p in sorted_proposals if p.name != 'self='] completion_flags = 0 if self.suppress_word_completions: completion_flags = sublime.INHIBIT_WORD_COMPLETIONS if self.suppress_explicit_completions: completion_flags |= sublime.INHIBIT_EXPLICIT_COMPLETIONS return (proposals, completion_flags)
def on_select_global(self, choice): def loc_to_str(loc): resource, line = loc return "%s:%s" % (resource.path, line) if choice != -1: selected_global = self.names[choice] with ropemate.context_for(self.view) as context: self.locs = context.importer.get_name_locations(selected_global) self.locs = [loc_to_str(l) for l in self.locs] if not self.locs: return if len(self.locs) == 1: self.on_select_location(0) else: self.view.window().show_quick_panel(self.locs, self.on_select_location, sublime.MONOSPACE_FONT)
def _on_select_global(self, choice): if choice is not -1: name, module = self.candidates[choice] with ropemate.context_for(self.view) as context: # check whether adding an import is necessary, and where all_lines = self.view.lines(sublime.Region(0, self.view.size())) line_no = context.importer.find_insertion_line(context.input) - 1 insert_import_str = "from %s import %s\n" % (module, name) existing_imports_str = self.view.substr(sublime.Region(all_lines[0].a, all_lines[line_no - 1].b)) if insert_import_str.rstrip() in existing_imports_str: return insert_import_point = all_lines[line_no].a e = self.view.begin_edit() self.view.insert(e, insert_import_point, insert_import_str) self.view.end_edit(e)
def _on_select_global(self, choice): if choice is not -1: name, module = self.candidates[choice] with ropemate.context_for(self.view) as context: # check whether adding an import is necessary, and where all_lines = self.view.lines(sublime.Region( 0, self.view.size())) line_no = context.importer.find_insertion_line(context.input) insert_import_str = "from %s import %s\n" % (module, name) existing_imports_str = self.view.substr( sublime.Region(all_lines[0].a, all_lines[line_no - 1].b)) if insert_import_str.rstrip() in existing_imports_str: return insert_import_point = all_lines[line_no].a e = self.view.begin_edit() self.view.insert(e, insert_import_point, insert_import_str) self.view.end_edit(e)
def run(self, edit): view = self.view row, col = view.rowcol(view.sel()[0].a) offset = view.text_point(row, col) if view.substr(offset) in [u"(", u")"]: offset = view.text_point(row, col - 1) with ropemate.context_for(view) as context: try: doc = codeassist.get_doc(context.project, context.input, offset, context.resource, maxfixes=3) if not doc: raise rope.base.exceptions.BadIdentifierError self.output(doc) except rope.base.exceptions.BadIdentifierError: word = self.view.substr(self.view.word(offset)) self.view.set_status("rope_documentation_error", "No documentation found for %s" % word) def clear_status_callback(): self.view.erase_status("rope_documentation_error") sublime.set_timeout(clear_status_callback, 5000)
def on_select_global(self, choice): def loc_to_str(loc): resource, line = loc return "%s:%s" % (resource.path, line) if choice != -1: selected_global = self.names[choice] with ropemate.context_for(self.view) as context: self.locs = context.importer.get_name_locations( selected_global) self.locs = [loc_to_str(l) for l in self.locs] if not self.locs: return if len(self.locs) == 1: self.on_select_location(0) else: self.view.window().show_quick_panel( self.locs, self.on_select_location, sublime.MONOSPACE_FONT)
def process_args(self, input_str): if input_str in self.defaults: sublime.status_message('You will provide valid arguments for this' ' renstructure. Cancelling...') return try: self.args.append(ast.literal_eval(input_str)) except: sublime.error_message("Malformed string detected in Args.\n\n" "The Args value must be a Python dictionary") return with ropemate.context_for(self.view) as context: self.refactoring = Restructure( context.project, self.args[0], self.args[1], self.args[2]) self.changes = self.refactoring.get_changes() try: context.project.do(self.changes) sublime.error_message(self.changes.get_description()) except ModuleNotFoundError, e: sublime.error_message(e)
def run(self, edit): with ropemate.context_for(self.view) as context: self.names = list(context.importer.get_all_names()) self.view.window().show_quick_panel( self.names, self.on_select_global, sublime.MONOSPACE_FONT)
def run(self, edit): with ropemate.context_for(self.view) as context: self.names = list(context.importer.get_all_names()) self.view.window().show_quick_panel(self.names, self.on_select_global, sublime.MONOSPACE_FONT)
def _regenerate_cache(self, view): with ropemate.context_for(view) as context: # TODO: general solution to syncronizing SublimeRope and Rope (Rope's observers, keep one project open) context.project.pycore._invalidate_resource_cache(context.resource) context.importer.generate_cache(resources=[context.resource])
def on_post_save(self, view): if not "Python" in view.settings().get('syntax'): return with ropemate.context_for(view) as context: context.importer.generate_cache( resources=[context.resource])