shell-lite 0.4.3__py3-none-any.whl → 0.4.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
shell_lite/formatter.py DELETED
@@ -1,75 +0,0 @@
1
- from typing import List
2
- from .lexer import Lexer, Token
3
- class Formatter:
4
- def __init__(self, source_code: str):
5
- self.source_code = source_code
6
- self.indent_size = 4
7
- def format(self) -> str:
8
- lexer = Lexer(self.source_code)
9
- try:
10
- tokens = lexer.tokenize()
11
- except Exception:
12
- raise
13
- formatted_lines = []
14
- current_indent = 0
15
- current_line_tokens: List[Token] = []
16
- def flush_line():
17
- nonlocal current_line_tokens
18
- if not current_line_tokens:
19
- pass
20
- line_str = self._format_line_tokens(current_line_tokens, current_indent)
21
- formatted_lines.append(line_str)
22
- current_line_tokens.clear()
23
- for token in tokens:
24
- if token.type == 'EOF':
25
- if current_line_tokens:
26
- flush_line()
27
- break
28
- elif token.type == 'INDENT':
29
- current_indent += 1
30
- elif token.type == 'DEDENT':
31
- current_indent -= 1
32
- if current_indent < 0: current_indent = 0
33
- elif token.type == 'NEWLINE':
34
- flush_line()
35
- pass
36
- else:
37
- current_line_tokens.append(token)
38
- return '\n'.join(formatted_lines)
39
- def _format_line_tokens(self, tokens: List[Token], indent_level: int) -> str:
40
- if not tokens:
41
- return ''
42
- line_parts = []
43
- line_parts.append(' ' * (indent_level * self.indent_size))
44
- for i, token in enumerate(tokens):
45
- val = token.value
46
- type = token.type
47
- if type == 'STRING':
48
- if '"' in val and "'" not in val:
49
- val = f"'{val}'"
50
- else:
51
- val = val.replace('"', '\\"')
52
- val = f'"{val}"'
53
- elif type == 'REGEX':
54
- val = f"/{val}/"
55
- if i > 0:
56
- prev = tokens[i-1]
57
- need_space = True
58
- if prev.type in ('LPAREN', 'LBRACKET', 'LBRACE', 'DOT', 'AT'):
59
- need_space = False
60
- if type in ('RPAREN', 'RBRACKET', 'RBRACE', 'DOT', 'COMMA', 'COLON'):
61
- need_space = False
62
- if type == 'LPAREN':
63
- if prev.type == 'ID':
64
- need_space = False
65
- elif prev.type in ('RPAREN', 'RBRACKET', 'STRING'):
66
- need_space = False
67
- else:
68
- pass
69
- if type == 'LBRACKET':
70
- if prev.type in ('ID', 'STRING', 'RPAREN', 'RBRACKET'):
71
- need_space = False
72
- if need_space:
73
- line_parts.append(' ')
74
- line_parts.append(val)
75
- return "".join(line_parts).rstrip()