def run(self, edit, *args, **kwargs): # return self.view.show_popup('why not?', location=0) self.window = self.view.window() self.selection = sublime.Selection(self.view.id()) self.settings = self.view.settings() sel = self.view.sel()[0] if not all_in(['text.html', 'string'], self.view.scope_name( sel.begin())): return False for region in self.view.find_by_selector('string.quoted.double.html'): if region.contains(sel): path = unquote(self.view.substr(region)) if not (path.startswith('http://') or path.startswith('https://') or path.startswith('file://')): path = 'file://' + os.path.join( os.path.dirname(self.view.file_name()), path).replace( os.path.sep, '/') html = """ <style> body { margin: 0; } </style> <body> <img src=[path]> </body> """.replace('[path]', path) return self.view.show_popup(html)
def run(self, paths=[]): self.view = sublime.active_window().active_view() self.selection = sublime.Selection(self.view.id()) self.settings = self.view.settings() cmder_path = self.settings.get("cmder_path", "C:/cmder/cmder.exe") def open_cmder(path): cmd = '"{}" /SINGLE "{}"'.format(cmder_path, path) os.popen(cmd) if len(paths) >= 1: for item in paths: if not os.path.isdir(item): item = os.path.dirname(item) open_cmder(item) return project_data = self.window.project_data() if project_data: path = project_data['folders'][0]['path'] else: path = os.path.dirname(self.view.file_name()) if not os.path.exists(cmder_path): return em("The path '{}' does not exits!".format(cmder_path)) sm('Opening cmder from "{}"'.format(cmder_path)) open_cmder(path)
def run(self, edit, *args, **kwargs): self.window = self.view.window() self.selection = sublime.Selection(self.view.id()) self.settings = self.view.settings() char = '' for region in self.view.sel(): begin = region.begin() while char not in (' ', '?', '!', '\n'): begin -= 1 char = self.view.substr(begin) if begin <= 0: char = ' ' begin += 1 end = region.end() char = '' while char not in (' ', '?', '!', '\n'): end += 1 char = self.view.substr(end) if end >= self.view.size(): char = ' ' text = self.view.substr(sublime.Region(begin, end)).split(':') view = self._look_for(text[0], self.on_found, text[1]) self.window.focus_view(view)
def show(view, symbol): if view.is_loading(): sublime.set_timeout(lambda: show(view, symbol), 30) return for sym in view.symbols(): if symbol in sym[1]: sel = sublime.Selection(0) sel.add(sym[0]) view.show(sel)
def goto_error(self, view, errors, direction='next'): """Go to the next/previous error in view.""" sel = view.sel() if len(sel) == 0: sel.add(sublime.Region(0, 0)) saved_sel = tuple(sel) empty_selection = len(sel) == 1 and sel[0].empty() # sublime.Selection() changes the view's selection, get the point first point = sel[0].begin() if direction == 'next' else sel[-1].end() regions = sublime.Selection(view.id()) regions.clear() for error_type in (highlight.WARNING, highlight.ERROR): regions.add_all(view.get_regions(highlight.MARK_KEY_FORMAT.format(error_type))) region_to_select = None # If going forward, find the first region beginning after the point. # If going backward, find the first region ending before the point. # If nothing is found in the given direction, wrap to the first/last region. if direction == 'next': for region in regions: if ( (point == region.begin() and empty_selection and not region.empty()) or (point < region.begin()) ): region_to_select = region break else: for region in reversed(regions): if ( (point == region.end() and empty_selection and not region.empty()) or (point > region.end()) ): region_to_select = region break # If there is only one error line and the cursor is in that line, we cannot move. # Otherwise wrap to the first/last error line unless settings disallow that. if region_to_select is None and ((len(regions) > 1 or not regions[0].contains(point))): if persist.settings.get('wrap_find', True): region_to_select = regions[0] if direction == 'next' else regions[-1] if region_to_select is not None: self.select_lint_region(self.view, region_to_select) else: sel.clear() sel.add_all(saved_sel) sublime.message_dialog('No {0} lint error.'.format(direction))
def test_sbot(self): '''Also has tests for the simple sbot.py.''' # self.assertEqual('foo'.upper(), 'FOO') view = sublime.View(600) sel = sublime.Selection(view.id()) sel.add(sublime.Region(10, 20, 101)) view.sel = MagicMock(return_value = sel) evt = sbot.SbotEvent() evt.on_selection_modified(view)
def run(self, edit, *args, **kwargs): # return self.view.run_command('api_replace', { 'region': [0, 0], 'text': 'yep' }) self.window = self.view.window() self.selection = sublime.Selection(self.view.id()) self.settings = self.view.settings() to_upper_case = self.settings.get('color_to_upper_case', False) # can create bug: if you click on a color, and then click again, it will disappear. html = """ <style> div { background-color: {bg}; color: {bg}; padding: {size}px; display: block; width: 100%; border-radius: 50px; } body, html { margin: 0; } </style> <body id="color"> <div href="somepage.html"></div> </body> """ point = self.view.sel()[0].begin() if 'color' in self.view.scope_name(point): region = self.view.word(point) color = self.view.substr(region) row, col = self.view.rowcol(point) point = self.view.text_point(row - 1, 0) lenght = len(self.view.line(point)) def hidden(): if to_upper_case: self.view.run_command('api_replace', { 'region': [region.a, region.b], 'text': color.upper() }) self.view.show_popup(html.replace('{bg}', color).replace('{size}', '10'), location=point, on_hide=hidden)
def run(self, edit, *args, **kwargs): self.window = self.view.window() self.selection = sublime.Selection(self.view.id()) self.settings = self.view.settings() group_index, view_index = self.window.get_view_index(self.view) error = True def build(view): main_group_index, main_view_index = self.window.get_view_index(view) view_to_focus = None if main_group_index != group_index: view_to_focus = self.window.active_view_in_group(main_group_index) self.window.focus_view(view) self.window.run_command('build') if view_to_focus is not None: self.window.focus_view(view_to_focus) self.window.focus_view(self.view) if self.can_be_run(self.view): build(self.view) return for view in self.window.views(): if self.can_be_run(view): build(view) error = False return if error is True: self.window.show_quick_panel( [ [ 'Error: no "_main.py" is open.', 'The plugin won\'t do anything.' ], [ 'Forget it buddy, run this file', self.view.file_name() ], ], self.on_done, sublime.KEEP_OPEN_ON_FOCUS_LOST, 1)
def find_error(self, view, errors, forward=True): sel = view.sel() saved_sel = tuple(sel) if len(sel) == 0: sel.add((0, 0)) point = sel[0].begin() if forward else sel[-1].end() regions = sublime.Selection(view.id()) regions.clear() regions.add_all( view.get_regions( Highlight.MARK_KEY_FORMAT.format(Highlight.WARNING))) regions.add_all( view.get_regions(Highlight.MARK_KEY_FORMAT.format( Highlight.ERROR))) region_to_select = None # If going forward, find the first region beginning after the point. # If going backward, find the first region ending before the point. # If nothing is found in the given direction, wrap to the first/last region. if forward: for region in regions: if point < region.begin(): region_to_select = region break else: for region in reversed(regions): if point > region.end(): region_to_select = region break # If there is only one error line and the cursor is in that line, we cannot move. # Otherwise wrap to the first/last error line unless settings disallow that. if region_to_select is None and ((len(regions) > 1 or not regions[0].contains(point))): if persist.settings.get('wrap_find', True): region_to_select = regions[0] if forward else regions[-1] if region_to_select is not None: self.select_lint_region(self.view, region_to_select) else: sel.clear() sel.add_all(saved_sel) sublime.error_message('No {0} lint errors.'.format( 'next' if forward else 'previous')) return region_to_select
def run(self, edit, *args, **kwargs): self.window = self.view.window() self.selection = sublime.Selection(self.view.id()) self.settings = self.view.settings() def move_file(path, new_path): if os.path.exists(new_path): return False with open(path, 'r') as fp: content = fp.read() with open(new_path, 'w') as fp: fp.write(content) return True project = self.window.project_data()['folders'] for folder in project: self.remove_pyc(folder['path'])
def run(self, edit, *args, **kwargs): self.window = self.view.window() self.selection = sublime.Selection(self.view.id()) self.settings = self.view.settings() info = [] info.append(self.view.file_name() or 'Untitled') info.append(self.view.size()) text = [] text.append('Name: ' + (self.view.file_name() or 'Untitled')) text.append('Char: ' + str(self.view.size())) arr = re.split( '[' + re.escape( self.settings.get('word_separators') ) + '\n]', self.view.substr( sublime.Region(0, self.view.size()) ) ) while '' in arr: arr.remove('') text.append('Word: ' + str( len(arr ) ) ) text = '\n'.join(text) md(text)
def run(self, edit, *arg, **args): self.window = self.view.window() self.selection = sublime.Selection(self.view.id()) self.setting = self.view.settings() for region in self.view.sel(): region = self.view.full_line(region) # indent = get_indent(self.view.substr(region)) indent = self.view.indentation_level(region.begin()) nb = str(rnd(100, 1000)) code = ' # counter' + nb text = ('\t' * indent) + 'counter' + nb + ' = 0\n' text += self.view.substr(region) text += ('\t' * indent) + '\tcounter' + nb + ' += 1\n' text += ('\t' * indent) + '\tif counter' + nb + ' > 100:\n' text += ('\t' * indent) + '\t\tprint("end up counter' + nb + '")\n' text += ('\t' * indent) + '\t\treturn False' + code + '\n' self.view.replace(edit, region, text)
def run(self, edit, *args, **kwargs): self.window = self.view.window() self.selection = sublime.Selection(self.view.id()) self.settings = self.view.settings() for region in self.view.sel(): # pygame.draw.circle text = self.view.substr(region) text = 'pygame.draw.circle' if text.startswith('pygame.'): text = text[len('pygame.'):] module, method = text.split('.') path = PATH_TO_PYGAME_DOC + '/' + module + '.html#' + module + '.' + method webbrowser.open_new_tab(path)
def run(self, edit, *args, **kwargs): self.settings = self.view.settings() self.window = self.view.window() self.selection = sublime.Selection(self.view.id()) # self.settings = self.view.settings() if not self.is_enabled(): return em('Cannot run: not a .coffee file!') fullpath = self.view.file_name() path = os.path.dirname(os.path.dirname(fullpath)) filename, ext = os.path.splitext(os.path.basename(fullpath)) path = os.path.join(path, filename) + '.js' if not os.path.exists(path): return em("The path '{}' does not exists!".format(path)) self.window.run_command("new_pane", {"move": False}) view = self.window.open_file(path) self.window.set_view_index(view, 1, 0)
def run(self, edit, *args, **kwargs): self.edit = edit self.window = self.view.window() self.selection = sublime.Selection(self.view.id()) self.settings = self.view.settings() self.replace_expression = kwargs.get('replace_expression', True) v = self.view self.ok_chars_list = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '/', '*', '(', ')', '.', ' ', 'r' ] for region in v.sel(): line = v.substr(v.line(region.begin())) keep_moving_backwards = True if region.empty(): start = region.begin() - 1 end = start + 1 # for the beginning char = '' while keep_moving_backwards: char = v.substr(start) if self.__is_an_expression_char(char) is False: keep_moving_backwards = False start += 1 else: start -= 1 keep_moving_forwards = True while keep_moving_forwards: char = v.substr(end) if self.__is_an_expression_char(char) is False: keep_moving_forwards = False else: end += 1 self.__replace(sublime.Region(start, end)) else: # if there is a selection, then I consider it as the expression # for char in self.view.substr(region): # if self.__is_an_expression_char(char) is False: # return em('Wrong expression: the char {} is not correct!'.format(repr(char))) self.__replace(region)
def run(self, edit): self.window = self.view.window() self.selection = sublime.Selection(self.view.id()) self.window.show_input_panel("Haiku cmd: ", "", self.on_done, None, None)