Ejemplo n.º 1
0
    def _get_dialog_frame(self):
        frame = Tkinter.Frame(self.toplevel)
        label = Tkinter.Label(frame, text='Change Method Signature :')
        label.grid(row=0, column=0)
        param_frame = Tkinter.Frame(frame)
        self.param_list = VolatileList(
            param_frame, _ParameterListHandle(self.definition_info),
            "Parameters", width=25)
        for pair in self.definition_info.args_with_defaults:
            self.param_list.add_entry(_Parameter(pair[0]))
        if self.definition_info.args_arg is not None:
            self.param_list.add_entry(
                _Parameter('*' + self.definition_info.args_arg))
        if self.definition_info.keywords_arg is not None:
            self.param_list.add_entry(
                _Parameter('**' + self.definition_info.keywords_arg))

        move_up = Tkinter.Button(frame, text='Move Up', width=20, underline=6,
                                 command=lambda: self.param_list.move_up())
        move_down = Tkinter.Button(frame, text='Move Down', width=20, underline=8,
                                   command=lambda: self.param_list.move_down())
        remove = Tkinter.Button(frame, text='Remove', width=20, underline=0,
                                command=lambda: self._remove())
        add = Tkinter.Button(frame, text='Add New Parameter', width=20,
                             underline=0, command=lambda: self._add())
        self.toplevel.bind('<Alt-r>', lambda event: self._remove())
        self.toplevel.bind('<Alt-a>', lambda event: self._add())
        param_frame.grid(row=0, column=0, rowspan=5)
        move_up.grid(row=0, column=1, sticky=Tkinter.S)
        move_down.grid(row=1, column=1, sticky=Tkinter.N)
        remove.grid(row=2, column=1, sticky=Tkinter.N)
        add.grid(row=3, column=1, sticky=Tkinter.N)
        self.in_hierarchy = Tkinter.IntVar()
        in_hierarchy = Tkinter.Checkbutton(
            frame, text='Do for all matching methods in class hierarchy',
            variable=self.in_hierarchy)
        if self.signature.is_method():
            in_hierarchy.grid(row=4, column=1, sticky=Tkinter.N)
        frame.grid()
        frame.focus_set()
        return frame
Ejemplo n.º 2
0
class ChangeMethodSignatureDialog(RefactoringDialog):

    def __init__(self, context):
        resource = context.get_active_editor().get_file()
        editor = context.get_active_editor().get_editor()
        self.project = context.get_core().get_open_project()
        super(ChangeMethodSignatureDialog, self).__init__(
            context, 'Change Method Signature Refactoring')
        self.signature = rope.refactor.change_signature.ChangeSignature(
            context.project, resource, editor.get_current_offset())
        self.definition_info = self.signature.get_definition_info()
        self.to_be_removed = []

    def _calculate_changes(self, handle):
        changers = []
        parameters = self.param_list.get_entries()
        definition_info = self.definition_info
        for parameter in self.to_be_removed:
            if parameter.is_added:
                continue
            remover = rope.refactor.change_signature.ArgumentRemover(
                _get_parameter_index(definition_info, parameter.name))
            changers.append(remover)
            remover.change_definition_info(definition_info)
        for index, parameter in enumerate(parameters):
            if parameter.is_added:
                adder = rope.refactor.change_signature.ArgumentAdder(
                    index, parameter.name, *(parameter.default_and_value))
                changers.append(adder)
                adder.change_definition_info(definition_info)
        new_ordering = [_get_parameter_index(definition_info, param.name)
                        for param in parameters if not param.name.startswith('*')]
        changers.append(rope.refactor.change_signature.ArgumentReorderer(new_ordering))
        return self.signature.get_changes(
            changers, in_hierarchy=self.in_hierarchy.get(), task_handle=handle)

    def _get_dialog_frame(self):
        frame = Tkinter.Frame(self.toplevel)
        label = Tkinter.Label(frame, text='Change Method Signature :')
        label.grid(row=0, column=0)
        param_frame = Tkinter.Frame(frame)
        self.param_list = VolatileList(
            param_frame, _ParameterListHandle(self.definition_info),
            "Parameters", width=25)
        for pair in self.definition_info.args_with_defaults:
            self.param_list.add_entry(_Parameter(pair[0]))
        if self.definition_info.args_arg is not None:
            self.param_list.add_entry(
                _Parameter('*' + self.definition_info.args_arg))
        if self.definition_info.keywords_arg is not None:
            self.param_list.add_entry(
                _Parameter('**' + self.definition_info.keywords_arg))

        move_up = Tkinter.Button(frame, text='Move Up', width=20, underline=6,
                                 command=lambda: self.param_list.move_up())
        move_down = Tkinter.Button(frame, text='Move Down', width=20, underline=8,
                                   command=lambda: self.param_list.move_down())
        remove = Tkinter.Button(frame, text='Remove', width=20, underline=0,
                                command=lambda: self._remove())
        add = Tkinter.Button(frame, text='Add New Parameter', width=20,
                             underline=0, command=lambda: self._add())
        self.toplevel.bind('<Alt-r>', lambda event: self._remove())
        self.toplevel.bind('<Alt-a>', lambda event: self._add())
        param_frame.grid(row=0, column=0, rowspan=5)
        move_up.grid(row=0, column=1, sticky=Tkinter.S)
        move_down.grid(row=1, column=1, sticky=Tkinter.N)
        remove.grid(row=2, column=1, sticky=Tkinter.N)
        add.grid(row=3, column=1, sticky=Tkinter.N)
        self.in_hierarchy = Tkinter.IntVar()
        in_hierarchy = Tkinter.Checkbutton(
            frame, text='Do for all matching methods in class hierarchy',
            variable=self.in_hierarchy)
        if self.signature.is_method():
            in_hierarchy.grid(row=4, column=1, sticky=Tkinter.N)
        frame.grid()
        frame.focus_set()
        return frame

    def _remove(self):
        self.to_be_removed.append(self.param_list.remove_entry())

    def _add(self):
        toplevel = Tkinter.Toplevel()
        toplevel.title('Add New Parameter')
        name_label = Tkinter.Label(toplevel, text='Name')
        name_entry = Tkinter.Entry(toplevel)
        default_label = Tkinter.Label(toplevel, text='Default')
        default_entry = Tkinter.Entry(toplevel)
        value_label = Tkinter.Label(toplevel, text='Value')
        value_entry = Tkinter.Entry(toplevel)
        name_label.grid(row=0, column=0)
        name_entry.grid(row=0, column=1)
        default_label.grid(row=1, column=0)
        default_entry.grid(row=1, column=1)
        value_label.grid(row=2, column=0)
        value_entry.grid(row=2, column=1)
        def ok(event=None):
            new_param = _Parameter(name_entry.get())
            value = None
            default = None
            if default_entry.get().strip() != '':
                default = default_entry.get()
            if value_entry.get().strip() != '':
                value = value_entry.get().strip()
            new_param.default_and_value = (default, value)
            new_param.is_added = True
            insertion_index = self.param_list.get_active_index()
            if self.param_list.get_entries():
                insertion_index += 1
            self.param_list.insert_entry(new_param, insertion_index)
            toplevel.destroy()
        def cancel(event=None):
            toplevel.destroy()
        toplevel.bind('<Return>', ok)
        toplevel.bind('<Escape>', cancel)
        toplevel.bind('<Control-g>', cancel)
        name_entry.focus_set()
        toplevel.grab_set()