1- import re
21import sublime
32import sublime_plugin
4- import timeit
53
64from functools import cached_property , wraps
5+ import re
6+ import timeit
7+ from typing import List , Optional , Tuple
78
89from . import completions
910
@@ -29,41 +30,41 @@ def wrap(*args, **kw):
2930 return wrap
3031
3132
32- def match_selector (view , pt , scope ) :
33+ def match_selector (view : sublime . View , pt : int , scope : str ) -> bool :
3334 # This will catch scenarios like:
3435 # - .foo {font-style: |}
3536 # - <style type="text/css">.foo { font-weight: b|</style>
3637 return any (view .match_selector (p , scope ) for p in (pt , pt - 1 ))
3738
3839
39- def next_none_whitespace (view , pt ) :
40+ def next_none_whitespace (view : sublime . View , pt : int ) -> Optional [ str ] :
4041 for pt in range (pt , view .size ()):
4142 ch = view .substr (pt )
4243 if ch not in ' \t ' :
4344 return ch
45+ return None
4446
4547
4648class CSSCompletions (sublime_plugin .EventListener ):
4749
4850 @cached_property
49- def func_args (self ):
51+ def func_args (self ) -> dict :
5052 return completions .get_func_args ()
5153
5254 @cached_property
53- def props (self ):
55+ def props (self ) -> dict :
5456 return completions .get_properties ()
5557
5658 @cached_property
57- def re_name (self ):
59+ def re_name (self ) -> re . Pattern :
5860 return re .compile (r"([a-zA-Z-]+)\s*:[^:;{}]*$" )
5961
6062 @cached_property
61- def re_value (self ):
63+ def re_value (self ) -> re . Pattern :
6264 return re .compile (r"^(?:\s*(:)|([ \t]*))([^:]*)([;}])" )
6365
6466 @timing
65- def on_query_completions (self , view , prefix , locations ):
66-
67+ def on_query_completions (self , view : sublime .View , prefix : str , locations : List [int ]) -> Optional [sublime .CompletionList ]:
6768 settings = sublime .load_settings ('CSS.sublime-settings' )
6869 if settings .get ('disable_default_completions' ):
6970 return None
@@ -87,16 +88,10 @@ def on_query_completions(self, view, prefix, locations):
8788 return sublime .CompletionList (items )
8889 return None
8990
90- def complete_property_name (self , view , prefix , pt ) :
91+ def complete_property_name (self , view : sublime . View , prefix : str , pt : int ) -> List [ sublime . CompletionItem ] :
9192 text = view .substr (sublime .Region (pt , view .line (pt ).end ()))
9293 matches = self .re_value .search (text )
93- if matches :
94- colon , space , value , term = matches .groups ()
95- else :
96- colon = ""
97- space = ""
98- value = ""
99- term = ""
94+ colon , space , value , term = matches .groups () if matches else ("" , "" , "" , "" )
10095
10196 # don't append anything if next character is a colon
10297 suffix = ""
@@ -111,16 +106,16 @@ def complete_property_name(self, view, prefix, pt):
111106 if not value and not term and not match_selector (view , pt , "meta.group" ):
112107 suffix += ";"
113108
114- return (
109+ return [
115110 sublime .CompletionItem (
116111 trigger = prop ,
117112 completion = prop + suffix ,
118113 completion_format = sublime .COMPLETION_FORMAT_SNIPPET ,
119114 kind = KIND_CSS_PROPERTY
120115 ) for prop in self .props
121- )
116+ ]
122117
123- def complete_property_value (self , view , prefix , pt ) :
118+ def complete_property_value (self , view : sublime . View , prefix : str , pt : int ) -> List [ sublime . CompletionItem ] :
124119 completions = [
125120 sublime .CompletionItem (
126121 trigger = "!important" ,
@@ -136,11 +131,7 @@ def complete_property_value(self, view, prefix, pt):
136131 values = self .props .get (prop )
137132 if values :
138133 details = f"<code>{ prop } </code> property-value"
139-
140- if match_selector (view , pt , "meta.group" ) or next_none_whitespace (view , pt ) == ";" :
141- suffix = ""
142- else :
143- suffix = "$0;"
134+ suffix = "" if match_selector (view , pt , "meta.group" ) or next_none_whitespace (view , pt ) == ";" else "$0;"
144135
145136 for value in values :
146137 if isinstance (value , list ):
@@ -161,7 +152,7 @@ def complete_property_value(self, view, prefix, pt):
161152
162153 return completions
163154
164- def complete_function_argument (self , view : sublime .View , prefix , pt ) :
155+ def complete_function_argument (self , view : sublime .View , prefix : str , pt : int ) -> List [ sublime . CompletionItem ] :
165156 func_name = ""
166157 nest_level = 1
167158 # Look for the beginning of the current function call's arguments list,
@@ -199,7 +190,7 @@ def complete_function_argument(self, view: sublime.View, prefix, pt):
199190
200191 args = self .func_args .get (func_name )
201192 if not args :
202- return None
193+ return []
203194
204195 completions = []
205196 details = f"{ func_name } () argument"
0 commit comments