Difference between revisions of "FAQ How can Content Assist make me the fastest coder ever?"

From Eclipsepedia

Jump to: navigation, search
 
(formatting)
 
(One intermediate revision by one user not shown)
Line 12: Line 12:
  
 
* <i>Finding a given type</i>.
 
* <i>Finding a given type</i>.
    Assume that you are writing
+
Assume that you are writing some code and want to use a button in your UI. You start typing the letter
    some code and want to use a button in your UI. You start typing the letter
+
&#147;B&#148; and don&#146;t remember the rest of the word. Simply press Ctrl+Space,
    &#147;B&#148; and don&#146;t remember the rest of the word. Simply press Ctrl+Space,
+
and the Java tooling will present you with a list of all types that start with the
    and the Java tooling will present you with a list of all types that start with the
+
letter &#147;B.&#148; The list starts with <tt>Boolean</tt>. Keep typing, and the list narrows
    letter &#147;B.&#148; The list starts with <tt>Boolean</tt>. Keep typing, and the list narrows
+
down. After typing <tt>But</tt>, you get to choose between  
    down. After typing <tt>But</tt>, you get to choose between  
+
<tt>java.awt.Button</tt> and <tt>org.eclipse.swt.widgets.Button</tt>.  
    <tt>java.awt.Button</tt> and <tt>org.eclipse.swt.widgets.Button</tt>.  
+
Choose the one you like, and the editor  inserts
    Choose the one you like, and the editor  inserts
+
the class <i>and</i> inserts an import statement for the class at the same time.
    the class <i>and</i> inserts an import statement for the class at the same time.
+
</li>
+
  
  
  
* <i>Finding a given field or method</i>. After typing a dot after a certain
 
    expression, Content Assist will suggest all possible fields and methods
 
    applicable to the expression&#146;s result type. This functionality is very useful
 
    for discovering what operations can be applied to a given object. Combined with
 
    pervasive use of getters and setters, browsing an API is really
 
    simple. For the <tt>Button</tt> example, continuations for <tt>get</tt> show
 
    all attributes that can be obtained from a button. The ones starting with
 
    <tt>set</tt> show the attributes that can be modified. Another frequent prefix used
 
    while writing plug-ins is <tt>add</tt> to add event listeners. Having Content Assist at
 
    your fingertips definitely improves coding speed by combining intuition
 
    with content-assisted browsing.
 
</li>
 
  
 +
* <i>Finding a given field or method</i>.
 +
After typing a dot after a certain
 +
expression, Content Assist will suggest all possible fields and methods
 +
applicable to the expression&#146;s result type. This functionality is very useful
 +
for discovering what operations can be applied to a given object. Combined with
 +
pervasive use of getters and setters, browsing an API is really
 +
simple. For the <tt>Button</tt> example, continuations for <tt>get</tt> show
 +
all attributes that can be obtained from a button. The ones starting with
 +
<tt>set</tt> show the attributes that can be modified. Another frequent prefix used
 +
while writing plug-ins is <tt>add</tt> to add event listeners. Having Content Assist at
 +
your fingertips definitely improves coding speed by combining intuition
 +
with content-assisted browsing.
  
  
* <i>Entering method parameter values</i>. When entering Ctrl+Space
 
after the &#147;(&#148; for a method call, Content Assist will provide the
 
expected type name for each parameter. When you advance to the next
 
parameter&#151;by pressing a comma&#151;the Content Assist hints move along with you.
 
This is especially useful for overloaded methods with ambiguous signatures
 
and for methods with many parameters.
 
</li>
 
  
 +
* <i>Entering method parameter values</i>.
 +
When entering Ctrl+Space
 +
after the &#147;(&#148; for a method call, Content Assist will provide the
 +
expected type name for each parameter. When you advance to the next
 +
parameter&#151;by pressing a comma&#151;the Content Assist hints move along with you.
 +
This is especially useful for overloaded methods with ambiguous signatures
 +
and for methods with many parameters.
  
  
* <i>Overriding inherited methods</i>. Invoke Content Assist when
 
the cursor is between method declarations. Proposals will be
 
shown for all possible methods that can be overridden from
 
superclasses.
 
</li>
 
  
 +
* <i>Overriding inherited methods</i>.
 +
Invoke Content Assist when
 +
the cursor is between method declarations. Proposals will be
 +
shown for all possible methods that can be overridden from
 +
superclasses.
  
  
* <i>Generating getters and setters</i>. Between two method
 
declarations, type <tt>get</tt>, and invoke Content Assist.
 
Proposals will be shown for creating accessor methods for any
 
fields in the class that do not yet have an accessor. The same
 
applies for generating setter methods by invoking Content
 
Assist on the prefix <tt>set</tt>.
 
</li>
 
  
 +
* <i>Generating getters and setters</i>.
 +
Between two method
 +
declarations, type <tt>get</tt>, and invoke Content Assist.
 +
Proposals will be shown for creating accessor methods for any
 +
fields in the class that do not yet have an accessor. The same
 +
applies for generating setter methods by invoking Content
 +
Assist on the prefix <tt>set</tt>.
  
  
* <i>Creating anonymous inner classes</i>. Eclipse likes loose coupling and
 
    hence works a lot with listeners that are registered on demand. The listeners
 
    implement a given interface with methods that are called when the
 
    event of interest happens.
 
    Typical usage is to declare an anonymous inner class, as in this example:
 
    <pre>
 
    button.addSelectionListener(new SelectionAdapter() {
 
        public void widgetSelected(SelectionEvent e) {
 
            // do something here
 
        }
 
    });</pre>
 
    Here is how an experienced Eclipse user might enter that code using
 
    Content Assist.
 
    <pre>
 
    but'''&lt;Ctrl+Space&gt;''' select 'button'
 
    .add'''&lt;Ctrl+Space&gt;''' select 'addSelectionListener'
 
    new Sel'''&lt;Ctrl+Space&gt;''' select 'SelectionAdapter'
 
    () { '''&lt;Ctrl+Space&gt;''' select 'widgetSelected'
 
</pre>
 
  
 +
* <i>Creating anonymous inner classes</i>.
 +
Eclipse likes loose coupling and
 +
hence works a lot with listeners that are registered on demand. The listeners
 +
implement a given interface with methods that are called when the
 +
event of interest happens.
 +
Typical usage is to declare an anonymous inner class, as in this example:
 +
<pre>
 +
button.addSelectionListener(new SelectionAdapter() {
 +
    public void widgetSelected(SelectionEvent e) {
 +
        // do something here
 +
    }
 +
});</pre>
 +
Here is how an experienced Eclipse user might enter that code using
 +
Content Assist.
 +
 +
but'''&lt;Ctrl+Space&gt;''' select 'button'
 +
.add'''&lt;Ctrl+Space&gt;''' select 'addSelectionListener'
 +
new Sel'''&lt;Ctrl+Space&gt;''' select 'SelectionAdapter'
 +
() { '''&lt;Ctrl+Space&gt;''' select 'widgetSelected'
  
  
Line 93: Line 91:
 
Note that Content Assist is also available inside javadoc comments and can help  
 
Note that Content Assist is also available inside javadoc comments and can help  
 
when declaring fields and can assist with named local variables and method arguments.  
 
when declaring fields and can assist with named local variables and method arguments.  
In[[FAQ_How_do_I_add_Content_Assist_to_my_language_editor%3F]]
+
In [[FAQ How do I add Content Assist to my language editor?]]
 
we explain how Content Assist is implemented in Eclipse.
 
we explain how Content Assist is implemented in Eclipse.
  
Line 107: Line 105:
 
== See Also: ==
 
== See Also: ==
  
 +
[[FAQ How can templates make me the fastest coder ever?]]
  
[[FAQ_How_can_templates_make_me_the_fastest_coder_ever%3F]]
+
[[FAQ How do I add Content Assist to my language editor?]]
 
+
  
[[FAQ_How_do_I_add_Content_Assist_to_my_language_editor%3F]]
+
{{Template:FAQ_Tagline}}

Latest revision as of 04:32, 15 January 2007

When extending Eclipse, plug-in authors are confronted with an overwhelming selection of API to choose from. In the good old days, books could be published with API references, and programmers could study the material and recite the proper incantations to drive the relatively simple APIs. With modern APIs, this is no longer possible. There simply is too much to read and remember. Content Assist to the rescue!


Content Assist can take the guesswork out of coding in a number of ways:


  • Finding a given type.

Assume that you are writing some code and want to use a button in your UI. You start typing the letter “B” and don’t remember the rest of the word. Simply press Ctrl+Space, and the Java tooling will present you with a list of all types that start with the letter “B.” The list starts with Boolean. Keep typing, and the list narrows down. After typing But, you get to choose between java.awt.Button and org.eclipse.swt.widgets.Button. Choose the one you like, and the editor inserts the class and inserts an import statement for the class at the same time.



  • Finding a given field or method.

After typing a dot after a certain expression, Content Assist will suggest all possible fields and methods applicable to the expression’s result type. This functionality is very useful for discovering what operations can be applied to a given object. Combined with pervasive use of getters and setters, browsing an API is really simple. For the Button example, continuations for get show all attributes that can be obtained from a button. The ones starting with set show the attributes that can be modified. Another frequent prefix used while writing plug-ins is add to add event listeners. Having Content Assist at your fingertips definitely improves coding speed by combining intuition with content-assisted browsing.


  • Entering method parameter values.

When entering Ctrl+Space after the “(” for a method call, Content Assist will provide the expected type name for each parameter. When you advance to the next parameter—by pressing a comma—the Content Assist hints move along with you. This is especially useful for overloaded methods with ambiguous signatures and for methods with many parameters.


  • Overriding inherited methods.

Invoke Content Assist when the cursor is between method declarations. Proposals will be shown for all possible methods that can be overridden from superclasses.


  • Generating getters and setters.

Between two method declarations, type get, and invoke Content Assist. Proposals will be shown for creating accessor methods for any fields in the class that do not yet have an accessor. The same applies for generating setter methods by invoking Content Assist on the prefix set.


  • Creating anonymous inner classes.

Eclipse likes loose coupling and hence works a lot with listeners that are registered on demand. The listeners implement a given interface with methods that are called when the event of interest happens. Typical usage is to declare an anonymous inner class, as in this example:

button.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
        // do something here
    }
});

Here is how an experienced Eclipse user might enter that code using Content Assist.

but<Ctrl+Space> select 'button'
.add<Ctrl+Space> select 'addSelectionListener'
new Sel<Ctrl+Space> select 'SelectionAdapter'
() { <Ctrl+Space> select 'widgetSelected'


Note that Content Assist is also available inside javadoc comments and can help when declaring fields and can assist with named local variables and method arguments. In FAQ How do I add Content Assist to my language editor? we explain how Content Assist is implemented in Eclipse.


Also note that Content Assist can be fully customized from the Java > Editor preference page.



[edit] See Also:

FAQ How can templates make me the fastest coder ever?

FAQ How do I add Content Assist to my language editor?


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.