Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/argcomplete/lexers.py: 95%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

41 statements  

1from __future__ import annotations 

2 

3import os 

4 

5from .exceptions import ArgcompleteException 

6from .io import debug 

7from .packages import _shlex 

8 

9 

10def split_line(line: str, point: int | None = None) -> tuple[str, str, str, list[str], int | None]: 

11 if point is None: 

12 point = len(line) 

13 line = line[:point] 

14 lexer = _shlex.shlex(line, posix=True) 

15 lexer.whitespace_split = True 

16 lexer.wordbreaks = os.environ.get("_ARGCOMPLETE_COMP_WORDBREAKS", "") 

17 words = [] 

18 

19 def split_word(word: str) -> tuple[str, str, str, list[str], int | None]: 

20 # TODO: make this less ugly 

21 point_in_word = len(word) + point - lexer.instream.tell() 

22 if isinstance(lexer.state, (str, bytes)) and lexer.state in lexer.whitespace: 

23 point_in_word += 1 

24 if point_in_word > len(word): 

25 debug("In trailing whitespace") 

26 words.append(word) 

27 word = "" 

28 prefix, suffix = word[:point_in_word], word[point_in_word:] 

29 prequote = "" 

30 # posix 

31 if lexer.state is not None and lexer.state in lexer.quotes: 

32 prequote = lexer.state 

33 # non-posix 

34 # if len(prefix) > 0 and prefix[0] in lexer.quotes: 

35 # prequote, prefix = prefix[0], prefix[1:] 

36 

37 return prequote, prefix, suffix, words, lexer.last_wordbreak_pos 

38 

39 while True: 

40 try: 

41 word = lexer.get_token() 

42 if word == lexer.eof: 

43 # TODO: check if this is ever unsafe 

44 # raise ArgcompleteException("Unexpected end of input") 

45 return "", "", "", words, None 

46 if lexer.instream.tell() >= point: 

47 debug("word", word, f"split, lexer state: '{lexer.state}'") 

48 return split_word(word) 

49 words.append(word) 

50 except ValueError: 

51 debug("word", lexer.token, f"split (lexer stopped, state: '{lexer.state}')") 

52 if lexer.instream.tell() >= point: 

53 return split_word(lexer.token) 

54 else: 

55 msg = ( 

56 "Unexpected internal state. " 

57 "Please report this bug at https://github.com/kislyuk/argcomplete/issues." 

58 ) 

59 raise ArgcompleteException(msg)