Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » ListRewrite removes whitespace(ListRewrite removes whitespace when removing a modifier but I don't want to do that)
ListRewrite removes whitespace [message #1011931] Wed, 20 February 2013 09:32 Go to next message
Tas Frangoullides is currently offline Tas Frangoullides
Messages: 16
Registered: July 2009
Junior Member
I'm implementing a shortcut for toggling the final modifier.

Where a final modifier is present I remove it by using a ListRewrite

    ASTRewrite rewrite = ASTRewrite.create(ast);
    ListRewrite listRewrite = rewrite.getListRewrite(declaration, declaration.getModifiersProperty());
    ....
    listRewrite.remove(finalModifier);


However when there are modifiers or annotation on the previous line I get unwanted removal of the carriage return.

E.g.
	@Override
	final void myMethod() {

	}


becomes

	@Override void myMethod() {

	}




Is there a way to preserve the white space or ensure that formatting rules are applied to the list of modifiers?

Thanks,
Tas
Re: ListRewrite removes whitespace [message #1012498 is a reply to message #1011931] Thu, 21 February 2013 12:05 Go to previous messageGo to next message
Tas Frangoullides is currently offline Tas Frangoullides
Messages: 16
Registered: July 2009
Junior Member
I thought I might have found the solution when I came across setTargetSourceRangeComputer(..) on ASTRewrite. I thought that maybe the whitespace was being considered part of the final modifier's AST node. However when I passed my own implementation I noticed that a correct length of 5 was already being used.

I've taken a look at the text edits return by the rewrite and I get the following
{MultiTextEdit} [53,8] [undefined]
{DeleteEdit} [53,3]
{DeleteEdit} [56,5]

It seems that ASTRewrite has distinctly determined that the whitespace also needs to be deleted ([56,5] is 'final').

I'll dig into ASTRewrite to see if I can understand why it does this. In the meantime if anyone has any suggestions they'd be very welcome.

Thanks,
Tas
Re: ListRewrite removes whitespace [message #1016645 is a reply to message #1012498] Thu, 07 March 2013 04:08 Go to previous messageGo to next message
Tas Frangoullides is currently offline Tas Frangoullides
Messages: 16
Registered: July 2009
Junior Member
I've not been able to find a suitable way around this and I'm thinking it's a bug in the ASTRewrite which means that formatting rules are not being applied properly for modifiers.

Are there any JDT developers on this group that can confirm this before I go opening a bug?

Thanks,
Tas
Re: ListRewrite removes whitespace [message #1016793 is a reply to message #1016645] Thu, 07 March 2013 12:24 Go to previous messageGo to next message
Stephan Herrmann is currently online Stephan Herrmann
Messages: 982
Registered: July 2009
Senior Member
which formatter rule do you see violated by this rewrite?
Specifically, what are your settings in the New Lines > Annotations setting of your formatter profile?
Re: ListRewrite removes whitespace [message #1017033 is a reply to message #1016793] Fri, 08 March 2013 12:15 Go to previous messageGo to next message
Tas Frangoullides is currently offline Tas Frangoullides
Messages: 16
Registered: July 2009
Junior Member
I've been using the built-in default for testing. Under annotations this has insert new life after everything except parameters.

If I have the following
	@Override
	protected void myMethod() {
	}

and use ASTRewrite to remove the visibility modifier I get this:
	@Override void myMethod() {
	}


If I then run Source->Format I get

	@Override
	void myMethod() {
	}

[Updated on: Fri, 08 March 2013 12:16]

Report message to a moderator

Re: ListRewrite removes whitespace [message #1018788 is a reply to message #1017033] Thu, 14 March 2013 09:25 Go to previous message
Tas Frangoullides is currently offline Tas Frangoullides
Messages: 16
Registered: July 2009
Junior Member
Sounding like a bug? Anyone?
Previous Topic:Improvement proposal for debugger
Next Topic:How does "Open Declaration" work?
Goto Forum:
  


Current Time: Tue Jul 29 13:15:06 EDT 2014

Powered by FUDForum. Page generated in 0.01775 seconds