Desired Editing Feature [message #120614] |
Mon, 24 November 2003 22:09  |
Eclipse User |
|
|
|
Originally posted by: dcorbin.machturtle.com
Is there an editing feature that will slap braces around the selection, and
indent the block contents if appropriate?
David
|
|
|
|
|
|
|
|
Re: Desired Editing Feature [message #122748 is a reply to message #121745] |
Thu, 27 November 2003 13:03   |
Eclipse User |
|
|
|
Originally posted by: eclipse-news.forritan.net
David Corbin wrote:
> Eyðun Nielsen wrote:
>
>
>>It's actually already there.
>>
>>Example:
>>
>>You have this:
>>
>>...
>>b();
>
>
> No, I don't. I already HAVE the if statement. I simply wish to turn a
> single statement into a blockstatement.
>
>
Oh, sorry - I think I misunderstood you...
Well, I took the challange... ;-) It's actually quite easy to implement.
(I have never worked with the internals of jdt before - and I came up
with this after a couple of hours today)
1. In org.eclipse.jdt.internal.ui.text.correction.QuickAssistProce ssor
make a new method:
<code>
private boolean getAddThenProposals(
IInvocationContext context,
ASTNode node,
Collection resultingCollections)
throws CoreException {
Statement statement= ASTResolving.findParentStatement(node);
if (!(statement instanceof IfStatement)) {
return false;
}
IfStatement ifStatement= (IfStatement) statement;
if (ifStatement.getThenStatement() instanceof Block) {
return false;
}
if (resultingCollections == null) {
return true;
}
AST ast= statement.getAST();
ASTRewrite rewrite= new ASTRewrite(statement);
ASTNode thenStatement=
ASTNode.copySubtree(ast, ifStatement.getThenStatement());
Block replacingBody= ast.newBlock();
replacingBody.statements().add(thenStatement);
rewrite.markAsInsert(
ifStatement,
ASTNodeConstants.THEN_STATEMENT,
replacingBody,
null);
String label=
CorrectionMessages.getString("QuickAssistProcessor.addthenblock.description ");
//$NON-NLS-1$
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD);
ASTRewriteCorrectionProposal proposal=
new ASTRewriteCorrectionProposal(
label,
context.getCompilationUnit(),
rewrite,
1,
image);
proposal.ensureNoModifications();
resultingCollections.add(proposal);
return true;
}
</code>
and make appropriate calls to this new method in
hasAssists(IInvocationContext)
and in
getAssists(IInvocationContext context, IProblemLocation[] locations)
also in org.eclipse.jdt.internal.ui.text.correction.QuickAssistProce ssor
2. add one line in
org.eclipse.jdt.internal.ui.text.correction.CorrectionMessag es.properties:
<code>
QuickAssistProcessor.addthenblock.description=Add then block
</code>
And thats it... Compile and be happy... ;-)
Regards,
-eyðun
|
|
|
|
|
|
|
Re: Desired Editing Feature [message #122852 is a reply to message #122840] |
Fri, 28 November 2003 08:35  |
Eclipse User |
|
|
|
Originally posted by: eclipse-news.forritan.net
David Corbin wrote:
> Thanks. But it looks like you took my request VERY litterally, and it's
> limited to if statements. I think the same should be doable for while and
> for statements.
Well, that only shows how carefull you should be when you make a wish!!! ;-)
>
> I'm pretty sure that covers all the cases where blocks are optional, short
> of just adding blocks for scoping sakes.
Well you are in luck, because so did Martin Aeschlimann (who committed
the code into the stream) :-)
*
*
*
Bugzilla Bug 47657:
Quick Assist Suggestion: add block to single-line then-statement
https://bugs.eclipse.org/bugs/show_bug.cgi?id=47657
*
------- Additional Comment #2 From Martin Aeschlimann 2003-11-28 05:07
-------
nice! I added the code and made some changes to complete it:
- added support for else block; while, do and for statements
- use 'createMove()' instead of ASTNode.copySubtree (otherwise you loose
all
formatting and comments of the body statement.
you can submit changes as a patch (in the TEAM menu), that's the most
easiest
way for me to take fixes.
Thanks a lot!
*
*
*
>
> Davdi
>
> Eyðun Nielsen wrote:
>
>
>>And FIXED as from 3.0 M6 ;-)
>>
>>Eyðun Nielsen wrote:
>>
>>>Done.
>>>
>>>Bugzilla Bug 47657:
>>>Quick Assist Suggestion: add block to single-line then-statement
>>>
>>>https://bugs.eclipse.org/bugs/show_bug.cgi?id=47657
>>>
>>>:-)
>>>-eyðun
>>>
>>>Tom Eicher wrote:
>>>
>>>
>>>>Eyðun Nielsen wrote:
>>>>
>>>>
>>>>>And thats it... Compile and be happy...
>>>>
>>>>
>>>>
>>>>... and attach your patch to a PR and make everyone else happy...
>>>>
>>>>;-) tom
>>>>
>>>
>
|
|
|
Powered by
FUDForum. Page generated in 0.06068 seconds