Skip to content

Commit

Permalink
More flexible coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasaglia committed Oct 20, 2024
1 parent 2c1e66d commit a5f5abc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/specs/shapes.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def polystar(shape: Bezier, p: Vector2D, pt: float, r: float, or_: float, os: fl
shape.add_vertex(p + v_in)

if is_ != 0 and ir != 0:
# we need to add bezier tangents
# We need to add bezier tangents
tan_in = v_in * tan_len_in / ir
shape.set_in_tangent(Vector2D(-tan_in.y, tan_in.x))
shape.set_out_tangent(Vector2D(tan_in.y, -tan_in.x))
Expand Down
5 changes: 2 additions & 3 deletions tools/code_processing/langs/ts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ast
from ..python_source import CLike, Range, snake_to_lower_camel
from ..python_source import CLike, Range, snake_to_lower_camel, KandRStyle


class TypeScriptTranslator(CLike):
Expand All @@ -11,9 +11,8 @@ class TypeScriptTranslator(CLike):
keywords = {"in"}

def __init__(self, type_annotations=True):
super().__init__()
super().__init__(KandRStyle())
self.type_annotations = type_annotations
self.brace_newline = False

def function_def(self, name, args, returns, body, is_async, is_method, is_getter):
start = ""
Expand Down
109 changes: 80 additions & 29 deletions tools/code_processing/python_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ def __exit__(self, *a):

class SourceCode:
def __init__(self, source):
self.source = source
self.comments = CommentData(source)
self.ast = ast.parse(source, type_comments=True)
if isinstance(source, ast.AST):
self.source = ast.unparse(source)
self.ast = ast
self.comments = CommentData("")
else:
self.source = source
self.comments = CommentData(source)
self.ast = ast.parse(source, type_comments=True)


class CommentData:
Expand Down Expand Up @@ -155,8 +160,8 @@ def __str__(self):


class AstTranslator:
def __init__(self):
self.indent_spaces = 4
def __init__(self, indent_style=None):
self.indent_style = indent_style or IndentationStyle()
self.indent_level = 0
self.output = ""
self.class_name = None
Expand Down Expand Up @@ -193,9 +198,8 @@ def maybe_range(self, ast_node):
return Range(ast_node, self)
return self.expression_to_string(ast_node, False)

@property
def indent(self):
return (self.indent_level * self.indent_spaces) * " "
def indent_string(self, offset=0):
return self.indent_style.indent_string(self.indent_level + offset)

def unknown(self, obj, as_string=False):
# breakpoint()
Expand All @@ -211,19 +215,19 @@ def process_line_comments(self):
if comment is None:
self.output += "\n"
else:
self.output += self.indent + self.convert_line_comment(comment) + "\n"
self.output += self.indent_string() + self.convert_line_comment(comment) + "\n"
self.comments.next()

def find_else(self, lineno):
self.comments.current_line = lineno

def push_code(self, code, line_comments=True):
def push_code(self, code, line_comments=True, indent_offset=0):
if line_comments:
self.process_line_comments()
if self.comments.has_else():
self.comments.next()

self.output += self.indent + code
self.output += self.indent_string(indent_offset) + code

if self.comments.has_tail_comment():
self.output += " " + self.convert_line_comment(self.comments.get_comment())
Expand Down Expand Up @@ -528,6 +532,66 @@ def expr_starred(self, value):
raise NotImplementedError


class IndentationStyle:
def __init__(self, spaces=4, character=" "):
self.spaces = spaces
self.character = character

def indent_string(self, level):
return int(level * self.spaces) * self.character

def begin_block(self, translator: AstTranslator, header: str):
raise NotImplementedError

def mid_block(self, translator: AstTranslator, header: str):
raise NotImplementedError

def end_block(self, translator: AstTranslator):
raise NotImplementedError


class AllmanStyle(IndentationStyle):
def __init__(self, offset=0, *a, **kw):
super().__init__(*a, **kw)
self.offset = offset

def begin_block(self, translator: AstTranslator, header: str):
translator.push_code(header)
translator.push_code("{", False, self.offset)
translator.comments.skip_newline()

def mid_block(self, translator: AstTranslator, header: str):
translator.comments.skip_newline()
translator.push_code("}", True, self.offset)
translator.push_code(header, False)
translator.push_code("{", False, self.offset)
translator.comments.skip_newline()

def end_block(self, translator: AstTranslator):
translator.push_code("}", True, self.offset)


class KandRStyle(IndentationStyle):
def begin_block(self, translator: AstTranslator, header: str):
translator.push_code(header + " {")

def mid_block(self, translator: AstTranslator, header: str):
translator.push_code("} " + header + " {")

def end_block(self, translator: AstTranslator):
translator.push_code("}")


class WhitesmithsStyle(AllmanStyle):
def __init__(self, *a, **kw):
super().__init__(1, *a, **kw)


class GnuStyle(AllmanStyle):
def __init__(self, *a, **kw):
super().__init__(0.5, *a, **kw)


class CLike(AstTranslator):
ops = {
"Eq": "==",
Expand Down Expand Up @@ -562,27 +626,14 @@ class CLike(AstTranslator):
}
keywords = []

def __init__(self):
super().__init__()
self.brace_newline = True
def __init__(self, indent_style=AllmanStyle()):
super().__init__(indent_style)

def begin_block(self, header):
if self.brace_newline:
self.push_code(header)
self.push_code("{", False)
self.comments.skip_newline()
else:
self.push_code(header + " {")
self.indent_style.begin_block(self, header)

def mid_block(self, header):
if self.brace_newline:
self.comments.skip_newline()
self.push_code("}")
self.push_code(header, False)
self.push_code("{", False)
self.comments.skip_newline()
else:
self.push_code("} %s {" % header)
self.indent_style.mid_block(self, header)

def styled_name(self, id):
id = id.strip("_")
Expand All @@ -594,7 +645,7 @@ def begin_class(self, obj):
self.begin_block("class %s" % obj.name)

def end_block(self):
self.push_code("}")
self.indent_style.end_block(self)

def assign(self, targets, value):
code = " = ".join(targets)
Expand Down

0 comments on commit a5f5abc

Please sign in to comment.