-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pharko.py
87 lines (74 loc) · 3.25 KB
/
Pharko.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import sublime
import sublime_api
import sublime_plugin
import sys #for finding python version
import subprocess #for running terminal commands
import os
import sys
import json
import html
import collections
import tempfile
import traceback
import pprint
import shutil #for copying files/dirs
pp = pprint.PrettyPrinter(indent=4)
PY3 = sys.version > '3'
if PY3:
import urllib.request as urllib
from .src.PharkoLoadedCommand import PharkoLoadedCommand
from .src.PharkoTextCommand import PharkoTextCommand
from .src.PharkoPanelCommand import PharkoPanelCommand
from .src.PharkoSampleCommand import PharkoSampleCommand
else:
import urllib2 as urllib
from src.PharkoLoadedCommand import PharkoLoadedCommand
from src.PharkoTextCommand import PharkoTextCommand
from src.PharkoPanelCommand import PharkoPanelCommand
from src.PharkoSampleCommand import PharkoSampleCommand
## Current files path
PLUGIN_PATH = os.path.abspath(os.path.dirname(__file__))
## All packages path
PACKAGES_PATH = sublime.packages_path() or os.path.dirname(PLUGIN_PATH)
PACKAGES_PATH = os.path.join(PACKAGES_PATH, 'User', 'Pharko')
def plugin_loaded():
PharkoLoadedCommand.isThisPluginLoaded()
## Runs the terminal command 'ls -la'
## Inserts the contents on the first line of the file
class PharkoListCommand(sublime_plugin.TextCommand):
staticIsPluginLoaded = False
def on_done(self, index):
# if user cancels with Esc key, do nothing
# if canceled, index is returned as -1
if index == -1:
return
# if user picks from list, return the correct entry
self.view.run_command("pharko_list", {"location":self.list[index]})
def run(self, edit, location='', **args):
# pp.pprint(location)
# pp.pprint(args)
PharkoLoadedCommand.isThisPluginLoaded()
# Copy sample php snippets if necessary
samplePHPSnippets = PACKAGES_PATH + '/php'
if not os.path.isdir(samplePHPSnippets):
shutil.copytree(PLUGIN_PATH + '/php', samplePHPSnippets)
# command = ["ls", "-la", PLUGIN_PATH]
command = ["php", PLUGIN_PATH + "/pharko.phar", "dir", "--base=" + PACKAGES_PATH, location]
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
response, _ = process.communicate()
returncode = process.returncode
response = response.decode('utf-8')
response = json.loads(response)
if (response['type'] == 'list'):
# this will populate the quick_panel
self.list = response['data']
# show_quick_panel(items, on_done, <flags>, <default_index>)
# ref: http://www.sublimetext.com/forum/viewtopic.php?f=4&t=7139
# take the list, and place it in a quick_panel, make 3rd item
# default pick
self.view.window().show_quick_panel(self.list, self.on_done, 1, 0)
elif (response['type'] == 'snippet'):
self.view.run_command("insert_snippet", { "name": "Packages/User/Pharko/" + response['data']})
self.view.window().run_command("pharko_panel", response) #works too
elif (response['type'] == 'file'):
self.view.run_command("insert_snippet", {'contents': response['data']})