Home Download Buy Blog Forum Support

Plugin not activating

Plugin not activating

Postby mwean on Fri Dec 14, 2012 1:41 am

I have this plugin that has two text commands. For some reason, the commands don't work until I open up the plugin file and then save it. I have the file saved in .../Packages/User, and I don't have this issue with any other plugins. Is there something in the code that interferes with it loading automatically? Thanks for your help!

Code: Select all
import sublime
import sublime_plugin
import re
import ex_location


def cursor_position(view):
  return view.sel()[0]


def previous_line(view):
  return view.line(cursor_position(view)).a - 1


def next_line(view):
  return view.rowcol(view.line(cursor_position(view)).b + 1)[0]


def str_to_left(view):
  sel_pt = cursor_position(view).a
  left_pt = view.line(sel_pt).a
  left_region = sublime.Region(left_pt, sel_pt)
  return view.substr(left_region)


def str_to_right(view):
  sel_pt = cursor_position(view).a
  right_pt = view.line(sel_pt).b
  right_region = sublime.Region(sel_pt, right_pt)
  return view.substr(right_region)


def current_line(view):
  line_region = view.line(cursor_position(view))
  return view.substr(line_region)


def current_indent_level(view):
  spaces = re.match(r"(\s*)", current_line(view))
  return spaces.group(0)


def scan(view, direction = 1):
  if re.match(r"^\s*$", str_to_left(view)) and re.match(r"^\s+\S+", str_to_right(view)):
    search_str = "^ {0," + str(len(str_to_left(view))) + "}\S+"
  else:
    search_str = "^" + current_indent_level(view) + "\S"

  if direction > 0:
    return ex_location.search(view, search_str, next_line(view)) - 1
  else:
    return ex_location.reverse_search(view, search_str, 0, previous_line(view)) - 1


class NextIndentCommand(sublime_plugin.TextCommand):
  def run(self, edit, extend_selection = False):
    view = self.view
    (current_row, current_col) = view.rowcol(view.sel()[0].begin())

    scanned = scan(view, 1)
    end_of_new_line = view.rowcol(view.line(view.text_point(scanned, 0)).b)[1]

    if current_col > end_of_new_line:
      new_col = end_of_new_line
    else:
      new_col = current_col

    target = view.text_point(scanned, new_col)

    if extend_selection:
      view.run_command('set_mark')

    view.sel().clear()
    view.sel().add(sublime.Region(target))
    view.show(target)

    if extend_selection:
      view.run_command('select_to_mark')
      view.run_command("clear_bookmarks", {"name": "mark"})


class PrevIndentCommand(sublime_plugin.TextCommand):
  def run(self, edit, extend_selection = False):
    view = self.view
    (current_row, current_col) = view.rowcol(view.sel()[0].begin())

    scanned = scan(view, -1)
    end_of_new_line = view.rowcol(view.line(view.text_point(scanned, 0)).b)[1]

    if current_col > end_of_new_line:
      new_col = end_of_new_line
    else:
      new_col = current_col

    target = view.text_point(scanned, new_col)

    if extend_selection:
      view.run_command('set_mark')

    view.sel().clear()
    view.sel().add(sublime.Region(target))
    view.show(target)

    if extend_selection:
      view.run_command('select_to_mark')
      view.run_command("clear_bookmarks", {"name": "mark"})
      new_selection = view.sel()[0]
      view.sel().clear()
      view.sel().add(sublime.Region(new_selection.b, new_selection.a))
mwean
 
Posts: 9
Joined: Wed Aug 01, 2012 9:48 pm

Re: Plugin not activating

Postby danmanstx on Thu Nov 20, 2014 6:47 am

did you ever find a solution for this? I seem to be running into the same issue for a plugin I am working on.
danmanstx
 
Posts: 2
Joined: Thu Nov 20, 2014 6:46 am

Re: Plugin not activating

Postby duy on Mon Nov 24, 2014 4:21 am

Could be a scoping problem, try to place your plugin into a different folder (e.g. /packages/myplugin/)
duy
 
Posts: 27
Joined: Mon Mar 10, 2014 12:28 am

Re: Plugin not activating

Postby MarcusF on Wed Nov 26, 2014 4:49 pm

I'm new to sublime plugins, but for a plugin to run automatically, doesn't it need an eventListener that hooks into sublime?

That's how I run my own plugin that runs on save, I have another that I hooked up to a keyboard shortcut and works just fine that way.
MarcusF
 
Posts: 6
Joined: Thu Nov 13, 2014 5:21 pm

Re: Plugin not activating

Postby duy on Thu Nov 27, 2014 9:25 am

MarcusF wrote:I'm new to sublime plugins, but for a plugin to run automatically, doesn't it need an eventListener that hooks into sublime?

That's how I run my own plugin that runs on save, I have another that I hooked up to a keyboard shortcut and works just fine that way.


Yes, in addition you have to map the Command to a keybinding or an event to execute it. The OP's problem stated that the plugin wasn't found initially, but after he saved his plugin file.
duy
 
Posts: 27
Joined: Mon Mar 10, 2014 12:28 am

Re: Plugin not activating

Postby MarcusF on Wed Dec 03, 2014 8:30 pm

Interestingly, I'm in a similar boat.

I have a plugin and a keybinding in User, but I have to open and save the plugin before the keybinding will trigger it.
MarcusF
 
Posts: 6
Joined: Thu Nov 13, 2014 5:21 pm

Re: Plugin not activating

Postby duy on Wed Dec 03, 2014 9:43 pm

MarcusF wrote:I have a plugin and a keybinding in User, but I have to open and save the plugin before the keybinding will trigger it.


try the following steps:
- create a folder "testmyplugin" under packages (thats the parent folder of User in ST3. or just open Preferences -> Browse Packages). The Name doesn't matter, however, it should be a new folder without any files.
- move your plugin file to the created folder
- restart sublime to make sure, the plugin wasn't loaded

If it doesn't work, just hint me to a gist of that plugin and your setup (what keybinding, where the file is placed, etc), and I'll be happy to help out
duy
 
Posts: 27
Joined: Mon Mar 10, 2014 12:28 am

Re: Plugin not activating

Postby mrstudz on Sat Dec 06, 2014 3:07 am

FML i just posted a topic on this... anyone figure this out yet?
mrstudz
 
Posts: 5
Joined: Sat Dec 06, 2014 2:56 am

Re: Plugin not activating

Postby mrstudz on Sat Dec 06, 2014 5:42 am

Ok so some good news,

I have narrowed it down now and it had to do with settings initialization. This problem has been bugging me for weeks and I finally cracked it as my completed plugin was just sitting there ready to be released.This was odd because every other setting was being initialized and used throughout the code in exactly the same fashion, and there are quite a few variables and various checks....all working 100%, except this one setting.

For anyone else having this problem how i figured it out was to open and close ST3, then immediately before doing anything else open the console (ctrl-tilde) and run the plugin command to check for errors.

I could then see the plugin was loading correctly, however I was getting a compile error. (yeah i know duh right) but the thing is it was error free in my linter, and error free in ST3 after a save so i just missed it somehow...The fix was to reinitialize my master settings variable inside the class that was being called.

The error I was getting : TypeError: argument of type 'NoneType' is not iterable... yet the setting was defined in the settings file as so:

"file_types" : ["php", "htm", "html"],

And even more strangely, after a save the list variable did become iterable. Say what???

Have a look:

Code: Select all

# global settings variable
s = sublime.load_settings("PhpBeautify.sublime-settings")

...
...
...

    # HERE IS THE CAUSE #
    if ext not in s.get("file_types"):
      return False
   
    # ALSO TRIED THIS, DIDN"T WORK #
    if ext not in s.get("file_types",  ['php', 'html', 'htm']):
      return false

    # THIS WORKAROUND FIXED THE LOADING ISSUE, HOWEVER NOW THE USER CAN'T SET FILETYPES #
    if ext not in ['php', 'html', 'htm']:
      return False

   #
   # What finally worked was "reinitializing" the settings variable using the
   # global keyword in my run function, kinda lame as i had already done this elsewhere
   # and it means the settings get checked every single time the command is run.
   #

   # FINAL PRODUCTION FIX #
   def run(self, edit, shadow_copy=False, process_directory=False):
     global s
     s = sublime.load_settings("PhpBeautify.sublime-settings")

mrstudz
 
Posts: 5
Joined: Sat Dec 06, 2014 2:56 am

Re: Plugin not activating

Postby tito on Sat Dec 06, 2014 7:39 am

sublime API is not available at startup, you need to wait.

https://www.sublimetext.com/docs/3/api_reference.html


Plugin Lifecycle
At importing time, plugins may not call any API functions, with the exception of sublime.version(), sublime.platform(), sublime.architecture() and sublime.channel().
If a plugin defines a module level function plugin_loaded(), this will be called when the API is ready to use.
Give APIs, let the community build the rest!
https://github.com/titoBouzout
tito
 
Posts: 876
Joined: Thu Sep 29, 2011 2:27 pm
Location: Montevideo, Uruguay

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 5 guests