Tiny formatter improvements

This commit is contained in:
gregorni 2023-12-19 02:29:09 +00:00 committed by James Westman
parent e261180dcc
commit e5cde71fc1
4 changed files with 155 additions and 166 deletions

View file

@ -44,7 +44,6 @@ class LineType(Enum):
COMMENT = 4 COMMENT = 4
class Formatter:
def format(data, tab_size=2, insert_space=True): def format(data, tab_size=2, insert_space=True):
indent_levels = 0 indent_levels = 0
tokens = tokenizer.tokenize(data) tokens = tokenizer.tokenize(data)
@ -95,25 +94,21 @@ class Formatter:
or (str_item == "<" and str(last_not_whitespace) == "typeof") or (str_item == "<" and str(last_not_whitespace) == "typeof")
) )
this_or_last_is_ident = ( this_or_last_is_ident = TokenType.IDENT in (item.type, last_not_whitespace.type)
item.type == TokenType.IDENT
or last_not_whitespace.type == TokenType.IDENT
)
current_line_is_empty = len(current_line) == 0 current_line_is_empty = len(current_line) == 0
is_function = str_item == "(" and not re.match( is_function = str_item == "(" and not re.match(
r"^([A-Za-z_\-])+(: bind)?$", current_line r"^([A-Za-z_\-])+(: bind)?$", current_line
) )
any_blockers = whitespace_blockers or current_line_is_empty or is_function any_blockers = whitespace_blockers or current_line_is_empty or is_function
if (whitespace_required or this_or_last_is_ident) and not any_blockers: if (whitespace_required or this_or_last_is_ident) and not any_blockers:
current_line += " " current_line += " "
current_line += str_item current_line += str_item
if str_item in ["[", "("]: if str_item in ("[", "("):
bracket_tracker.append(str_item) bracket_tracker.append(str_item)
elif str_item in ["]", ")"]: elif str_item in ("]", ")"):
bracket_tracker.pop() bracket_tracker.pop()
needs_newline_treatment = ( needs_newline_treatment = (
@ -128,19 +123,17 @@ class Formatter:
if is_child_type: if is_child_type:
if str(last_not_whitespace) not in OPENING_TOKENS: if str(last_not_whitespace) not in OPENING_TOKENS:
end_str = ( end_str = (
end_str.strip() end_str.strip() + "\n\n" + (indent_item * indent_levels)
+ "\n\n"
+ (indent_item * indent_levels)
) )
last_not_whitespace = item last_not_whitespace = item
continue continue
indent_levels += 1 indent_levels += 1
keep_same_indent = not prev_line_type in [ keep_same_indent = prev_line_type not in (
LineType.CHILD_TYPE, LineType.CHILD_TYPE,
LineType.COMMENT, LineType.COMMENT,
LineType.BLOCK_OPEN, LineType.BLOCK_OPEN,
] )
if keep_same_indent: if keep_same_indent:
end_str = ( end_str = (
end_str.strip() + "\n\n" + indent_item * (indent_levels - 1) end_str.strip() + "\n\n" + indent_item * (indent_levels - 1)
@ -165,22 +158,22 @@ class Formatter:
elif str_item == ";": elif str_item == ";":
line_type = LineType.STATEMENT line_type = LineType.STATEMENT
newlines = 1
if len(current_line) == 1: if len(current_line) == 1:
newlines = 0 newlines = 0
line_type = LineType.BLOCK_CLOSE line_type = LineType.BLOCK_CLOSE
elif prev_line_type == LineType.BLOCK_CLOSE: elif prev_line_type == LineType.BLOCK_CLOSE:
newlines = 2 newlines = 2
else:
newlines = 1
commit_current_line(line_type, newlines_before=newlines) commit_current_line(line_type, newlines_before=newlines)
elif item.type == TokenType.COMMENT: elif item.type == TokenType.COMMENT:
require_extra_newline = [ require_extra_newline = (
LineType.BLOCK_CLOSE, LineType.BLOCK_CLOSE,
LineType.STATEMENT, LineType.STATEMENT,
LineType.COMMENT, LineType.COMMENT,
] )
single_line_comment = str_item.startswith("//") single_line_comment = str_item.startswith("//")
newlines = 1 newlines = 1
@ -203,7 +196,7 @@ class Formatter:
commit_current_line() commit_current_line()
elif str_item == "(" and ( elif str_item == "(" and (
re.match("^([A-Za-z_\-])+\s*\(", current_line) or watch_parentheses re.match(r"^([A-Za-z_\-])+\s*\(", current_line) or watch_parentheses
): ):
watch_parentheses = True watch_parentheses = True
parentheses_balance += 1 parentheses_balance += 1
@ -213,9 +206,7 @@ class Formatter:
all_parentheses_closed = parentheses_balance == 0 all_parentheses_closed = parentheses_balance == 0
if all_parentheses_closed: if all_parentheses_closed:
commit_current_line( commit_current_line(
newlines_before=2 newlines_before=2 if prev_line_type == LineType.BLOCK_CLOSE else 1
if prev_line_type == LineType.BLOCK_CLOSE
else 1
) )
watch_parentheses = False watch_parentheses = False
@ -224,7 +215,7 @@ class Formatter:
last_in_tracker = bracket_tracker[-1] last_in_tracker = bracket_tracker[-1]
is_list_comma = last_in_tracker == "[" and str_item == "," is_list_comma = last_in_tracker == "[" and str_item == ","
if is_list_comma: if is_list_comma:
last_was_list_item = end_str.strip()[-1] not in ["[", ","] last_was_list_item = end_str.strip()[-1] not in ("[", ",")
if last_was_list_item: if last_was_list_item:
end_str = end_str.strip() end_str = end_str.strip()
commit_current_line() commit_current_line()

View file

@ -24,11 +24,10 @@ import traceback
import typing as T import typing as T
from difflib import SequenceMatcher from difflib import SequenceMatcher
from . import decompiler, parser, tokenizer, utils, xml_reader from . import decompiler, formatter, parser, tokenizer, utils, xml_reader
from .ast_utils import AstNode from .ast_utils import AstNode
from .completions import complete from .completions import complete
from .errors import CompileError, MultipleErrors from .errors import CompileError, MultipleErrors
from .formatter import Formatter
from .lsp_utils import * from .lsp_utils import *
from .outputs.xml import XmlOutput from .outputs.xml import XmlOutput
from .tokenizer import Token from .tokenizer import Token
@ -292,7 +291,7 @@ class LanguageServer:
return return
try: try:
formatted_blp = Formatter.format( formatted_blp = formatter.format(
open_file.text, open_file.text,
params["options"]["tabSize"], params["options"]["tabSize"],
params["options"]["insertSpaces"], params["options"]["insertSpaces"],

View file

@ -24,9 +24,8 @@ import os
import sys import sys
import typing as T import typing as T
from . import interactive_port, parser, tokenizer from . import formatter, interactive_port, parser, tokenizer
from .errors import CompileError, CompilerBugError, PrintableError, report_bug from .errors import CompileError, CompilerBugError, PrintableError, report_bug
from .formatter import Formatter
from .gir import add_typelib_search_path from .gir import add_typelib_search_path
from .lsp import LanguageServer from .lsp import LanguageServer
from .outputs import XmlOutput from .outputs import XmlOutput
@ -211,7 +210,7 @@ class BlueprintApp:
except: except:
errored = True errored = True
formatted_str = Formatter.format(data, opts.spaces_num, not opts.tabs) formatted_str = formatter.format(data, opts.spaces_num, not opts.tabs)
if data != formatted_str: if data != formatted_str:
happened = "Would format" happened = "Would format"

View file

@ -21,7 +21,7 @@
import unittest import unittest
from pathlib import Path from pathlib import Path
from blueprintcompiler.formatter import Formatter from blueprintcompiler import formatter
class TestFormatter(unittest.TestCase): class TestFormatter(unittest.TestCase):
@ -38,7 +38,7 @@ class TestFormatter(unittest.TestCase):
) as f: ) as f:
expected = f.read() expected = f.read()
actual = Formatter.format(input_data) actual = formatter.format(input_data)
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
def test_formatter(self): def test_formatter(self):