[autocomplete] 04/143: Fixing input issue when >1 JTextComponent has an AutoComplete installed. Adding beginnings of language-aware completion support.
Benjamin Mesing
ben at alioth.debian.org
Sat Oct 19 12:53:07 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 c6488a255748db475dd87a1710afe8cadd260a77
Author: bobbylight <robert at fifesoft.com>
Date: Sat Jan 3 06:36:25 2009 +0000
Fixing input issue when >1 JTextComponent has an AutoComplete installed. Adding beginnings of language-aware completion support.
---
.classpath | 1 +
.../ui/autocomplete/AutoCompletePopupWindow.java | 1 -
src/org/fife/ui/autocomplete/AutoCompletion.java | 116 ++++++++-----
src/org/fife/ui/autocomplete/BasicCompletion.java | 107 ++++++++++++
.../fife/ui/autocomplete/FunctionCompletion.java | 12 +-
.../LanguageAwareCompletionProvider.java | 182 ++++++++++++++++++++
.../ProceduralLanguageCellRenderer.java | 6 +-
.../ProceduralLanguageCompletionProvider.java | 156 +++++++++++------
.../fife/ui/autocomplete/VariableCompletion.java | 18 +-
9 files changed, 500 insertions(+), 99 deletions(-)
diff --git a/.classpath b/.classpath
index fe5be44..3e0eb1d 100644
--- a/.classpath
+++ b/.classpath
@@ -4,5 +4,6 @@
<classpathentry kind="src" path="i18n"/>
<classpathentry kind="src" path="img"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry combineaccessrules="false" kind="src" path="/RSyntaxTextArea"/>
<classpathentry kind="output" path="bin"/>
</classpath>
diff --git a/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java b/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java
index ef5fe05..0dafc7e 100644
--- a/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java
+++ b/src/org/fife/ui/autocomplete/AutoCompletePopupWindow.java
@@ -32,7 +32,6 @@ import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
-
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JList;
diff --git a/src/org/fife/ui/autocomplete/AutoCompletion.java b/src/org/fife/ui/autocomplete/AutoCompletion.java
index 2f40157..91fe502 100644
--- a/src/org/fife/ui/autocomplete/AutoCompletion.java
+++ b/src/org/fife/ui/autocomplete/AutoCompletion.java
@@ -42,7 +42,7 @@ import javax.swing.text.*;
* It also handles communication between the CompletionProvider and the actual
* popup Window.
*/
-public class AutoCompletion implements HierarchyListener, ComponentListener {
+public class AutoCompletion implements HierarchyListener {
/**
* The text component we're providing completion for.
@@ -94,11 +94,28 @@ public class AutoCompletion implements HierarchyListener, ComponentListener {
private KeyStroke trigger;
/**
+ * The previous key in the text component's <code>InputMap</code> for the
+ * trigger key.
+ */
+ private Object oldTriggerKey;
+
+ /**
* The action previously assigned to {@link #trigger}, so we can reset it
* if the user disables auto-completion.
*/
private Action oldTriggerAction;
+ /**
+ * Listens for events in the parent window that affect the visibility of
+ * the popup window.
+ */
+ private Listener parentWindowListener;
+
+ /**
+ * The key used in the input map for the AutoComplete action.
+ */
+ private static final String ACTION_MAP_KEY = "AutoComplete";
+
/**
* Constructor.
@@ -112,25 +129,7 @@ public class AutoCompletion implements HierarchyListener, ComponentListener {
setAutoCompleteEnabled(true);
setAutoCompleteSingleChoices(true);
setShowDescWindow(false);
- }
-
-
- public void componentHidden(ComponentEvent e) {
- hidePopupWindow();
- }
-
-
- public void componentMoved(ComponentEvent e) {
- hidePopupWindow();
- }
-
-
- public void componentResized(ComponentEvent e) {
- hidePopupWindow();
- }
-
-
- public void componentShown(ComponentEvent e) {
+ parentWindowListener = new Listener();
}
@@ -163,7 +162,7 @@ public class AutoCompletion implements HierarchyListener, ComponentListener {
*/
public static KeyStroke getDefaultTriggerKey() {
// Default to CTRL, even on Mac, since Ctrl+Space activates Spotlight
- int mask = Event.CTRL_MASK;
+ int mask = InputEvent.CTRL_MASK;
return KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, mask);
}
@@ -187,13 +186,6 @@ public class AutoCompletion implements HierarchyListener, ComponentListener {
}
- CompletionProvider getProviderAtCaretPosition() {
- // TODO: Delegate to provider in case the provider itself delegates.
- //return provider.getProviderAtCaretPosition(textComponent);
- return provider;
- }
-
-
/**
* Returns whether the "description window" should be shown alongside
* the completion window.
@@ -245,10 +237,10 @@ public class AutoCompletion implements HierarchyListener, ComponentListener {
parentWindow = SwingUtilities.getWindowAncestor(textComponent);
if (parentWindow!=oldParentWindow) {
if (oldParentWindow!=null) {
- oldParentWindow.removeComponentListener(this);
+ parentWindowListener.removeFrom(oldParentWindow);
}
if (parentWindow!=null) {
- parentWindow.addComponentListener(this);
+ parentWindowListener.addTo(parentWindow);
}
}
@@ -330,9 +322,12 @@ try {
* @see #uninstallTriggerKey()
*/
private void installTriggerKey(KeyStroke ks) {
- Keymap km = textComponent.getKeymap();
- oldTriggerAction = km.getAction(ks);
- km.addActionForKeyStroke(ks, new AutoCompleteAction());
+ InputMap im = textComponent.getInputMap();
+ oldTriggerKey = im.get(ks);
+ im.put(ks, ACTION_MAP_KEY);
+ ActionMap am = textComponent.getActionMap();
+ oldTriggerAction = am.get(ACTION_MAP_KEY);
+ am.put(ACTION_MAP_KEY, new AutoCompleteAction());
}
@@ -525,7 +520,7 @@ try {
uninstallTriggerKey();
textComponent.removeHierarchyListener(this);
if (parentWindow!=null) {
- parentWindow.removeComponentListener(this);
+ parentWindowListener.removeFrom(parentWindow);
}
textComponent = null;
}
@@ -539,13 +534,10 @@ try {
* @see #installTriggerKey(KeyStroke)
*/
private void uninstallTriggerKey() {
- Keymap km = textComponent.getKeymap();
- if (oldTriggerAction!=null) {
- km.addActionForKeyStroke(trigger, oldTriggerAction);
- }
- else {
- km.removeKeyStrokeBinding(trigger);
- }
+ InputMap im = textComponent.getInputMap();
+ im.put(trigger, oldTriggerKey);
+ ActionMap am = textComponent.getActionMap();
+ am.put(ACTION_MAP_KEY, oldTriggerAction);
}
@@ -582,4 +574,46 @@ try {
}
+ /**
+ * Listens for events in the parent window of the text component with
+ * autocompletion enabled.
+ *
+ * @author Robert Futrell
+ * @version 1.0
+ */
+ private class Listener extends ComponentAdapter
+ implements WindowFocusListener {
+
+ public void addTo(Window w) {
+ w.addComponentListener(this);
+ w.addWindowFocusListener(this);
+ }
+
+ public void componentHidden(ComponentEvent e) {
+ hidePopupWindow();
+ }
+
+ public void componentMoved(ComponentEvent e) {
+ hidePopupWindow();
+ }
+
+ public void componentResized(ComponentEvent e) {
+ hidePopupWindow();
+ }
+
+ public void removeFrom(Window w) {
+ w.removeComponentListener(this);
+ w.removeWindowFocusListener(this);
+ }
+
+ public void windowGainedFocus(WindowEvent e) {
+ }
+
+ public void windowLostFocus(WindowEvent e) {
+ hidePopupWindow();
+ }
+
+ }
+
+
}
\ No newline at end of file
diff --git a/src/org/fife/ui/autocomplete/BasicCompletion.java b/src/org/fife/ui/autocomplete/BasicCompletion.java
new file mode 100644
index 0000000..a2034fd
--- /dev/null
+++ b/src/org/fife/ui/autocomplete/BasicCompletion.java
@@ -0,0 +1,107 @@
+/*
+ * 01/03/2009
+ *
+ * BasicCompletion.java - A straightforward Completion implementation.
+ * Copyright (C) 2009 Robert Futrell
+ * robert_futrell at users.sourceforge.net
+ * http://fifesoft.com/rsyntaxtextarea
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+package org.fife.ui.autocomplete;
+
+
+/**
+ * A straightforward completion implementation. This implementation can be
+ * used if you have a relatively short number of static completions with no
+ * (or short) summaries.
+ *
+ * @author Robert Futrell
+ * @version 1.0
+ */
+public class BasicCompletion extends AbstractCompletion {
+
+ private String replacementText;
+ private String shortDesc;
+ private String summary;
+
+
+ /**
+ * Constructor.
+ *
+ * @param provider The parent completion provider.
+ * @param replacementText The text to replace.
+ */
+ public BasicCompletion(CompletionProvider provider, String replacementText){
+ this(provider, replacementText, null, null);
+ }
+
+
+ /**
+ * Constructor.
+ *
+ * @param provider The parent completion provider.
+ * @param replacementText The text to replace.
+ * @param shortDesc A short description of the completion. This will be
+ * displayed in the completion list. This may be <code>null</code>.
+ * @param summary The summary of this completion. This should be HTML.
+ * This may be <code>null</code>.
+ */
+ public BasicCompletion(CompletionProvider provider, String replacementText,
+ String shortDesc, String summary) {
+ super(provider);
+ this.replacementText = replacementText;
+ this.shortDesc = shortDesc;
+ this.summary = summary;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getReplacementText() {
+ return replacementText;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getSummary() {
+ return summary;
+ }
+
+
+ /**
+ * Returns a string representation of this completion. If the short
+ * description is not <code>null</code>, this method will return:
+ *
+ * <code>getReplacementText() + " - " + shortDesc</code>
+ *
+ * otherwise, it will return <tt>getReplacementText()</tt>.
+ *
+ * @return A string representation of this completion.
+ */
+ public String toString() {
+ if (shortDesc==null) {
+ return getReplacementText();
+ }
+ else {
+ return getReplacementText() + " - " + shortDesc;
+ }
+ }
+
+
+}
\ 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 74352f7..785557e 100644
--- a/src/org/fife/ui/autocomplete/FunctionCompletion.java
+++ b/src/org/fife/ui/autocomplete/FunctionCompletion.java
@@ -58,8 +58,9 @@ public class FunctionCompletion extends VariableCompletion {
sb.append("<html><b>");
// Add the return type if applicable (C macros like NULL have no type).
- if (getType()!=null) {
- sb.append(getType());
+ String type = getType();
+ if (type!=null) {
+ appendPossibleDataType(sb, type);
sb.append(' ');
}
@@ -70,7 +71,12 @@ public class FunctionCompletion extends VariableCompletion {
sb.append('(');
for (int i=0; i<getParamCount(); i++) {
Parameter param = getParam(i);
- sb.append(param.toString());
+ type = param.getType();
+ if (type!=null) {
+ appendPossibleDataType(sb, type);
+ sb.append(' ');
+ }
+ sb.append(param.getName());
if (i<params.size()-1) {
sb.append(", ");
}
diff --git a/src/org/fife/ui/autocomplete/LanguageAwareCompletionProvider.java b/src/org/fife/ui/autocomplete/LanguageAwareCompletionProvider.java
new file mode 100644
index 0000000..5aca9b8
--- /dev/null
+++ b/src/org/fife/ui/autocomplete/LanguageAwareCompletionProvider.java
@@ -0,0 +1,182 @@
+/*
+ * 01/03/2009
+ *
+ * LanguageAwareCompletionProvider.java - A completion provider that is aware
+ * of the language it is working with.
+ * Copyright (C) 2009 Robert Futrell
+ * robert_futrell at users.sourceforge.net
+ * http://fifesoft.com/rsyntaxtextarea
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+package org.fife.ui.autocomplete;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.swing.text.JTextComponent;
+
+import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
+import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
+import org.fife.ui.rsyntaxtextarea.RSyntaxUtilities;
+import org.fife.ui.rsyntaxtextarea.Token;
+
+
+/**
+ * A completion provider that is aware of the programming language it is
+ * providing auto-completion for.
+ *
+ * @author Robert Futrell
+ * @version 1.0
+ */
+public class LanguageAwareCompletionProvider extends AbstractCompletionProvider{
+
+ /**
+ * The provider to use when no provider is assigned to a particular token
+ * type.
+ */
+ private CompletionProvider defaultProvider;
+
+ /**
+ * The provider to use when completing a string.
+ */
+ private CompletionProvider stringCompletionProvider;
+
+
+ /**
+ * Constructor.
+ *
+ * @param defaultProvider The provider to use when no provider is assigned
+ * to a particular token type. This cannot be <code>null</code>.
+ */
+ public LanguageAwareCompletionProvider(CompletionProvider defaultProvider) {
+ setDefaultCompletionProvider(defaultProvider);
+ completions = new java.util.ArrayList(0); // TODO: Remove me.
+ }
+
+
+ public String getAlreadyEnteredText(JTextComponent comp) {
+ if (!(comp instanceof RSyntaxTextArea)) {
+ return EMPTY_STRING;
+ }
+ CompletionProvider provider = getProviderFor(comp);
+ return provider.getAlreadyEnteredText(comp);
+ }
+
+
+ /**
+ * Does the dirty work of creating a list of completions.
+ *
+ * @param comp The text component to look in.
+ * @return The list of possible completions, or an empty list if there
+ * are none.
+ */
+ protected List getCompletionsImpl(JTextComponent comp) {
+ if (!(comp instanceof RSyntaxTextArea)) {
+ return new ArrayList(0);
+ }
+ CompletionProvider provider = getProviderFor(comp);
+ return provider.getCompletions(comp);
+ }
+
+
+ /**
+ * Returns the completion provider used when one isn't defined for a
+ * particular token type.
+ *
+ * @return The completion provider to use.
+ * @see #setDefaultCompletionProvider(CompletionProvider)
+ */
+ public CompletionProvider getDefaultCompletionProvider() {
+ return defaultProvider;
+ }
+
+
+ private CompletionProvider getProviderFor(JTextComponent comp) {
+
+ RSyntaxTextArea rsta = (RSyntaxTextArea)comp;
+ RSyntaxDocument doc = (RSyntaxDocument)rsta.getDocument();
+ Token t = doc.getTokenListForLine(rsta.getCaretLineNumber());
+ if (t==null) {
+ return getDefaultCompletionProvider();
+ }
+
+ int dot = rsta.getCaretPosition();
+ Token curToken = RSyntaxUtilities.getTokenAtOffset(t, dot);
+ int type = 0;
+ if (curToken==null) { // At end of the line
+ Token temp = t.getLastPaintableToken();
+ if (temp==null) {
+ return getDefaultCompletionProvider();
+ }
+ type = temp.type;
+ }
+ else {
+ type = curToken.type;
+ }
+
+ switch (type) {
+ case Token.LITERAL_STRING_DOUBLE_QUOTE:
+ case Token.ERROR_STRING_DOUBLE:
+ return getStringCompletionProvider();
+ default:
+ return getDefaultCompletionProvider();
+ }
+
+ }
+
+
+ /**
+ * Returns the completion provider to use for strings. This may be the
+ * default provider if one isn't explicitly set for strings.
+ *
+ * @return The completion provider to use.
+ * @see #setStringCompletionProvider(CompletionProvider)
+ */
+ public CompletionProvider getStringCompletionProvider() {
+ return stringCompletionProvider==null ? defaultProvider :
+ stringCompletionProvider;
+ }
+
+
+ /**
+ * Sets the default completion provider.
+ *
+ * @param provider The provider to use when no provider is assigned to a
+ * particular token type. This cannot be <code>null</code>.
+ * @see #getDefaultCompletionProvider()
+ */
+ public void setDefaultCompletionProvider(CompletionProvider provider) {
+ if (provider==null) {
+ throw new IllegalArgumentException("provider cannot be null");
+ }
+ this.defaultProvider = provider;
+ }
+
+
+ /**
+ * Sets the completion provider to use while in a string.
+ *
+ * @param provider The provider to use. If this is <code>null</code>, the
+ * default completion provider will be used.
+ * @see #getStringCompletionProvider()
+ */
+ public void setStringCompletionProvider(CompletionProvider provider) {
+ stringCompletionProvider = provider;
+ }
+
+
+}
\ No newline at end of file
diff --git a/src/org/fife/ui/autocomplete/ProceduralLanguageCellRenderer.java b/src/org/fife/ui/autocomplete/ProceduralLanguageCellRenderer.java
index 23f8479..6e1d4ee 100644
--- a/src/org/fife/ui/autocomplete/ProceduralLanguageCellRenderer.java
+++ b/src/org/fife/ui/autocomplete/ProceduralLanguageCellRenderer.java
@@ -114,13 +114,11 @@ public class ProceduralLanguageCellRenderer extends DefaultListCellRenderer {
if (!selected) {
sb.append("</font>");
}
+ sb.append(' ');
}
String name = param.getName();
if (name!=null) {
- if (type!=null) {
- sb.append(' ');
- sb.append(name);
- }
+ sb.append(name);
}
if (i<paramCount-1) {
sb.append(", ");
diff --git a/src/org/fife/ui/autocomplete/ProceduralLanguageCompletionProvider.java b/src/org/fife/ui/autocomplete/ProceduralLanguageCompletionProvider.java
index b9194f6..127b5d8 100644
--- a/src/org/fife/ui/autocomplete/ProceduralLanguageCompletionProvider.java
+++ b/src/org/fife/ui/autocomplete/ProceduralLanguageCompletionProvider.java
@@ -24,6 +24,7 @@
package org.fife.ui.autocomplete;
import java.awt.Color;
+import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -33,17 +34,16 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
-import javax.imageio.ImageIO;
-import javax.swing.DefaultListCellRenderer;
-import javax.swing.ImageIcon;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.JTextComponent;
import javax.swing.text.Segment;
+import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
@@ -74,17 +74,18 @@ public class ProceduralLanguageCompletionProvider
private boolean colorizeHeader;
- public ProceduralLanguageCompletionProvider(String fileName) {
-
- completions = new ArrayList();
- seg = new Segment();
+ public ProceduralLanguageCompletionProvider(InputStream in) {
+ try {
+ this.completions = loadXMLFromStream(in);
+ Collections.sort(this.completions);
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
+ }
+ init();
+ }
- setColorizeHeader(false);
- long start = System.currentTimeMillis();
-
- SAXParserFactory factory = SAXParserFactory.newInstance();
- XMLParser handler = new XMLParser();
+ public ProceduralLanguageCompletionProvider(String fileName) {
try {
InputStream in = null;
File file = new File(fileName);
@@ -95,23 +96,12 @@ public class ProceduralLanguageCompletionProvider
ClassLoader cl = getClass().getClassLoader();
in = cl.getResourceAsStream(fileName);
}
- try {
- SAXParser saxParser = factory.newSAXParser();
- saxParser.parse(in, handler);
- this.completions = handler.getCompletions();
- Collections.sort(this.completions);
- } finally {
- in.close();
- }
- } catch (Throwable err) {
- err.printStackTrace ();
+ this.completions = loadXMLFromStream(in);
+ Collections.sort(this.completions);
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
}
-
- long time = System.currentTimeMillis() - start;
- System.out.println("XML loaded in: " + time + "ms");
-
- setListCellRenderer(new ProceduralLanguageCellRenderer());
-
+ init();
}
@@ -148,12 +138,6 @@ public class ProceduralLanguageCompletionProvider
}
- private String getColorFor(String text) {
- int index = dataTypes==null ? -1 : Arrays.binarySearch(dataTypes, text);
- return index>=0 ? dataTypeFG : null;
- }
-
-
/**
* Returns whether the description area should have its header information
* syntax highlighted.
@@ -167,6 +151,46 @@ public class ProceduralLanguageCompletionProvider
/**
+ * Does initialization common to various constructors.
+ */
+ private void init() {
+ seg = new Segment();
+ setColorizeHeader(false);
+ setListCellRenderer(new ProceduralLanguageCellRenderer());
+ }
+
+
+ public String isDataType(String str) {
+ return Arrays.binarySearch(dataTypes, str)>=0 ?
+ dataTypeFG : null;
+ }
+
+
+ private List loadXMLFromStream(InputStream in) throws IOException {
+
+ long start = System.currentTimeMillis();
+
+ SAXParserFactory factory = SAXParserFactory.newInstance();
+ XMLParser handler = new XMLParser();
+ BufferedInputStream bin = new BufferedInputStream(in);
+ try {
+ SAXParser saxParser = factory.newSAXParser();
+ saxParser.parse(bin, handler);
+ return handler.getCompletions();
+ } catch (SAXException se) {
+ throw new IOException(se.toString());
+ } catch (ParserConfigurationException pce) {
+ throw new IOException(pce.toString());
+ } finally {
+ long time = System.currentTimeMillis() - start;
+ System.out.println("XML loaded in: " + time + "ms");
+ bin.close();
+ }
+
+ }
+
+
+ /**
* Sets whether the header text of the description area should be
* syntax highlighted.
*
@@ -239,23 +263,32 @@ public class ProceduralLanguageCompletionProvider
private String type;
private String returnType;
private StringBuffer desc;
+ private String paramName;
+ private String paramType;
+ private StringBuffer paramDesc;
private List params;
private String definedIn;
private boolean doingKeywords;
private boolean inKeyword;
private boolean gettingDesc;
private boolean gettingParams;
+ private boolean inParam;
+ private boolean gettingParamDesc;
public XMLParser() {
completions = new ArrayList();
params = new ArrayList(1);
desc = new StringBuffer();
+ paramDesc = new StringBuffer();
}
public void characters(char[] ch, int start, int length) {
if (gettingDesc) {
desc.append(ch, start, length);
}
+ else if (gettingParamDesc) {
+ paramDesc.append(ch, start, length);
+ }
}
public void endElement(String uri, String localName, String qName) {
@@ -271,7 +304,10 @@ public class ProceduralLanguageCompletionProvider
if ("function".equals(type)) {
FunctionCompletion fc = new FunctionCompletion(
ProceduralLanguageCompletionProvider.this, name, returnType);
- fc.setDescription(desc.toString());
+ if (desc.length()>0) {
+ fc.setDescription(desc.toString());
+ desc.setLength(0);
+ }
fc.setParams(params);
fc.setDefinedIn(definedIn);
c = fc;
@@ -279,7 +315,10 @@ public class ProceduralLanguageCompletionProvider
else if ("constant".equals(type)) {
VariableCompletion vc = new VariableCompletion(
ProceduralLanguageCompletionProvider.this, name, returnType);
- vc.setDescription(desc.toString());
+ if (desc.length()>0) {
+ vc.setDescription(desc.toString());
+ desc.setLength(0);
+ }
vc.setDefinedIn(definedIn);
c = vc;
}
@@ -293,8 +332,26 @@ public class ProceduralLanguageCompletionProvider
if ("desc".equals(qName)) {
gettingDesc = false;
}
- else if ("params".equals(qName)) {
- gettingParams = false;
+ else if (gettingParams) {
+ if ("params".equals(qName)) {
+ gettingParams = false;
+ }
+ else if ("param".equals(qName)) {
+ FunctionCompletion.Parameter param =
+ new FunctionCompletion.Parameter(paramType,
+ paramName);
+ if (paramDesc.length()>0) {
+ param.setDescription(paramDesc.toString());
+ paramDesc.setLength(0);
+ }
+ params.add(param);
+ inParam = false;
+ }
+ else if (inParam) {
+ if ("desc".equals(qName)) {
+ gettingParamDesc = false;
+ }
+ }
}
}
@@ -320,22 +377,23 @@ public class ProceduralLanguageCompletionProvider
inKeyword = true;
}
else if (inKeyword) {
- if ("desc".equals(qName)) {
- gettingDesc = true;
- desc.setLength(0);
- }
- else if ("params".equals(qName)) {
+ if ("params".equals(qName)) {
gettingParams = true;
}
else if (gettingParams) {
if ("param".equals(qName)) {
- String name = attrs.getValue("name");
- String type = attrs.getValue("type");
- FunctionCompletion.Parameter param =
- new FunctionCompletion.Parameter(type, name);
- // TODO: Get desc.
- params.add(param);
+ paramName = attrs.getValue("name");
+ paramType = attrs.getValue("type");
+ inParam = true;
}
+ if (inParam) {
+ if ("desc".equals(qName)) {
+ gettingParamDesc = true;
+ }
+ }
+ }
+ else if ("desc".equals(qName)) {
+ gettingDesc = true;
}
}
}
diff --git a/src/org/fife/ui/autocomplete/VariableCompletion.java b/src/org/fife/ui/autocomplete/VariableCompletion.java
index 68e7d92..64fa0d5 100644
--- a/src/org/fife/ui/autocomplete/VariableCompletion.java
+++ b/src/org/fife/ui/autocomplete/VariableCompletion.java
@@ -59,7 +59,7 @@ public class VariableCompletion extends AbstractCompletion {
// Add the return type if applicable (C macros like NULL have no type).
if (type!=null) {
- sb.append(type);
+ appendPossibleDataType(sb, type);
sb.append(' ');
}
@@ -71,6 +71,22 @@ public class VariableCompletion extends AbstractCompletion {
}
+ protected void appendPossibleDataType(StringBuffer sb, String type) {
+ if (type!=null) {
+ ProceduralLanguageCompletionProvider p =
+ (ProceduralLanguageCompletionProvider)getProvider();
+ String dataTypeFG = p.isDataType(type);
+ if (dataTypeFG!=null) {
+ sb.append("<font color=\"").append(dataTypeFG).append("\">");
+ }
+ sb.append(type);
+ if (dataTypeFG!=null) {
+ sb.append("</font>");
+ }
+ }
+ }
+
+
/**
* Returns where this variable is defined.
*
--
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