Skip to content

Commit

Permalink
Merge pull request #576 from tweag/572-scope-prepend-and-append
Browse files Browse the repository at this point in the history
Create append/prepend versions of scope captures
  • Loading branch information
Erin van der Veen authored Jul 13, 2023
2 parents fc03b3f + f63b6fb commit d4dbf3f
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 216 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This name should be decided amongst the team before the release.
### Changed
* [#535](https://github.com/tweag/topiary/pull/535) Improved error message when idempotency fails due to invalid output in the first pass.
* [#533](https://github.com/tweag/topiary/pull/533) Update tree-sitter-ocaml to 0.20.3
* [#576](https://github.com/tweag/topiary/pull/576) Allows prepending/appending `@begin_scope` and `@end_scope`

## v0.2.3 - Cyclic Cypress - 2023-06-20

Expand Down
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -833,18 +833,17 @@ top-level node `product_expression (0, 1) - (1, 1)` is multi-line.

To solve this issue, we introduce user-defined scopes and softlines.

#### `@begin_scope` / `@end_scope`
#### `@prepend_begin_scope` / `@append_begin_scope` / `@prepend_end_scope` / `@append_end_scope`

`@begin_scope` and `@end_scope` tags are used to define custom scopes.
In conjunction with the `#scope_id!` predicate, they define scopes that
can span multiple CST nodes, or only part of one. For instance, this
scope matches anything between parenthesis in a
These tags are used to define custom scopes. In conjunction with the `#scope_id!
` predicate, they define scopes that can span multiple CST nodes, or only part
of one. For instance, this scope matches anything between parenthesis in a
`parenthesized_expression`:

```scheme
(parenthesized_expression
"(" @begin_scope
")" @end_scope
"(" @append_begin_scope
")" @prepend_end_scope
(#scope_id! "tuple")
)
```
Expand Down
40 changes: 20 additions & 20 deletions topiary/languages/nickel.scm
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@
[
(infix_expr)
(annotated_infix_expr)
] @begin_scope
) @end_scope
] @prepend_begin_scope
) @append_end_scope

(infix_expr
(#scope_id! "infix_chain")
Expand Down Expand Up @@ -169,9 +169,9 @@

(_
(#scope_id! "bound_rhs")
"=" @begin_scope
"=" @prepend_begin_scope
.
(term) @end_scope
(term) @append_end_scope
)

(_
Expand Down Expand Up @@ -237,9 +237,9 @@
(let_expr
(#scope_id! "let_result")
(let_in_block
"in" @begin_scope @prepend_spaced_softline
"in" @prepend_begin_scope @prepend_spaced_softline
)
(term) @end_scope
(term) @append_end_scope
)

(let_expr
Expand All @@ -253,9 +253,9 @@
; coexist, so create a scope that covers them both.
(
(#scope_id! "annotated_assignment")
(annot) @begin_scope
(annot) @prepend_begin_scope
.
"=" @end_scope
"=" @append_end_scope
)

; Start an indentation block from the start of the annotations to the
Expand All @@ -274,9 +274,9 @@
; id | a -> a
(
(#scope_id! "annotations")
(_) @begin_scope
(_) @prepend_begin_scope
.
(annot) @end_scope
(annot) @append_end_scope
)

; Put each annotation -- and the equals sign, if it follows annotations
Expand All @@ -303,8 +303,8 @@
; This also defines an indentation block.
(fun_expr
(#scope_id! "function_definition")
"=>" @begin_scope @append_indent_start
) @append_indent_end @end_scope
"=>" @prepend_begin_scope @append_indent_start
) @append_indent_end @append_end_scope

(fun_expr
(#scope_id! "function_definition")
Expand All @@ -319,8 +319,8 @@
; like the operands to start on their own line, each indented.
(infix_expr
(#scope_id! "applicative_chain")
(applicative) @begin_scope
) @end_scope
(applicative) @prepend_begin_scope
) @append_end_scope

(
(#scope_id! "applicative_chain")
Expand Down Expand Up @@ -387,9 +387,9 @@
(_
(#scope_id! "container")
.
"{" @append_spaced_softline @append_indent_start @begin_scope
"{" @append_spaced_softline @append_indent_start @prepend_begin_scope
(_)
"}" @prepend_indent_end @prepend_spaced_softline @end_scope
"}" @prepend_indent_end @prepend_spaced_softline @append_end_scope
.
)

Expand All @@ -399,9 +399,9 @@
(_
(#scope_id! "container")
.
"[" @append_empty_softline @append_indent_start @begin_scope
"[" @append_empty_softline @append_indent_start @prepend_begin_scope
(_)
"]" @prepend_indent_end @prepend_empty_softline @end_scope
"]" @prepend_indent_end @prepend_empty_softline @append_end_scope
.
)

Expand All @@ -410,8 +410,8 @@
(_
(#scope_id! "container")
.
"[|" @append_spaced_softline @append_indent_start @begin_scope
"|]" @prepend_indent_end @prepend_spaced_softline @end_scope
"[|" @append_spaced_softline @append_indent_start @prepend_begin_scope
"|]" @prepend_indent_end @prepend_spaced_softline @append_end_scope
.
)

Expand Down
52 changes: 26 additions & 26 deletions topiary/languages/ocaml.scm
Original file line number Diff line number Diff line change
Expand Up @@ -754,11 +754,11 @@
; in
; bar
(let_binding
"=" @begin_scope
"=" @prepend_begin_scope
.
(fun_expression
"->" @append_spaced_scoped_softline
) @end_scope
) @append_end_scope
(#scope_id! "fun_definition")
)

Expand All @@ -778,11 +778,11 @@
; in
; bar
(let_binding
"=" @begin_scope
"=" @prepend_begin_scope
.
(function_expression
"function" @append_spaced_scoped_softline
) @end_scope
) @append_end_scope
(#scope_id! "function_definition")
)
; The following is the general case, which should happen anywhere except in let bindings
Expand All @@ -791,7 +791,7 @@
.
(function_expression
"function" @append_spaced_scoped_softline
) @begin_scope @end_scope
) @prepend_begin_scope @append_end_scope
(#scope_id! "function_definition")
)
(function_expression
Expand Down Expand Up @@ -935,17 +935,17 @@
(field_declaration)
(attribute)
(comment)
]? @end_scope
]? @append_end_scope
.
(field_declaration) @begin_scope
(field_declaration) @prepend_begin_scope
)
(record_declaration
(#scope_id! "field_declaration")
[
(field_declaration)
(attribute)
(comment)
] @end_scope
] @append_end_scope
.
"}"
)
Expand Down Expand Up @@ -977,17 +977,17 @@
(field_expression)
(attribute)
(comment)
]? @end_scope
]? @append_end_scope
.
(field_expression) @begin_scope
(field_expression) @prepend_begin_scope
)
(record_expression
(#scope_id! "field_expression")
[
(field_expression)
(attribute)
(comment)
] @end_scope
] @append_end_scope
.
"}"
)
Expand Down Expand Up @@ -1151,7 +1151,7 @@
[
"="
"+="
] @begin_scope @append_spaced_scoped_softline
] @prepend_begin_scope @append_spaced_scoped_softline
.
[
(constructed_type)
Expand All @@ -1164,7 +1164,7 @@
(type_constructor_path)
(type_variable)
(variant_declaration)
] @end_scope
] @append_end_scope
(#scope_id! "type_binding_before_constraint")
)

Expand Down Expand Up @@ -1269,8 +1269,8 @@
)
(let_binding
.
(_) @begin_scope
"=" @end_scope
(_) @prepend_begin_scope
"=" @append_end_scope
(#scope_id! "let_binding_before_equal")
)
(let_binding
Expand Down Expand Up @@ -1299,8 +1299,8 @@
)
(fun_expression
.
"fun" @begin_scope
"->" @end_scope
"fun" @prepend_begin_scope
"->" @append_end_scope
(#scope_id! "fun_expr_before_arrow")
)
(fun_expression
Expand Down Expand Up @@ -1341,7 +1341,7 @@
; We only want to define a scope around the outermost `product_expression`,
; which is the one that *isn't* followed by a comma.
(
(product_expression) @begin_scope @end_scope
(product_expression) @prepend_begin_scope @append_end_scope
.
","? @do_nothing
(#scope_id! "tuple")
Expand Down Expand Up @@ -1375,7 +1375,7 @@
(
"->"? @do_nothing
.
(function_type) @begin_scope @end_scope
(function_type) @prepend_begin_scope @append_end_scope
(#scope_id! "function_type")
)
(function_type
Expand All @@ -1398,7 +1398,7 @@
(or_operator)
]? @do_nothing
.
(infix_expression) @begin_scope @end_scope
(infix_expression) @prepend_begin_scope @append_end_scope
(#scope_id! "infix_expression")
)
(infix_expression
Expand Down Expand Up @@ -1440,7 +1440,7 @@
";"
.
"%"? @do_nothing
) @begin_scope @end_scope
) @prepend_begin_scope @append_end_scope
(#scope_id! "sequence_expression")
)
(sequence_expression
Expand All @@ -1462,7 +1462,7 @@
";"
.
"%"
) @begin_scope @end_scope
) @prepend_begin_scope @append_end_scope
(#scope_id! "ppx_sequence_expression")
)
(sequence_expression
Expand Down Expand Up @@ -1519,17 +1519,17 @@
; let foo = x
; end
(module_binding
(module_name) @append_indent_start @begin_scope
"=" @prepend_empty_scoped_softline @prepend_indent_end @end_scope
(module_name) @append_indent_start @prepend_begin_scope
"=" @prepend_empty_scoped_softline @prepend_indent_end @append_end_scope
(#scope_id! "module_binding_before_equal")
)
; if a module binding has no equal sign and isn't just a signature, everything enters the scope
(module_binding
(#scope_id! "module_binding_before_equal")
(module_name) @append_indent_start @begin_scope
(module_name) @append_indent_start @prepend_begin_scope
"="? @do_nothing
(signature)? @do_nothing
) @append_indent_end @end_scope
) @append_indent_end @append_end_scope
(module_binding
(module_name) @append_empty_scoped_softline
(module_parameter) @prepend_spaced_scoped_softline
Expand Down
10 changes: 5 additions & 5 deletions topiary/languages/ocamllex.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

(
(#scope_id! "action")
(action) @begin_scope @end_scope
(action) @prepend_begin_scope @append_end_scope
)

; If the action spanned multiple lines, add newlines
Expand Down Expand Up @@ -43,7 +43,7 @@
(_) @append_empty_scoped_softline
.
"]" @prepend_indent_end @prepend_antispace
) @begin_scope @end_scope
) @prepend_begin_scope @append_end_scope

(aliased_regexp
"as" @prepend_space @append_space
Expand All @@ -53,11 +53,11 @@
(#scope_id! "parenthesized_regexp")
"(" @append_empty_scoped_softline @append_indent_start
")" @prepend_empty_scoped_softline @prepend_indent_end
) @begin_scope @end_scope @prepend_spaced_softline @append_spaced_softline
) @prepend_begin_scope @append_end_scope @prepend_spaced_softline @append_spaced_softline

(
(#scope_id! "regexp_alternative")
(regexp_alternative) @begin_scope @end_scope
(regexp_alternative) @prepend_begin_scope @append_end_scope
)

(regexp_alternative
Expand All @@ -81,7 +81,7 @@
(#scope_id! "lexer_entry")
"=" @prepend_space @append_space
["parse" "shortest"] @append_spaced_scoped_softline @append_indent_start
) @begin_scope @end_scope
) @prepend_begin_scope @append_end_scope

(lexer_entry
(lexer_argument) @prepend_space @append_space
Expand Down
Loading

0 comments on commit d4dbf3f

Please sign in to comment.