33import sublime_plugin , sublime , re , os , sys , shutil
44
55cache = {}
6+ go_to = None
67
78def plugin_loaded ():
89 global cache
10+ global go_to
911
1012 cache_dir = os .path .join (sublime .cache_path (), GLOBAL_SET ['package_name' ])
1113 cache ['libs' ] = DataCache ([get_erl_lib_dir ()], 'libs' , cache_dir )
@@ -16,6 +18,8 @@ def plugin_loaded():
1618 cache ['project' ] = DataCache (project_folder , 'project' , cache_dir )
1719 cache ['project' ].build_data_async ()
1820
21+ go_to = GoTo ()
22+
1923def plugin_unloaded ():
2024 from package_control import events
2125
@@ -29,16 +33,7 @@ def plugin_unloaded():
2933 plugin_loaded ()
3034 unload_handler = plugin_unloaded
3135
32- class SaveFileRebuildListener (sublime_plugin .EventListener ):
33- def on_post_save (self , view ):
34- caret = view .sel ()[0 ].a
35-
36- if not ('source.erlang' in view .scope_name (caret )):
37- return
38-
39- cache ['project' ].build_data_async ()
40-
41- class CompletionsListener (sublime_plugin .EventListener ):
36+ class ErlListener (sublime_plugin .EventListener ):
4237 def on_query_completions (self , view , prefix , locations ):
4338 if cache ['libs' ].libs == {}:
4439 return
@@ -62,103 +57,24 @@ def on_query_completions(self, view, prefix, locations):
6257 if module_name in cache ['project' ].libs :
6358 return (cache ['project' ].libs [module_name ], flag )
6459
65- class GotoCommand (sublime_plugin .TextCommand , DataCache ):
66- def __init__ (self , view ):
67- sublime_plugin .TextCommand .__init__ (self , view )
68- DataCache .__init__ (self )
69- self .window = sublime .active_window ()
70-
71- def run (self , edit ):
72- line_str = self .__get_line_str (self .view )
73-
74- math = self .re_dict ['take_mf' ].search (line_str )
75- if math is not None and (len (math .groups ()) == 2 ):
76- module_name = math .group (1 )
77- fun_name = math .group (2 )
78-
79- key = (module_name , fun_name )
80- if key in cache ['libs' ].fun_postion :
81- self .__window_quick_panel_open_window (cache ['libs' ].fun_postion [key ])
82- elif key in cache ['project' ].fun_postion :
83- self .__window_quick_panel_open_window (cache ['project' ].fun_postion [key ])
84-
85- return
60+ def on_text_command (self , view , command_name , args ):
61+ if command_name == 'goto' and 'event' in args :
62+ event = args ['event' ]
63+ point = view .window_to_text ((event ['x' ], event ['y' ]))
8664
87- math = self .re_dict ['take_fun' ].search (line_str )
88- if math is not None and (len (math .groups ()) == 1 ):
89- fun_name = math .group (1 )
90-
91- filepath = self .view .file_name ()
92- libs_key = ('erlang' , fun_name )
93- cur_module = self .get_module_from_path (filepath )
94- project_key = (cur_module , fun_name )
95- if libs_key in cache ['libs' ].fun_postion :
96- self .__window_quick_panel_open_window (cache ['libs' ].fun_postion [libs_key ])
97- else :
98- row_id = 1
99- cur_view_postion = {}
100- all_cur_view_fun = []
101- module = self .get_module_from_path (filepath )
102- code = self .view .substr (sublime .Region (0 , self .view .size ()))
103- for line in code .split ('\n ' ):
104- funhead = self .re_dict ['funline' ].search (line )
105- if funhead is not None :
106- fun_name = funhead .group (1 )
107- param_str = funhead .group (2 )
108- param_list = self .format_param (param_str )
109- key = (module , fun_name )
110- param_len = len (param_list )
111- format_fun_name = '{0}/{1}' .format (fun_name , param_len )
112-
113- if (key , param_len ) not in all_cur_view_fun :
114- if key not in cur_view_postion :
115- cur_view_postion [key ] = []
116- cur_view_postion [key ].append ((format_fun_name , filepath , row_id ))
117- all_cur_view_fun .append ((key , param_len ))
118- row_id += 1
119-
120- self .__window_quick_panel_open_window (cur_view_postion [project_key ])
121-
122- return
123-
124- def __get_line_str (self , view ):
125- location = view .sel ()[0 ].begin ()
126- line_region = view .line (location )
127- line_str = view .substr (line_region )
65+ if not view .match_selector (point , "source.erlang" ):
66+ return
12867
129- return line_str
68+ go_to . run ( point , view , cache )
13069
131- def __window_quick_panel_open_window (self , options ):
132- self .point = self .view .sel ()[0 ]
133- self .options = options
70+ def on_post_save (self , view ):
71+ caret = view .sel ()[0 ].a
13472
135- if len (options ) == 1 :
136- (_fun_name , path , row ) = options [0 ]
137- self .window .open_file ('{}:{}:0' .format (path , row ), sublime .ENCODED_POSITION )
73+ if not ('source.erlang' in view .scope_name (caret )):
13874 return
13975
140- self .window .show_quick_panel (
141- [self .__show_option (o ) for o in options ],
142- self ._jump_to_in_window ,
143- on_highlight = partial (self ._jump_to_in_window , transient = sublime .TRANSIENT ))
144-
145- def __show_option (self , options ):
146- return '{1}:{0} line: {2}' .format (* options )
147-
148- def _jump_to_in_window (self , index , line_number = None , param_cnt = None , transient = 0 ):
149-
150- try :
151- if self .view .sel ()[0 ] != self .point :
152- self .view .sel ().clear ()
153- self .view .sel ().add (self .point )
154- except AttributeError :
155- pass
156-
157- if isinstance (index , int ):
158- if index == - 1 :
159- self .window .focus_view (self .view )
160- self .view .show (self .point )
161- return
76+ cache ['project' ].build_data_async ()
16277
163- (fun_name , filename , line_number ) = self .options [index ]
164- self .window .open_file ('{0}:{1}' .format (filename , line_number or 0 ), sublime .ENCODED_POSITION | transient )
78+ class GotoCommand (sublime_plugin .TextCommand ):
79+ def run (self , edit ):
80+ return
0 commit comments