def import_snippets(self, filename): errors = {} total_snippets = 0 valid_snippets = 0 with open(filename, 'r') as f: try: snippets = json.load(f) except ValueError as e: errors['loading'] = e.msg if len(errors) == 0: try: json_validate(instance=snippets, schema=SNIPPETS_SCHEMA) except ValidationError as e: index_path = ['snippets'] for part in e.absolute_path: index_path.append('[{0}]'.format(part)) full_message = '{0} on instance {1}:<br>{2}'.format( e.message, ''.join(index_path), e.instance ) errors['validation'] = full_message if len(errors) == 0: for language_info in snippets: language = language_info['language'] triggers = language_info['triggers'] for trigger_info in triggers: trigger = trigger_info['trigger'] descriptions = trigger_info['descriptions'] for description_info in descriptions: description = description_info['description'] snippet = description_info['snippet'] snippet_text = snippet['text'] total_snippets += 1 try: build_snippet_ast(snippet_text) self.update_or_enqueue( language, trigger, description, snippet) valid_snippets += 1 except SyntaxError as e: syntax_errors = errors.get('syntax', {}) key = '{0}/{1}/{2}'.format( language, trigger, description) syntax_errors[key] = e.msg errors['syntax'] = syntax_errors return valid_snippets, total_snippets, errors
def validate(self): trigger_text = self.trigger_text_cb.currentText() description_text = self.description_input.text() snippet_text = self.snippet_input.toPlainText() invalid = False try: build_snippet_ast(snippet_text) self.snippet_valid_label.setText(self.SNIPPET_VALID) except SyntaxError: invalid = True self.snippet_valid_label.setText(self.SNIPPET_INVALID) if trigger_text == '': invalid = True self.trigger_text_cb.setStyleSheet(self.INVALID_CB_CSS) else: self.trigger_text_cb.setStyleSheet(self.VALID_CB_CSS) if trigger_text in self.descriptions: if self.trigger_text != trigger_text: if description_text in self.descriptions[trigger_text]: invalid = True self.description_input.setStyleSheet( self.INVALID_LINE_CSS) else: self.description_input.setStyleSheet( self.VALID_LINE_CSS) else: if description_text != self.description: if description_text in self.descriptions[trigger_text]: invalid = True self.description_input.setStyleSheet( self.INVALID_LINE_CSS) else: self.description_input.setStyleSheet( self.VALID_LINE_CSS) else: self.description_input.setStyleSheet( self.VALID_LINE_CSS) self.button_ok.setEnabled(not invalid)
def insert_snippet(self, text): line, column = self.editor.get_cursor_line_column() visitor = SnippetSearcherVisitor(line, column, self.node_number) ast = build_snippet_ast(text) ast.compute_position((line, column)) ast.accept(visitor) self.inserting_snippet = True self.editor.insert_text(ast.text(), will_insert_text=False) self.editor.document_did_change() if not self.editor.code_snippets: return if not rtree_available: return new_snippet = True if self.is_snippet_active: with QMutexLocker(self.modification_lock): # This is a nested snippet / text on snippet leaf, snippet_root, _ = self._find_node_by_position( line, column) if snippet_root is not None: new_snippet = False root_number = snippet_root.number next_number = root_number + 1 snippet_map = visitor.snippet_map for snippet_number in snippet_map: snippet_nodes = snippet_map[snippet_number] for snippet_node in snippet_nodes: snippet_node.number = next_number next_number += 1 for snippet_number in self.snippets_map: if snippet_number > root_number: snippet_nodes = self.snippets_map[snippet_number] for snippet_node in snippet_nodes: snippet_node.number = next_number next_number += 1 self.update_undo_stack() self._insert_snippet_at_node( leaf, snippet_root, ast, line, column) self._update_ast() if len(snippet_map) > 0: self.select_snippet(snippet_number=root_number + 1) self.draw_snippets() elif leaf is not None: self.reset() if new_snippet: self.reset() self.ast = ast self.snippets_map = visitor.snippet_map if len(self.snippets_map) > 0: # Completion contains snippets self.is_snippet_active = True self.starting_position = (line, column) self.update_position_tree(visitor) self.draw_snippets() self.select_snippet(snippet_number=1) else: # Completion does not contain snippets self.reset()