[autocomplete] 91/143: Tweaks to template completions, added more template completions to JavaLanguageSupport demo.

Benjamin Mesing ben at alioth.debian.org
Sat Oct 19 12:53:23 UTC 2013


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

ben pushed a commit to branch master
in repository autocomplete.

commit 67bde52b93652a3a3f32c3713eead9f26850bd55
Author: bobbylight <robert at fifesoft.com>
Date:   Tue Jun 26 03:19:29 2012 +0000

    Tweaks to template completions, added more template completions to JavaLanguageSupport demo.
---
 src/org/fife/ui/autocomplete/AutoCompletion.java   |   26 ++-
 .../autocomplete/AutoCompletionStyleContext.java   |  120 ++++++++++++
 .../fife/ui/autocomplete/FunctionCompletion.java   |   10 +-
 .../ui/autocomplete/ParameterizedCompletion.java   |   12 +-
 .../ParameterizedCompletionContext.java            |  201 +++++++++++---------
 .../fife/ui/autocomplete/TemplateCompletion.java   |    8 +
 6 files changed, 275 insertions(+), 102 deletions(-)

diff --git a/src/org/fife/ui/autocomplete/AutoCompletion.java b/src/org/fife/ui/autocomplete/AutoCompletion.java
index 9c46c9e..b5f5141 100644
--- a/src/org/fife/ui/autocomplete/AutoCompletion.java
+++ b/src/org/fife/ui/autocomplete/AutoCompletion.java
@@ -203,6 +203,13 @@ public class AutoCompletion {
 	private static final String PARAM_COMPLETE_KEY = "AutoCompletion.FunctionStart";
 
 	/**
+	 * Stores how to render auto-completion-specific highlights in text
+	 * components.
+	 */
+	private static final AutoCompletionStyleContext styleContext =
+			new AutoCompletionStyleContext();
+
+	/**
 	 * Whether debug messages should be printed to stdout as AutoCompletion
 	 * runs.
 	 */
@@ -378,6 +385,17 @@ public class AutoCompletion {
 
 
 	/**
+	 * Returns the style context describing how auto-completion related
+	 * highlights in the editor are rendered.
+	 *
+	 * @return The style context.
+	 */
+	public static AutoCompletionStyleContext getStyleContext() {
+		return styleContext;
+	}
+
+
+	/**
 	 * Returns the text component for which auto-completion is enabled.
 	 *
 	 * @return The text component, or <code>null</code> if this
@@ -432,7 +450,7 @@ public class AutoCompletion {
 	 */
 	private boolean hideParameterCompletionPopups() {
 		if (pcc!=null) {
-			pcc.setVisible(false, false);
+			pcc.deactivate();
 			pcc = null;
 			return true;
 		}
@@ -930,8 +948,8 @@ public class AutoCompletion {
 	 *        {@link CompletionProvider#getParameterListStart()} should be
 	 *        added to the text component.
 	 */
-	private void startParameterizedCompletionAssistance(ParameterizedCompletion pc,
-										boolean addParamListStart) {
+	private void startParameterizedCompletionAssistance(
+				ParameterizedCompletion pc, boolean addParamListStart) {
 
 		// Get rid of the previous tooltip window, if there is one.
 		hideParameterCompletionPopups();
@@ -949,7 +967,7 @@ public class AutoCompletion {
 		}
 
 		pcc = new ParameterizedCompletionContext(parentWindow, this, pc);
-		pcc.setVisible(true, addParamListStart);
+		pcc.activate(addParamListStart);
 
 	}
 
diff --git a/src/org/fife/ui/autocomplete/AutoCompletionStyleContext.java b/src/org/fife/ui/autocomplete/AutoCompletionStyleContext.java
new file mode 100644
index 0000000..94c1f41
--- /dev/null
+++ b/src/org/fife/ui/autocomplete/AutoCompletionStyleContext.java
@@ -0,0 +1,120 @@
+/*
+ * 06/24/2012
+ *
+ * AutoCompletionStyleContext.java - Manages styles related to auto-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.Color;
+
+
+/**
+ * Manages the colors shared across the library.
+ *
+ * @author Robert Futrell
+ * @version 1.0
+ */
+public class AutoCompletionStyleContext {
+
+	/**
+	 * The color used to denote the ending caret position for parameterized
+	 * completions.
+	 */
+	private Color parameterizedCompletionCursorPositionColor;
+
+	/**
+	 * The color used to highlight copies of editable parameters in
+	 * parameterized completions.
+	 */
+	private Color parameterCopyColor;
+
+	/**
+	 * The color of the outline highlight used to denote editable parameters
+	 * in parameterized completions.
+	 */
+	private Color parameterOutlineColor;
+
+
+	public AutoCompletionStyleContext() {
+		setParameterOutlineColor(Color.gray);
+		setParameterCopyColor(new Color(0xb4d7ff));
+		setParameterizedCompletionCursorPositionColor(new Color(0x00b400));
+	}
+
+
+	/**
+	 * Returns the color of the highlight painted on copies of editable
+	 * parameters in parameterized completions.
+	 *
+	 * @return The color used.
+	 * @see #setParameterCopyColor(Color)
+	 */
+	public Color getParameterCopyColor() {
+		return parameterCopyColor;
+	}
+
+	
+	/**
+	 * Returns the color used to denote the ending caret position for
+	 * parameterized completions.
+	 *
+	 * @return The color used.
+	 * @see #setParameterizedCompletionCursorPositionColor(Color)
+	 */
+	public Color getParameterizedCompletionCursorPositionColor() {
+		return parameterizedCompletionCursorPositionColor;
+	}
+
+
+	/**
+	 * Returns the color of the outline highlight used to denote editable
+	 * parameters in parameterized completions.
+	 *
+	 * @return The color used.
+	 * @see #setParameterOutlineColor(Color)
+	 */
+	public Color getParameterOutlineColor() {
+		return parameterOutlineColor;
+	}
+
+
+	/**
+	 * Sets the color of the highlight painted on copies of editable
+	 * parameters in parameterized completions.
+	 *
+	 * @param color The color to use.
+	 * @see #setParameterCopyColor(Color)
+	 */
+	public void setParameterCopyColor(Color parameterCopyColor) {
+		this.parameterCopyColor = parameterCopyColor;
+	}
+
+
+	/**
+	 * Sets the color used to denote the ending caret position for
+	 * parameterized completions.
+	 *
+	 * @param color The color to use.
+	 * @see #getParameterizedCompletionCursorPositionColor()
+	 */
+	public void setParameterizedCompletionCursorPositionColor(Color color) {
+		this.parameterizedCompletionCursorPositionColor = color;
+	}
+
+
+	/**
+	 * Sets the color of the outline highlight used to denote editable
+	 * parameters in parameterized completions.
+	 *
+	 * @param color The color to use.
+	 * @see #getParameterOutlineColor()
+	 */
+	public void setParameterOutlineColor(Color color) {
+		this.parameterOutlineColor = color;
+	}
+
+
+}
\ No newline at end of file
diff --git a/src/org/fife/ui/autocomplete/FunctionCompletion.java b/src/org/fife/ui/autocomplete/FunctionCompletion.java
index 427d146..8f42b11 100644
--- a/src/org/fife/ui/autocomplete/FunctionCompletion.java
+++ b/src/org/fife/ui/autocomplete/FunctionCompletion.java
@@ -164,10 +164,8 @@ public class FunctionCompletion extends VariableCompletion
 		// this tool tip.
 		int minPos = dot;
 		Position maxPos = null;
-		int defaultEndOffs = -1;
 		try {
 			maxPos = tc.getDocument().createPosition(dot-sb.length()+1);
-			defaultEndOffs = dot-sb.length();
 		} catch (BadLocationException ble) {
 			ble.printStackTrace(); // Never happens
 		}
@@ -230,6 +228,14 @@ public class FunctionCompletion extends VariableCompletion
 
 
 	/**
+	 * {@inheritDoc}
+	 */
+	public boolean getShowParameterToolTip() {
+		return true;
+	}
+
+
+	/**
 	 * Returns the text to insert for a parameter.
 	 *
 	 * @param param The parameter.
diff --git a/src/org/fife/ui/autocomplete/ParameterizedCompletion.java b/src/org/fife/ui/autocomplete/ParameterizedCompletion.java
index acd674f..2b4531e 100644
--- a/src/org/fife/ui/autocomplete/ParameterizedCompletion.java
+++ b/src/org/fife/ui/autocomplete/ParameterizedCompletion.java
@@ -55,10 +55,16 @@ public interface ParameterizedCompletion extends Completion {
 
 
 	/**
-	 * A parameter passed to a parameterized {@link Completion}.
+	 * Returns whether a tool tip displaying assistance for each parameter
+	 * while it is being edited is appropriate for this completion.
 	 *
-	 * @author Robert Futrell
-	 * @version 1.0
+	 * @return Whether the tool tip is appropriate to display.
+	 */
+	public boolean getShowParameterToolTip();
+
+
+	/**
+	 * A parameter passed to a parameterized {@link Completion}.
 	 */
 	public static class Parameter {
 
diff --git a/src/org/fife/ui/autocomplete/ParameterizedCompletionContext.java b/src/org/fife/ui/autocomplete/ParameterizedCompletionContext.java
index 62e2a5e..8cd6fe4 100644
--- a/src/org/fife/ui/autocomplete/ParameterizedCompletionContext.java
+++ b/src/org/fife/ui/autocomplete/ParameterizedCompletionContext.java
@@ -9,7 +9,6 @@
  */
 package org.fife.ui.autocomplete;
 
-import java.awt.Color;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.Window;
@@ -39,6 +38,7 @@ import javax.swing.text.Highlighter;
 import javax.swing.text.JTextComponent;
 import javax.swing.text.Position;
 import javax.swing.text.Highlighter.Highlight;
+import javax.swing.text.Highlighter.HighlightPainter;
 
 import org.fife.ui.autocomplete.ParameterizedCompletion.Parameter;
 import org.fife.ui.autocomplete.ParameterizedCompletionInsertionInfo.ReplacementCopy;
@@ -76,6 +76,11 @@ class ParameterizedCompletionContext {
 	private ParameterizedCompletion pc;
 
 	/**
+	 * Whether parameterized completion assistance is active.
+	 */
+	private boolean active;
+
+	/**
 	 * A tool tip displaying the currently edited parameter name and type.
 	 */
 	private ParameterizedCompletionDescriptionToolTip tip;
@@ -85,6 +90,8 @@ class ParameterizedCompletionContext {
 	 */
 	private Highlighter.HighlightPainter p;
 
+	private Highlighter.HighlightPainter endingP;
+
 	private Highlighter.HighlightPainter paramCopyP;
 
 	/**
@@ -167,13 +174,61 @@ class ParameterizedCompletionContext {
 		this.pc = pc;
 		listener = new Listener();
 
-		p = new OutlineHighlightPainter(Color.GRAY);
-		paramCopyP = new ChangeableHighlightPainter(new Color(255, 224, 224));
+		AutoCompletionStyleContext sc = AutoCompletion.getStyleContext();
+		p = new OutlineHighlightPainter(sc.getParameterOutlineColor());
+		endingP = new OutlineHighlightPainter(
+				sc.getParameterizedCompletionCursorPositionColor());
+		paramCopyP = new ChangeableHighlightPainter(sc.getParameterCopyColor());
 		tags = new ArrayList(1); // Usually small
 		paramCopyInfos = new ArrayList(1);
 
-		tip = new ParameterizedCompletionDescriptionToolTip(parentWindow,
-															this, ac, pc);
+	}
+
+
+	/**
+	 * Activates parameter completion support.
+	 *
+	 * @param addParamListStart Whether the parameter list start token should
+	 *        be inserted at the caret position before the parameters.
+	 * @see #deactivate()
+	 */
+	public void activate(boolean addParamListStart) {
+
+		if (active) {
+			return;
+		}
+
+		active = true;
+		JTextComponent tc = ac.getTextComponent();
+		lastSelectedParam = -1;
+
+		if (pc.getShowParameterToolTip()) {
+			tip = new ParameterizedCompletionDescriptionToolTip(
+					parentWindow, this, ac, pc);
+			try {
+				int dot = tc.getCaretPosition();
+				Rectangle r = tc.modelToView(dot);
+				Point p = new Point(r.x, r.y);
+				SwingUtilities.convertPointToScreen(p, tc);
+				r.x = p.x;
+				r.y = p.y;
+				tip.setLocationRelativeTo(r);
+				tip.setVisible(true);
+			} catch (BadLocationException ble) { // Should never happen
+				UIManager.getLookAndFeel().provideErrorFeedback(tc);
+				ble.printStackTrace();
+				tip = null;
+			}
+		}
+
+		listener.install(tc, addParamListStart);
+		// First time through, we'll need to create this window.
+		if (paramChoicesWindow==null) {
+			paramChoicesWindow = createParamChoicesWindow();
+		}
+		lastSelectedParam = getCurrentParameterIndex();
+		prepareParamChoicesWindow();
+		paramChoicesWindow.setVisible(true);
 
 	}
 
@@ -193,6 +248,27 @@ class ParameterizedCompletionContext {
 
 
 	/**
+	 * Hides any popup windows and terminates parameterized completion
+	 * assistance.
+	 *
+	 * @see #activate(boolean)
+	 */
+	public void deactivate() {
+		if (!active) {
+			return;
+		}
+		active = false;
+		listener.uninstall();
+		if (tip!=null) {
+			tip.setVisible(false);
+		}
+		if (paramChoicesWindow!=null) {
+			paramChoicesWindow.setVisible(false);
+		}
+	}
+
+
+	/**
 	 * Returns the text inserted for the parameter containing the specified
 	 * offset.
 	 *
@@ -336,11 +412,12 @@ class ParameterizedCompletionContext {
 
 
 	public List getParameterHighlights() {
-		List paramHighlights = new ArrayList(1);
+		List paramHighlights = new ArrayList(2);
 		JTextComponent tc = ac.getTextComponent();
 		Highlight[] highlights = tc.getHighlighter().getHighlights();
 		for (int i=0; i<highlights.length; i++) {
-			if (highlights[i].getPainter()==p) {
+			HighlightPainter painter = highlights[i].getPainter();
+			if (painter==p || painter==endingP) {
 				paramHighlights.add(highlights[i]);
 			}
 		}
@@ -451,7 +528,7 @@ class ParameterizedCompletionContext {
 		int tagCount = tags.size();
 		if (tagCount==0) {
 			tc.setCaretPosition(maxPos.getOffset());
-			setVisible(false, false);
+			deactivate();
 		}
 
 		Highlight currentNext = null;
@@ -497,7 +574,7 @@ class ParameterizedCompletionContext {
 		int tagCount = tags.size();
 		if (tagCount==0) { // Should never happen
 			tc.setCaretPosition(maxPos.getOffset());
-			setVisible(false, false);
+			deactivate();
 		}
 
 		int dot = tc.getCaretPosition();
@@ -536,7 +613,7 @@ class ParameterizedCompletionContext {
 		}
 		else {
 			tc.setCaretPosition(maxPos.getOffset());
-			setVisible(false, false);
+			deactivate();
 		}
 
 	}
@@ -551,7 +628,7 @@ class ParameterizedCompletionContext {
 			// Typing in an "end parameter" => stop parameter assistance.
 			Parameter param = pc.getParam(index);
 			if (param.isEndParam()) {
-				setVisible(false, false);
+				deactivate();
 				return;
 			}
 
@@ -579,7 +656,7 @@ class ParameterizedCompletionContext {
 		}
 
 		else { // Probably the "end parameter" for FunctionCompletions.
-			setVisible(false, false);
+			deactivate();
 		}
 
 	}
@@ -680,62 +757,6 @@ class ParameterizedCompletionContext {
 
 
 	/**
-	 * Toggles the visibility of this tool tip.
-	 *
-	 * @param visible Whether the tool tip should be visible.
-	 * @param addParamListStart Whether or not
-	 *        {@link CompletionProvider#getParameterListStart()} should be
-	 *        added to the text component.  If <code>visible</code> is
-	 *        <code>false</code>, this parameter is ignored.
-	 */
-	public void setVisible(boolean visible, boolean addParamListStart) {
-
-		if (visible!=tip.isVisible()) {
-
-			JTextComponent tc = ac.getTextComponent();
-
-			if (visible) {
-
-				lastSelectedParam = -1;
-
-				try {
-					int dot = tc.getCaretPosition();
-					Rectangle r = tc.modelToView(dot);
-					Point p = new Point(r.x, r.y);
-					SwingUtilities.convertPointToScreen(p, tc);
-					r.x = p.x;
-					r.y = p.y;
-					tip.setLocationRelativeTo(r);
-				} catch (BadLocationException ble) { // Should never happen
-					UIManager.getLookAndFeel().provideErrorFeedback(tc);
-					ble.printStackTrace();
-				}
-
-				listener.install(tc, addParamListStart);
-				// First time through, we'll need to create this window.
-				if (paramChoicesWindow==null) {
-					paramChoicesWindow = createParamChoicesWindow();
-				}
-				lastSelectedParam = getCurrentParameterIndex();
-				prepareParamChoicesWindow();
-
-			}
-			else {
-				listener.uninstall();
-			}
-
-			tip.setVisible(visible);
-			if (paramChoicesWindow!=null) {
-				// Only really needed to hide the window (i.e. visible==false)
-				paramChoicesWindow.setVisible(visible);
-			}
-
-		}
-
-	}
-
-
-	/**
 	 * Removes the key bindings we installed.
 	 *
 	 * @see #installKeyBindings()
@@ -826,7 +847,9 @@ class ParameterizedCompletionContext {
 
 	private void updateToolTipText(int selectedParam) {
 		if (selectedParam!=lastSelectedParam) {
-			tip.updateText(selectedParam);
+			if (tip!=null) {
+				tip.updateText(selectedParam);
+			}
 			this.lastSelectedParam = selectedParam;
 		}
 	}
@@ -837,7 +860,9 @@ class ParameterizedCompletionContext {
 	 * manages.
 	 */
 	public void updateUI() {
-		tip.updateUI();
+		if (tip!=null) {
+			tip.updateUI();
+		}
 		if (paramChoicesWindow!=null) {
 			paramChoicesWindow.updateUI();
 		}
@@ -846,9 +871,6 @@ class ParameterizedCompletionContext {
 
 	/**
 	 * Called when the user presses Enter while entering parameters.
-	 *
-	 * @author Robert Futrell
-	 * @version 1.0
 	 */
 	private class GotoEndAction extends AbstractAction {
 
@@ -865,7 +887,7 @@ class ParameterizedCompletionContext {
 			// Otherwise, just move to the end.
 			JTextComponent tc = ac.getTextComponent();
 			tc.setCaretPosition(defaultEndOffs.getOffset());
-			setVisible(false, false);
+			deactivate();
 
 		}
 
@@ -875,9 +897,6 @@ class ParameterizedCompletionContext {
 	/**
 	 * Called when the user types the character marking the closing of the
 	 * parameter list, such as '<code>)</code>'.
-	 *
-	 * @author Robert Futrell
-	 * @version 1.0
 	 */
 	private class ClosingAction extends AbstractAction {
 
@@ -911,7 +930,7 @@ class ParameterizedCompletionContext {
 					tc.setCaretPosition(tc.getCaretPosition()+1);
 				}
 
-				setVisible(false, false);
+				deactivate();
 
 			}
 
@@ -939,9 +958,6 @@ class ParameterizedCompletionContext {
 
 	/**
 	 * Action performed when the user hits the escape key.
-	 *
-	 * @author Robert Futrell
-	 * @version 1.0
 	 */
 	private class HideAction extends AbstractAction {
 
@@ -955,7 +971,7 @@ class ParameterizedCompletionContext {
 				paramChoicesWindow = null;
 			}
 			else {
-				setVisible(false, false);
+				deactivate();
 			}
 		}
 
@@ -965,9 +981,6 @@ class ParameterizedCompletionContext {
 	/**
 	 * Listens for various events in the text component while this tool tip
 	 * is visible.
-	 *
-	 * @author Robert Futrell
-	 * @version 1.0
 	 */
 	private class Listener implements FocusListener, CaretListener,
 							DocumentListener {
@@ -981,17 +994,16 @@ class ParameterizedCompletionContext {
 		 */
 		public void caretUpdate(CaretEvent e) {
 			if (maxPos==null) { // Sanity check
-				setVisible(false, false);
+				deactivate();
 				return;
 			}
 			int dot = e.getDot();
 			if (dot<minPos || dot>=maxPos.getOffset()) {
-				System.err.println(">>>>> dot==" + dot + ", " + minPos + ", " + maxPos);
-				setVisible(false, false);
+				deactivate();
 				return;
 			}
 			paramPrefix = updateToolTipText();
-			if (tip.isVisible()) {
+			if (active) {
 				prepareParamChoicesWindow();
 			}
 		}
@@ -1017,7 +1029,7 @@ class ParameterizedCompletionContext {
 		 * @param e The event.
 		 */
 		public void focusLost(FocusEvent e) {
-			setVisible(false, false);
+			deactivate();
 		}
 
 
@@ -1068,10 +1080,13 @@ class ParameterizedCompletionContext {
 				tc.replaceSelection(info.getTextToInsert());
 
 				// Add highlights around the parameters.
-				for (int i=0; i<info.getReplacementCount(); i++) {
+				final int replacementCount = info.getReplacementCount();
+				for (int i=0; i<replacementCount; i++) {
 					DocumentRange dr = info.getReplacementLocation(i);
+					HighlightPainter painter = i<replacementCount-1 ? p : endingP;
 					 // "-1" is a workaround for Java Highlight issues.
-					tags.add(h.addHighlight(dr.getStartOffset()-1, dr.getEndOffset(), p));
+					tags.add(h.addHighlight(
+							dr.getStartOffset()-1, dr.getEndOffset(), painter));
 				}
 				for (int i=0; i<info.getReplacementCopyCount(); i++) {
 					ReplacementCopy rc = info.getReplacementCopy(i);
@@ -1163,7 +1178,7 @@ class ParameterizedCompletionContext {
 				oldAction.actionPerformed(e);
 			}
 			else {
-				setVisible(false, false);
+				deactivate();
 			}
 		}
 
diff --git a/src/org/fife/ui/autocomplete/TemplateCompletion.java b/src/org/fife/ui/autocomplete/TemplateCompletion.java
index 6e5efad..c2b35c4 100644
--- a/src/org/fife/ui/autocomplete/TemplateCompletion.java
+++ b/src/org/fife/ui/autocomplete/TemplateCompletion.java
@@ -125,6 +125,14 @@ public class TemplateCompletion extends AbstractCompletion
 	}
 
 
+	/**
+	 * {@inheritDoc}
+	 */
+	public boolean getShowParameterToolTip() {
+		return false;
+	}
+
+
 	public ParameterizedCompletionInsertionInfo getInsertionInfo(
 			JTextComponent tc, boolean addParamStartList,
 			boolean replaceTabsWithSpaces) {

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



More information about the pkg-java-commits mailing list