Home Download Buy Blog Forum Support

Trying to build a plug-in...

Trying to build a plug-in...

Postby rtalexander on Wed Aug 29, 2012 9:49 pm

Hi,

I'm trying to build a new plug-in following the instructions found in [1]:

Let’s write a “Hello, World!” plugin for Sublime Text 2:

Select Tools | New Plugin… in the menu.
Save to Packages/User/hello_world.py.


which results in the following generated code:

Code: Select all
class ExampleCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    self.view.insert(edit, 0, "Hello, World!")

However, nothing happens when I try it out by following these directions:

You’ve just written your first plugin. Let’s put it to use:

Create a new buffer (Ctrl+n).
Open the python console (Ctrl+`).
Type: view.run_command("example") and press enter.

You should see the text “Hello, World!” in your new buffer.

In particular, when I when I execute view.run_command("example"), nothing happens. That is, "Hello, World!" does not show up in file, and the only thing printed in the Python console is: >>> view.run_command("example").

I'm at a loss as what to do at this point. I'd be grateful for any suggestion.

Thanks,

Roger Alexander.

1. http://docs.sublimetext.info/en/latest/ ... ugins.html
rtalexander
 
Posts: 8
Joined: Thu Nov 17, 2011 5:30 pm

Re: Trying to build a plug-in...

Postby agibsonsw on Thu Aug 30, 2012 1:17 am

Worked for me straight-away? So it doesn't type "Hello World" at the top of your view?

Are there any previous errors in the console?
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Trying to build a plug-in...

Postby bizoo on Thu Aug 30, 2012 9:21 am

Are you sure you saved it to the right place ?
Code: Select all
\Sublime Text 2\Packages\User\hello_world.py


When you start ST2, the list of loaded plugin is displayed in the console. Make sure your file is loaded.
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Trying to build a plug-in...

Postby rtalexander on Thu Aug 30, 2012 4:45 pm

The console reports that .../User/hello_world.py is being loaded, and as far as I can tell, there are no errors reported:

startup, version: 2210 linux x64 channel: stable
executable: /mnt/sdb2/users/rogealex/Applications/Sublime Text 2/sublime_text
working dir: /mnt/sdb2/SEL-Projects/java-training/lectures/java-feature-overview
packages path: /home/rogealex/.Sublime Text 2/Packages
settings path: /home/rogealex/.Sublime Text 2/Settings
PackageSetup not required
Py_GetProgramName(): sublime_text
Py_GetExecPrefix(): .
Py_GetProgramFullPath(): /home/rogealex/bin/sublime_text
Py_GetPath(): ./lib/python26.zip:./lib/python2.6/:./lib/python2.6/plat-linux2:./lib/python2.6/lib-tk:./lib/python2.6/lib-old:./lib/python2.6/lib-dynload
Py_GetPythonHome(): .
catalogue loaded
found 23 files for base name Default.sublime-keymap
found 1 files for base name Default.sublime-mousemap
found 31 files for base name Main.sublime-menu
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Abacus/Abacus.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/BracketHighlighter/BracketHighlighter.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/BracketHighlighter/Elements.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/BracketHighlighter/bracket_plugin.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/CSS/css_completions.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Case Conversion/case_conversion.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Change Tracker/changetracker.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/ChangeQuotes/change_quotes.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Clipboard History/clipboard.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/comment.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/copy_path.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/delete_word.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/detect_indentation.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/duplicate_line.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/echo.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/exec.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/fold.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/font.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/goto_line.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/indentation.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/kill_ring.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/mark.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/new_templates.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/open_file_settings.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/open_in_browser.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/paragraph.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/save_on_focus_lost.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/scroll.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/set_unsaved_view_name.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/side_bar.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/sort.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/swap_line.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/switch_file.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/transform.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/transpose.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Default/trim_trailing_white_space.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/DetectSyntax/DetectSyntax.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/DetectSyntax/is_rails_file.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Diff/diff.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Edit History/Edit History.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Function Name Display/FunctionNameStatus.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/HTML/encode_html_entities.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/HTML/html_completions.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/KeymapManager/KeymapManager.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Modific/Modific.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Open Recent Files/open_recent_files.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Package Control/Package Control.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/PlistJsonConverter/plist_json_convert.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Pretty JSON/PrettyJson.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Print to HTML/PrintToHTML.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Python Coverage/SublimePythonCoverage.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/PythonOpenModule/python_open_module.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/SideBarEnhancements/SideBar.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/SideBarEnhancements/StatusBarFileSize.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/SideBarEnhancements/StatusBarModifiedTime.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/SublimeExternalCommand/external_command.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/SublimeManpage/SublimeManpage.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/SublimeREPL/lang_integration.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/SublimeREPL/run_existing_command.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/SublimeREPL/sublimerepl.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/SublimeREPL/text_transfer.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/SublimeRope/sublime_rope.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Terminal/Terminal.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Todo Manager/TodoManager.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Toggle Read-Only/toggle_readonly.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/User/hello_world.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/User/underline.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/Wrap Plus/wrap_plus.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/copy-file-name/copy_filename.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/coverage/__init__.py
Reloading plugin /home/rogealex/.Sublime Text 2/Packages/coverage/__main__.py
plugin init time: 0.320757
loading bindings
loading pointer bindings
found 2 files for base name Default.sublime-theme
theme loaded
app ready
pre session restore time: 0.413435
Loading dictionary Packages/Language - English/en_US.dic
using gamma: 1 (err: 0)
wrote startup cache, added files: 2 orphaned files: 0 total files: 173 cache hits: 171
startup time: 0.604412 (package setup was not run)
loaded 358 snippets
>>> view.run_command("example")



Actually, the following showed up in the console just after I posted the above:

Code: Select all
Can't connect
Unable to fetch update url contents
rtalexander
 
Posts: 8
Joined: Thu Nov 17, 2011 5:30 pm

Re: Trying to build a plug-in...

Postby bizoo on Thu Aug 30, 2012 6:02 pm

Look good.

Try adding a print statement to your plugin and look if it is printed in the console when you run your command:
Code: Select all
class ExampleCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    print "My plugin must work"
    self.view.insert(edit, 0, "Hello, World!")
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Trying to build a plug-in...

Postby agibsonsw on Thu Aug 30, 2012 6:37 pm

Just a thought but you may already have a command named "example". Try changing it to "class VoilaCommand" and then view.run_command("voila").
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Trying to build a plug-in...

Postby rtalexander on Thu Aug 30, 2012 10:22 pm

I tried both things:

class VoilaCommand(sublime_plugin.TextCommand):
def run(self, edit):
print "My plugin must work"
self.view.insert(edit, 0, "Hello, World!")
print "Did my plugin work?"


Nothing appeared in the edit window nor did either of the two print statements produce anything in the console. Further suggestions?

Thanks!
rtalexander
 
Posts: 8
Joined: Thu Nov 17, 2011 5:30 pm

Re: Trying to build a plug-in...

Postby agibsonsw on Fri Aug 31, 2012 12:21 am

I would try running the command from a key binding:

Code: Select all
{ "keys": ["ctrl+alt+q"], "command": "voila" }


When you choose Preferences/ Browse Packages, does it take you to the correct folder (where your command file is stored)?

Failing this you might consider a clean install of ST2. Someone might supply information/ a link to do this - there's a way to do it without
having to un-re-install everything :?:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], phil.b, Yahoo [Bot] and 21 guests