[autocomplete] 59/143: Improvements to parameter completion choices. Keyboard shortcuts (escape, enter, tab) should now mimic Eclipse when param completion suggestion window is visible.

Benjamin Mesing ben at alioth.debian.org
Sat Oct 19 12:53:17 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 beb4751a625aa7ea9dd314aa32ae097f39a9da73
Author: bobbylight <robert at fifesoft.com>
Date:   Thu Dec 16 23:51:02 2010 +0000

    Improvements to parameter completion choices.  Keyboard shortcuts (escape, enter, tab) should now mimic Eclipse when param completion suggestion window is visible.
---
 .../ParameterizedCompletionChoicesWindow.java      |   41 +++++--
 .../ParameterizedCompletionDescriptionToolTip.java |  129 +++++++++++++++-----
 2 files changed, 127 insertions(+), 43 deletions(-)

diff --git a/src/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java b/src/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java
index 6914879..c1f7783 100644
--- a/src/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java
+++ b/src/org/fife/ui/autocomplete/ParameterizedCompletionChoicesWindow.java
@@ -99,6 +99,17 @@ public class ParameterizedCompletionChoicesWindow extends JWindow {
 
 
 	/**
+	 * Returns the selected value.
+	 *
+	 * @return The selected value, or <code>null</code> if nothing is
+	 *         selected.
+	 */
+	public String getSelectedChoice() {
+		return (String)list.getSelectedValue();
+	}
+
+
+	/**
 	 * Changes the selected index.
 	 *
 	 * @param amount The amount by which to change the selected index.
@@ -187,9 +198,10 @@ public class ParameterizedCompletionChoicesWindow extends JWindow {
 	 * @param param The index of the parameter the caret is currently in.
 	 *        This may be <code>-1</code> if not in a parameter (i.e., on
 	 *        the comma between parameters).
-	 * @param alreadyEntered Text in the parameter before the dot.
+	 * @param prefix Text in the parameter before the dot.  This may
+	 *        be <code>null</code> to represent the empty string.
 	 */
-	public void setParameter(int param, String alreadyEntered) {
+	public void setParameter(int param, String prefix) {
 
 		model.clear();
 
@@ -198,7 +210,8 @@ public class ParameterizedCompletionChoicesWindow extends JWindow {
 			List choices = (List)choicesListList.get(param);
 			for (Iterator i=choices.iterator(); i.hasNext(); ) {
 				String choice = (String)i.next();
-				if (Util.startsWithIgnoreCase(choice, alreadyEntered)) {
+				if (prefix==null ||
+						Util.startsWithIgnoreCase(choice, prefix)) {
 					model.addElement(choice);
 				}
 			}
@@ -206,7 +219,17 @@ public class ParameterizedCompletionChoicesWindow extends JWindow {
 			int visibleRowCount = Math.min(model.size(), 10);
 			list.setVisibleRowCount(visibleRowCount);
 
-			pack();
+			// Toggle visibility, if necessary.
+			if (visibleRowCount==0 && isVisible()) {
+				setVisible(false);
+			}
+			else if (visibleRowCount>0) {
+				pack();
+				list.setSelectedIndex(0);
+				if (!isVisible()) {
+					setVisible(true);
+				}
+			}
 
 		}
 
@@ -219,17 +242,13 @@ public class ParameterizedCompletionChoicesWindow extends JWindow {
 	 * @param visible Whether this window should be visible.
 	 */
 	public void setVisible(boolean visible) {
-
 		if (visible!=isVisible()) {
-
-			if (visible) {
-				
+			// i.e. if no possibilities matched what's been typed
+			if (visible && list.getVisibleRowCount()==0) {
+				return;
 			}
-
 			super.setVisible(visible);
-
 		}
-
 	}
 
 
diff --git a/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java b/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java
index c03d698..e73353f 100644
--- a/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java
+++ b/src/org/fife/ui/autocomplete/ParameterizedCompletionDescriptionToolTip.java
@@ -120,7 +120,7 @@ class ParameterizedCompletionDescriptionToolTip {
 	 * {@link #paramChoicesWindow} is non-<code>null</code>, this is used to
 	 * determine what parameter choices to actually show.
 	 */
-	private String paramAlreadyEntered;
+	private String paramPrefix;
 
 	/**
 	 * The currently "selected" parameter in the displayed text.
@@ -201,12 +201,13 @@ class ParameterizedCompletionDescriptionToolTip {
 
 
 	/**
-	 * Returns the starting offset of the current parameter.
+	 * Returns the highlight of the current parameter.
 	 *
-	 * @return The current parameter's starting offset, or <code>-1</code> if
+	 * @return The current parameter's highlight, or <code>null</code> if
 	 *         the caret is not in a parameter's bounds.
+	 * @see #getCurrentParameterStartOffset()
 	 */
-	private int getCurrentParameterStartOffset() {
+	private Highlight getCurrentParameterHighlight() {
 
 		JTextComponent tc = ac.getTextComponent();
 		int dot = tc.getCaretPosition();
@@ -218,15 +219,28 @@ class ParameterizedCompletionDescriptionToolTip {
 		for (int i=0; i<paramHighlights.size(); i++) {
 			Highlight h = (Highlight)paramHighlights.get(i);
 			if (dot>=h.getStartOffset() && dot<h.getEndOffset()) {
-				return h.getStartOffset() + 1;
+				return h;
 			}
 		}
 
-		return -1;
+		return null;
 
 	}
 
 
+	/**
+	 * Returns the starting offset of the current parameter.
+	 *
+	 * @return The current parameter's starting offset, or <code>-1</code> if
+	 *         the caret is not in a parameter's bounds.
+	 * @see #getCurrentParameterHighlight()
+	 */
+	private int getCurrentParameterStartOffset() {
+		Highlight h = getCurrentParameterHighlight();
+		return h!=null ? h.getStartOffset()+1 : -1;
+	}
+
+
 	private List getParameterHighlights() {
 		List paramHighlights = new ArrayList(1);
 		JTextComponent tc = ac.getTextComponent();
@@ -367,21 +381,28 @@ class ParameterizedCompletionDescriptionToolTip {
 		int selStart = tc.getSelectionStart()-1; // Workaround for Java Highlight issues.
 		Highlight currentPrev = null;
 		int pos = 0;
-		Highlighter h = tc.getHighlighter();
-		Highlight[] highlights = h.getHighlights();
-		for (int i=0; i<highlights.length; i++) {
-			Highlight hl = highlights[i];
-			if (hl.getPainter()==p) { // Only way to identify our own highlights
-				if (currentPrev==null || currentPrev.getStartOffset()>=dot ||
-						(hl.getStartOffset()<selStart &&
-						hl.getStartOffset()>currentPrev.getStartOffset())) {
-					currentPrev = hl;
-					pos = i;
-				}
+		List highlights = getParameterHighlights();
+
+		for (int i=0; i<highlights.size(); i++) {
+			Highlight h = (Highlight)highlights.get(i);
+			if (currentPrev==null || currentPrev.getStartOffset()>=dot ||
+					(h.getStartOffset()<selStart &&
+					h.getStartOffset()>currentPrev.getStartOffset())) {
+				currentPrev = h;
+				pos = i;
 			}
 		}
 
-		if (currentPrev!=null && dot>currentPrev.getStartOffset()) {
+		// Loop back from param 0 to last param.
+		if (pos==0 && lastSelectedParam==0 && highlights.size()>1) {
+			pos = highlights.size() - 1;
+			currentPrev = (Highlight)highlights.get(pos);
+			 // "+1" is a workaround for Java Highlight issues.
+			tc.setSelectionStart(currentPrev.getStartOffset()+1);
+			tc.setSelectionEnd(currentPrev.getEndOffset());
+			updateText(pos);
+		}
+		else if (currentPrev!=null && dot>currentPrev.getStartOffset()) {
 			 // "+1" is a workaround for Java Highlight issues.
 			tc.setSelectionStart(currentPrev.getStartOffset()+1);
 			tc.setSelectionEnd(currentPrev.getEndOffset());
@@ -400,12 +421,12 @@ class ParameterizedCompletionDescriptionToolTip {
 	 */
 	private void prepareParamChoicesWindow() {
 
+		// If this window was set to null, the user pressed Escape to hide it
 		if (paramChoicesWindow!=null) {
 
 			int offs = getCurrentParameterStartOffset();
 			if (offs==-1) {
 				paramChoicesWindow.setVisible(false);
-				paramChoicesWindow = null;
 				return;
 			}
 
@@ -422,9 +443,8 @@ class ParameterizedCompletionDescriptionToolTip {
 				ble.printStackTrace();
 			}
 
-			paramChoicesWindow.setParameter(lastSelectedParam,
-											paramAlreadyEntered);
-			paramChoicesWindow.setVisible(true);
+			// Toggles visibility, if necessary.
+			paramChoicesWindow.setParameter(lastSelectedParam, paramPrefix);
 
 		}
 
@@ -497,6 +517,10 @@ class ParameterizedCompletionDescriptionToolTip {
 
 			if (visible) {
 				listener.install(tc, addParamListStart);
+				// First time through, we'll need to create this window.
+				if (paramChoicesWindow==null) {
+					paramChoicesWindow = createParamChoicesWindow();
+				}
 				prepareParamChoicesWindow();
 			}
 			else {
@@ -505,6 +529,7 @@ class ParameterizedCompletionDescriptionToolTip {
 
 			tooltip.setVisible(visible);
 			if (paramChoicesWindow!=null) {
+				// Only really needed to hide the window (i.e. visible==false)
 				paramChoicesWindow.setVisible(visible);
 			}
 
@@ -571,23 +596,29 @@ class ParameterizedCompletionDescriptionToolTip {
 	private boolean updateText() {
 
 		JTextComponent tc = ac.getTextComponent();
-		int dot = tc.getCaretPosition();
-		if (dot>0) {
-			dot--; // Workaround for Java Highlight issues
-		}
+		int dot = tc.getSelectionStart();
+		int mark = tc.getSelectionEnd();
 		int index = -1;
+		paramPrefix = null;
 
 		List paramHighlights = getParameterHighlights();
 		for (int i=0; i<paramHighlights.size(); i++) {
 			Highlight h = (Highlight)paramHighlights.get(i);
 			// "+1" because of param hack - see OutlineHighlightPainter
 			int start = h.getStartOffset()+1;
-			if (dot>=start && dot<h.getEndOffset()) {
+			if (dot>=start && dot<=h.getEndOffset()) {
 				try {
-					paramAlreadyEntered = tc.getText(start, dot-start);
+					// All text selected => offer all suggestions
+					if (dot==start && mark==h.getEndOffset()) {
+						paramPrefix = null;
+					}
+					// Not everything selected => use prefix before selection
+					else {
+						paramPrefix = tc.getText(start, dot-start);
+					}
 				} catch (BadLocationException ble) {
 					ble.printStackTrace();
-					paramAlreadyEntered = null;
+					paramPrefix = null;
 				}
 				index = i;
 				break;
@@ -669,9 +700,33 @@ class ParameterizedCompletionDescriptionToolTip {
 	private class GotoEndAction extends AbstractAction {
 
 		public void actionPerformed(ActionEvent e) {
+
+			// If the param choices window is visible and something is chosen,
+			// replace the parameter with it and move to the next one.
+			if (paramChoicesWindow!=null && paramChoicesWindow.isVisible()) {
+				String choice = paramChoicesWindow.getSelectedChoice();
+				if (choice!=null) {
+					JTextComponent tc = ac.getTextComponent();
+					Highlight h = getCurrentParameterHighlight();
+					if (h!=null) {
+						 // "+1" is a workaround for Java Highlight issues.
+						tc.setSelectionStart(h.getStartOffset()+1);
+						tc.setSelectionEnd(h.getEndOffset());
+						tc.replaceSelection(choice);
+						moveToNextParam();
+					}
+					else {
+						UIManager.getLookAndFeel().provideErrorFeedback(tc);
+					}
+					return;
+				}
+			}
+
+			// Otherwise, just move to the end.
 			JTextComponent tc = ac.getTextComponent();
 			tc.setCaretPosition(maxPos.getOffset());
 			setVisible(false, false);
+
 		}
 
 	}
@@ -774,7 +829,17 @@ class ParameterizedCompletionDescriptionToolTip {
 	private class HideAction extends AbstractAction {
 
 		public void actionPerformed(ActionEvent e) {
-			setVisible(false, false);
+			// On first escape press, if the param choices window is visible,
+			// just remove it, but keep ability to tab through params.  If
+			// param choices window isn't visible, or second escape press,
+			// exit tabbing through params entirely.
+			if (paramChoicesWindow!=null && paramChoicesWindow.isVisible()) {
+				paramChoicesWindow.setVisible(false);
+				paramChoicesWindow = null;
+			}
+			else {
+				setVisible(false, false);
+			}
 		}
 
 	}
@@ -804,8 +869,8 @@ class ParameterizedCompletionDescriptionToolTip {
 				setVisible(false, false);
 				return;
 			}
-			boolean updated = updateText();
-			if (updated) {
+			/*boolean updated = */updateText();
+			if (tooltip.isVisible()) {
 				prepareParamChoicesWindow();
 			}
 		}

-- 
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