mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
looking to fix text for completions; instead cleaning code
This commit is contained in:
@@ -37,7 +37,6 @@ import javax.swing.JList;
|
||||
import javax.swing.JPopupMenu;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.text.BadLocationException;
|
||||
|
||||
import processing.app.Base;
|
||||
import processing.app.Messages;
|
||||
@@ -47,32 +46,22 @@ import processing.app.ui.Toolkit;
|
||||
|
||||
|
||||
public class CompletionPanel {
|
||||
/**
|
||||
* The completion list generated by ASTGenerator
|
||||
*/
|
||||
private JList<CompletionCandidate> completionList;
|
||||
/** The completion list generated by ASTGenerator */
|
||||
final private JList<CompletionCandidate> completionList;
|
||||
|
||||
/**
|
||||
* The popup menu in which the suggestion list is shown
|
||||
*/
|
||||
private JPopupMenu popupMenu;
|
||||
/** The popup menu in which the suggestion list is shown */
|
||||
final private JPopupMenu popupMenu;
|
||||
|
||||
/**
|
||||
* Partial word which triggered the code completion and which needs to be completed
|
||||
*/
|
||||
private String subWord;
|
||||
/** Partial word that triggered the code completion */
|
||||
final private String subWord;
|
||||
|
||||
/**
|
||||
* Position where the completion has to be inserted
|
||||
*/
|
||||
private int insertionPosition;
|
||||
/** Position where the completion has to be inserted */
|
||||
final private int insertionPosition;
|
||||
|
||||
private JavaTextArea textarea;
|
||||
final private JavaTextArea textArea;
|
||||
|
||||
/**
|
||||
* Scroll pane in which the completion list is displayed
|
||||
*/
|
||||
private JScrollPane scrollPane;
|
||||
/** Scroll pane in which the completion list is displayed */
|
||||
final private JScrollPane scrollPane;
|
||||
|
||||
protected JavaEditor editor;
|
||||
|
||||
@@ -91,7 +80,6 @@ public class CompletionPanel {
|
||||
|
||||
/**
|
||||
* Triggers the completion popup
|
||||
* @param textarea
|
||||
* @param position - insertion position(caret pos)
|
||||
* @param subWord - Partial word which triggered the code completion and which needs to be completed
|
||||
* @param items - completion candidates
|
||||
@@ -101,7 +89,7 @@ public class CompletionPanel {
|
||||
int position, String subWord,
|
||||
DefaultListModel<CompletionCandidate> items,
|
||||
final Point location, JavaEditor editor) {
|
||||
this.textarea = (JavaTextArea) textarea;
|
||||
this.textArea = (JavaTextArea) textarea;
|
||||
this.editor = editor;
|
||||
this.insertionPosition = position;
|
||||
if (subWord.indexOf('.') != -1 && subWord.indexOf('.') != subWord.length()-1) {
|
||||
@@ -130,9 +118,7 @@ public class CompletionPanel {
|
||||
popupMenu.setBorder(null);
|
||||
|
||||
scrollPane = new JScrollPane();
|
||||
// styleScrollPane();
|
||||
//scrollPane.setViewportView(completionList = createSuggestionList(position, items));
|
||||
completionList = new JList<CompletionCandidate>(items) {
|
||||
completionList = new JList<>(items) {
|
||||
{
|
||||
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
setSelectedIndex(0);
|
||||
@@ -163,66 +149,6 @@ public class CompletionPanel {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
private void styleScrollPane() {
|
||||
String laf = UIManager.getLookAndFeel().getID();
|
||||
if (!laf.equals("Nimbus") && !laf.equals("Windows")) return;
|
||||
|
||||
String thumbColor = null;
|
||||
if (laf.equals("Nimbus")) {
|
||||
UIDefaults defaults = new UIDefaults();
|
||||
defaults.put("PopupMenu.contentMargins", new InsetsUIResource(0, 0, 0, 0));
|
||||
defaults.put("ScrollPane[Enabled].borderPainter", new Painter<JComponent>() {
|
||||
public void paint(Graphics2D g, JComponent t, int w, int h) {}
|
||||
});
|
||||
popupMenu.putClientProperty("Nimbus.Overrides", defaults);
|
||||
scrollPane.putClientProperty("Nimbus.Overrides", defaults);
|
||||
thumbColor = "nimbusBlueGrey";
|
||||
} else if (laf.equals("Windows")) {
|
||||
thumbColor = "ScrollBar.thumbShadow";
|
||||
}
|
||||
|
||||
scrollPane.getHorizontalScrollBar().setPreferredSize(new Dimension(Integer.MAX_VALUE, 8));
|
||||
scrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(8, Integer.MAX_VALUE));
|
||||
scrollPane.getHorizontalScrollBar().setUI(new CompletionScrollBarUI(thumbColor));
|
||||
scrollPane.getVerticalScrollBar().setUI(new CompletionScrollBarUI(thumbColor));
|
||||
}
|
||||
|
||||
|
||||
private static class CompletionScrollBarUI extends BasicScrollBarUI {
|
||||
private String thumbColorName;
|
||||
|
||||
protected CompletionScrollBarUI(String thumbColorName) {
|
||||
this.thumbColorName = thumbColorName;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintThumb(Graphics g, JComponent c, Rectangle trackBounds) {
|
||||
g.setColor((Color) UIManager.get(thumbColorName));
|
||||
g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JButton createDecreaseButton(int orientation) {
|
||||
return createZeroButton();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JButton createIncreaseButton(int orientation) {
|
||||
return createZeroButton();
|
||||
}
|
||||
|
||||
static private JButton createZeroButton() {
|
||||
JButton jbutton = new JButton();
|
||||
jbutton.setPreferredSize(new Dimension(0, 0));
|
||||
jbutton.setMinimumSize(new Dimension(0, 0));
|
||||
jbutton.setMaximumSize(new Dimension(0, 0));
|
||||
return jbutton;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
public boolean isVisible() {
|
||||
return popupMenu.isVisible();
|
||||
}
|
||||
@@ -238,7 +164,7 @@ public class CompletionPanel {
|
||||
*/
|
||||
private int calcHeight(int itemCount) {
|
||||
int maxHeight = 250;
|
||||
FontMetrics fm = textarea.getGraphics().getFontMetrics();
|
||||
FontMetrics fm = textArea.getGraphics().getFontMetrics();
|
||||
float itemHeight = Math.max((fm.getHeight() + (fm.getDescent()) * 0.5f),
|
||||
classIcon.getIconHeight() * 1.2f);
|
||||
|
||||
@@ -263,7 +189,7 @@ public class CompletionPanel {
|
||||
private int calcWidth() {
|
||||
int maxWidth = 300;
|
||||
float min = 0;
|
||||
FontMetrics fm = textarea.getGraphics().getFontMetrics();
|
||||
FontMetrics fm = textArea.getGraphics().getFontMetrics();
|
||||
for (int i = 0; i < completionList.getModel().getSize(); i++) {
|
||||
float h = fm.stringWidth(completionList.getModel().getElementAt(i).getLabel());
|
||||
min = Math.max(min, h);
|
||||
@@ -277,84 +203,25 @@ public class CompletionPanel {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Created the popup list to be displayed
|
||||
* @param position
|
||||
* @param items
|
||||
* @return
|
||||
private JList<CompletionCandidate> createSuggestionList(final int position,
|
||||
final DefaultListModel<CompletionCandidate> items) {
|
||||
|
||||
JList<CompletionCandidate> list = new JList<CompletionCandidate>(items);
|
||||
//list.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
|
||||
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
list.setSelectedIndex(0);
|
||||
list.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
if (e.getClickCount() == 2) {
|
||||
insertSelection(MOUSE_COMPLETION);
|
||||
setInvisible();
|
||||
}
|
||||
}
|
||||
});
|
||||
list.setCellRenderer(new CustomListRenderer());
|
||||
list.setFocusable(false);
|
||||
return list;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
// possibly defunct
|
||||
private boolean updateList(final DefaultListModel<CompletionCandidate> items, String newSubword,
|
||||
final Point location, int position) {
|
||||
this.subWord = new String(newSubword);
|
||||
if (subWord.indexOf('.') != -1)
|
||||
this.subWord = subWord.substring(subWord.lastIndexOf('.') + 1);
|
||||
insertionPosition = position;
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
scrollPane.getViewport().removeAll();
|
||||
completionList.setModel(items);
|
||||
completionList.setSelectedIndex(0);
|
||||
scrollPane.setViewportView(completionList);
|
||||
popupMenu.setPopupSize(calcWidth(), calcHeight(items.getSize()));
|
||||
//log("Suggestion updated" + System.nanoTime());
|
||||
textarea.requestFocusInWindow();
|
||||
popupMenu.show(textarea, location.x, textarea.getBaseline(0, 0)
|
||||
+ location.y);
|
||||
completionList.validate();
|
||||
scrollPane.validate();
|
||||
popupMenu.validate();
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Inserts the CompletionCandidate chosen from the suggestion list
|
||||
* @param completionSource - whether being completed via keypress or mouse click.
|
||||
* @param completionSource - whether being completed via key press or mouse click.
|
||||
* @return true - if code was successfully inserted at the caret position
|
||||
*/
|
||||
protected boolean insertSelection(int completionSource) {
|
||||
if (completionList.getSelectedValue() != null) {
|
||||
try {
|
||||
// If user types 'abc.', subword becomes '.' and null is returned
|
||||
String currentSubword = fetchCurrentSubword();
|
||||
int currentSubwordLen =
|
||||
(currentSubword == null) ? 0 : currentSubword.length();
|
||||
//logE(currentSubword + " <= subword,len => " + currentSubword.length());
|
||||
// If user types 'abc.', sub word becomes '.' and null is returned
|
||||
String currentSubWord = fetchCurrentSubWord();
|
||||
int currentSubWordLen =
|
||||
(currentSubWord == null) ? 0 : currentSubWord.length();
|
||||
String selectedSuggestion =
|
||||
completionList.getSelectedValue().getCompletionString();
|
||||
|
||||
if (currentSubword != null) {
|
||||
selectedSuggestion = selectedSuggestion.substring(currentSubwordLen);
|
||||
if (currentSubWord != null) {
|
||||
selectedSuggestion = selectedSuggestion.substring(currentSubWordLen);
|
||||
} else {
|
||||
currentSubword = "";
|
||||
currentSubWord = "";
|
||||
}
|
||||
|
||||
String completionString =
|
||||
@@ -376,43 +243,35 @@ public class CompletionPanel {
|
||||
}
|
||||
}
|
||||
|
||||
Messages.err(subWord + " <= subword, Inserting suggestion=> " +
|
||||
selectedSuggestion + " Current sub: " + currentSubword);
|
||||
if (currentSubword.length() > 0) {
|
||||
textarea.getDocument().remove(insertionPosition - currentSubwordLen,
|
||||
currentSubwordLen);
|
||||
Messages.err(subWord + " <= sub word, Inserting suggestion=> " +
|
||||
selectedSuggestion + " Current sub: " + currentSubWord);
|
||||
if (currentSubWord.length() > 0) {
|
||||
textArea.getDocument().remove(insertionPosition - currentSubWordLen,
|
||||
currentSubWordLen);
|
||||
}
|
||||
|
||||
textarea.getDocument().insertString(insertionPosition - currentSubwordLen,
|
||||
textArea.getDocument().insertString(insertionPosition - currentSubWordLen,
|
||||
completionString, null);
|
||||
if (selectedSuggestion.endsWith(")") && !selectedSuggestion.endsWith("()")) {
|
||||
// place the caret between '( and first ','
|
||||
int x = selectedSuggestion.indexOf(',');
|
||||
if (x == -1) {
|
||||
// the case of single param methods, containing no ','
|
||||
textarea.setCaretPosition(textarea.getCaretPosition() - 1); // just before ')'
|
||||
textArea.setCaretPosition(textArea.getCaretPosition() - 1); // just before ')'
|
||||
} else {
|
||||
textarea.setCaretPosition(insertionPosition + x);
|
||||
textArea.setCaretPosition(insertionPosition + x);
|
||||
}
|
||||
}
|
||||
|
||||
Messages.log("Suggestion inserted: " + System.currentTimeMillis());
|
||||
if (completionList.getSelectedValue().getLabel().contains("...")) {
|
||||
// log("No hide");
|
||||
// Why not hide it? Coz this is the case of
|
||||
// overloaded methods. See #2755
|
||||
} else {
|
||||
// https://github.com/processing/processing/issues/2755
|
||||
if (!completionList.getSelectedValue().getLabel().contains("...")) {
|
||||
setInvisible();
|
||||
}
|
||||
|
||||
if (mouseClickOnOverloadedMethods) {
|
||||
// See #2755
|
||||
((JavaTextArea) editor.getTextArea()).fetchPhrase();
|
||||
}
|
||||
return true;
|
||||
|
||||
} catch (BadLocationException e1) {
|
||||
e1.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -422,8 +281,7 @@ public class CompletionPanel {
|
||||
}
|
||||
|
||||
|
||||
private String fetchCurrentSubword() {
|
||||
//log("Entering fetchCurrentSubword");
|
||||
private String fetchCurrentSubWord() {
|
||||
JEditTextArea ta = editor.getTextArea();
|
||||
int off = ta.getCaretPosition();
|
||||
//log2("off " + off);
|
||||
@@ -433,34 +291,25 @@ public class CompletionPanel {
|
||||
if (line < 0)
|
||||
return null;
|
||||
String s = ta.getLineText(line);
|
||||
//log2("lin " + line);
|
||||
//log2(s + " len " + s.length());
|
||||
|
||||
int x = ta.getCaretPosition() - ta.getLineStartOffset(line) - 1, x1 = x - 1;
|
||||
if (x >= s.length() || x < 0)
|
||||
return null; //TODO: Does this check cause problems? Verify.
|
||||
if (Base.DEBUG) System.out.print(" x char: " + s.charAt(x));
|
||||
//int xLS = off - getLineStartNonWhiteSpaceOffset(line);
|
||||
|
||||
String word = (x < s.length() ? s.charAt(x) : "") + "";
|
||||
if (s.trim().length() == 1) {
|
||||
// word = ""
|
||||
// + (keyChar == KeyEvent.CHAR_UNDEFINED ? s.charAt(x - 1) : keyChar);
|
||||
//word = (x < s.length()?s.charAt(x):"") + "";
|
||||
word = word.trim();
|
||||
if (word.endsWith("."))
|
||||
word = word.substring(0, word.length() - 1);
|
||||
|
||||
return word;
|
||||
}
|
||||
//log("fetchCurrentSubword 1 " + word);
|
||||
if(word.equals(".")) return null; // If user types 'abc.', subword becomes '.'
|
||||
// if (keyChar == KeyEvent.VK_BACK_SPACE || keyChar == KeyEvent.VK_DELETE)
|
||||
// ; // accepted these keys
|
||||
// else if (!(Character.isLetterOrDigit(keyChar) || keyChar == '_' || keyChar == '$'))
|
||||
// return null;
|
||||
int i = 0;
|
||||
|
||||
// If user types 'abc.', sub word becomes '.'
|
||||
if (word.equals(".")) return null;
|
||||
|
||||
int i = 0;
|
||||
while (true) {
|
||||
i++;
|
||||
//TODO: currently works on single line only. "a. <new line> b()" won't be detected
|
||||
@@ -481,17 +330,13 @@ public class CompletionPanel {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if (keyChar != KeyEvent.CHAR_UNDEFINED)
|
||||
//log("fetchCurrentSubword 2 " + word);
|
||||
if (Character.isDigit(word.charAt(0)))
|
||||
return null;
|
||||
word = word.trim();
|
||||
if (word.endsWith("."))
|
||||
if (word.endsWith(".")) {
|
||||
word = word.substring(0, word.length() - 1);
|
||||
//log("fetchCurrentSubword 3 " + word);
|
||||
//showSuggestionLater();
|
||||
}
|
||||
return word;
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user