Beispiel #1
0
    def _toggle_line_comment(self, action):
        """
        Toggles line comment in the selection.
        """
        buffer = self._window.get_active_document()

        if buffer == None:
            return

        bounds = buffer.get_selection_bounds()

        if len(bounds) == 0:
            # no selection
            start_line_nb = utils.get_iter_cursor(buffer).get_line()
            end_line_nb = start_line_nb
        else:
            start_line_nb = bounds[0].get_line()
            end_line_nb = bounds[1].get_line()

        # check if all of the lines begin with '//'
        add_mode = False
        for line_nb in xrange(start_line_nb, end_line_nb + 1):
            lcs_start = buffer.get_iter_at_line(line_nb)

            # skip indentation whitespace
            while lcs_start.get_char() in ('\t', ' '):
                lcs_start.forward_char()

            lcs_end = lcs_start.copy()
            lcs_end.forward_chars(2)

            lcs = buffer.get_slice(lcs_start, lcs_end)
            if lcs != '//':
                add_mode = True
                break

        if add_mode:
            # add '//' to beginning of all selection lines
            buffer.begin_user_action()
            for i in xrange(start_line_nb, end_line_nb + 1):
                buffer.insert(buffer.get_iter_at_line(i), "//")
            buffer.end_user_action()
        else:
            # remove '//' from beginning of all selection lines
            buffer.begin_user_action()
            for i in xrange(start_line_nb, end_line_nb + 1):
                lcs_start = buffer.get_iter_at_line(i)

                # skip indentation whitespace
                while lcs_start.get_char() in ('\t', ' '):
                    lcs_start.forward_char()

                lcs_end = lcs_start.copy()
                lcs_end.forward_chars(2)

                buffer.delete(lcs_start, lcs_end)
            buffer.end_user_action()
Beispiel #2
0
    def _format(self, show_err):
        buffer = self._window.get_active_document()
        text = buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter())

        try:
            p = subprocess.Popen([self._plugin.model.gobin_path + '/gofmt'],
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)

            stdoutdata, stderrdata = p.communicate(text)
        except OSError as e:
            if show_err:
                dialog = gtk.MessageDialog(flags=gtk.DIALOG_DESTROY_WITH_PARENT,
                                           type=gtk.MESSAGE_ERROR,
                                           buttons=gtk.BUTTONS_OK)
                dialog.set_markup(_("An error occurred when <i>gofmt</i> was attempted to run:"))
                dialog.format_secondary_markup('<span font_family="monospace">' +
                                               glib.markup_escape_text(str(e)) +
                                               '</span>')
                dialog.run()
                dialog.destroy()

        if len(stderrdata) != 0:
            if show_err:
                dialog = gtk.MessageDialog(flags=gtk.DIALOG_DESTROY_WITH_PARENT,
                                           type=gtk.MESSAGE_ERROR,
                                           buttons=gtk.BUTTONS_OK)
                dialog.set_markup(_("An error occurred while running <i>gofmt</i>:"))
                if len(stderrdata) > utils.MAX_ERR_MSG_LEN: # cut down too long error messages
                    stderrdata = stderrdata[:utils.MAX_ERR_MSG_LEN] + "..."
                dialog.format_secondary_markup('<span font_family="monospace">' +
                                               glib.markup_escape_text(stderrdata) +
                                               '</span>')
                dialog.run()
                dialog.destroy()
            return

        # save the cursor position before modifying the document
        cursor_iter = utils.get_iter_cursor(buffer)
        offset = cursor_iter.get_offset()
        old_buf_len = buffer.get_char_count()

        buffer.begin_user_action()

        buffer.set_text(stdoutdata)

        # attempt to return the cursor back to where it was
        new_buf_len = buffer.get_char_count()
        # adjust offset to account for indentation changes
        new_offset = offset + (new_buf_len - old_buf_len)
        buffer.place_cursor(buffer.get_iter_at_offset(new_offset))
        view = self._window.get_active_view()
        utils.scroll_to_insert(buffer, view)

        buffer.end_user_action()
Beispiel #3
0
    def on_key_press(self, view, event, buffer):
        key_name = gtk.gdk.keyval_name(event.keyval)

        # if user deletes non-letter or non-decimal-digit unicode character then hide code completion window
        if key_name == 'BackSpace':
            delchariter = utils.get_iter_cursor(buffer)
            delchariter.backward_char()
            del_char = delchariter.get_char()
            # L == letter, Nd  == number, decimal digit
            if unicodedata.category(del_char)[0] != 'L' and unicodedata.category(del_char) != 'Nd':
                completion = view.get_completion()
                completion.hide()
Beispiel #4
0
    def do_populate(self, context):
        buffer = context.get_iter().get_buffer()
        odata = self._get_odata(buffer, utils.get_iter_cursor(buffer))

        if not odata:
            # no proposals
            return context.add_proposals(self, [], True)
        
        proposals = []
        for po in self._get_podata(odata):
                proposals.append(gsv.CompletionItem(po[0], po[1], po[2], po[3]))

        context.add_proposals(self, proposals, True)
Beispiel #5
0
 def do_get_start_iter(self, context, proposal):
     return utils.get_iter_cursor(context.get_iter().get_buffer())