Sublime Forum

Problems running my plugin

#1

I’m fairly new to sublime.

I made a plugin for it that runs perfectly fine when I save/run it.

But I can’t figure out how to make it into a command…

Here’s the code I have to implement it in Context.sublime-menu

{ "id" : "DenizenUpdater", "caption" : "Update denizen library", "command" : "updateDenizen" } ]

and here’s the plugin I wrote: (it’s still a work in progress, but it works like it should and now I’m stuck on actually running the plugin from any sublime view)

[code]import sublime, sublime_plugin
import urllib
import os
import json as simplejson
from urllib import request
import re

class updateDenizen(sublime_plugin.WindowCommand):

def run(self, edit):

	print("cmd")
	response = urllib.request.urlopen('http://mcmonkey.org/denizen/tags')
	html = response.read()
	#print(html)

	data = re.findall(r'(?<=<td>&lt;).*?(?=&gt;</td>)',str(html))
	#tags2 = re.findall(r'@.*',str(tags))
	# (?<=@).*?(?=&gt;)

	array = ]
	triggers = ]
	contexts = ]

	for item in data:
		array.append(item)

	for i in array:
		if set('@').intersection(i):
			# remove before @
			#print(i.split('@',1)-1])
			i = i.split('@',1)-1]

			# fix "<" and ">"
			i = i.replace('&gt;','>')
			i = i.replace('&lt;','<')
			tmp = i.split('.')

	#		if len(tmp) == 1:
			if tmp[0] not in triggers:
				triggers.append(tmp[0])

			if re.search(r'(?<=\).*?(?=\])', tmp[0]):
				x = re.sub(r'(?<=\).*?(?=\])','$0', tmp[0])
				if x not in contexts:
					contexts.append(x)
			else:
				if tmp[0] not in contexts:
					contexts.append(tmp[0])


			if len(tmp) == 2:
				tmp_tag = tmp[0] + "." + tmp[1]
				if tmp_tag not in triggers:
					triggers.append(tmp_tag)

				if re.search(r'(?<=\).*?(?=\])', tmp[0]):
					x = re.sub(r'(?<=\).*?(?=\])','$0', tmp[0])+'.'+tmp[1]
					if x not in contexts:
						contexts.append(x)
				elif re.search(r'(?<=\).*?(?=\])', tmp[1]):
					x = tmp[0]+'.'+re.sub(r'(?<=\).*?(?=\])','$0', tmp[1])
					if x not in contexts:
						contexts.append(x)
				else:
					if tmp_tag not in contexts:
						contexts.append(tmp_tag)


			if len(tmp) == 3:
				tmp_tag = tmp[0] + "." + tmp[1] + "." + tmp[2]
				if tmp_tag not in triggers:
					triggers.append(tmp_tag)

				if re.search(r'(?<=\).*?(?=\])', tmp[0]):
					x = re.sub(r'(?<=\).*?(?=\])','$0', tmp[0])+'.'+tmp[1]+'.'+tmp[2]
					if x not in contexts:
						contexts.append(x)
				elif re.search(r'(?<=\).*?(?=\])', tmp[1]):
					x = tmp[0]+'.'+re.sub(r'(?<=\).*?(?=\])','$0', tmp[1])+'.'+re.sub(r'(?<=\).*?(?=\])','', tmp[2])
					if x not in contexts:
						contexts.append(x)
				elif re.search(r'(?<=\).*?(?=\])', tmp[2]):
					x = tmp[0]+'.'+tmp[1]+'.'+re.sub(r'(?<=\).*?(?=\])','$0', tmp[2])
					if x not in contexts:
						contexts.append(x)
				else:
					if tmp_tag not in contexts:
						contexts.append(tmp_tag)

		else:
			# Remove from array
			array.remove(i)


	data = {
		'scope': 'dscript.yaml - source, punctuation.definition.tag.begin',
		'completions':]
	}

	c=0 
	while c < len(triggers):
		dict = {
			'trigger':triggers[c],
			'contents':contexts[c]
		}
		data'completions'].append(dict)
		c+=1
	encoded_str = simplejson.dumps(data, indent=4, sort_keys=True)
	#print(encoded_str)

	os.chdir(r'C:\Users\mark\AppData\Roaming\Sublime Text 3\Packages\User')
	with open('denizen.sublime-completions',"w") as file:
		file.write(encoded_str)


	print("done")[/code]

When I rightclick to open the menu however, “Update Denizen Library” is grayed out. (i tried to exchance the command for “example” and that works fine, so it must be a problem within the plugin I assume)

also tried running it from console using this “view.run_command(“updateDenizen”)” with no luck
I hope you guys can help me!

0 Likes

#2

Thats wrong. Class names must created in camel case and commands needs to append “Command”.
So you should use:

class UpdateDenizenCommand(sublime_plugin.WindowCommand):

The related command is the snake case from this – “update_denizen”.
If you use it so, ST will detect your command and you can bind it to menu/keymap.

0 Likes

#3

[quote=“BugFix”]

Thats wrong. Class names must created in camel case and commands needs to append “Command”.
So you should use:

class UpdateDenizenCommand(sublime_plugin.WindowCommand):

The related command is the snake case from this – “update_denizen”.
If you use it so, ST will detect your command and you can bind it to menu/keymap.[/quote]

Thank you soo much for the quick reply!

0 Likes