{"action":"created","issue":{"url":"https://api.github.com/repos/rust-analyzer/rust-analyzer/issues/2227","repository_url":"https://api.github.com/repos/rust-analyzer/rust-analyzer","labels_url":"https://api.github.com/repos/rust-analyzer/rust-analyzer/issues/2227/labels{/name}","comments_url":"https://api.github.com/repos/rust-analyzer/rust-analyzer/issues/2227/comments","events_url":"https://api.github.com/repos/rust-analyzer/rust-analyzer/issues/2227/events","html_url":"https://github.com/rust-analyzer/rust-analyzer/issues/2227","id":522060867,"node_id":"MDU6SXNzdWU1MjIwNjA4Njc=","number":2227,"title":"Quasy quoting for assists","user":{"login":"matklad","id":1711539,"node_id":"MDQ6VXNlcjE3MTE1Mzk=","avatar_url":"https://avatars3.githubusercontent.com/u/1711539?v=4","gravatar_id":"","url":"https://api.github.com/users/matklad","html_url":"https://github.com/matklad","followers_url":"https://api.github.com/users/matklad/followers","following_url":"https://api.github.com/users/matklad/following{/other_user}","gists_url":"https://api.github.com/users/matklad/gists{/gist_id}","starred_url":"https://api.github.com/users/matklad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matklad/subscriptions","organizations_url":"https://api.github.com/users/matklad/orgs","repos_url":"https://api.github.com/users/matklad/repos","events_url":"https://api.github.com/users/matklad/events{/privacy}","received_events_url":"https://api.github.com/users/matklad/received_events","type":"User","site_admin":false},"labels":[{"id":818681174,"node_id":"MDU6TGFiZWw4MTg2ODExNzQ=","url":"https://api.github.com/repos/rust-analyzer/rust-analyzer/labels/E-hard","name":"E-hard","color":"86ef5d","default":false},{"id":825219390,"node_id":"MDU6TGFiZWw4MjUyMTkzOTA=","url":"https://api.github.com/repos/rust-analyzer/rust-analyzer/labels/fun","name":"fun","color":"d93f0b","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2019-11-13T09:13:41Z","updated_at":"2019-11-13T09:14:58Z","closed_at":null,"author_association":"MEMBER","body":"Assist (and, once we have more of those, fixits and refactorings) need to produce a lot of syntax trees.\r\n\r\nThis is done with the help of `make` module at the moment. Here's how a moderately-complex let statement is produced:\r\n\r\n```rust\r\nlet match_expr = {\r\n    let happy_arm = make::match_arm(\r\n        once(\r\n            make::tuple_struct_pat(\r\n                path,\r\n                once(make::bind_pat(make::name(\"it\")).into()),\r\n            )\r\n            .into(),\r\n        ),\r\n        make::expr_path(make::path_from_name_ref(make::name_ref(\"it\"))).into(),\r\n    );\r\n\r\n    let sad_arm = make::match_arm(\r\n        once(make::placeholder_pat().into()),\r\n        early_expression.into(),\r\n    );\r\n\r\n    make::expr_match(cond_expr, make::match_arm_list(vec![happy_arm, sad_arm]))\r\n};\r\n\r\nlet let_stmt = make::let_stmt(\r\n    make::bind_pat(make::name(&bound_ident.syntax().to_string())).into(),\r\n    Some(match_expr.into()),\r\n);\r\n```\r\n\r\nIt would be sweet if the above code could be condensed to the following:\r\n\r\n```rust\r\nlet let_stmt = magic! {\r\n    let $bound_ident = match $cond_expr {\r\n        $path(it) => it,\r\n        _ => $early_expression,\r\n    }\r\n};\r\n```\r\n\r\nThere exists a trivial solution: use `format!`, produce a string, re-parse string back into an AST. This actually works great! This is the solution employed by IntelliJ Rust([example](https://github.com/intellij-rust/intellij-rust/blob/ba41a47c170ac0d912eafd70646f00549ac0c8b7/src/main/kotlin/org/rust/ide/intentions/IfLetToMatchIntention.kt#L56-L75)), and I don't remember having any problems with it. \r\n\r\nHowever, adding more type-safety seems like a good thing to try! So, the `magic!` macro should ideally produce the code above, and not some formatting-based thing."},"comment":{"url":"https://api.github.com/repos/rust-analyzer/rust-analyzer/issues/comments/553310835","html_url":"https://github.com/rust-analyzer/rust-analyzer/issues/2227#issuecomment-553310835","issue_url":"https://api.github.com/repos/rust-analyzer/rust-analyzer/issues/2227","id":553310835,"node_id":"MDEyOklzc3VlQ29tbWVudDU1MzMxMDgzNQ==","user":{"login":"matklad","id":1711539,"node_id":"MDQ6VXNlcjE3MTE1Mzk=","avatar_url":"https://avatars3.githubusercontent.com/u/1711539?v=4","gravatar_id":"","url":"https://api.github.com/users/matklad","html_url":"https://github.com/matklad","followers_url":"https://api.github.com/users/matklad/followers","following_url":"https://api.github.com/users/matklad/following{/other_user}","gists_url":"https://api.github.com/users/matklad/gists{/gist_id}","starred_url":"https://api.github.com/users/matklad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matklad/subscriptions","organizations_url":"https://api.github.com/users/matklad/orgs","repos_url":"https://api.github.com/users/matklad/repos","events_url":"https://api.github.com/users/matklad/events{/privacy}","received_events_url":"https://api.github.com/users/matklad/received_events","type":"User","site_admin":false},"created_at":"2019-11-13T09:14:58Z","updated_at":"2019-11-13T09:14:58Z","author_association":"MEMBER","body":"Random thoughts:\r\n\r\n* handing of whitespace is a known unknown\r\n* if we fail at fully type-safe solution, we might resort to a build string & reparse solution, with an additional runtime check that each interpolated value preserves it's kind after a reparse. "}}
{"actor":{"display_login":"matklad"}}