Esempio n. 1
0
    def delete_node(self, path):
        node = self.root.value_node_at(path)
        parent, pos = node.parent, node.position

        enode = self.node_being_edited

        if enode is not None and enode.value_node_or_self is node:
            # If deleting a node that's currently being edited, we terminate editing mode
            # and delete whatever was typed so far by the user. Also handle the case when
            # the user is editing an object key,
            reg = self.reh.enclosing_reg()
            if enode.keyval_match:
                reg = reg.cover(enode.keyval_match.region)

            self.done_editing()
        else:
            reg = parent.entries[pos].region
        
        if node.is_first and node.is_last:
            # TODO: deletion of all nodes from the root is not supported
            diereg = sublime.Region(parent.begin + 1, parent.end - 1)
        elif node.is_first:
            diereg = sublime.Region(reg.a, parent.entries[pos + 1].begin)
        else:
            diereg = sublime.Region(parent.entries[pos - 1].end, reg.b)

        with read_only_set_to(self.view, False):
            self.view.erase(edit_for[self.view], diereg)

        parent.delete_at(pos)
Esempio n. 2
0
    def run(self):
        text = self.view.substr(self.repl.edit_region)
        stripped_text = text.rstrip()
        if stripped_text != text:
            self.repl.replace_edit_region_contents(stripped_text)
            text = stripped_text

        try:
            ws_handler.run_async_op('replEval', {
                'spaceId': self.repl.inspection_space_id,
                'mid': self.repl.cur_module_id,
                'code': text
            })
            jsval = yield 
            error = None
        except BackendError as e:
            error = e

        cur = StructuredCursor(self.repl.edit_region.b, self.view)
        with read_only_set_to(self.view, False):
            if error:
                cur.insert('\n! ')
                cur.insert(error.message)
            else:
                cur.insert('\n< ')
                insert_js_value(cur, jsval)
            cur.insert('\n\n')
            self.repl.insert_prompt(cur)

        set_selection(self.view, to=cur.pos, show=True)
Esempio n. 3
0
    def undo_modifications_outside_edit_region(self):
        """Undo modifications to portions of the buffer outside the edit region.

        We only detect such modifications when the sizes of the corresponding pre and post
        regions change.  This cannot detect e.g. line swaps outside the edit region but
        is still very useful.

        Also, we detect insertion of text right before the edit region and right after it,
        and extend the edit region to include what was just inserted.
        """
        while True:
            pre, post, rowcol = self._get_state()
            if pre == self.pre and post == self.post and rowcol == self.rowcol:
                break
            elif pre > self.pre and post == self.post and \
                    self._is_after_insertion_at_reg_begin(pre - self.pre):
                delta = pre - self.pre
                reg = self.get_edit_region()
                self.set_edit_region(sublime.Region(reg.a - delta, reg.b))
                break
            elif post > self.post and pre == self.pre and \
                    self._is_after_insertion_at_reg_end(post - self.post):
                delta = post - self.post
                reg = self.get_edit_region()
                self.set_edit_region(sublime.Region(reg.a, reg.b + delta))
                break

            with read_only_set_to(self.view, False):
                self.view.run_command('undo')
            sublime.status_message("Cannot edit outside the editing region")
Esempio n. 4
0
 def prepare_for_activation(self):
     """Invalidate the view before first activating it"""
     if self.is_offline and self.is_pristine:
         with read_only_set_to(self.view, False):
             self.view.replace(
                 edit_for[self.view],
                 sublime.Region(0, self.view.size()),
                 self.MSG_NEEDS_REFRESH
             )
             self.is_pristine = False
Esempio n. 5
0
    def refresh(self, root_object):
        if self.is_online:
            self.root.put_offline()
            self.root = None

        with read_only_set_to(self.view, False),\
                viewport_and_selection_globally_preserved(self.view):
            self.view.erase(edit_for[self.view], sublime.Region(0, self.view.size()))

            cur = StructuredCursor(0, self.view)
            cur.insert('$ = ')

            self.root, _ = self._insert_js_value(cur, root_object)
            self.root.put_online(self.view)

            self.view.window().focus_view(self.view)
Esempio n. 6
0
    def replace_key_node(self, path, new_name):
        node = self.root.key_node_at(path)

        if node is self.node_being_edited:
            reg = self.reh.enclosing_reg()
            self.done_editing()
        else:
            reg = node.region

        with read_only_set_to(self.view, False):
            self.view.erase(edit_for[self.view], reg)
            cur = Cursor(reg.a, self.view)
            cur.push()
            cur.insert(new_name)

        node.parent.replace_key_node_region_at(node.position, cur.pop_region())
Esempio n. 7
0
    def replace_value_node(self, path, new_value):
        """Replace value node at given path with new_value"""
        node = self.root.value_node_at(path)

        if node is self.node_being_edited:
            reg = self.reh.enclosing_reg()
            self.done_editing()
        else:
            reg = node.region

        with read_only_set_to(self.view, False),\
                selection_rowcol_preserved_on_replace(self.view, reg),\
                viewport_position_preserved(self.view):
            self.view.erase(edit_for[self.view], reg)
            cur = self._make_cursor(reg.a, node.parent)
            new_node, new_region = self._insert_js_value(cur, new_value)

        node.parent.replace_value_node_at(node.position, new_node, new_region)
Esempio n. 8
0
    def insert_node(self, path, key, value):
        path, new_index = path[:-1], path[-1]
        parent = self.root.value_node_at(path)

        if (key is not None) != parent.is_object:
            raise RuntimeError("Object/array mismatch")

        if self.is_editing_new_node and self.new_node_parent is parent and\
                self.new_node_position == new_index:
            # In this Code Browser view we were editing the new node which is now being
            # inserted. This is typical after the user commits. TODO: erase() below won't
            # work if the cursor is outside editing region
            self.view.erase(edit_for[self.view], self.reh.enclosing_reg())
            self.done_editing()

        with read_only_set_to(self.view, False):
            if parent.num_children == 0:
                cur = self._make_cursor(parent.begin + 1, parent)
                cur.insert_initial_sep()
                with cur.pos_preserved():
                    cur.insert_terminal_sep()
            elif new_index >= parent.num_children:
                cur = self._make_cursor(parent.entries[-1].end, parent)
                cur.insert_inter_sep()
            else:
                cur = self._make_cursor(parent.entries[new_index].begin, parent)
                with cur.pos_preserved():
                    cur.insert_inter_sep()

            if key is not None:
                cur.push()
                cur.insert(key)
                key_region = cur.pop_region()
                cur.insert_keyval_sep()
                value_node, value_region = self._insert_js_value(cur, value)
                parent.insert_at(new_index, key_region, value_node, value_region)
            else:
                node, region = self._insert_js_value(cur, value)
                parent.insert_at(new_index, node, region)
Esempio n. 9
0
 def replace_edit_region_contents(self, s):
     ereg = self.edit_region
     with read_only_set_to(self.view, False):
         self.view.replace(edit_for[self.view], ereg, s)
     self.edit_region = sublime.Region(ereg.a, ereg.a + len(s))