Comments (9)
I'm planning to implement foreach
option which will execute callback for each node. It'll allow transforming text and change brackets to do
or end
, e.t.c.
I think, this features will solve this case
from treesj.
I created draft PR (#68) what can handle block
and do_block
. Can you add more examples with same case, please?
If you know examples no only on ruby – it will be excellent.
from treesj.
One more ruby example:
Ruby: Block without arguments
Code
# Joined
10.times { puts "foo" }
# Split
10.times do
puts "foo"
end
Treesitter
call [0, 0] - [0, 23]
receiver: integer [0, 0] - [0, 2]
operator: "." [0, 2] - [0, 3]
method: identifier [0, 3] - [0, 8]
block: block [0, 9] - [0, 23]
"{" [0, 9] - [0, 10]
body: block_body [0, 11] - [0, 21]
call [0, 11] - [0, 21]
method: identifier [0, 11] - [0, 15]
arguments: argument_list [0, 16] - [0, 21]
string [0, 16] - [0, 21]
""" [0, 16] - [0, 17]
string_content [0, 17] - [0, 20]
""" [0, 20] - [0, 21]
"}" [0, 22] - [0, 23]
call [2, 0] - [4, 3]
receiver: integer [2, 0] - [2, 2]
operator: "." [2, 2] - [2, 3]
method: identifier [2, 3] - [2, 8]
block: do_block [2, 9] - [4, 3]
"do" [2, 9] - [2, 11]
body: body_statement [3, 2] - [3, 12]
call [3, 2] - [3, 12]
method: identifier [3, 2] - [3, 6]
arguments: argument_list [3, 7] - [3, 12]
string [3, 7] - [3, 12]
""" [3, 7] - [3, 8]
string_content [3, 8] - [3, 11]
""" [3, 11] - [3, 12]
"end" [4, 0] - [4, 3]
Python: multiline strings
As for other languages, the only similar constructs I can think of are single-line vs. multi-line strings. For example:
Code
# Joined
some_string = "I am\na\nmultiline\nstring"
# Split
some_string = """
I am
a
multiline
string
"""
Treesitter
expression_statement [0, 0] - [0, 42]
assignment [0, 0] - [0, 42]
left: identifier [0, 0] - [0, 11]
"=" [0, 12] - [0, 13]
right: string [0, 14] - [0, 42]
""" [0, 14] - [0, 15]
escape_sequence [0, 19] - [0, 21]
escape_sequence [0, 22] - [0, 24]
escape_sequence [0, 33] - [0, 35]
""" [0, 41] - [0, 42]
expression_statement [2, 0] - [7, 3]
assignment [2, 0] - [7, 3]
left: identifier [2, 0] - [2, 11]
"=" [2, 12] - [2, 13]
right: string [2, 14] - [7, 3]
""" [2, 14] - [2, 17]
""" [7, 0] - [7, 3]
Edit: @Wansmer I updated this comment to also contain the TS nodes
from treesj.
@Wansmer I found a case that appears to be incorrectly splitting:
It is not incorrectly: recursive
is false by default for split
.
It can be activated with:
block = u.set_preset_for_dict({
split = {
recursive = true,
recursive_ignore = { 'hash' }, -- not splitting hash
},
}),
from treesj.
Also, here is a snippet of code and its corresponding treesitter representation:
Code
x.each { |y| puts y }
x.each do |y|
puts y
end
Treesitter
call [0, 0] - [0, 21]
receiver: identifier [0, 0] - [0, 1]
operator: "." [0, 1] - [0, 2]
method: identifier [0, 2] - [0, 6]
block: block [0, 7] - [0, 21]
"{" [0, 7] - [0, 8]
parameters: block_parameters [0, 9] - [0, 12]
"|" [0, 9] - [0, 10]
identifier [0, 10] - [0, 11]
"|" [0, 11] - [0, 12]
body: block_body [0, 13] - [0, 19]
call [0, 13] - [0, 19]
method: identifier [0, 13] - [0, 17]
arguments: argument_list [0, 18] - [0, 19]
identifier [0, 18] - [0, 19]
"}" [0, 20] - [0, 21]
call [2, 0] - [4, 3]
receiver: identifier [2, 0] - [2, 1]
operator: "." [2, 1] - [2, 2]
method: identifier [2, 2] - [2, 6]
block: do_block [2, 7] - [4, 3]
"do" [2, 7] - [2, 9]
parameters: block_parameters [2, 10] - [2, 13]
"|" [2, 10] - [2, 11]
identifier [2, 11] - [2, 12]
"|" [2, 12] - [2, 13]
body: body_statement [3, 2] - [3, 8]
call [3, 2] - [3, 8]
method: identifier [3, 2] - [3, 6]
arguments: argument_list [3, 7] - [3, 8]
identifier [3, 7] - [3, 8]
"end" [4, 0] - [4, 3]
from treesj.
Awesome! Looking forward to it.
from treesj.
@Wansmer I found a case that appears to be incorrectly splitting:
Code (Ruby)
# Original
let(:some_array) do
[
{ bar: "bar" }
]
end
# Joined
let(:some_array) { [ { bar: "bar" } ] }
# Split again
let(:some_array) do
[ { bar: "bar" } ]
end
Expected: split should be reversible and not change the inner array ([]
)
from treesj.
Done in #68
from treesj.
This works fantastic. Thanks you!
from treesj.
Related Issues (20)
- Allow fallback when no node is found HOT 2
- Adds semicolon at the end of function declarations in Rust
- [BUG] Incorrect behaviour for toggling Ruby blocks HOT 2
- Join adds unnecessary space for empty Lua table HOT 1
- Join adds unnecessary commas and spaces, in Python HOT 2
- [Feature Request]: Toggle between <a></a> and <a /> (if tag is empty) in HTML/JSX etc. HOT 1
- Paranthesized expression support for Rust HOT 1
- Fix tests that fail
- module 'treesj' not found HOT 2
- FR: when on comment, just run `gqq` HOT 9
- Can only split/join/toggle once HOT 11
- Cant disable the preset keybindings. HOT 4
- Typescript: Split/Join some arrow functions results in invalid code HOT 1
- Error when Running Lua Config from README
- [TreeSJ]: Cannot join "table_constructor" containing node from one of this: { "commend" } HOT 3
- Allow ignoring the max join length for single calls HOT 1
- Fallback to `mini.splitjoin` HOT 2
- [golang] "The current node is already splitted" when attempting to split a multiline statement HOT 1
- JS/TS: split-join should support curly-less if statements HOT 2
- `dot_repeat` is not working for me
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from treesj.