Class TemplateCompletion
- java.lang.Object
-
- org.fife.ui.autocomplete.AbstractCompletion
-
- org.fife.ui.autocomplete.TemplateCompletion
-
- All Implemented Interfaces:
java.lang.Comparable<Completion>
,Completion
,ParameterizedCompletion
public class TemplateCompletion extends AbstractCompletion implements ParameterizedCompletion
A completion made up of a template with arbitrary parameters that the user can tab through and fill in. This completion type is useful for inserting common boilerplate code, such as for-loops.The format of a template is similar to those in Eclipse. The following example would be the format for a for-loop template:
for (int ${i} = 0; ${i} < ${array}.length; ${i}++) { ${cursor} }
In the above example, the first${i}
is a parameter for the user to type into; all the other${i}
instances are automatically changed to what the user types in the first one. The parameter named${cursor}
is the "ending position" of the template. It's where the caret moves after it cycles through all other parameters. If the user types into it, template mode terminates. If more than one${cursor}
parameter is specified, behavior is undefined.Two dollar signs in a row ("
$$
") will be evaluated as a single dollar sign. Otherwise, the template parsing is pretty straightforward and fault-tolerant.Leading whitespace is automatically added to lines if the template spans more than one line, and if used with a text component using a
PlainDocument
, tabs will be converted to spaces if requested.In order to use any instance of a
ParameterizedCompletion
, you need to callAutoCompletion.setParameterAssistanceEnabled(boolean)
with a value oftrue
first. Otherwise, any completion choices will be visible in the completion list, but won't insert anything when selected.- Version:
- 1.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.fife.ui.autocomplete.ParameterizedCompletion
ParameterizedCompletion.Parameter
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
definitionString
private java.lang.String
inputText
private java.util.List<ParameterizedCompletion.Parameter>
params
The template's parameters.private java.util.List<TemplatePiece>
pieces
private java.lang.String
shortDescription
private java.lang.String
summary
-
Constructor Summary
Constructors Constructor Description TemplateCompletion(CompletionProvider provider, java.lang.String inputText, java.lang.String definitionString, java.lang.String template)
TemplateCompletion(CompletionProvider provider, java.lang.String inputText, java.lang.String definitionString, java.lang.String template, java.lang.String shortDescription, java.lang.String summary)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addTemplatePiece(TemplatePiece piece)
java.lang.String
getDefinitionString()
Returns the "definition string" for this completion.java.lang.String
getInputText()
Returns the text the user has to (start) typing for this completion to be offered.ParameterizedCompletionInsertionInfo
getInsertionInfo(javax.swing.text.JTextComponent tc, boolean replaceTabsWithSpaces)
Returns completion information for this parameterized completion, given the specified text component.ParameterizedCompletion.Parameter
getParam(int index)
Returns the specifiedParameterizedCompletion.Parameter
.int
getParamCount()
Returns the number of parameters this completion takes.private java.lang.String
getPieceText(int index, java.lang.String leadingWS)
java.lang.String
getReplacementText()
Returnsnull
; template completions insert all of their text viagetInsertionInfo()
.java.lang.String
getShortDescription()
boolean
getShowParameterToolTip()
Returns whether a tool tip displaying assistance for each parameter while it is being edited is appropriate for this completion.java.lang.String
getSummary()
Returns the description of this auto-complete choice.private boolean
isParamDefined(java.lang.String name)
Returns whether a parameter is already defined with a specific name.private void
parse(java.lang.String template)
Parses a template string into logical pieces used by this class.private int
possiblyReplaceTabsWithSpaces(java.lang.StringBuilder sb, java.lang.String text, javax.swing.text.JTextComponent tc, int start)
void
setShortDescription(java.lang.String shortDesc)
Sets the short description of this template completion.java.lang.String
toString()
Returns a string representation of this completion.-
Methods inherited from class org.fife.ui.autocomplete.AbstractCompletion
compareTo, getAlreadyEntered, getIcon, getProvider, getRelevance, getToolTipText, setIcon, setRelevance
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.fife.ui.autocomplete.Completion
compareTo, getAlreadyEntered, getIcon, getProvider, getRelevance, getToolTipText
-
-
-
-
Field Detail
-
pieces
private java.util.List<TemplatePiece> pieces
-
inputText
private java.lang.String inputText
-
definitionString
private java.lang.String definitionString
-
shortDescription
private java.lang.String shortDescription
-
summary
private java.lang.String summary
-
params
private java.util.List<ParameterizedCompletion.Parameter> params
The template's parameters.
-
-
Constructor Detail
-
TemplateCompletion
public TemplateCompletion(CompletionProvider provider, java.lang.String inputText, java.lang.String definitionString, java.lang.String template)
-
TemplateCompletion
public TemplateCompletion(CompletionProvider provider, java.lang.String inputText, java.lang.String definitionString, java.lang.String template, java.lang.String shortDescription, java.lang.String summary)
-
-
Method Detail
-
addTemplatePiece
private void addTemplatePiece(TemplatePiece piece)
-
getInputText
public java.lang.String getInputText()
Description copied from class:AbstractCompletion
Returns the text the user has to (start) typing for this completion to be offered. The default implementation simply returnsCompletion.getReplacementText()
.- Specified by:
getInputText
in interfaceCompletion
- Overrides:
getInputText
in classAbstractCompletion
- Returns:
- The text the user has to (start) typing for this completion.
- See Also:
Completion.getReplacementText()
-
getPieceText
private java.lang.String getPieceText(int index, java.lang.String leadingWS)
-
getReplacementText
public java.lang.String getReplacementText()
Returnsnull
; template completions insert all of their text viagetInsertionInfo()
.- Specified by:
getReplacementText
in interfaceCompletion
- Returns:
null
always.- See Also:
Completion.getInputText()
-
getSummary
public java.lang.String getSummary()
Description copied from interface:Completion
Returns the description of this auto-complete choice. This can be used in a popup "description window."- Specified by:
getSummary
in interfaceCompletion
- Returns:
- This item's description. This should be HTML. It may be
null
if there is no description for this completion.
-
getDefinitionString
public java.lang.String getDefinitionString()
Description copied from interface:ParameterizedCompletion
Returns the "definition string" for this completion. For example, for the C "printf
" function, this would return "int printf(const char *, ...)
".- Specified by:
getDefinitionString
in interfaceParameterizedCompletion
- Returns:
- The definition string.
-
getShortDescription
public java.lang.String getShortDescription()
-
getShowParameterToolTip
public boolean getShowParameterToolTip()
Description copied from interface:ParameterizedCompletion
Returns whether a tool tip displaying assistance for each parameter while it is being edited is appropriate for this completion.- Specified by:
getShowParameterToolTip
in interfaceParameterizedCompletion
- Returns:
- Whether the tool tip is appropriate to display.
-
getInsertionInfo
public ParameterizedCompletionInsertionInfo getInsertionInfo(javax.swing.text.JTextComponent tc, boolean replaceTabsWithSpaces)
Description copied from interface:ParameterizedCompletion
Returns completion information for this parameterized completion, given the specified text component.- Specified by:
getInsertionInfo
in interfaceParameterizedCompletion
- Parameters:
tc
- The text component.replaceTabsWithSpaces
- Whether to replace tabs with spaces.- Returns:
- The completion info.
-
getParam
public ParameterizedCompletion.Parameter getParam(int index)
Description copied from interface:ParameterizedCompletion
Returns the specifiedParameterizedCompletion.Parameter
.- Specified by:
getParam
in interfaceParameterizedCompletion
- Parameters:
index
- The index of the parameter to retrieve.- Returns:
- The parameter.
- See Also:
ParameterizedCompletion.getParamCount()
-
getParamCount
public int getParamCount()
Description copied from interface:ParameterizedCompletion
Returns the number of parameters this completion takes.- Specified by:
getParamCount
in interfaceParameterizedCompletion
- Returns:
- The number of parameters this completion takes.
- See Also:
ParameterizedCompletion.getParam(int)
-
isParamDefined
private boolean isParamDefined(java.lang.String name)
Returns whether a parameter is already defined with a specific name.- Parameters:
name
- The name.- Returns:
- Whether a parameter is defined with that name.
-
parse
private void parse(java.lang.String template)
Parses a template string into logical pieces used by this class.- Parameters:
template
- The template to parse.
-
possiblyReplaceTabsWithSpaces
private int possiblyReplaceTabsWithSpaces(java.lang.StringBuilder sb, java.lang.String text, javax.swing.text.JTextComponent tc, int start)
-
setShortDescription
public void setShortDescription(java.lang.String shortDesc)
Sets the short description of this template completion.- Parameters:
shortDesc
- The new short description.- See Also:
getShortDescription()
-
toString
public java.lang.String toString()
Description copied from class:AbstractCompletion
Returns a string representation of this completion. The default implementation returnsAbstractCompletion.getInputText()
.- Overrides:
toString
in classAbstractCompletion
- Returns:
- A string representation of this completion.
-
-