Skip to content

Commit d55223d

Browse files
cmagliefacchinm
authored andcommitted
Added CompletionsRenderer
1 parent aa02646 commit d55223d

18 files changed

+207
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
* This file is part of Arduino.
5+
*
6+
* Copyright 2015 Ricardo JL Rufino (ricardo@criativasoft.com.br)
7+
* Copyright 2015 Arduino LLC
8+
*
9+
* Arduino is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 2 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program; if not, write to the Free Software
21+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
*
23+
* As a special exception, you may use this file as part of a free software
24+
* library without restriction. Specifically, if other files instantiate
25+
* templates or use macros or inline functions from this file, or you compile
26+
* this file and link it with other files to produce an executable, this
27+
* file does not by itself cause the resulting executable to be covered by
28+
* the GNU General Public License. This exception does not however
29+
* invalidate any other reasons why the executable file might be covered by
30+
* the GNU General Public License.
31+
*/
32+
package cc.arduino.autocomplete;
33+
34+
public enum CompletionType {
35+
LIBRARY, CLASS, ENUM, STRUCT, LOCAL_VAR, STATIC_VAR, VARIABLE, FUNCTION, TEMPLATE, ERROR
36+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
/*
3+
* This file is part of Arduino.
4+
*
5+
* Copyright 2015 Ricardo JL Rufino (ricardo@criativasoft.com.br)
6+
* Copyright 2015 Arduino LLC
7+
*
8+
* Arduino is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation; either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
*
22+
* As a special exception, you may use this file as part of a free software
23+
* library without restriction. Specifically, if other files instantiate
24+
* templates or use macros or inline functions from this file, or you compile
25+
* this file and link it with other files to produce an executable, this
26+
* file does not by itself cause the resulting executable to be covered by
27+
* the GNU General Public License. This exception does not however
28+
* invalidate any other reasons why the executable file might be covered by
29+
* the GNU General Public License.
30+
*/
31+
package cc.arduino.autocomplete;
32+
33+
import java.awt.Color;
34+
import java.awt.Component;
35+
import java.awt.Font;
36+
import java.util.HashMap;
37+
import java.util.Map;
38+
39+
import javax.swing.DefaultListCellRenderer;
40+
import javax.swing.Icon;
41+
import javax.swing.ImageIcon;
42+
import javax.swing.JList;
43+
44+
import org.fife.ui.autocomplete.BasicCompletion;
45+
import org.fife.ui.autocomplete.FunctionCompletion;
46+
import org.fife.ui.autocomplete.ShorthandCompletion;
47+
import org.fife.ui.autocomplete.TemplateCompletion;
48+
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
49+
50+
//import br.com.criativasoft.cpluslibparser.metadata.TElement;
51+
//import br.com.criativasoft.cpluslibparser.metadata.TFunction;
52+
53+
/**
54+
* Class responsible for formatting the elements of the autocomplete list
55+
*/
56+
public class CompletionsRenderer extends DefaultListCellRenderer {
57+
58+
private static final long serialVersionUID = 1L;
59+
60+
private static final Color HIGHLIGHT_COLOR = new Color(74, 144, 217);
61+
62+
private static final String GRAY = "#A0A0A0";
63+
private static final String LIGHT_BLUE = "#008080";
64+
65+
/**
66+
* Keeps the HTML descriptions from "wrapping" in the list, which cuts off
67+
* words.
68+
*/
69+
private static final String PREFIX = "<html><nobr>";
70+
71+
private static Map<CompletionType, Icon> iconsTypes = new HashMap<CompletionType, Icon>();
72+
73+
static {
74+
iconsTypes.put(CompletionType.CLASS, getIcon("class.gif"));
75+
iconsTypes.put(CompletionType.ENUM, getIcon("enum.gif"));
76+
iconsTypes.put(CompletionType.VARIABLE, getIcon("variable.gif"));
77+
iconsTypes.put(CompletionType.LOCAL_VAR, getIcon("variable_local.gif"));
78+
iconsTypes.put(CompletionType.STATIC_VAR, getIcon("variable_static.gif"));
79+
iconsTypes.put(CompletionType.FUNCTION, getIcon("function.gif"));
80+
iconsTypes.put(CompletionType.ERROR, getIcon("error.gif"));
81+
iconsTypes.put(CompletionType.TEMPLATE, getIcon("template_obj.gif"));
82+
}
83+
84+
private static Icon getIcon(String image) {
85+
return new ImageIcon(
86+
CompletionsRenderer.class.getResource("icons/" + image));
87+
}
88+
89+
public static Icon getIcon(CompletionType tokenType) {
90+
return iconsTypes.get(tokenType);
91+
}
92+
93+
public CompletionsRenderer() {
94+
setOpaque(true);
95+
// Font f = Theme.getDefaultFont();
96+
Font f = RSyntaxTextArea.getDefaultFont();
97+
setFont(f.deriveFont(f.getStyle() & ~Font.BOLD)); // remove bold.
98+
}
99+
100+
@Override
101+
public Component getListCellRendererComponent(JList list, Object value,
102+
int index, boolean isSelected,
103+
boolean cellHasFocus) {
104+
105+
String text = null;
106+
CompletionType tokenType = null;
107+
108+
// if (value instanceof TElementCompletion) {
109+
//
110+
// TElementCompletion completion = (TElementCompletion) value;
111+
// TElement element = completion.getElement();
112+
// tokenType = completion.getType();
113+
// text = element.getHtmlRepresentation();
114+
//
115+
// } else if (value instanceof TFunctionCompletion) {
116+
//
117+
// TFunctionCompletion completion = (TFunctionCompletion) value;
118+
// TFunction function = completion.getFunction();
119+
// text = function.getHtmlRepresentation();
120+
// tokenType = CompletionType.FUNCTION;
121+
//
122+
// } else
123+
if (value instanceof ShorthandCompletion) {
124+
text = ((ShorthandCompletion) value).getShortDescription();
125+
tokenType = CompletionType.TEMPLATE;
126+
} else if (value instanceof FunctionCompletion) {
127+
text = ((FunctionCompletion) value).getShortDescription();
128+
tokenType = CompletionType.FUNCTION;
129+
} else if (value instanceof BasicCompletion) {
130+
text = ((BasicCompletion) value).getInputText();
131+
if (((BasicCompletion) value).getShortDescription() != null) {
132+
text = ((BasicCompletion) value).getShortDescription();
133+
}
134+
} else if (value instanceof TemplateCompletion) {
135+
TemplateCompletion template = (TemplateCompletion) value;
136+
text = font(template.getInputText(), LIGHT_BLUE)
137+
+ font(" - " + template.getDefinitionString(), GRAY);
138+
}
139+
140+
if (text == null) {
141+
text = value.toString();
142+
}
143+
144+
// Find Icon
145+
if (tokenType != null) {
146+
setIcon(iconsTypes.get(tokenType));
147+
} else {
148+
setIcon(iconsTypes.get(CompletionType.TEMPLATE));
149+
}
150+
151+
if (isSelected) {
152+
setText(text.replaceAll("\\<[^>]*>", ""));
153+
setBackground(HIGHLIGHT_COLOR);
154+
setForeground(Color.white);
155+
} else {
156+
setText(PREFIX + text);
157+
setBackground(Color.white);
158+
setForeground(Color.black);
159+
}
160+
161+
return this;
162+
}
163+
164+
private String font(String text, String color) {
165+
return "<font color='" + color + "'>" + text + "</font>";
166+
}
167+
168+
}
586 Bytes
Loading
361 Bytes
Loading
339 Bytes
Loading
Loading
Loading
193 Bytes
Loading
Loading
574 Bytes
Loading
Loading
107 Bytes
Loading
Loading
176 Bytes
Loading
Loading
Loading

app/src/processing/app/EditorTab.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
import cc.arduino.UpdatableBoardsLibsFakeURLsHandler;
6161
import cc.arduino.autocomplete.ClangCompletionProvider;
62+
import cc.arduino.autocomplete.CompletionsRenderer;
6263
import processing.app.helpers.DocumentTextChangeListener;
6364
import processing.app.syntax.ArduinoTokenMakerFactory;
6465
import processing.app.syntax.PdeKeywords;
@@ -120,6 +121,7 @@ public EditorTab(Editor editor, SketchFile file, String contents)
120121
ac.setShowDescWindow(false);
121122
ac.setAutoCompleteSingleChoices(true);
122123
ac.setParameterAssistanceEnabled(true);
124+
ac.setListCellRenderer(new CompletionsRenderer());
123125
// ac.setParamChoicesRenderer(new CompletionsRenderer());
124126
// ac.setListCellRenderer(new CompletionsRenderer());
125127
ac.install(textarea);

app/src/processing/app/syntax/SketchTextArea.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
* @author Ricardo JL Rufino (ricardo@criativasoft.com.br)
8181
* @since 1.6.4
8282
*/
83+
@SuppressWarnings("unused")
8384
public class SketchTextArea extends RSyntaxTextArea {
8485

8586
private final static Logger LOG = Logger.getLogger(SketchTextArea.class.getName());

0 commit comments

Comments
 (0)