Sublime Forum

Plugin not activating

#1

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!

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))
1 Like

Plugin doesnt load until i save it
Plugin doesnt load until i save it
#2

did you ever find a solution for this? I seem to be running into the same issue for a plugin I am working on.

0 Likes

#3

Could be a scoping problem, try to place your plugin into a different folder (e.g. /packages/myplugin/)

0 Likes

#4

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.

0 Likes

#5

[quote=“MarcusF”]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.[/quote]

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.

0 Likes

#6

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.

0 Likes

#7

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

0 Likes

#8

FML i just posted a topic on this… anyone figure this out yet?

0 Likes

#9

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:


# 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")
0 Likes

#10

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

sublimetext.com/docs/3/api_reference.html

1 Like

#11

Looks like I should have read the fine print… but very strange that the other non-list variables worked fine.

0 Likes

#12

I happened to me occasionally that not all settings (or none at all) have been load when the plugin was loaded, not even after plugin_loaded was called.

Your best bet is to only load settings when you really need them and don’t cache them for a long duration, which also allows the user to change the settings while ST is running and your plugin would catch those changes. For example when the command is run.

0 Likes