1414# constructing them here
1515#
1616# VALUE_REGEX is a common suffix to hand the `= {<value>,<value>}` part
17- VALUE_REGEX = (
18- r'(?!.*\})\s*(?P<ENTRIES>(?:,[^,]*)+\b)?\s*(?P<OPEN>\{)?'
19- r'(?P<EQUALS>\s*=\s*)?'
20- )
17+ VALUE_REGEX = r"(?!.*\})\s*(?P<ENTRIES>(?:,[^,]*)+\b)?\s*(?P<OPEN>\{)?" r"(?P<EQUALS>\s*=\s*)?"
2118
22- CROSSREF_REGEX = re .compile (
23- VALUE_REGEX + r'crossref' [::- 1 ] + r'\b' ,
24- re .IGNORECASE
25- )
19+ CROSSREF_REGEX = re .compile (VALUE_REGEX + r"crossref" [::- 1 ] + r"\b" , re .IGNORECASE )
2620
2721BIBLATEX_REGEX = re .compile (
28- VALUE_REGEX +
29- r'(?:' + r'|' .join ((s [::- 1 ] for s in ('xref' , 'related' ))) + r')' + r'\b' ,
30- re .IGNORECASE
22+ VALUE_REGEX + r"(?:" + r"|" .join ((s [::- 1 ] for s in ("xref" , "related" ))) + r")" + r"\b" ,
23+ re .IGNORECASE ,
3124)
3225
33- ENTRY_SET_REGEX = re .compile (
34- VALUE_REGEX + r'entryset' [::- 1 ] + r'\b' ,
35- re .IGNORECASE
36- )
26+ ENTRY_SET_REGEX = re .compile (VALUE_REGEX + r"entryset" [::- 1 ] + r"\b" , re .IGNORECASE )
3727
38- XDATA_REGEX = re .compile (
39- VALUE_REGEX + r'xdata' [::- 1 ] + r'\b' ,
40- re .IGNORECASE
41- )
28+ XDATA_REGEX = re .compile (VALUE_REGEX + r"xdata" [::- 1 ] + r"\b" , re .IGNORECASE )
4229
4330# set indicating entries that have their own special handling...
44- SPECIAL_ENTRIES = set ([' @xdata' , ' @set' ])
31+ SPECIAL_ENTRIES = set ([" @xdata" , " @set" ])
4532
4633
4734def _get_keys_by_type (view , valid_types ):
@@ -51,19 +38,22 @@ def _get_keys_by_type(view, valid_types):
5138 if callable (valid_types ):
5239 validator = valid_types
5340 elif isinstance (valid_types , str ):
41+
5442 def validator (s ):
5543 return s == valid_types
44+
5645 else :
46+
5747 def validator (s ):
5848 return s in valid_types
5949
6050 keys = []
6151
6252 contents = view .substr (sublime .Region (0 , view .size ()))
6353 for entry_type , key in re .findall (
64- r' (@(?!preamble|comment|string)[a-zA-Z]+)\s*\{\s*([^,]+)\b' ,
54+ r" (@(?!preamble|comment|string)[a-zA-Z]+)\s*\{\s*([^,]+)\b" ,
6555 contents ,
66- re .IGNORECASE
56+ re .IGNORECASE ,
6757 ):
6858 if validator (entry_type ):
6959 keys .append (key )
@@ -78,15 +68,9 @@ def _get_keys_from_id_field(view):
7868 contents = view .substr (sublime .Region (0 , view .size ()))
7969 # TODO: Should probably figure out how to work out the entry-type
8070 for ids in re .findall (
81- r'\bids\s*=\s*\{([^}]+)\}' ,
82- contents ,
83- re .IGNORECASE | re .UNICODE | re .DOTALL
71+ r"\bids\s*=\s*\{([^}]+)\}" , contents , re .IGNORECASE | re .UNICODE | re .DOTALL
8472 ):
85- for key in re .findall (
86- r'\b([^,]+)\b' ,
87- ids ,
88- re .IGNORECASE | re .UNICODE
89- ):
73+ for key in re .findall (r"\b([^,]+)\b" , ids , re .IGNORECASE | re .UNICODE ):
9074 keys .append (key )
9175
9276 return keys
@@ -97,16 +81,15 @@ def _get_cite_keys_validator(s):
9781
9882
9983def get_cite_keys (view ):
100- return _get_keys_by_type (view , _get_cite_keys_validator ) + \
101- _get_keys_from_id_field (view )
84+ return _get_keys_by_type (view , _get_cite_keys_validator ) + _get_keys_from_id_field (view )
10285
10386
10487def get_xdata_keys (view ):
105- return _get_keys_by_type (view , ' @xdata' )
88+ return _get_keys_by_type (view , " @xdata" )
10689
10790
10891def get_entryset_keys (view ):
109- return _get_keys_by_type (view , ' @set' )
92+ return _get_keys_by_type (view , " @set" )
11093
11194
11295def get_text_to_cursor (view ):
@@ -117,59 +100,49 @@ def get_text_to_cursor(view):
117100
118101# builds the replacement string depending on the current context of the line
119102def _get_replacement (matcher , key ):
120- if not matcher .group (' ENTRIES' ):
121- return ' {0}{1}{2}{3}' .format (
122- '' if matcher .group (' EQUALS' ) else '= ' ,
123- '' if matcher .group (' OPEN' ) else '{' ,
103+ if not matcher .group (" ENTRIES" ):
104+ return " {0}{1}{2}{3}" .format (
105+ "" if matcher .group (" EQUALS" ) else "= " ,
106+ "" if matcher .group (" OPEN" ) else "{" ,
124107 key ,
125- '' if matcher .group (' OPEN' ) else '}'
108+ "" if matcher .group (" OPEN" ) else "}" ,
126109 )
127110
128- return '{0}{1}' .format (
129- ',' if matcher .group ('ENTRIES' )[0 ] != ',' else '' ,
130- key
131- )
111+ return "{0}{1}" .format ("," if matcher .group ("ENTRIES" )[0 ] != "," else "" , key )
132112
133113
134114def get_completions_if_matches (regex , line , get_key_list_func , view ):
135115 matcher = regex .match (line )
136116 if matcher :
137117 return (
138- [
139- (key , _get_replacement (matcher , key ))
140- for key in sorted (set (get_key_list_func (view )))
141- ],
142- sublime .INHIBIT_WORD_COMPLETIONS
118+ [(key , _get_replacement (matcher , key )) for key in sorted (set (get_key_list_func (view )))],
119+ sublime .INHIBIT_WORD_COMPLETIONS ,
143120 )
144121 else :
145122 return []
146123
147124
148125class BiblatexCrossrefCompletions (sublime_plugin .EventListener ):
149126 def on_query_completions (self , view , prefix , locations ):
150- if not view .match_selector (locations [0 ], ' text.bibtex, text.biblatex' ):
127+ if not view .match_selector (locations [0 ], " text.bibtex, text.biblatex" ):
151128 return []
152129
153130 current_line = get_text_to_cursor (view )[::- 1 ]
154131
155132 if current_line .startswith (prefix [::- 1 ]):
156- current_line = current_line [len (prefix ):]
133+ current_line = current_line [len (prefix ) :]
157134
158- result = get_completions_if_matches (
159- CROSSREF_REGEX , current_line , get_cite_keys , view )
135+ result = get_completions_if_matches (CROSSREF_REGEX , current_line , get_cite_keys , view )
160136
161137 if result :
162138 return result
163139
164- if not view .match_selector (locations [0 ], ' text.biblatex' ):
140+ if not view .match_selector (locations [0 ], " text.biblatex" ):
165141 return []
166142
167143 return (
168- get_completions_if_matches (
169- BIBLATEX_REGEX , current_line , get_cite_keys , view ) or
170- get_completions_if_matches (
171- XDATA_REGEX , current_line , get_xdata_keys , view ) or
172- get_completions_if_matches (
173- ENTRY_SET_REGEX , current_line , get_entryset_keys , view ) or
174- []
144+ get_completions_if_matches (BIBLATEX_REGEX , current_line , get_cite_keys , view )
145+ or get_completions_if_matches (XDATA_REGEX , current_line , get_xdata_keys , view )
146+ or get_completions_if_matches (ENTRY_SET_REGEX , current_line , get_entryset_keys , view )
147+ or []
175148 )
0 commit comments