Skip to content

Base implementation of autocomplete #849 #6235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry combineaccessrules="false" kind="src" path="/arduino-core"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="lib" path="lib/apple.jar"/>
<classpathentry kind="lib" path="lib/batik-1.8.jar"/>
<classpathentry kind="lib" path="lib/batik-anim-1.8.jar"/>
Expand Down Expand Up @@ -44,6 +43,7 @@
<classpathentry kind="lib" path="lib/jsch-0.1.50.jar"/>
<classpathentry kind="lib" path="lib/jssc-2.8.0.jar"/>
<classpathentry kind="lib" path="lib/rsyntaxtextarea-2.6.1.jar"/>
<classpathentry kind="lib" path="lib/autocomplete-2.6.1.jar" sourcepath="/media/ricardo/Dados/Workspace/Arduino/arduino-dep/AutoComplete-2.6.1"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the autocomplete library here?
https://github.com/bobbylight/AutoComplete
where did you found the jar?

I think that the launchers for Windows must be updated too:

build/windows/launcher/config.xml
build/windows/launcher/config_debug.xml

<classpathentry kind="lib" path="lib/xml-apis-1.3.04.jar"/>
<classpathentry kind="lib" path="lib/xml-apis-ext-1.3.04.jar"/>
<classpathentry kind="lib" path="lib/xmlgraphics-commons-2.0.jar"/>
Expand All @@ -53,4 +53,5 @@
<classpathentry kind="lib" path="test-lib/fest-swing-1.2.jar"/>
<classpathentry kind="lib" path="test-lib/fest-util-1.1.2.jar"/>
<classpathentry kind="lib" path="test-lib/jcip-annotations-1.0.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 1 addition & 1 deletion app/.project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>processing</name>
<name>app</name>
<comment></comment>
<projects>
</projects>
Expand Down
Binary file added app/lib/autocomplete-2.6.1.jar
Binary file not shown.
44 changes: 44 additions & 0 deletions app/src/cc/arduino/autocomplete/CompletionContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cc.arduino.autocomplete;

import org.fife.ui.autocomplete.CompletionProvider;

import processing.app.Sketch;
import processing.app.syntax.SketchTextArea;

public class CompletionContext {

private Sketch sketch;

private SketchTextArea textArea;

private CompletionProvider delegate; // of: AutoComplete/RSyntaxTextArea

public CompletionContext(Sketch sketch, SketchTextArea textArea,CompletionProvider delegate) {
this.sketch = sketch;
this.textArea = textArea;
this.delegate = delegate;
}


public String getAlreadyEnteredText() {
return delegate.getAlreadyEnteredText(textArea);
}

public int getLineNumber() {
return textArea.getCaretLineNumber();
}


public Sketch getSketch() {
return sketch;
}

public SketchTextArea getTextArea() {
return textArea;
}

public CompletionProvider getDelegate() {
return delegate;
}

}
11 changes: 11 additions & 0 deletions app/src/cc/arduino/autocomplete/CompletionProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cc.arduino.autocomplete;

import java.util.List;

import org.fife.ui.autocomplete.Completion;

public interface CompletionProvider {

List<Completion> getSuggestions(CompletionContext context);

}
22 changes: 22 additions & 0 deletions app/src/cc/arduino/autocomplete/FakeCompletionProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cc.arduino.autocomplete;

import java.util.LinkedList;
import java.util.List;

import org.fife.ui.autocomplete.BasicCompletion;
import org.fife.ui.autocomplete.Completion;

public class FakeCompletionProvider implements CompletionProvider {


@Override
public List<Completion> getSuggestions(CompletionContext context) {
List<Completion> list = new LinkedList<>();
list.add(new BasicCompletion(context.getDelegate(), "Text: " + context.getAlreadyEnteredText()));
list.add(new BasicCompletion(context.getDelegate(), "Line: " + context.getLineNumber()));

return list;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cc.arduino.autocomplete.rsyntax;

import java.util.List;

import javax.swing.text.JTextComponent;

import org.fife.ui.autocomplete.Completion;
import org.fife.ui.autocomplete.DefaultCompletionProvider;

import cc.arduino.autocomplete.CompletionContext;
import cc.arduino.autocomplete.CompletionProvider;

public class CompletionProviderBridge extends DefaultCompletionProvider{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, it seems that rsyntaxtextarea's autocomplete.jar already has a CompletionProvider, I didn't know that, having a cc.arduino.CompletionProvider is any useful? maybe we could use directly the rsyntax version instead of making our own and a Bridge class between the two?
Also the cc.arduino.CompletionContext class looks like useless boilerplate code now.


private CompletionProvider provider;

private CompletionContext context;

public CompletionProviderBridge(CompletionContext context, CompletionProvider provider) {
this.provider = provider;
this.context = context;
}


@Override
public List<Completion> getCompletionsImpl(JTextComponent comp) {
return provider.getSuggestions(context);

}

@Override
protected boolean isValidChar(char ch) {
return super.isValidChar(ch) || '.' == ch || '>' == ch || '-' == ch || '<' == ch || '#' == ch || ':' == ch /**|| getParameterListStart() == ch */;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cc.arduino.autocomplete.rsyntax;

import org.fife.ui.autocomplete.LanguageAwareCompletionProvider;

import cc.arduino.autocomplete.CompletionContext;
import cc.arduino.autocomplete.CompletionProvider;
import processing.app.Sketch;
import processing.app.syntax.SketchTextArea;


/**
* CompletionProvider for Arduino/CPP Language. <br/>
* Setup basic logic for completions using {@link LanguageAwareCompletionProvider}
* Filtering and decision will appear in the autocomplete dialog by implementations of: {@link CompletionProvider}. <br/>
*
* @author Ricardo JL Rufino ([email protected])
* @date 28/04/2017
*/
public class SketchCompletionProvider extends LanguageAwareCompletionProvider{

public SketchCompletionProvider(Sketch sketch, SketchTextArea textArea, CompletionProvider provider) {

CompletionContext context = new CompletionContext(sketch, textArea, this);

setDefaultCompletionProvider(new CompletionProviderBridge(context, provider));
// provider.setParameterChoicesProvider(new ParameterChoicesProvider(this));
// provider.setParameterizedCompletionParams('(', ", ", ')');

}



}
3 changes: 3 additions & 0 deletions app/src/processing/app/EditorTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.fife.ui.rtextarea.RTextScrollPane;

import cc.arduino.UpdatableBoardsLibsFakeURLsHandler;
import cc.arduino.autocomplete.FakeCompletionProvider;
import processing.app.helpers.DocumentTextChangeListener;
import processing.app.syntax.ArduinoTokenMakerFactory;
import processing.app.syntax.PdeKeywords;
Expand Down Expand Up @@ -106,6 +107,8 @@ public EditorTab(Editor editor, SketchFile file, String contents)
file.setStorage(this);
applyPreferences();
add(scrollPane, BorderLayout.CENTER);

textarea.setupAutoComplete(file.getSketch(), new FakeCompletionProvider());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you inline this call and remove setupAutoComplete from SketchTextArea?
Doesn't seems to be any useful to have a SketchCompletionProvider reference in SketchTextArea.

You can also use editor.getSketch() instead of file.getSketch() and remove the getSketch() method from FileSketch.

}

private RSyntaxDocument createDocument(String contents) {
Expand Down
66 changes: 50 additions & 16 deletions app/src/processing/app/syntax/SketchTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,12 @@

package processing.app.syntax;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;
import org.apache.commons.compress.utils.IOUtils;
import org.fife.ui.rsyntaxtextarea.*;
import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rtextarea.RTextArea;
import org.fife.ui.rtextarea.RTextAreaUI;
import processing.app.Base;
import processing.app.BaseNoGui;
import processing.app.PreferencesData;

import javax.swing.event.EventListenerList;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Segment;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -56,6 +44,34 @@
import java.net.URL;
import java.util.Map;
import java.util.logging.Logger;

import javax.swing.KeyStroke;
import javax.swing.event.EventListenerList;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Segment;

import org.apache.commons.compress.utils.IOUtils;
import org.fife.ui.autocomplete.AutoCompletion;
import org.fife.ui.rsyntaxtextarea.LinkGenerator;
import org.fife.ui.rsyntaxtextarea.LinkGeneratorResult;
import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Style;
import org.fife.ui.rsyntaxtextarea.Theme;
import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rsyntaxtextarea.TokenImpl;
import org.fife.ui.rsyntaxtextarea.TokenTypes;
import org.fife.ui.rtextarea.RTextArea;
import org.fife.ui.rtextarea.RTextAreaUI;

import cc.arduino.autocomplete.CompletionProvider;
import cc.arduino.autocomplete.rsyntax.SketchCompletionProvider;
import processing.app.Base;
import processing.app.BaseNoGui;
import processing.app.PreferencesData;
import processing.app.Sketch;
import processing.app.helpers.OSUtils;

/**
Expand All @@ -69,6 +85,8 @@ public class SketchTextArea extends RSyntaxTextArea {
private final static Logger LOG = Logger.getLogger(SketchTextArea.class.getName());

private PdeKeywords pdeKeywords;

private SketchCompletionProvider completionProvider;

public SketchTextArea(RSyntaxDocument document, PdeKeywords pdeKeywords) throws IOException {
super(document);
Expand All @@ -81,6 +99,22 @@ public void setKeywords(PdeKeywords keywords) {
pdeKeywords = keywords;
setLinkGenerator(new DocLinkGenerator(pdeKeywords));
}

public void setupAutoComplete(Sketch sketch, CompletionProvider provider) {

this.completionProvider = new SketchCompletionProvider(sketch, this, provider);

AutoCompletion ac = new AutoCompletion( this.completionProvider);

ac.setAutoActivationEnabled(true);
ac.setShowDescWindow(false);
ac.setAutoCompleteSingleChoices(true);
ac.setParameterAssistanceEnabled(true);
// ac.setParamChoicesRenderer(new CompletionsRenderer());
// ac.setListCellRenderer(new CompletionsRenderer());
ac.install(this);

}

private void installFeatures() throws IOException {
setTheme(PreferencesData.get("editor.syntax_theme", "default"));
Expand Down
20 changes: 10 additions & 10 deletions arduino-core/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
<classpathentry kind="lib" path="lib/jssc-2.8.0.jar"/>
<classpathentry kind="lib" path="lib/jsch-0.1.50.jar"/>
<classpathentry kind="lib" path="lib/commons-exec-1.1.jar"/>
<classpathentry kind="lib" path="../app/lib/commons-codec-1.7.jar"/>
<classpathentry kind="lib" path="../app/lib/commons-compress-1.8.jar"/>
<classpathentry kind="lib" path="../app/lib/commons-exec-1.1.jar"/>
<classpathentry kind="lib" path="../app/lib/commons-httpclient-3.1.jar"/>
<classpathentry kind="lib" path="../app/lib/jackson-annotations-2.6.3.jar"/>
<classpathentry kind="lib" path="../app/lib/jackson-core-2.6.3.jar"/>
<classpathentry kind="lib" path="../app/lib/jackson-databind-2.6.3.jar"/>
<classpathentry kind="lib" path="../app/lib/jackson-module-mrbean-2.6.3.jar"/>
<classpathentry kind="lib" path="../app/lib/bcpg-jdk15on-152.jar"/>
<classpathentry kind="lib" path="../app/lib/bcprov-jdk15on-152.jar"/>
<classpathentry kind="lib" path="/app/lib/commons-codec-1.7.jar"/>
<classpathentry kind="lib" path="/app/lib/commons-compress-1.8.jar"/>
<classpathentry kind="lib" path="/app/lib/commons-exec-1.1.jar"/>
<classpathentry kind="lib" path="/app/lib/commons-httpclient-3.1.jar"/>
<classpathentry kind="lib" path="/app/lib/jackson-annotations-2.6.3.jar"/>
<classpathentry kind="lib" path="/app/lib/jackson-core-2.6.3.jar"/>
<classpathentry kind="lib" path="/app/lib/jackson-databind-2.6.3.jar"/>
<classpathentry kind="lib" path="/app/lib/jackson-module-mrbean-2.6.3.jar"/>
<classpathentry kind="lib" path="/app/lib/bcpg-jdk15on-152.jar"/>
<classpathentry kind="lib" path="/app/lib/bcprov-jdk15on-152.jar"/>
<classpathentry kind="lib" path="lib/bcpg-jdk15on-152.jar"/>
<classpathentry kind="lib" path="lib/bcprov-jdk15on-152.jar"/>
<classpathentry kind="lib" path="lib/commons-codec-1.7.jar"/>
Expand Down
4 changes: 4 additions & 0 deletions arduino-core/src/processing/app/SketchFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ public boolean isModified() {
public boolean equals(Object o) {
return (o instanceof SketchFile) && file.equals(((SketchFile) o).file);
}

public Sketch getSketch() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method can be removed see my previous comment (it may turns out to be useful in the future but I'd like to avoid adding it until really necessary).

return sketch;
}

/**
* Load this piece of code from a file and return the contents. This
Expand Down