22import sublime_plugin
33import os
44import re
5-
6- mark_dirs = ["objects" , "rooms" , "tilesets" , "sprites" , "shaders" , "sounds" ]
7- completion_dirs = ["objects" , "scripts" , "rooms" , "tilesets" , "sprites" , "shaders" , "sounds" , "timelines" ]
8-
9- def dynamic_completions (search_dirs ):
10- completion_list = []
11- var_dict = sublime .active_window ().extract_variables ()
12-
13- if 'project_path' in var_dict :
14- path = var_dict ['project_path' ]
15- elif 'folder' in var_dict :
16- path = var_dict ['folder' ]
17- else :
18- return []
19-
20- for entry in os .listdir (path ):
21- abs_path = os .path .join (path , entry )
22- if os .path .isdir (abs_path ):
23- if entry in search_dirs :
24- completion_list .extend (collect_all_names (abs_path ))
25- return completion_list
26-
27- def collect_all_names (root ):
28- for path , _ , files in os .walk (root ):
29- if os .path .isdir (path ) and any (re .search ("^.*\.(yy|fsh|vsh)$" , f ) for f in files ):
30- yield os .path .basename (path )
5+ import json
6+
7+ yy_file_regex_pattern = "^.*\.yy$"
8+
9+ def parse_yy_file (path ):
10+ with open (path , 'r' , encoding = 'utf-8-sig' ) as f :
11+ try :
12+ data = json .load (f )
13+ model = data ['modelName' ][2 :]
14+ name = data ['name' ]
15+ if model == "Room" :
16+ for layer in data ['layers' ]:
17+ yield ("Layer" , layer ['name' ])
18+ yield (model , name )
19+ except Exception as e :
20+ pass
21+
22+ def generate_completions (exclude_folders = ["views" ]):
23+ folders = sublime .active_window ().folders ()
24+ completions = []
25+ for folder in folders :
26+ for path , _ , files in os .walk (folder ):
27+ if os .path .basename (path ) not in exclude_folders :
28+ for file in files :
29+ if re .search (yy_file_regex_pattern , file ):
30+ completions .extend (parse_yy_file (os .path .join (path , file )))
31+ return completions
3132
3233class GameMakerLanguageCompletions (sublime_plugin .EventListener ):
3334 def on_query_completions (self , view , prefix , locations ):
3435 if view .match_selector (locations [0 ], "source.gml" ):
35- return [[c , c ] for c in dynamic_completions ( completion_dirs )]
36- else :
37- return []
38-
39- def on_modified ( self , view ):
40- if view . match_selector ( locations [ 0 ], "source.gml" ):
41- view . run_command ( "mark_dynamic_names" )
42-
43- class MarkDynamicNames ( sublime_plugin . TextCommand ):
44- def run ( self , edit ):
45- pattern = "(?<![A-Za-z0-9_])" + "|" .join (dynamic_completions ( mark_dirs ) ) + "(?![A-Za-z0-9_])"
46- matches = self .view .find_all (pattern )
47- self .view .add_regions ("gml_names" , matches , scope = "source.gml" ,
48- flags = sublime .DRAW_NO_OUTLINE | sublime .DRAW_NO_FILL | sublime .DRAW_STIPPLED_UNDERLINE )
36+ return [["%s \t 〔%s〕" % ( name , model ), name ] for model , name in generate_completions ( )]
37+
38+ # def on_modified(self, view):
39+ # if re.search(".*\.gml", view.file_name()):
40+ # view.run_command("mark_dynamic_names")
41+
42+ # class MarkDynamicNames(sublime_plugin.TextCommand):
43+ # def run(self, edit):
44+ # names = [name for _, name in generate_completions(["views", "scripts"])]
45+ # print(names)
46+ # pattern = "(?<![A-Za-z0-9_])" + "|".join(names ) + "(?![A-Za-z0-9_])"
47+ # matches = self.view.find_all(pattern)
48+ # self.view.add_regions("gml_names", matches, scope="source.gml",
49+ # flags=sublime.DRAW_NO_OUTLINE | sublime.DRAW_NO_FILL | sublime.DRAW_STIPPLED_UNDERLINE)
0 commit comments