Sublime Forum

New begin_edit() signature in ST3

#1

I noticed in ST3, begin_edit requires two arguments: cmd and args. What are those attributes supposed to receive?

0 Likes

#2

OK, I got this working. When you have a TextCommand, instead of creating an edit yourself with begin_edit(), you receive an additional edit parameter that is prepended to the usual args in ST2. On to the next challenge: async processing.

0 Likes

#3

I have the same issue with begin_edit, except that i was using for an output panel :

def print_to_panel_mainthread(self,cmd,str_a): win = sublime.active_window() strtxt = u"".join(str(line) for line in str_a) if(len(str_a)): v = win.create_output_panel('hg_out') if cmd=="diff" : v.set_syntax_file('Packages/Diff/Diff.tmLanguage') v.settings().set('word_wrap', self.view.settings().get('word_wrap')) edit = v.begin_edit(None,None) ##### ISSUE HERE ##### v.insert(edit, 0, strtxt) v.end_edit(edit) win.run_command("show_panel", {"panel": "output.hg_out"})
To have the code compile i tried None and 0, but the issue is that now my panel shows up empty. Any idea ?

I donā€™t mind using sublime.py and sublime_plugin.py as ā€œdocumentationā€ for the API, but slightly more comment (as just a bit more than none :stuck_out_tongue: ) would help a lot, at least for stuff that change compare to ST2.

0 Likes

#4

I take it you havenā€™t read the porting guide.

0 Likes

#5

I did read it, but Iā€™m talking about the .py file that jon suggest to use as reference for the new API functions. Putting some comment about the parameter expected by the function could help plugin dev a lot (and could even be used to automatically generate basic API reference).

Anyway, in my case i found a way to make it work: replacing all the part between begin_edit end_edit by a v.run_command(ā€˜appendā€™, {ā€˜charactersā€™:strtxt})

0 Likes

#6

I was trying to update the excellent plugin AlignTab for ST3 and i had an issue around this edit parameter.

After modification to remove begin_edit/end_edit, and using directly the edit from the TextCommand I get:

[code]class AlignTabCommand(sublime_plugin.TextCommand):

def run(self, edit, user_input=None):
    self.edit = edit
    print("Called with params " + str(user_input))
    if not user_input:
        v = self.view.window().show_input_panel('Align with regex:', '',
                self.align_tab, None, None)
        # print os.getcwd()
        v.set_syntax_file('Packages/AlignTab/AlignTab.tmLanguage')
        v.settings().set('gutter', False)
        v.settings().set('rulers', ])
    else:
       self.align_tab(user_input)[/code]

If the command is called directly with some parameters everything works fine. But if there is no parameters, it shows the panel and then the text is not aligned (but the function is correctly called, i can see some change in selection and stuff).

I managed to workaround the issue (instead of calling align_tab when show_input_panel is done, I do a lambda x : self.view.run_command(ā€œalign_tabā€,{ā€œuser_inputā€:x}) ), but I still donā€™t understand what is wrong in the initial code .Is it a bug, or Iā€™m missing something ?

0 Likes

#7

Itā€™s async code, so after showing the input panel, the command finishes and that original edit object (that you stowing away as self.edit) has expired

0 Likes

#8

Ok I understand now :smile: So the way I handle it is fine I guess .

0 Likes

#9

Please me and share with examples. Thank youā€¦

0 Likes

#10

args attribute receives a numerical value corresponding to the command that is passed to the cmd attribute. This is how it usually works. But I have heard that there is some differences in ST3. So just run through the guide once again to make sure the attributes goes as mentioned above.

0 Likes