def run(self, edit, sort=True, sort_numeric=True, sort_order=None, remove_single_maps=True, **kwargs): # Check the environment (view, args, ...) if self.view.is_scratch(): return # Collect parameters file_path = self.view.file_name() if sort_order is None: sort_order = self.sort_order vp = get_viewport_coords(self.view) output = OutputPanel(self.view.window() or sublime.active_window(), "aaa_package_dev") output.show() self.start_time = time.time() # Init the Loader loader = loaders.YAMLLoader(None, self.view, file_path=file_path, output=output) data = None try: data = loader.load() except NotImplementedError, e: # use NotImplementedError to make the handler report the message as it pleases output.write_line(str(e)) self.status(str(e), file_path)
def run(self, edit, sort=True, sort_numeric=True, sort_order=None, remove_single_line_maps=True, insert_newlines=True, save=False, **kwargs): """Available parameters: sort (bool) = True Whether the list should be sorted at all. If this is not ``True`` the dicts' keys' order is likely not going to equal the input. sort_numeric (bool) = True A language definition's captures are assigned to a numeric key which is in fact a string. If you want to bypass the string sorting and instead sort by the strings number value, you will want this to be ``True``. sort_order (list) When this is passed it will be used instead of the default. The first element in the list will be the first key to be written for ALL dictionaries. Set to ``False`` to skip this step. The default order is: comment, name, scopeName, fileTypes, uuid, begin, beginCaptures, end, endCaptures, match, captures, include, patterns, repository remove_single_line_maps (bool) = True This will in fact turn the "- {include: '#something'}" lines into "- include: '#something'". Also splits mappings like "- {name: anything, match: .*}" to multiple lines. Be careful though because this is just a simple regexp that's safe for *usual* syntax definitions. insert_newlines (bool) = True Add newlines where appropriate to make the whole data appear better organized. Essentially add a new line: - before global "patterns" key - before global "repository" key - before every repository key except for the first save (bool) = False Save the view after processing is done. **kwargs Forwarded to yaml.dump (if they are valid). """ # Check the environment (view, args, ...) if self.view.is_scratch(): return if self.view.is_loading(): # The view has not yet loaded, recall the command in this case until ST is done kwargs.update( dict(sort=sort, sort_numeric=sort_numeric, sort_order=sort_order, remove_single_line_maps=remove_single_line_maps, insert_newlines=insert_newlines, save=save)) sublime.set_timeout( lambda: self.view.run_command("rearrange_yaml_syntax_def", kwargs), 20) return # Collect parameters file_path = self.view.file_name() if sort_order is None: sort_order = self.default_order vp = get_viewport_coords(self.view) output = OutputPanel(self.view.window() or sublime.active_window(), "aaa_package_dev") output.show() self.start_time = time.time() # Init the Loader loader = loaders.YAMLLoader(None, self.view, file_path=file_path, output=output) data = None try: data = loader.load() except NotImplementedError, e: # Use NotImplementedError to make the handler report the message as it pleases output.write_line(str(e)) self.status(str(e), file_path)
def run(self, edit, sort=True, sort_numeric=True, sort_order=None, remove_single_line_maps=True, insert_newlines=True, save=False, **kwargs): """Available parameters: sort (bool) = True Whether the list should be sorted at all. If this is not ``True`` the dicts' keys' order is likely not going to equal the input. sort_numeric (bool) = True A language definition's captures are assigned to a numeric key which is in fact a string. If you want to bypass the string sorting and instead sort by the strings number value, you will want this to be ``True``. sort_order (list) When this is passed it will be used instead of the default. The first element in the list will be the first key to be written for ALL dictionaries. Set to ``False`` to skip this step. The default order is: comment, name, scopeName, fileTypes, uuid, begin, beginCaptures, end, endCaptures, match, captures, include, patterns, repository remove_single_line_maps (bool) = True This will in fact turn the "- {include: '#something'}" lines into "- include: '#something'". Also splits mappings like "- {name: anything, match: .*}" to multiple lines. Be careful though because this is just a simple regexp that's safe for *usual* syntax definitions. insert_newlines (bool) = True Add newlines where appropriate to make the whole data appear better organized. Essentially add a new line: - before global "patterns" key - before global "repository" key - before every repository key except for the first save (bool) = False Save the view after processing is done. **kwargs Forwarded to yaml.dump (if they are valid). """ # Check the environment (view, args, ...) if self.view.is_scratch(): return if self.view.is_loading(): # The view has not yet loaded, recall the command in this case until ST is done kwargs.update(dict( sort=sort, sort_numeric=sort_numeric, sort_order=sort_order, remove_single_line_maps=remove_single_line_maps, insert_newlines=insert_newlines, save=save )) sublime.set_timeout( lambda: self.view.run_command("rearrange_yaml_syntax_def", kwargs), 20 ) return # Collect parameters file_path = self.view.file_name() if sort_order is None: sort_order = self.default_order vp = get_viewport_coords(self.view) output = OutputPanel(self.view.window() or sublime.active_window(), "aaa_package_dev") output.show() self.start_time = time.time() # Init the Loader loader = loaders.YAMLLoader(None, self.view, file_path=file_path, output=output) data = None try: data = loader.load() except NotImplementedError, e: # Use NotImplementedError to make the handler report the message as it pleases output.write_line(str(e)) self.status(str(e), file_path)
def run(self, edit, sort=True, sort_numeric=True, sort_order=None, remove_single_line_maps=True, insert_newlines=True, save=False, **kwargs): """Available parameters: sort (bool) = True Whether the list should be sorted at all. If this is not ``True`` the dicts' keys' order is likely not going to equal the input. sort_numeric (bool) = True A language definition's captures are assigned to a numeric key which is in fact a string. If you want to bypass the string sorting and instead sort by the strings number value, you will want this to be ``True``. sort_order (list) When this is passed it will be used instead of the default. The first element in the list will be the first key to be written for ALL dictionaries. Set to ``False`` to skip this step. The default order is: comment, name, scopeName, fileTypes, uuid, begin, beginCaptures, end, endCaptures, match, captures, include, patterns, repository remove_single_line_maps (bool) = True This will in fact turn the "- {include: '#something'}" lines into "- include: '#something'". Also splits mappings like "- {name: anything, match: .*}" to multiple lines. Be careful though because this is just a simple regexp that's safe for *usual* syntax definitions. insert_newlines (bool) = True Add newlines where appropriate to make the whole data appear better organized. Essentially add a new line: - before global "patterns" key - before global "repository" key - before every repository key except for the first save (bool) = False Save the view after processing is done. **kwargs Forwarded to yaml.dump (if they are valid). """ # Check the environment (view, args, ...) if self.view.is_scratch(): return if self.view.is_loading(): # The view has not yet loaded, recall the command in this case until ST is done kwargs.update(dict( sort=sort, sort_numeric=sort_numeric, sort_order=sort_order, remove_single_line_maps=remove_single_line_maps, insert_newlines=insert_newlines, save=save )) sublime.set_timeout( lambda: self.view.run_command("rearrange_yaml_syntax_def", kwargs), 20 ) return # Collect parameters file_path = self.view.file_name() if sort_order is None: sort_order = self.default_order vp = get_viewport_coords(self.view) output = OutputPanel(self.view.window() or sublime.active_window(), "aaa_package_dev") output.show() self.start_time = time.time() # Init the Loader loader = loaders.YAMLLoader(None, self.view, file_path=file_path, output=output) data = None try: data = loader.load() except NotImplementedError as e: # Use NotImplementedError to make the handler report the message as it pleases output.write_line(str(e)) self.status(str(e), file_path) if not data: output.write_line("No contents in file.") return self.finish(output) # Dump dumper = YAMLOrderedTextDumper(output=output) if remove_single_line_maps: kwargs["Dumper"] = YAMLLanguageDevDumper text = dumper.dump(data, sort, sort_order, sort_numeric, **kwargs) if not text: self.status("Error re-dumping the data.") return # Replace the whole buffer (with default options) self.view.replace( edit, sublime.Region(0, self.view.size()), "# [PackageDev] target_format: plist, ext: tmLanguage\n" + text ) # Insert the new lines using the syntax definition (which has hopefully been set) if insert_newlines: find = self.view.find_by_selector def select(l, only_first=True, not_first=True): # 'only_first' has priority if not l: return l # empty elif only_first: return l[:1] elif not_first: return l[1:] return l def filter_pattern_regs(reg): # Only use those keys where the region starts at column 0 and begins with '-' # because these are apparently the first keys of a 1-scope list beg = reg.begin() return self.view.rowcol(beg)[1] == 0 and self.view.substr(beg) == '-' regs = ( select(find('meta.patterns - meta.repository-block')) + select(find('meta.repository-block')) + select(find('meta.repository-block meta.repository-key'), False) + select(list(filter(filter_pattern_regs, find('meta'))), False) ) # Iterate in reverse order to not clash the regions because we will be modifying the source regs.sort() regs.reverse() for reg in regs: self.view.insert(edit, reg.begin(), '\n') if save: self.view.run_command("save") output.write_line("File saved") # Finish set_viewport(self.view, vp) self.finish(output)
def run(self, edit, sort=True, sort_numeric=True, sort_order=None, remove_single_line_maps=True, insert_newlines=True, save=False, _output_text=None, **kwargs): """Available parameters: sort (bool) = True Whether the list should be sorted at all. If this is not ``True`` the dicts' keys' order is likely not going to equal the input. sort_numeric (bool) = True A language definition's captures are assigned to a numeric key which is in fact a string. If you want to bypass the string sorting and instead sort by the strings number value, you will want this to be ``True``. sort_order (list) When this is passed it will be used instead of the default. The first element in the list will be the first key to be written for ALL dictionaries. Set to ``False`` to skip this step. The default order is: comment, name, scopeName, fileTypes, uuid, begin, beginCaptures, end, endCaptures, match, captures, include, patterns, repository remove_single_line_maps (bool) = True This will in fact turn the "- {include: '#something'}" lines into "- include: '#something'". Also splits mappings like "- {name: anything, match: .*}" to multiple lines. Be careful though because this is just a simple regexp that's safe for *usual* syntax definitions. insert_newlines (bool) = True Add newlines where appropriate to make the whole data appear better organized. Essentially add a new line: - before global "patterns" key - before global "repository" key - before every repository key except for the first save (bool) = False Save the view after processing is done. _output_text (str) = None Text to be prepended to the output panel since it gets cleared by `window.get_output_panel`. **kwargs Forwarded to yaml.dump (if they are valid). """ # Check the environment (view, args, ...) if self.view.is_scratch(): return if self.view.is_loading(): # The view has not yet loaded, recall the command in this case until ST is done kwargs.update( dict(sort=sort, sort_numeric=sort_numeric, sort_order=sort_order, remove_single_line_maps=remove_single_line_maps, insert_newlines=insert_newlines, save=save)) sublime.set_timeout( lambda: self.view.run_command("rearrange_yaml_syntax_def", kwargs), 20) return # Collect parameters file_path = self.view.file_name() if sort_order is None: sort_order = self.default_order vp = get_viewport_coords(self.view) with OutputPanel(self.view.window() or sublime.active_window(), "aaa_package_dev") as output: output.show() if _output_text: output.write_line(_output_text) # With additional newline self.start_time = time.time() # Init the Loader loader = loaders.YAMLLoader(None, self.view, file_path=file_path, output=output) data = None try: data = loader.load(**kwargs) except: output.write_line("Unexpected error occured while parsing, " "please see the console for details.") raise if not data: output.write_line("No contents in file.") return # Dump dumper = YAMLOrderedTextDumper(output=output) if remove_single_line_maps: kwargs["Dumper"] = YAMLLanguageDevDumper try: text = dumper.dump(data, sort, sort_order, sort_numeric, **kwargs) except: output.write_line("Unexpected error occured while dumping, " "please see the console for details.") raise if not text: output.write_line( "Error re-dumping the data in file (no output).") self.status("Error re-dumping the data (no output).") return # Replace the whole buffer (with default options) self.view.replace( edit, sublime.Region(0, self.view.size()), "# [PackageDev] target_format: plist, ext: tmLanguage\n" + text) # Insert the new lines using the syntax definition (which has hopefully been set) if insert_newlines: output.write_line("Inserting newlines...") find = self.view.find_by_selector def select(l, only_first=True, not_first=True): # 'only_first' has priority if not l: return l # empty elif only_first: return l[:1] elif not_first: return l[1:] return l def filter_pattern_regs(reg): # Only use those keys where the region starts at column 0 and begins with '-' # because these are apparently the first keys of a 1-scope list beg = reg.begin() return self.view.rowcol(beg)[1] == 0 and self.view.substr( beg) == '-' regs = (select(find('meta.patterns - meta.repository-block')) + select(find('meta.repository-block')) + select( find('meta.repository-block meta.repository-key'), False) + select(list(filter(filter_pattern_regs, find('meta'))), False)) # Iterate in reverse order to not clash the regions because we will be modifying the source regs.sort() regs.reverse() for reg in regs: self.view.insert(edit, reg.begin(), '\n') if save: output.write_line("Saving...") # Otherwise the "dirty" indicator is not removed sublime.set_timeout(lambda: self.view.run_command("save"), 20) # Finish set_viewport(self.view, vp) output.write("[Finished in %.3fs]" % (time.time() - self.start_time))