Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option "Place trailing closure arguments after closing parenthesis" should not apply to constructor calls #1093

Closed
mauromol opened this issue Apr 21, 2020 · 6 comments
Assignees
Milestone

Comments

@mauromol
Copy link

If the option Place trailing closure arguments after closing parenthesis is checked and you try to write a constructor call with a closure as the last parameter, Greclipse pushes the opening brace outside the closing round parenthesis:

MyClass c = new MyClass('foo', |)

If you type { at "|", you'll get:

MyClass c = new MyClass('foo', ) {

Apart from the fact that the comma is left behind, this won't produce a valid result because:

MyClass c = new MyClass('foo') {
}

is seen as a creation of an anonymous in-place extension of MyClass rather than a call to MyClass constructor with trailing closure, in fact:

MyClass c = new MyClass('foo') { foo, bar ->
}

is seen as a syntax error ("unexpected token: foo").
For constructor calls, you can't use the syntax that puts the trailing closure parameters outside the round parenthesis, at least in Groovy 2.x (I don't know how it works in Groovy 3, but I think it would be ambiguous if it let do it).

@eric-milles
Copy link
Member

This behavior is controlled by this editor preference:
image

@mauromol
Copy link
Author

Oh, yes, you're right, I thougth it was the other option, which then I guess only controls generated code rather then live typing...

@eric-milles
Copy link
Member

ready to test

@mauromol
Copy link
Author

mauromol commented May 6, 2020

Tested with 3.8.0.v202005052348-e1912, there's still one broken case:

package test68
class GBean {
	public GBean(Closure c) {}
}
package test68

class Test68 {
	
	void foo() {
		def g = new GBean(|)
	}
}

Let "|" be the cursor position, hit "{". The result is:

def g = new GBean() {

which is not the most common behaviour, IMHO, which is instead to invoke GBean constructor passing in a closure.

@eric-milles
Copy link
Member

I think that is a separate issue. In the original example, there was a trailing comma that clearly indicates an upcoming expression. In the case of "new T()", it is difficult to separate intent to pass a closure from intent to create anon. inner class.

@mauromol
Copy link
Author

mauromol commented May 6, 2020

Well, to be honest the original issue was that when calling a constructor with a closure as its last parameter, the editor was moving the "{" outside the round parenthesis so that the final syntax is expressing something else (i.e.: an anonymous inner class creation). The presence of the trailing comma was an additional problem described while describing the former issue.

But if you prefer I'll open another report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants