diff --git a/java/src/processing/mode/java/CompletionPanel.java b/java/src/processing/mode/java/CompletionPanel.java index b23df460e..ffebb2ed7 100644 --- a/java/src/processing/mode/java/CompletionPanel.java +++ b/java/src/processing/mode/java/CompletionPanel.java @@ -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 completionList; + /** The completion list generated by ASTGenerator */ + final private JList 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 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(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() { - 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 createSuggestionList(final int position, - final DefaultListModel items) { - - JList list = new JList(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 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. 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; - //} } diff --git a/todo.txt b/todo.txt index fd872dff0..45e9c79b8 100755 --- a/todo.txt +++ b/todo.txt @@ -29,7 +29,12 @@ X close the interface issue, major changes done X remove underscore and use (half?) space for tabs? X half space character not implemented, but the space looks good X disable behavior with sketch.name.replace_underscore = false -X add other naming options (cooking and classic) and prefs + +naming +X Friendly Names for new Sketches (includes UI for switching it back) +X https://github.com/processing/processing/issues/6045 +X https://github.com/processing/processing/pull/6048 +X https://github.com/processing/processing4/pull/144 X "friendly" naming for sketches X master list of words X https://github.com/glitchdotcom/friendly-words/tree/master/words @@ -39,6 +44,7 @@ X https://github.com/glitchdotcom/friendly-words/blob/master/words/objects.t X how friendly names are used in the p5.js web editor X https://github.com/processing/p5.js-web-editor/blob/develop/client/utils/generateRandomName.js X p5.js uses predicate followed by an object +X add other naming options (cooking and classic) and prefs bugs X re-post bugzilla entries at https://download.processing.org/bugzilla/ @@ -344,6 +350,8 @@ X re-save svg files using svg 1.0 X sort out hover/press states here (only hovers atm) X also add state for shift-click to search _ theme colors for emoji buttons (new themes across the board) +_ code completion icon updates (class, field, protected, method) +_ these go into CompletionPanel.java _ reset the theme because of significant changes _ move away from writing theme.txt? _ instead store the theme name, for easier updating @@ -495,11 +503,6 @@ _ add rank for libraries/modes/tools (use unicode chars?) _ probably not sort by default to avoid confusion -design -_ code completion icon updates (class, field, protected, method) -_ these go into CompletionPanel.java - - design/implementation _ updateTheme() in Theme Selector _ theme_selector.combo_box.enabled.bgcolor @@ -566,8 +569,6 @@ _ Export to Application was broken with OpenGL (Casey) decisions before final 4.0 release _ can we compress jdk/Contents/Home/legal into a single zip? _ it's ~300 files of the ~500 in the entire jdk directory -_ Friendly Names for new Sketches (includes UI for switching it back) -_ https://github.com/processing/processing/pull/6048 _ change the license from GPL _ then pass through the source to update licenses _ add Processing Foundation as 2012-15