codecoinbot · 2022-05-09 09:56
kaushalmodi · 2022-05-09 10:50
Transposing words, bound by default to `M-t`, should work here. Bring the point to the right of the comma and hit `M-t`.
Phil-Hudson · 2022-05-09 12:45
In the same vein, if you have more complex arguments, for example wanting to swap the arguments to be interpolated in `(format tmpl (my-func arg) other-value)`, place point between `(my-func arg)` and `other-value` and type `C-M-t` (or `M-x transpose-sexps`).
nandryshak · 2022-05-09 14:27
Nope, that only works if the arguments are words. There is a `transpose-sexps` in `simple.el`, but that only works if the arguments are words or sexps. For complex function calls like this:
someFunction(foo.bar(), baz, qux())
You'll need to use something better. There is a solution here that works most of the time: https://emacs.stackexchange.com/a/11067/654
kaushalmodi · 2022-05-09 14:59
>Nope, that only works if the arguments are words.
Correct! That's why I chose to use the word "should" when I suggested it :)
`M-t` almost always works in "transpose the args" scenarios for me.
ieure · 2022-05-09 15:59
`C-M-t` runs `transpose-sexps`, which *should* work for this — but your idea of an what an expression is may differ in non-Lisp languages or their modes.
nandryshak · 2022-05-09 16:24
> M-t almost always works in "transpose the args" scenarios for me.
I feel like for most people `M-t` would almost always *fail*. If you had said `C-M-t` for `transpose-sexps` I might agree, but are your arguments really "almost always" only one word? It also doesn't work if an argument is a string.
Just looking through a few files at work it's hard to find two single words next to each other. It's mostly non-single-word symbol names (camel/snake case), or one or more of the arguments has field/method access, like `foo(request.data, object.get_something())`.
Using a `transpose-args` command also lets you not think about what type of thing the arguments are. You never have to think, "Would transpose-words work here? No, I need to use transpose-sexps. Actually wait that didn't work."
geza42 · 2022-05-10 07:51
I've been using `c-transpose-args` from here, seems to work correctly for functions (but not for c++ templates, but maybe it's easy to fix): [https://github.com/elbeno/dotemacs/blob/master/.emacs.d/cpp-modes.el](https://github.com/elbeno/dotemacs/blob/master/.emacs.d/cpp-modes.el)
(I think the original source is here: [https://emacs.stackexchange.com/a/47934](https://emacs.stackexchange.com/a/47934))
codecoinbot · 2022-05-10 10:14
I wonder if there is a way to hook into LSP and get the expression from there instead of dealing with regex and trying to guess what’s happening
nandryshak · 2022-05-10 12:33
That's a good thought, that or maybe tree sitter. I don't know anything about the internals of each, but I'll check it out this week because I've always wanted a reliable version.
codecoinbot · 2022-05-11 09:19
It seems there isn’t a generic way that always works M-t or C-M-t all fail in any slightly complex setting. The other custom functions rely on regex or custom rules instead of using LSP or tree sitter