Skip to content

Commit

Permalink
Merge pull request #37 from benber86/fix/autobribe
Browse files Browse the repository at this point in the history
Handle imported types in hashmap/array/dynarrays def
  • Loading branch information
benber86 authored Aug 26, 2024
2 parents 321c6c1 + 5d32824 commit 2217513
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ build-backend = "setuptools.build_meta"

[tool.poetry]
name = "mamushi"
version = "0.0.3"
version = "0.0.4-a0"
description = "Vyper Formatter"
authors = ["benny <benoitb@tutanota.de>"]

Expand Down
20 changes: 10 additions & 10 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[bumpversion]
current_version = 0.0.3
current_version = 0.0.4-a0
commit = False
tag = True
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+)(?P<build>\d+))?
serialize =
serialize =
{major}.{minor}.{patch}-{release}{build}
{major}.{minor}.{patch}

[options]
package_dir =
package_dir =
= src
packages = find:
python_requires = >= 3.9
install_requires =
install_requires =
click>=8.0.0
pathspec>=0.9.0
lark>=1.0.0
Expand All @@ -24,11 +24,11 @@ include_package_data = True
mamushi.parsing = *.lark

[options.entry_points]
console_scripts =
console_scripts =
mamushi=mamushi:main

[options.extras_require]
dev =
dev =
black==23.1.0
pre-commit==2.20.0
mypy_extensions==0.4.3
Expand All @@ -42,7 +42,7 @@ dev =

[options.packages.find]
where = src
exclude =
exclude =
tests

[metadata]
Expand All @@ -53,10 +53,10 @@ long_description = file: README.md
long_description_content_type = text/markdown
version = attr: mamushi.__version__.__version__
url = https://github.com/benber86/mamushi
project_urls =
project_urls =
Source = https://github.com/benber86/mamushi
Tracker = https://github.com/benber86/mamushi/issues
classifiers =
classifiers =
Development Status :: 4 - Beta
Environment :: Console
Intended Audience :: Developers
Expand All @@ -71,7 +71,7 @@ classifiers =

[bumpversion:part:release]
optional_value = release
values =
values =
a
b
release
Expand Down
2 changes: 1 addition & 1 deletion src/mamushi/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.3"
__version__ = "0.0.4-a0"
12 changes: 10 additions & 2 deletions src/mamushi/formatting/brackets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
Priority = int


COMPREHENSION_PRIORITY: Final = 18
COMMA_PRIORITY: Final = 16
COMPREHENSION_PRIORITY: Final = 20
COMMA_PRIORITY: Final = 18
TERNARY_PRIORITY: Final = 16
LOGIC_PRIORITY: Final = 14
STRING_PRIORITY: Final = 12
COMPARATOR_PRIORITY: Final = 10
Expand Down Expand Up @@ -225,6 +226,13 @@ def is_split_before_delimiter(
):
return COMPREHENSION_PRIORITY

if (
leaf.value in {"if", "else"}
and leaf.parent
and leaf.parent.type == tokens.TERNARY
):
return TERNARY_PRIORITY

if leaf.value in tokens.LOGIC_OPERATORS and leaf.parent:
return LOGIC_PRIORITY

Expand Down
6 changes: 5 additions & 1 deletion src/mamushi/formatting/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Tuple,
TypeVar,
Union,
cast,
)
from lark import Tree, Token
from dataclasses import dataclass, field
Expand All @@ -19,13 +20,13 @@
from mamushi.formatting.nodes import (
wrap_in_parentheses,
is_atom_with_invisible_parens,
ensure_visible,
)
from mamushi.formatting.strings import (
normalize_string_quotes,
is_multiline_string,
fix_docstring,
is_pragma,
remove_double_spaces,
)
from mamushi.parsing.pytree import Leaf, Node
from mamushi.formatting.lines import Line
Expand Down Expand Up @@ -106,6 +107,9 @@ def visit_default(self, node: LN) -> Iterator[Line]:
yield from self.line()
yield from super().visit_default(node)

def visit_external_call(self, node: Node) -> Iterator[Line]:
yield from self.visit_call(node)

def visit_DOCSTRING(self, node: Leaf) -> Iterator[Line]:
# ensure single/double quote consistency
docstring = normalize_string_quotes(node.value)
Expand Down
3 changes: 2 additions & 1 deletion src/mamushi/formatting/whitespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def whitespace(leaf: Leaf) -> str:
):
return NO

elif p.type == tokens.ATTRIBUTE:
elif p.type in {tokens.ATTRIBUTE, tokens.IMPORTED_TYPE}:
# variable access
if t == tokens.DOT or prev.type == tokens.DOT:
return NO
Expand All @@ -99,6 +99,7 @@ def whitespace(leaf: Leaf) -> str:
in {
tokens.FUNCTION_SIG,
tokens.CALL,
tokens.EXTERNAL_CALL,
tokens.EMPTY,
tokens.ABI_DECODE,
tokens.SUBSCRIPT,
Expand Down
9 changes: 4 additions & 5 deletions src/mamushi/parsing/grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ flag_body: _NEWLINE _INDENT (flag_member _NEWLINE)+ _DEDENT
flag_def: _FLAG_DECL NAME ":" flag_body

// Types
array_def: (NAME | array_def | dyn_array_def) "[" expr "]"
dyn_array_def: "DynArray" "[" (NAME | array_def | dyn_array_def) "," expr "]"
tuple_def: "(" ( NAME | array_def | dyn_array_def | tuple_def ) ( "," ( NAME | array_def | dyn_array_def | tuple_def ) )* [","] ")"
array_def: (NAME | imported_type | array_def | dyn_array_def) "[" expr "]"
dyn_array_def: "DynArray" "[" (NAME | imported_type | array_def | dyn_array_def) "," expr "]"
tuple_def: "(" ( NAME | imported_type | array_def | dyn_array_def | tuple_def ) ( "," ( NAME | imported_type | array_def | dyn_array_def | tuple_def ) )* [","] ")"
// NOTE: Map takes a basic type and maps to another type (can be non-basic, including maps)
_MAP: "HashMap"
map_def: _MAP "[" ( NAME | array_def ) "," type "]"
map_def: _MAP "[" ( NAME | imported_type | array_def ) "," type "]"
imported_type: NAME ("." NAME)+
type: ( NAME | imported_type | array_def | tuple_def | map_def | dyn_array_def )

Expand Down Expand Up @@ -220,7 +220,6 @@ dict: "{" "}" | "{" (NAME ":" expr) ("," (NAME ":" expr))* [","] "}"
?assignment_expr: ternary
| NAME ":=" assignment_expr
// ternary operator
?operation: bool_or
?ternary: bool_or
| ternary "if" ternary "else" ternary

Expand Down
3 changes: 3 additions & 0 deletions src/mamushi/parsing/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
ARRAY_DEF = "array_def"
DINARRAY_DEF = "dyn_array_def"
CALL = "call"
EXTERNAL_CALL = "external_call"
EMPTY = "empty"
ABI_DECODE = "abi_decode"
ATTRIBUTE = "attribute"
Expand All @@ -150,6 +151,7 @@
COND_EXEC = "cond_exec"
FOR_STMT = "for_stmt"
IF_STMT = "if_stmt"
TERNARY = "ternary"
FOR = "FOR"
IF = "IF"
IN = "_IN"
Expand All @@ -174,6 +176,7 @@
BREAK_STMT = "break_stmt"
CONTINUE_STMT = "continue_stmt"
INITIALIZES_STMT = "initializes_stmt"
IMPORTED_TYPE = "imported_type"
RAISE = "raise"
RAISE_WITH_REASON = "raise_with_reason"
IMMUTABLE_DEF = "immutable_def"
Expand Down
6 changes: 6 additions & 0 deletions tests/data/imports/imported_type_splitting.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
exampleMapping: HashMap[a_fairly_long_name.a_fairly_long_name, a_fairly_long_name.a_fairly_long_name]

# output
exampleMapping: HashMap[
a_fairly_long_name.a_fairly_long_name, a_fairly_long_name.a_fairly_long_name
]

0 comments on commit 2217513

Please sign in to comment.