def apply_snippet(self, snippet, start = None, end = None): if not snippet.valid: return False buf = self.view.get_buffer() s = Snippet(snippet) if not start: start = buf.get_iter_at_mark(buf.get_insert()) if not end: end = buf.get_iter_at_mark(buf.get_selection_bound()) if start.equal(end) and self.uses_current_word(s): # There is no tab trigger and no selection and the snippet uses # the current word. Set start and end to the word boundary so that # it will be removed start, end = buffer_word_boundary(buf) # Set environmental variables self.update_environment() # You know, we could be in an end placeholder (current, next) = self.next_placeholder() if current and current.__class__ == PlaceholderEnd: self.goto_placeholder(current, None) buf.begin_user_action() # Remove the tag, selection or current word buf.delete(start, end) # Insert the snippet holders = len(self.placeholders) if len(self.active_snippets) == 0: self.first_snippet_inserted() sn = s.insert_into(self, start) self.active_snippets.append(sn) # Put cursor at first tab placeholder keys = filter(lambda x: x > 0, sn.placeholders.keys()) if len(keys) == 0: buf.place_cursor(sn.begin_iter()) else: self.goto_placeholder(self.active_placeholder, sn.placeholders[keys[0]]) buf.end_user_action() return True
def snippet_changed(self, piter = None): if piter: node = self.model.get_value(piter, self.OBJ_COLUMN) s = Snippet(node) else: s = self.snippet piter = self.find_iter(self.model.get_iter(self.language_path), s) if piter: nm = s.display() self.model.set(piter, self.NAME_COLUMN, nm, self.SORT_COLUMN, nm) self.update_buttons() self.entry_tab_trigger_update_valid() return piter
def on_cell_edited(self, cell, path, new_text): if new_text != '': piter = self.model.get_iter(path) node = self.model.get_value(piter, self.SNIPPET_COLUMN) if node: if node == self.snippet.data: s = self.snippet else: s = Snippet(node) s['description'] = new_text self.snippet_changed(piter) self.select_iter(piter) else: # This is the `Add a new snippet...` item # We create a new snippet snippet = self.new_snippet({'description': new_text}) if snippet: self.model.set_value(piter, self.SNIPPET_COLUMN, snippet.data) self.snippet_changed(piter) self.snippet = snippet self.selection_changed()
def snippet_changed(self, piter=None): if piter: node = self.model.get_value(piter, self.OBJ_COLUMN) s = Snippet(node) else: s = self.snippet piter = self.find_iter(self.model.get_iter(self.language_path), s) if piter: nm = s.display() self.model.set(piter, self.NAME_COLUMN, nm, self.SORT_COLUMN, nm) self.update_buttons() self.entry_tab_trigger_update_valid() return piter
def new_snippet(self, properties=None): if not self.language_path: return None snippet = Library().new_snippet(self.get_language(self.language_path), properties) return Snippet(snippet)
def create_snippet(self): # get info for snippet from user, create snippet label, language, snippet_string = self.get_snippet_info() while True: user_input = self.get_user_choice() if user_input == 'save': self.save_snippet(Snippet(label, language, snippet_string)) break elif user_input == 'change': user_choice = self.get_user_choice(option=1) if user_choice == 'label': label = input( 'What would you like to change the label to?\nEnter label: ' ) elif user_choice == 'language': language = input( 'What would you like to change the language to?\nEnter language: ' ) elif user_choice == 'snippet': snippet_string = input( 'What would you like to change the code to?\nEnter code: ' ) else: break
def delete_snippet(self, _label): temp_snippet_arr = [] # open storage file with open('code_snippets.txt', 'r') as storage_file: # Read in all the lines from the text file lines = storage_file.read() # Finds all the lables, languages, and snippets labels = re.findall(r'Label:(.+)', lines) languages = re.findall(r'Language:(.+)', lines) code = re.findall(r'Code:\n(.+)', lines) # prints all snippets with the given label for index, label in enumerate(labels): # had to use strip because of a weird space that was getting added to the front of strings if label.strip() != _label: temp_snippet_arr.append( Snippet(labels[index].strip(), languages[index], code[index])) with open('code_snippets.txt', 'w') as storage_file: for snippet in temp_snippet_arr: storage_file.write( f"\nLabel: {snippet.label}\nLanguage: {snippet.language}\nCode:\n{snippet.snippet_string}\n" ) print("Deleted!") input("Press enter to continue.")
class Proposal(gobject.GObject, gsv.CompletionProposal): def __init__(self, snippet): gobject.GObject.__init__(self) self._snippet = Snippet(snippet) def snippet(self): return self._snippet.data # Interface implementation def do_get_markup(self): return self._snippet.display() def do_get_info(self): return self._snippet.data['text']
class Proposal(GObject.Object, GtkSource.CompletionProposal): __gtype_name__ = "PlumaSnippetsProposal" def __init__(self, snippet): GObject.Object.__init__(self) self._snippet = Snippet(snippet) def snippet(self): return self._snippet.data # Interface implementation def do_get_markup(self): return self._snippet.display() def do_get_info(self): return self._snippet.data['text']
def on_tree_view_selection_changed(self, selection): parent, piter, node = self.selected_snippet() if self.snippet: self.on_entry_tab_trigger_focus_out(self['entry_tab_trigger'], None) self.on_source_view_snippet_focus_out(self['source_view_snippet'], None) self.on_entry_drop_targets_focus_out( self['combo_drop_targets'].get_child(), None) self.update_language_path() if node: self.snippet = Snippet(node) else: self.snippet = None self.selection_changed()
def get_snippet(self, _label): # open storage file with open('code_snippets.txt', 'r') as storage_file: # Read in all the lines from the text file lines = storage_file.read() # Finds all the lables, languages, and snippets labels = re.findall(r'Label:(.+)', lines) languages = re.findall(r'Language:(.+)', lines) code = re.findall(r'Code:\n(.+)', lines) # prints all snippets with the given label for index, label in enumerate(labels): # had to use strip because of a weird space that was getting added to the front of strings if label.strip() == _label: print(Snippet(labels[index], languages[index], code[index])) input("Press enter to continue.")
def apply_snippet(self, snippet, start = None, end = None): if not snippet.valid: return False buf = self.view.get_buffer() s = Snippet(snippet) if not start: start = buf.get_iter_at_mark(buf.get_insert()) if not end: end = buf.get_iter_at_mark(buf.get_selection_bound()) if start.equal(end) and self.uses_current_word(s): # There is no tab trigger and no selection and the snippet uses # the current word. Set start and end to the word boundary so that # it will be removed start, end = buffer_word_boundary(buf) elif start.equal(end) and self.uses_current_line(s): # There is no tab trigger and no selection and the snippet uses # the current line. Set start and end to the line boundary so that # it will be removed start, end = buffer_line_boundary(buf) # Set environmental variables self.update_environment() # You know, we could be in an end placeholder (current, next) = self.next_placeholder() if current and current.__class__ == PlaceholderEnd: self.goto_placeholder(current, None) buf.begin_user_action() # Remove the tag, selection or current word buf.delete(start, end) # Insert the snippet holders = len(self.placeholders) if len(self.active_snippets) == 0: self.first_snippet_inserted() sn = s.insert_into(self, start) self.active_snippets.append(sn) # Put cursor at first tab placeholder keys = filter(lambda x: x > 0, sn.placeholders.keys()) if len(keys) == 0: if 0 in sn.placeholders: self.goto_placeholder(self.active_placeholder, sn.placeholders[0]) else: buf.place_cursor(sn.begin_iter()) else: self.goto_placeholder(self.active_placeholder, sn.placeholders[keys[0]]) if sn in self.active_snippets: # Check if we can get end_iter in view without moving the # current cursor position out of view cur = buf.get_iter_at_mark(buf.get_insert()) last = sn.end_iter() curloc = self.view.get_iter_location(cur) lastloc = self.view.get_iter_location(last) if (lastloc.y + lastloc.height) - curloc.y <= \ self.view.get_visible_rect().height: self.view.scroll_mark_onscreen(sn.end_mark) buf.end_user_action() self.view.grab_focus() return True
def __init__(self, snippet): gobject.GObject.__init__(self) self._snippet = Snippet(snippet)