org.fife.ui.autocomplete
Class LanguageAwareCompletionProvider

java.lang.Object
  extended by org.fife.ui.autocomplete.CompletionProviderBase
      extended by org.fife.ui.autocomplete.LanguageAwareCompletionProvider
All Implemented Interfaces:
CompletionProvider, ToolTipSupplier

public class LanguageAwareCompletionProvider
extends CompletionProviderBase
implements ToolTipSupplier

A completion provider for the C programming language (and other languages with similar syntax). This provider simply delegates to another provider, depending on whether the caret is in:

This allows for different completion choices in comments than in code, for example.

This provider also implements the org.fife.ui.rtextarea.ToolTipSupplier interface, which allows it to display tooltips for completion choices. Thus the standard VariableCompletion and FunctionCompletion completions should be able to display tooltips with the variable declaration or function definition (provided the RSyntaxTextArea was registered with the javax.swing.ToolTipManager).

Version:
1.0
Author:
Robert Futrell

Field Summary
 
Fields inherited from class org.fife.ui.autocomplete.CompletionProviderBase
EMPTY_STRING
 
Constructor Summary
protected LanguageAwareCompletionProvider()
          Constructor subclasses can use when they don't have their default provider created at construction time.
  LanguageAwareCompletionProvider(CompletionProvider defaultProvider)
          Constructor.
 
Method Summary
 void clearParameterizedCompletionParams()
          Calling this method will result in an UnsupportedOperationException being thrown.
 java.lang.String getAlreadyEnteredText(javax.swing.text.JTextComponent comp)
          Returns the text just before the current caret position that could be the start of something auto-completable.
 CompletionProvider getCommentCompletionProvider()
          Returns the completion provider to use for comments.
 java.util.List<Completion> getCompletionsAt(javax.swing.text.JTextComponent tc, java.awt.Point p)
          Returns the completions that have been entered at the specified visual location.
protected  java.util.List<Completion> getCompletionsImpl(javax.swing.text.JTextComponent comp)
          Does the dirty work of creating a list of completions.
 CompletionProvider getDefaultCompletionProvider()
          Returns the completion provider used when one isn't defined for a particular token type.
 CompletionProvider getDocCommentCompletionProvider()
          Returns the completion provider to use for documentation comments.
 java.util.List<ParameterizedCompletion> getParameterizedCompletions(javax.swing.text.JTextComponent tc)
          Returns a list of parameterized completions that have been entered at the current caret position of a text component (and thus can have their completion choices displayed).
 char getParameterListEnd()
          Returns the text that marks the end of a list of parameters to a function or method.
 java.lang.String getParameterListSeparator()
          Returns the text that separates parameters to a function or method.
 char getParameterListStart()
          Returns the text that marks the start of a list of parameters to a function or method.
 CompletionProvider getStringCompletionProvider()
          Returns the completion provider to use for strings.
 java.lang.String getToolTipText(RTextArea textArea, java.awt.event.MouseEvent e)
          Returns the tool tip to display for a mouse event.
 boolean isAutoActivateOkay(javax.swing.text.JTextComponent tc)
          This method is called if auto-activation is enabled in the parent AutoCompletion after the user types a single character. This provider should check the text at the current caret position of the text component, and decide whether auto-activation would be appropriate here. For example, a CompletionProvider for Java might want to return true for this method only if the last character typed was a '.'.
 void setCommentCompletionProvider(CompletionProvider provider)
          Sets the comment completion provider.
 void setDefaultCompletionProvider(CompletionProvider provider)
          Sets the default completion provider.
 void setDocCommentCompletionProvider(CompletionProvider provider)
          Sets the documentation comment completion provider.
 void setParameterizedCompletionParams(char listStart, java.lang.String separator, char listEnd)
          Calling this method will result in an UnsupportedOperationException being thrown.
 void setStringCompletionProvider(CompletionProvider provider)
          Sets the completion provider to use while in a string.
 
Methods inherited from class org.fife.ui.autocomplete.CompletionProviderBase
getCompletions, getListCellRenderer, getParameterChoicesProvider, getParent, setAutoActivationRules, setListCellRenderer, setParameterChoicesProvider, setParent
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LanguageAwareCompletionProvider

protected LanguageAwareCompletionProvider()
Constructor subclasses can use when they don't have their default provider created at construction time. They should call setDefaultCompletionProvider(CompletionProvider) in this constructor.


LanguageAwareCompletionProvider

public LanguageAwareCompletionProvider(CompletionProvider defaultProvider)
Constructor.

Parameters:
defaultProvider - The provider to use when no provider is assigned to a particular token type. This cannot be null.
Method Detail

clearParameterizedCompletionParams

public void clearParameterizedCompletionParams()
Calling this method will result in an UnsupportedOperationException being thrown. To set the parameter completion parameters, do so on the provider returned by getDefaultCompletionProvider().

Specified by:
clearParameterizedCompletionParams in interface CompletionProvider
Overrides:
clearParameterizedCompletionParams in class CompletionProviderBase
Throws:
java.lang.UnsupportedOperationException - Always.
See Also:
setParameterizedCompletionParams(char, String, char)

getAlreadyEnteredText

public java.lang.String getAlreadyEnteredText(javax.swing.text.JTextComponent comp)
Returns the text just before the current caret position that could be the start of something auto-completable.

Specified by:
getAlreadyEnteredText in interface CompletionProvider
Parameters:
comp - The text component.
Returns:
The text. A return value of null means nothing should be auto-completed; a value of an empty string ("") means auto-completion should still be considered (i.e., all possible choices are valid).

getCommentCompletionProvider

public CompletionProvider getCommentCompletionProvider()
Returns the completion provider to use for comments.

Returns:
The completion provider to use.
See Also:
setCommentCompletionProvider(CompletionProvider)

getCompletionsAt

public java.util.List<Completion> getCompletionsAt(javax.swing.text.JTextComponent tc,
                                                   java.awt.Point p)
Returns the completions that have been entered at the specified visual location. This can be used for tool tips when the user hovers the mouse over completed text.

Specified by:
getCompletionsAt in interface CompletionProvider
Parameters:
tc - The text component.
p - The position, usually from a MouseEvent.
Returns:
The completions, or an empty list if there are none.

getCompletionsImpl

protected java.util.List<Completion> getCompletionsImpl(javax.swing.text.JTextComponent comp)
Does the dirty work of creating a list of completions.

Specified by:
getCompletionsImpl in class CompletionProviderBase
Parameters:
comp - The text component to look in.
Returns:
The list of possible completions, or an empty list if there are none.

getDefaultCompletionProvider

public CompletionProvider getDefaultCompletionProvider()
Returns the completion provider used when one isn't defined for a particular token type.

Returns:
The completion provider to use.
See Also:
setDefaultCompletionProvider(CompletionProvider)

getDocCommentCompletionProvider

public CompletionProvider getDocCommentCompletionProvider()
Returns the completion provider to use for documentation comments.

Returns:
The completion provider to use.
See Also:
setDocCommentCompletionProvider(CompletionProvider)

getParameterizedCompletions

public java.util.List<ParameterizedCompletion> getParameterizedCompletions(javax.swing.text.JTextComponent tc)
Returns a list of parameterized completions that have been entered at the current caret position of a text component (and thus can have their completion choices displayed).

Specified by:
getParameterizedCompletions in interface CompletionProvider
Parameters:
tc - The text component.
Returns:
The list of ParameterizedCompletions. If no completions are available, this may be null.

getParameterListEnd

public char getParameterListEnd()
Returns the text that marks the end of a list of parameters to a function or method.

Specified by:
getParameterListEnd in interface CompletionProvider
Overrides:
getParameterListEnd in class CompletionProviderBase
Returns:
The text for a parameter list end, for example, ')'.
See Also:
CompletionProvider.getParameterListStart(), CompletionProvider.getParameterListSeparator(), CompletionProvider.setParameterizedCompletionParams(char, String, char)

getParameterListSeparator

public java.lang.String getParameterListSeparator()
Returns the text that separates parameters to a function or method.

Specified by:
getParameterListSeparator in interface CompletionProvider
Overrides:
getParameterListSeparator in class CompletionProviderBase
Returns:
The text that separates parameters, for example, ", ".
See Also:
CompletionProvider.getParameterListStart(), CompletionProvider.getParameterListEnd(), CompletionProvider.setParameterizedCompletionParams(char, String, char)

getParameterListStart

public char getParameterListStart()
Returns the text that marks the start of a list of parameters to a function or method.

Specified by:
getParameterListStart in interface CompletionProvider
Overrides:
getParameterListStart in class CompletionProviderBase
Returns:
The text for a parameter list start, for example, "(".
See Also:
CompletionProvider.getParameterListEnd(), CompletionProvider.getParameterListSeparator(), CompletionProvider.setParameterizedCompletionParams(char, String, char)

getStringCompletionProvider

public CompletionProvider getStringCompletionProvider()
Returns the completion provider to use for strings.

Returns:
The completion provider to use.
See Also:
setStringCompletionProvider(CompletionProvider)

isAutoActivateOkay

public boolean isAutoActivateOkay(javax.swing.text.JTextComponent tc)
This method is called if auto-activation is enabled in the parent AutoCompletion after the user types a single character. This provider should check the text at the current caret position of the text component, and decide whether auto-activation would be appropriate here. For example, a CompletionProvider for Java might want to return true for this method only if the last character typed was a '.'.

Specified by:
isAutoActivateOkay in interface CompletionProvider
Overrides:
isAutoActivateOkay in class CompletionProviderBase
Parameters:
tc - The text component.
Returns:
Whether auto-activation would be appropriate.

setCommentCompletionProvider

public void setCommentCompletionProvider(CompletionProvider provider)
Sets the comment completion provider.

Parameters:
provider - The provider to use in comments.
See Also:
getCommentCompletionProvider()

setDefaultCompletionProvider

public void setDefaultCompletionProvider(CompletionProvider provider)
Sets the default completion provider.

Parameters:
provider - The provider to use when no provider is assigned to a particular token type. This cannot be null.
See Also:
getDefaultCompletionProvider()

setDocCommentCompletionProvider

public void setDocCommentCompletionProvider(CompletionProvider provider)
Sets the documentation comment completion provider.

Parameters:
provider - The provider to use in comments.
See Also:
getDocCommentCompletionProvider()

setParameterizedCompletionParams

public void setParameterizedCompletionParams(char listStart,
                                             java.lang.String separator,
                                             char listEnd)
Calling this method will result in an UnsupportedOperationException being thrown. To set the parameter completion parameters, do so on the provider returned by getDefaultCompletionProvider().

Specified by:
setParameterizedCompletionParams in interface CompletionProvider
Overrides:
setParameterizedCompletionParams in class CompletionProviderBase
Parameters:
listStart - The character that marks the beginning of a list of parameters, such as '(' in C or Java.
separator - Text that should separate parameters in a parameter list when one is inserted. For example, ", ".
listEnd - The character that marks the end of a list of parameters, such as ')' in C or Java.
Throws:
java.lang.UnsupportedOperationException - Always.
See Also:
clearParameterizedCompletionParams()

setStringCompletionProvider

public void setStringCompletionProvider(CompletionProvider provider)
Sets the completion provider to use while in a string.

Parameters:
provider - The provider to use.
See Also:
getStringCompletionProvider()

getToolTipText

public java.lang.String getToolTipText(RTextArea textArea,
                                       java.awt.event.MouseEvent e)
Returns the tool tip to display for a mouse event.

For this method to be called, the RSyntaxTextArea must be registered with the javax.swing.ToolTipManager like so:

 ToolTipManager.sharedInstance().registerComponent(textArea);
 

Specified by:
getToolTipText in interface ToolTipSupplier
Parameters:
textArea - The text area.
e - The mouse event.
Returns:
The tool tip text, or null if none.