11"""SublimeSalesforceReference: Quick access to Salesforce Documentation from Sublime Text"""
2- __version__ = "1.1.1 "
2+ __version__ = "1.2.0 "
33__author__ = "James Hill (oblongmana@gmail.com)"
44__copyright__ = "SublimeSalesforceReference: (C) 2014 James Hill. GNU GPL 3."
55__credits__ = ["All Salesforce Documentation is © Copyright 2000–2014 salesforce.com, inc." , "ThreadProgress.py is under the MIT License, Will Bond <will@wbond.net>, and SalesforceReference.py's RetrieveIndexThread method is a derives in part from code under the same license" ]
1111import threading
1212from .ThreadProgress import ThreadProgress
1313
14- # def plugin_loaded():
15- # thread = RetrieveIndexThread(sublime.active_window())
16- # thread.start()
17- # ThreadProgress(thread, 'Retrieving Salesforce Reference Index...', '')
14+ class SalesforceReferenceCache ():
15+ def __init__ (self ):
16+ self .sf_ref_pages_titles = []
17+ self .sf_ref_pages_links = []
18+
19+ reference_cache = SalesforceReferenceCache ()
20+
21+ def plugin_loaded ():
22+ thread = RetrieveIndexThread (sublime .active_window (),False )
23+ thread .start ()
24+ ThreadProgress (thread , 'Retrieving Salesforce Reference Index...' , '' )
25+
1826
1927class SalesforceReferenceCommand (sublime_plugin .WindowCommand ):
2028
@@ -26,32 +34,38 @@ def run(self):
2634
2735class RetrieveIndexThread (threading .Thread ):
2836 """
29- A thread to run retrieval of the Saleforce Documentation induex
37+ A thread to run retrieval of the Saleforce Documentation index, or access the reference_cache
3038 """
3139
32- def __init__ (self , window ):
40+ def __init__ (self , window , open_when_done = True ):
3341 """
3442 :param window:
3543 An instance of :class:`sublime.Window` that represents the Sublime
3644 Text window to show the available package list in.
45+ :param open_when_done:
46+ Whether this thread is being run solely for caching, or should open
47+ the documentation list for user selection when done. Defaults to
48+ true - should open the documentaton list when done
3749 """
3850
3951 self .window = window
52+ self .open_when_done = open_when_done
53+ global reference_cache
4054 threading .Thread .__init__ (self )
4155
56+ def run (self ):
57+ if not (reference_cache .sf_ref_pages_titles and reference_cache .sf_ref_pages_links ):
58+ sf_xml = urllib .request .urlopen ('http://www.salesforce.com/us/developer/docs/apexcode/Data/Toc.xml' ).read ().decode ('utf-8' )
59+ sf_tree = ElementTree .fromstring (sf_xml )
60+ leaf_parents = sf_tree .findall ("*[@Title='Reference'].//TocEntry[@DescendantCount='0'].." )
61+ for parent in leaf_parents :
62+ reference_cache .sf_ref_pages_titles .append (parent .attrib ['Title' ])
63+ reference_cache .sf_ref_pages_links .append (parent .attrib ['Link' ])
64+
65+ if (self .open_when_done ):
66+ self .window .show_quick_panel (reference_cache .sf_ref_pages_titles , self .open_documentation )
67+
4268 def open_documentation (self , reference_index ):
4369 if (reference_index != - 1 ):
4470 base_url = 'http://www.salesforce.com/us/developer/docs/apexcode'
45- webbrowser .open_new_tab (base_url + self .sf_ref_pages_links [reference_index ])
46-
47- def run (self ):
48- sf_xml = urllib .request .urlopen ('http://www.salesforce.com/us/developer/docs/apexcode/Data/Toc.xml' ).read ().decode ('utf-8' )
49- sf_tree = ElementTree .fromstring (sf_xml )
50- leaf_parents = sf_tree .findall ("*[@Title='Reference'].//TocEntry[@DescendantCount='0'].." )
51- self .sf_ref_pages_titles = []
52- self .sf_ref_pages_links = []
53- for parent in leaf_parents :
54- self .sf_ref_pages_titles .append (parent .attrib ['Title' ])
55- self .sf_ref_pages_links .append (parent .attrib ['Link' ])
56-
57- self .window .show_quick_panel (self .sf_ref_pages_titles , self .open_documentation )
71+ webbrowser .open_new_tab (base_url + reference_cache .sf_ref_pages_links [reference_index ])
0 commit comments