@@ -100,10 +100,9 @@ def strip_line_label(line: str) -> tuple[str, str | None]:
100
100
match = FRegex .LINE_LABEL .match (line )
101
101
if match is None :
102
102
return line , None
103
- else :
104
- line_label = match .group (1 )
105
- out_str = line [: match .start (1 )] + " " * len (line_label ) + line [match .end (1 ) :]
106
- return out_str , line_label
103
+ line_label = match .group (1 )
104
+ out_str = line [: match .start (1 )] + " " * len (line_label ) + line [match .end (1 ) :]
105
+ return out_str , line_label
107
106
108
107
109
108
def strip_strings (in_line : str , maintain_len : bool = False ) -> str :
@@ -172,7 +171,7 @@ def separate_def_list(test_str: str) -> list[str] | None:
172
171
if curr_str != "" :
173
172
def_list .append (curr_str )
174
173
curr_str = ""
175
- elif ( curr_str == "" ) and ( len ( def_list ) == 0 ) :
174
+ elif not def_list :
176
175
return None
177
176
continue
178
177
curr_str += char
@@ -198,17 +197,20 @@ def find_word_in_line(line: str, word: str) -> Range:
198
197
start and end positions (indices) of the word if not found it returns
199
198
-1, len(word) -1
200
199
"""
201
- i = - 1
202
- for poss_name in FRegex .WORD .finditer (line ):
203
- if poss_name .group () == word :
204
- i = poss_name .start ()
205
- break
200
+ i = next (
201
+ (
202
+ poss_name .start ()
203
+ for poss_name in FRegex .WORD .finditer (line )
204
+ if poss_name .group () == word
205
+ ),
206
+ - 1 ,
207
+ )
206
208
# TODO: if i == -1: return None makes more sense
207
209
return Range (i , i + len (word ))
208
210
209
211
210
212
def find_paren_match (string : str ) -> int :
211
- """Find matching closing parenthesis ** from an already open parenthesis scope**
213
+ """Find matching closing parenthesis from an already open parenthesis scope
212
214
by forward search of the string, returns -1 if no match is found
213
215
214
216
Parameters
@@ -237,15 +239,14 @@ def find_paren_match(string: str) -> int:
237
239
-1
238
240
"""
239
241
paren_count = 1
240
- ind = - 1
241
242
for i , char in enumerate (string ):
242
243
if char == "(" :
243
244
paren_count += 1
244
245
elif char == ")" :
245
246
paren_count -= 1
246
247
if paren_count == 0 :
247
248
return i
248
- return ind
249
+ return - 1
249
250
250
251
251
252
def get_line_prefix (
@@ -282,17 +283,16 @@ def get_line_prefix(
282
283
col += len (prepend_string )
283
284
line_prefix = curr_line [:col ].lower ()
284
285
# Ignore string literals
285
- if qs :
286
- if (line_prefix .find ("'" ) > - 1 ) or (line_prefix .find ('"' ) > - 1 ):
287
- sq_count = 0
288
- dq_count = 0
289
- for char in line_prefix :
290
- if (char == "'" ) and (dq_count % 2 == 0 ):
291
- sq_count += 1
292
- elif (char == '"' ) and (sq_count % 2 == 0 ):
293
- dq_count += 1
294
- if (dq_count % 2 == 1 ) or (sq_count % 2 == 1 ):
295
- return None
286
+ if qs and ((line_prefix .find ("'" ) > - 1 ) or (line_prefix .find ('"' ) > - 1 )):
287
+ sq_count = 0
288
+ dq_count = 0
289
+ for char in line_prefix :
290
+ if (char == "'" ) and (dq_count % 2 == 0 ):
291
+ sq_count += 1
292
+ elif (char == '"' ) and (sq_count % 2 == 0 ):
293
+ dq_count += 1
294
+ if (dq_count % 2 == 1 ) or (sq_count % 2 == 1 ):
295
+ return None
296
296
return line_prefix
297
297
298
298
@@ -329,14 +329,12 @@ def resolve_globs(glob_path: str, root_path: str = None) -> list[str]:
329
329
>>> resolve_globs('test') == [str(pathlib.Path(os.getcwd()) / 'test')]
330
330
True
331
331
"""
332
- # Resolve absolute paths i.e. not in our root_path
333
- if os .path .isabs (glob_path ) or not root_path :
334
- p = Path (glob_path ).resolve ()
335
- root = p .anchor # drive letter + root path
336
- rel = str (p .relative_to (root )) # contains glob pattern
337
- return [str (p .resolve ()) for p in Path (root ).glob (rel )]
338
- else :
332
+ if not os .path .isabs (glob_path ) and root_path :
339
333
return [str (p .resolve ()) for p in Path (root_path ).resolve ().glob (glob_path )]
334
+ p = Path (glob_path ).resolve ()
335
+ root = p .anchor # drive letter + root path
336
+ rel = str (p .relative_to (root )) # contains glob pattern
337
+ return [str (p .resolve ()) for p in Path (root ).glob (rel )]
340
338
341
339
342
340
def only_dirs (paths : list [str ]) -> list [str ]:
@@ -406,7 +404,9 @@ def map_keywords(keywords: list[str]):
406
404
return mapped_keywords , keyword_info
407
405
408
406
409
- def get_keywords (keywords : list , keyword_info : dict = {}):
407
+ def get_keywords (keywords : list , keyword_info : dict = None ):
408
+ if keyword_info is None :
409
+ keyword_info = {}
410
410
keyword_strings = []
411
411
for keyword_id in keywords :
412
412
string_rep = KEYWORD_LIST [keyword_id ]
@@ -461,10 +461,7 @@ def get_paren_substring(string: str) -> str | None:
461
461
"""
462
462
i1 = string .find ("(" )
463
463
i2 = string .rfind (")" )
464
- if - 1 < i1 < i2 :
465
- return string [i1 + 1 : i2 ]
466
- else :
467
- return None
464
+ return string [i1 + 1 : i2 ] if - 1 < i1 < i2 else None
468
465
469
466
470
467
def get_paren_level (line : str ) -> tuple [str , list [Range ]]:
@@ -496,7 +493,7 @@ def get_paren_level(line: str) -> tuple[str, list[Range]]:
496
493
('', [Range(start=0, end=0)])
497
494
498
495
"""
499
- if line == "" :
496
+ if not line :
500
497
return "" , [Range (0 , 0 )]
501
498
level = 0
502
499
in_string = False
@@ -526,9 +523,7 @@ def get_paren_level(line: str) -> tuple[str, list[Range]]:
526
523
if level == 0 :
527
524
sections .append (Range (i , i1 ))
528
525
sections .reverse ()
529
- out_string = ""
530
- for section in sections :
531
- out_string += line [section .start : section .end ]
526
+ out_string = "" .join (line [section .start : section .end ] for section in sections )
532
527
return out_string , sections
533
528
534
529
@@ -564,7 +559,7 @@ def get_var_stack(line: str) -> list[str]:
564
559
>>> get_var_stack('')
565
560
['']
566
561
"""
567
- if len ( line ) == 0 :
562
+ if not line :
568
563
return ["" ]
569
564
final_var , sections = get_paren_level (line )
570
565
if final_var == "" :
@@ -574,10 +569,9 @@ def get_var_stack(line: str) -> list[str]:
574
569
for i , section in enumerate (sections ):
575
570
if not line [section .start : section .end ].strip ().startswith ("%" ):
576
571
iLast = i
577
- final_var = ""
578
- for section in sections [iLast :]:
579
- final_var += line [section .start : section .end ]
580
-
572
+ final_var = "" .join (
573
+ line [section .start : section .end ] for section in sections [iLast :]
574
+ )
581
575
if final_var is not None :
582
576
final_var = "%" .join ([i .strip () for i in final_var .split ("%" )])
583
577
final_op_split : list [str ] = FRegex .OBJBREAK .split (final_var )
@@ -586,6 +580,43 @@ def get_var_stack(line: str) -> list[str]:
586
580
return None
587
581
588
582
583
+ def get_placeholders (arg_list : list [str ]) -> tuple [str , str ]:
584
+ """
585
+ Function used to generate placeholders for snippets
586
+
587
+ Parameters
588
+ ----------
589
+ arg_list : list[str]
590
+ Method arguments list
591
+
592
+ Returns
593
+ -------
594
+ Tuple[str, str]
595
+ Tuple of arguments as a string and snippet string
596
+
597
+ Examples
598
+ --------
599
+ >>> get_placeholders(['x', 'y'])
600
+ ('(x, y)', '(${1:x}, ${2:y})')
601
+
602
+ >>> get_placeholders(['x=1', 'y=2'])
603
+ ('(x=1, y=2)', '(x=${1:1}, y=${2:2})')
604
+
605
+ >>> get_placeholders(['x', 'y=2', 'z'])
606
+ ('(x, y=2, z)', '(${1:x}, y=${2:2}, ${3:z})')
607
+ """
608
+ place_holders = []
609
+ for i , arg in enumerate (arg_list ):
610
+ opt_split = arg .split ("=" )
611
+ if len (opt_split ) > 1 :
612
+ place_holders .append (f"{ opt_split [0 ]} =${{{ i + 1 } :{ opt_split [1 ]} }}" )
613
+ else :
614
+ place_holders .append (f"${{{ i + 1 } :{ arg } }}" )
615
+ arg_str = f"({ ', ' .join (arg_list )} )"
616
+ arg_snip = f"({ ', ' .join (place_holders )} )"
617
+ return arg_str , arg_snip
618
+
619
+
589
620
def fortran_md (code : str , docs : str | None ):
590
621
"""Convert Fortran code to markdown
591
622
0 commit comments