This is an automated email from the git hooks/post-receive script.

ben pushed a commit to branch master
in repository autocomplete.

commit 1d0f7b6a193373cbe658a650ad401fc9439be63c
Author: bobbylight <[email protected]>
Date:   Sat May 26 16:04:20 2012 +0000

    AutoComplete: Starting to add support for arbitrary template completions 
(for-loops, etc.).
---
 .../fife/ui/autocomplete/FunctionCompletion.java   |   96 +++++++--
 .../ui/autocomplete/ParameterizedCompletion.java   |    6 +
 .../ParameterizedCompletionDescriptionToolTip.java |   76 ++------
 .../ParameterizedCompletionInsertionInfo.java      |  192 ++++++++++++++++++
 .../fife/ui/autocomplete/TemplateCompletion.java   |  205 ++++++++++++++++++++
 5 files changed, 493 insertions(+), 82 deletions(-)

diff --git a/src/org/fife/ui/autocomplete/FunctionCompletion.java 
b/src/org/fife/ui/autocomplete/FunctionCompletion.java
index c0b5da4..9f9b13e 100644
--- a/src/org/fife/ui/autocomplete/FunctionCompletion.java
+++ b/src/org/fife/ui/autocomplete/FunctionCompletion.java
@@ -11,7 +11,9 @@ package org.fife.ui.autocomplete;
 import java.util.ArrayList;
 import java.util.List;
 
+import javax.swing.text.BadLocationException;
 import javax.swing.text.JTextComponent;
+import javax.swing.text.Position;
 
 
 /**
@@ -144,12 +146,63 @@ public class FunctionCompletion extends VariableCompletion
        }
 
 
+       public ParameterizedCompletionInsertionInfo getInsertionInfo(
+                       JTextComponent tc, boolean addParamStartList) {
+
+               ParameterizedCompletionInsertionInfo info =
+                       new ParameterizedCompletionInsertionInfo();
+
+               StringBuffer sb = new StringBuffer();
+               if (addParamStartList) {
+                       sb.append(getProvider().getParameterListStart());
+               }
+               int dot = tc.getCaretPosition() + sb.length();
+               int paramCount = getParamCount();
+
+               // Get the range in which the caret can move before we hide
+               // this tool tip.
+               int minPos = dot;
+               Position maxPos = null;
+               try {
+                       maxPos = 
tc.getDocument().createPosition(dot-sb.length());
+               } catch (BadLocationException ble) {
+                       ble.printStackTrace(); // Never happens
+               }
+               info.setCaretRange(minPos, maxPos);
+               int firstParamLen = 0;
+
+               // Create the text to insert (keep it one completion for
+               // performance and simplicity of undo/redo).
+               int start = dot;
+               for (int i=0; i<paramCount; i++) {
+                       Parameter param = getParam(i);
+                       String paramText = getParamText(param);
+                       if (i==0) {
+                               firstParamLen = paramText.length();
+                       }
+                       sb.append(paramText);
+                       int end = start + paramText.length();
+                       info.addReplacementLocation(start, end);
+                       // Patch for param. list separators with length > 2 -
+                       // thanks to Matthew Adereth!
+                       String sep = getProvider().getParameterListSeparator();
+                       if (i<paramCount-1 && sep!=null) {
+                               sb.append(sep);
+                               start = end + sep.length();
+                       }
+               }
+               sb.append(getProvider().getParameterListEnd());
+
+               int selectionEnd = paramCount>0 ? (dot+firstParamLen) : dot;
+               info.setInitialSelection(dot, selectionEnd);
+               info.setTextToInsert(sb.toString());
+               return info;
+
+       }
+
+
        /**
-        * Returns the specified {@link ParameterizedCompletion.Parameter}.
-        *
-        * @param index The index of the parameter to retrieve.
-        * @return The parameter.
-        * @see #getParamCount()
+        * {@inheritDoc}
         */
        public Parameter getParam(int index) {
                return (Parameter)params.get(index);
@@ -168,6 +221,24 @@ public class FunctionCompletion extends VariableCompletion
 
 
        /**
+        * Returns the text to insert for a parameter.
+        *
+        * @param param The parameter.
+        * @return The text.
+        */
+       private String getParamText(ParameterizedCompletion.Parameter param) {
+               String text = param.getName();
+               if (text==null) {
+                       text = param.getType();
+                       if (text==null) { // Shouldn't ever happen
+                               text = "arg";
+                       }
+               }
+               return text;
+       }
+
+
+       /**
         * Returns the description of the return value of this function.
         *
         * @return The description, or <code>null</code> if there is none.
@@ -194,20 +265,7 @@ public class FunctionCompletion extends VariableCompletion
 
 
        /**
-        * Returns the tool tip text to display for mouse hovers over this
-        * completion.<p>
-        *
-        * Note that for this functionality to be enabled, a
-        * <tt>JTextComponent</tt> must be registered with the
-        * <tt>ToolTipManager</tt>, and the text component must know to search
-        * for this value.  In the case of an
-        * <a href="http://fifesoft.com/rsyntaxtextarea";>RSyntaxTextArea</a>, 
this
-        * can be done with a <tt>org.fife.ui.rtextarea.ToolTipSupplier</tt> 
that
-        * calls into
-        * {@link CompletionProvider#getCompletionsAt(JTextComponent, 
java.awt.Point)}.
-        *
-        * @return The tool tip text for this completion, or <code>null</code> 
if
-        *         none.
+        * {@inheritDoc}
         */
        public String getToolTipText() {
                String text = getSummary();
diff --git a/src/org/fife/ui/autocomplete/ParameterizedCompletion.java 
b/src/org/fife/ui/autocomplete/ParameterizedCompletion.java
index 9c5152e..1d995f0 100644
--- a/src/org/fife/ui/autocomplete/ParameterizedCompletion.java
+++ b/src/org/fife/ui/autocomplete/ParameterizedCompletion.java
@@ -8,6 +8,8 @@
  */
 package org.fife.ui.autocomplete;
 
+import javax.swing.text.JTextComponent;
+
 
 /**
  * A completion option that takes parameters, such as a function or method.
@@ -47,6 +49,10 @@ public interface ParameterizedCompletion extends Completion {
        public int getParamCount();
 
 
+       public ParameterizedCompletionInsertionInfo getInsertionInfo(
+                       JTextComponent tc, boolean addParamStartList);
+
+
        /**
         * A parameter passed to a parameterized {@link Completion}.
         *
diff --git 
a/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java 
b/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java
index 7d19ca9..677c2f7 100644
--- 
a/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java
+++ 
b/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java
@@ -1,8 +1,8 @@
 /*
  * 12/21/2008
  *
- * AutoCompleteDescWindow.java - A window containing a description of the
- * currently selected completion.
+ * ParameterizedCompletionDescriptionToolTip.java - A "tool tip" displaying
+ * information on the function or method currently being entered.
  * 
  * This library is distributed under a modified BSD license.  See the included
  * RSyntaxTextArea.License.txt file for details.
@@ -948,24 +948,6 @@ class ParameterizedCompletionDescriptionToolTip {
 
 
                /**
-                * Returns the text to insert for a parameter.
-                *
-                * @param param The parameter.
-                * @return The text.
-                */
-               private String getParamText(ParameterizedCompletion.Parameter 
param) {
-                       String text = param.getName();
-                       if (text==null) {
-                               text = param.getType();
-                               if (text==null) { // Shouldn't ever happen
-                                       text = "arg";
-                               }
-                       }
-                       return text;
-               }
-
-
-               /**
                 * Installs this listener onto a text component.
                 *
                 * @param tc The text component to install onto.
@@ -981,63 +963,31 @@ class ParameterizedCompletionDescriptionToolTip {
                        tc.addFocusListener(this);
                        installKeyBindings();
 
-                       StringBuffer sb = new StringBuffer();
-                       if (addParamStartList) {
-                               
sb.append(pc.getProvider().getParameterListStart());
-                       }
-                       int dot = tc.getCaretPosition() + sb.length();
-                       int paramCount = pc.getParamCount();
-                       List paramLocs = null;
-                       if (paramCount>0) {
-                               paramLocs = new ArrayList(paramCount);
-                       }
                        Highlighter h = tc.getHighlighter();
 
                        try {
 
-                               // Get the range in which the caret can move 
before we hide
-                               // this tooltip.
-                               minPos = dot;
-                               maxPos = 
tc.getDocument().createPosition(dot-sb.length());
-                               int firstParamLen = 0;
-
-                               // Create the text to insert (keep it one 
completion for
-                               // performance and simplicity of undo/redo).
-                               int start = dot;
-                               for (int i=0; i<paramCount; i++) {
-                                       FunctionCompletion.Parameter param = 
pc.getParam(i);
-                                       String paramText = getParamText(param);
-                                       if (i==0) {
-                                               firstParamLen = 
paramText.length();
-                                       }
-                                       sb.append(paramText);
-                                       int end = start + paramText.length();
-                                       paramLocs.add(new Point(start, end));
-                                       // Patch for param. list separators 
with length > 2 -
-                                       // thanks to Matthew Adereth!
-                                       String sep = 
pc.getProvider().getParameterListSeparator();
-                                       if (i<paramCount-1 && sep!=null) {
-                                               sb.append(sep);
-                                               start = end + sep.length();
-                                       }
-                               }
-                               
sb.append(pc.getProvider().getParameterListEnd());
+                               ParameterizedCompletionInsertionInfo info =
+                                                               
pc.getInsertionInfo(tc, addParamStartList);
 
                                // Insert the parameter text and add highlights 
around the
                                // parameters.
-                               tc.replaceSelection(sb.toString());
-                               for (int i=0; i<paramCount; i++) {
-                                       Point pt = (Point)paramLocs.get(i);
+                               tc.replaceSelection(info.getTextToInsert());
+                               for (int i=0; i<info.getReplacementCount(); 
i++) {
+                                       Point pt = 
info.getReplacementLocation(i);
                                         // "-1" is a workaround for Java 
Highlight issues.
                                        tags.add(h.addHighlight(pt.x-1, pt.y, 
p));
                                }
 
                                // Go back and start at the first parameter.
-                               tc.setCaretPosition(dot);
-                               if (pc.getParamCount()>0) {
-                                       tc.moveCaretPosition(dot+firstParamLen);
+                               tc.setCaretPosition(info.getSelectionStart());
+                               if (info.hasSelection()) {
+                                       
tc.moveCaretPosition(info.getSelectionEnd());
                                }
 
+                               minPos = info.getMinOffset();
+                               maxPos = info.getMaxOffset();
+
                        } catch (BadLocationException ble) {
                                ble.printStackTrace(); // Never happens
                        }
diff --git 
a/src/org/fife/ui/autocomplete/ParameterizedCompletionInsertionInfo.java 
b/src/org/fife/ui/autocomplete/ParameterizedCompletionInsertionInfo.java
new file mode 100644
index 0000000..b82430d
--- /dev/null
+++ b/src/org/fife/ui/autocomplete/ParameterizedCompletionInsertionInfo.java
@@ -0,0 +1,192 @@
+/*
+ * 05/26/2012
+ *
+ * ParameterizedCompletionInsertionInfo.java - Used internally to track the
+ * changes required for a specific parameterized completion.
+ * 
+ * This library is distributed under a modified BSD license.  See the included
+ * RSyntaxTextArea.License.txt file for details.
+ */
+package org.fife.ui.autocomplete;
+
+import java.awt.Point;
+import java.util.ArrayList;
+import java.util.List;
+import javax.swing.text.Position;
+
+
+/**
+ * Describes a parameterized completion - what's being inserted, where the
+ * parameters are in the inserted text, etc.
+ *
+ * @author Robert Futrell
+ * @version 1.0
+ */
+class ParameterizedCompletionInsertionInfo {
+
+       private int minOffs;
+       private Position maxOffs;
+       private int selStart;
+       private int selEnd;
+       private String textToInsert;
+       private List replacementLocations;
+
+
+       public ParameterizedCompletionInsertionInfo() {
+       }
+
+
+       /**
+        * Marks a region of the replacement text as representing a variable 
name
+        * or some other construct that the user should replace.
+        *
+        * @param start The start offset.
+        * @param end The end offset.
+        * @see #getReplacementCount()
+        * @see #getReplacementLocation(int)
+        */
+       public void addReplacementLocation(int start, int end) {
+               if (replacementLocations==null) {
+                       replacementLocations = new ArrayList(1);
+               }
+               replacementLocations.add(new Point(start, end));
+       }
+
+
+       /**
+        * Returns the maximum offset the caret can move to before being outside
+        * of the text inserted for this completion.
+        *
+        * @return The maximum offset.
+        * @see #getMinOffset()
+        */
+       public Position getMaxOffset() {
+               return maxOffs;
+       }
+
+
+       /**
+        * Returns the minimum offset the caret can move to before being outside
+        * of the text inserted for this completion.
+        *
+        * @return The minimum offset.
+        * @see #getMaxOffset()
+        */
+       public int getMinOffset() {
+               return minOffs;
+       }
+
+
+       /**
+        * Returns the number of replacements in the completion.
+        *
+        * @return The number of replacements in the completion.
+        */
+       public int getReplacementCount() {
+               return replacementLocations==null ? 0 : 
replacementLocations.size();
+       }
+
+
+       /**
+        * Returns the starting- and ending-offsets of the replacement regions
+        * in the completion.
+        *
+        * @param index The replacement region.
+        * @return The range in the document of that replacement region.
+        * @see #getReplacementCount()
+        */
+       public Point getReplacementLocation(int index) {
+               return (Point)replacementLocations.get(index);
+       }
+
+
+       /**
+        * Returns the offset that should be the end of the initially selected
+        * text when the completion is inserted (i.e., the end offset of the 
first
+        * replacement region).
+        *
+        * @return The end offset for the initial selection.
+        * @see #getSelectionStart()
+        */
+       public int getSelectionEnd() {
+               return selEnd;
+       }
+
+
+       /**
+        * Returns the offset that should be the start of the initially selected
+        * text when the completion is inserted (i.e., the start offset of the
+        * first replacement region).
+        *
+        * @return The start offset for the initial selection.
+        * @see #getSelectionEnd()
+        */
+       public int getSelectionStart() {
+               return selStart;
+       }
+
+
+       /**
+        * Returns the actual text to insert when the completion is selected.
+        *
+        * @return The text to insert.
+        * @see #setTextToInsert(String)
+        */
+       public String getTextToInsert() {
+               return textToInsert;
+       }
+
+
+       /**
+        * Returns whether or not there is an initial selected region for the
+        * completion (i.e., whether the completion actually has any 
parameters).
+        *
+        * @return Whether there is a region to initially select for the 
completion.
+        */
+       public boolean hasSelection() {
+               return selEnd!=selStart;
+       }
+
+
+       /**
+        * Sets the initially selected region for the completion.
+        *
+        * @param selStart The selection start.
+        * @param selEnd The selection end.
+        * @see #getSelectionEnd()
+        * @see #getSelectionStart()
+        */
+       public void setInitialSelection(int selStart, int selEnd) {
+               this.selStart = selStart;
+               this.selEnd = selEnd;
+       }
+
+
+       /**
+        * Sets the document range the caret can move around in before being
+        * outside of the text inserted for the completion.
+        *
+        * @param minOffs The minimum offset.
+        * @param maxOffs The maximum offset, that will track its location as 
the
+        *        document is modified.
+        * @see #getMinOffset()
+        * @see #getMaxOffset()
+        */
+       public void setCaretRange(int minOffs, Position maxOffs) {
+               this.minOffs = minOffs;
+               this.maxOffs = maxOffs;
+       }
+
+
+       /**
+        * Sets the text to insert for the completion.
+        *
+        * @param text The text to insert.
+        * @see #getTextToInsert()
+        */
+       public void setTextToInsert(String text) {
+               this.textToInsert = text;
+       }
+
+
+}
\ No newline at end of file
diff --git a/src/org/fife/ui/autocomplete/TemplateCompletion.java 
b/src/org/fife/ui/autocomplete/TemplateCompletion.java
new file mode 100644
index 0000000..19a085c
--- /dev/null
+++ b/src/org/fife/ui/autocomplete/TemplateCompletion.java
@@ -0,0 +1,205 @@
+/*
+ * 05/26/2012
+ *
+ * TemplateCompletion.java - A completion used to insert boilerplate code
+ * snippets that have arbitrary sections the user will want to change, such as
+ * for-loops.
+ * 
+ * This library is distributed under a modified BSD license.  See the included
+ * RSyntaxTextArea.License.txt file for details.
+ */
+package org.fife.ui.autocomplete;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.JTextComponent;
+import javax.swing.text.Position;
+
+import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities;
+
+
+/**
+ * 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.<p>
+ *
+ * This class is a work in progress and currently should not be used.
+ *
+ * @author Robert Futrell
+ * @version 1.0
+ */
+public class TemplateCompletion extends AbstractCompletion
+                                                               implements 
ParameterizedCompletion {
+
+       private List pieces;
+
+       private String replacementText;
+
+       private String definitionString;
+
+       /**
+        * The template's parameters.
+        */
+       private List params;
+
+
+
+       public TemplateCompletion(CompletionProvider provider, String 
replacementText,
+                       String definitionString) {
+               super(provider);
+               this.replacementText = replacementText;
+               this.definitionString = definitionString;
+               pieces = new ArrayList(3);
+               params = new ArrayList(3);
+       }
+
+
+       public void addTemplatePiece(TemplatePiece piece) {
+               pieces.add(piece);
+               if (piece.isParam) {
+                       final String type = null; // TODO
+                       Parameter param = new Parameter(type, piece.text);
+                       params.add(param);
+               }
+       }
+
+
+       private String getPieceText(int index, String leadingWS) {
+               String text = null;
+               TemplatePiece piece = (TemplatePiece)pieces.get(index);
+               if (piece.id!=null) {
+                       final String id = piece.id;
+                       for (int i=0; i<pieces.size(); i++) {
+                               piece = (TemplatePiece)pieces.get(i);
+                               if (id.equals(piece.id)) {
+                                       text = piece.text;
+                                       break;
+                               }
+                       }
+               }
+               else {
+                       text = piece.text;
+               }
+               if (text.indexOf('\n')>-1) {
+                       text = text.replaceAll("\n", "\n" + leadingWS);
+               }
+               return text;
+       }
+
+
+       /**
+        * For template completions, the "replacement text" is really just the
+        * first piece of the template, not the entire thing.  You should add
+        * template pieces so that the rest of the template is dynamically
+        * generated.
+        */
+       public String getReplacementText() {
+               return replacementText;
+       }
+
+
+       public String getSummary() {
+               // TODO Auto-generated method stub
+               return null;
+       }
+
+
+       public String getDefinitionString() {
+               return definitionString;
+       }
+
+
+       public ParameterizedCompletionInsertionInfo getInsertionInfo(
+                       JTextComponent tc, boolean addParamStartList) {
+
+               ParameterizedCompletionInsertionInfo info =
+                       new ParameterizedCompletionInsertionInfo();
+
+               StringBuffer sb = new StringBuffer();
+               int dot = tc.getCaretPosition();
+               int paramCount = getParamCount();
+
+               // Get the range in which the caret can move before we hide
+               // this tool tip.
+               int minPos = dot;
+               Position maxPos = null;
+               try {
+                       maxPos = tc.getDocument().createPosition(dot);
+               } catch (BadLocationException ble) {
+                       ble.printStackTrace(); // Never happens
+               }
+               info.setCaretRange(minPos, maxPos);
+               int firstParamLen = 0;
+
+               Document doc = tc.getDocument();
+               String leadingWS = null;
+               try {
+                       leadingWS = RSyntaxUtilities.getLeadingWhitespace(doc, 
dot);
+               } catch (BadLocationException ble) { // Never happens
+                       ble.printStackTrace();
+                       leadingWS = "";
+               }
+
+               // Create the text to insert (keep it one completion for
+               // performance and simplicity of undo/redo).
+               int start = dot;
+               for (int i=0; i<pieces.size(); i++) {
+                       TemplatePiece piece = (TemplatePiece)pieces.get(i);
+                       String text = getPieceText(i, leadingWS);
+                       sb.append(text);
+                       int end = start + text.length();
+                       if (piece.isParam) {
+                               info.addReplacementLocation(start, end);
+                               if (firstParamLen==0) {
+                                       firstParamLen = text.length();
+                               }
+                       }
+                       start = end;
+               }
+               int selectionEnd = paramCount>0 ? (dot+firstParamLen) : dot;
+               info.setInitialSelection(dot, selectionEnd);
+               info.setTextToInsert(sb.toString());
+               return info;
+
+       }
+
+
+       /**
+        * {@inheritDoc}
+        */
+       public Parameter getParam(int index) {
+               return (Parameter)params.get(index);
+       }
+
+
+       /**
+        * {@inheritDoc}
+        */
+       public int getParamCount() {
+               return params==null ? 0 : params.size();
+       }
+
+
+       public String toString() {
+               return getDefinitionString();
+       }
+
+
+       public static class TemplatePiece {
+
+               private String id;
+               private String text;
+               private boolean isParam;
+
+               public TemplatePiece(String id, String text, boolean isParam) {
+                       this.id = id;
+                       this.text = text;
+                       this.isParam = isParam;
+               }
+
+       }
+
+
+}
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on 
/srv/git.debian.org/git/pkg-java/autocomplete.git

_______________________________________________
pkg-java-commits mailing list
[email protected]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-java-commits

Reply via email to