/** * Removes the bounding boxes around parameters. */ private void removeParameterHighlights() { JTextComponent tc = ac.getTextComponent(); Highlighter h = tc.getHighlighter(); for (int i=0; i<tags.size(); i++) { h.removeHighlight(tags.get(i)); } tags.clear(); for (ParamCopyInfo pci : paramCopyInfos) { h.removeHighlight(pci.h); } paramCopyInfos.clear(); }
public List<Highlight> getParameterHighlights() { List<Highlight> paramHighlights = new ArrayList<Highlight>(2); JTextComponent tc = ac.getTextComponent(); Highlight[] highlights = tc.getHighlighter().getHighlights(); for (int i=0; i<highlights.length; i++) { HighlightPainter painter = highlights[i].getPainter(); if (painter==p || painter==endingP) { paramHighlights.add(highlights[i]); } } return paramHighlights; }
JTextComponent tc = ac.getTextComponent(); InputMap im = tc.getInputMap(); ActionMap am = tc.getActionMap();
private int getCurrentParameterIndex() { JTextComponent tc = ac.getTextComponent(); int dot = tc.getCaretPosition(); if (dot>0) { dot--; // Workaround for Java Highlight issues } List<Highlight> paramHighlights = getParameterHighlights(); for (int i=0; i<paramHighlights.size(); i++) { Highlight h = paramHighlights.get(i); if (dot>=h.getStartOffset() && dot<h.getEndOffset()) { return i; } } return -1; }
@Override public void actionPerformed(ActionEvent e) { boolean doNormalCopy = false; if (descWindow!=null && descWindow.isVisible()) { doNormalCopy = !descWindow.copy(); } if (doNormalCopy) { ac.getTextComponent().copy(); } }
/** * Uninstalls this listener from the current text component. */ public void uninstall() { JTextComponent tc = ac.getTextComponent(); tc.removeCaretListener(this); tc.removeFocusListener(this); tc.getDocument().removeDocumentListener(this); uninstallKeyBindings(); if (markOccurrencesEnabled) { ((RSyntaxTextArea)tc).setMarkOccurrences(markOccurrencesEnabled); } // Remove WeakReferences in javax.swing.text. maxPos = null; minPos = -1; removeParameterHighlights(); }
/** * Initializes this window to offer suggestions for the parameters of * a specific completion. * * @param pc The completion whose parameters we should offer suggestions * for. */ public void initialize(ParameterizedCompletion pc) { CompletionProvider provider = pc.getProvider(); ParameterChoicesProvider pcp = provider.getParameterChoicesProvider(); if (pcp==null) { choicesListList = null; return; } int paramCount = pc.getParamCount(); choicesListList = new ArrayList<>(paramCount); JTextComponent tc = ac.getTextComponent(); for (int i=0; i<paramCount; i++) { ParameterizedCompletion.Parameter param = pc.getParam(i); List<Completion> choices = pcp.getParameterChoices(tc, param); choicesListList.add(choices); } }
@Override public void actionPerformed(ActionEvent e) { if (isVisible()) { JTextComponent comp = ac.getTextComponent(); Caret c = comp.getCaret(); int dot = c.getDot(); if (dot < comp.getDocument().getLength()) { c.setDot(++dot); // Ensure moving right hasn't moved us up a line, thus // hiding the popup window. if (comp.isVisible()) { if (lastLine!=-1) { doAutocomplete(); } } } } }
/** * Returns the text inserted for the parameter containing the specified * offset. * * @param offs The offset into the document. * @return The text of the parameter containing the offset, or * <code>null</code> if the offset is not in a parameter. */ public String getArgumentText(int offs) { List<Highlight> paramHighlights = getParameterHighlights(); if (paramHighlights==null || paramHighlights.size()==0) { return null; } for (Highlight h : paramHighlights) { if (offs>=h.getStartOffset() && offs<=h.getEndOffset()) { int start = h.getStartOffset() + 1; int len = h.getEndOffset() - start; JTextComponent tc = ac.getTextComponent(); Document doc = tc.getDocument(); try { return doc.getText(start, len); } catch (BadLocationException ble) { UIManager.getLookAndFeel().provideErrorFeedback(tc); ble.printStackTrace(); return null; } } } return null; }
/** * Returns the highlight of the current parameter. * * @return The current parameter's highlight, or <code>null</code> if * the caret is not in a parameter's bounds. * @see #getCurrentParameterStartOffset() */ private Highlight getCurrentParameterHighlight() { JTextComponent tc = ac.getTextComponent(); int dot = tc.getCaretPosition(); if (dot>0) { dot--; // Workaround for Java Highlight issues } List<Highlight> paramHighlights = getParameterHighlights(); for (Highlight h : paramHighlights) { if (dot>=h.getStartOffset() && dot<h.getEndOffset()) { return h; } } return null; }
/** * Updates the optional window listing likely completion choices. */ private void prepareParamChoicesWindow() { // If this window was set to null, the user pressed Escape to hide it if (paramChoicesWindow!=null) { int offs = getCurrentParameterStartOffset(); if (offs==-1) { paramChoicesWindow.setVisible(false); return; } JTextComponent tc = ac.getTextComponent(); try { Rectangle r = tc.modelToView(offs); Point p = new Point(r.x, r.y); SwingUtilities.convertPointToScreen(p, tc); r.x = p.x; r.y = p.y; paramChoicesWindow.setLocationRelativeTo(r); } catch (BadLocationException ble) { // Should never happen UIManager.getLookAndFeel().provideErrorFeedback(tc); ble.printStackTrace(); } // Toggles visibility, if necessary. paramChoicesWindow.setParameter(lastSelectedParam, paramPrefix); } }
Highlighter highlighter = ac.getTextComponent().getHighlighter(); highlighter.removeHighlight(h);
@Override public void actionPerformed(ActionEvent e) { JTextComponent tc = ac.getTextComponent(); int dot = tc.getCaretPosition(); char end = pc.getProvider().getParameterListEnd(); // Are they at or past the end of the parameters? if (dot>=maxPos.getOffset()-2) { // ">=" for overwrite mode // Try to decide if we're closing a paren that is a part // of the (last) arg being typed. String text = getArgumentText(dot); if (text!=null) { char start = pc.getProvider().getParameterListStart(); int startCount = getCount(text, start); int endCount = getCount(text, end); if (startCount>endCount) { // Just closing a paren tc.replaceSelection(Character.toString(end)); return; } } //tc.setCaretPosition(maxPos.getOffset()); tc.setCaretPosition(Math.min(tc.getCaretPosition()+1, tc.getDocument().getLength())); deactivate(); } // If not (in the middle of parameters), just insert the paren. else { tc.replaceSelection(Character.toString(end)); } }
@Override public void actionPerformed(ActionEvent e) { if (isVisible()) { JTextComponent comp = ac.getTextComponent(); Caret c = comp.getCaret(); int dot = c.getDot(); if (dot > 0) { c.setDot(--dot); // Ensure moving left hasn't moved us up a line, thus // hiding the popup window. if (comp.isVisible()) { if (lastLine!=-1) { doAutocomplete(); } } } } }
/** * Inserts a completion. Any time a code completion event occurs, the actual * text insertion happens through this method. * * @param c A completion to insert. This cannot be <code>null</code>. * @param typedParamListStartChar Whether the parameterized completion start * character was typed (typically <code>'('</code>). */ protected void insertCompletion(Completion c, boolean typedParamListStartChar) { JTextComponent textComp = getTextComponent(); String alreadyEntered = c.getAlreadyEntered(textComp); hidePopupWindow(); Caret caret = textComp.getCaret(); int dot = caret.getDot(); int len = alreadyEntered.length(); int start = dot - len; String replacement = getReplacementText(c, textComp.getDocument(), start, len); caret.setDot(start); caret.moveDot(dot); textComp.replaceSelection(replacement); if (isParameterAssistanceEnabled() && (c instanceof ParameterizedCompletion)) { ParameterizedCompletion pc = (ParameterizedCompletion) c; startParameterizedCompletionAssistance(pc, typedParamListStartChar); } }
@Override public void actionPerformed(ActionEvent e) { // If the param choices window is visible and something is chosen, // replace the parameter with it and move to the next one. if (paramChoicesWindow!=null && paramChoicesWindow.isVisible()) { if (insertSelectedChoice()) { return; } } // Otherwise, just move to the end. deactivate(); JTextComponent tc = ac.getTextComponent(); int dot = tc.getCaretPosition(); if (dot!=defaultEndOffs.getOffset()) { tc.setCaretPosition(defaultEndOffs.getOffset()); } else { // oldEnterAction isn't what we're looking for (wrong key) Action a = getDefaultEnterAction(tc); if (a!=null) { a.actionPerformed(e); } else { tc.replaceSelection("\n"); } } }
JTextComponent tc = ac.getTextComponent(); int dot = tc.getCaretPosition(); int tagCount = tags.size();
/** * Inserts the choice selected in the parameter choices window. * * @return Whether the choice was inserted. This will be <code>false</code> * if the window is not visible, or no choice is selected. */ boolean insertSelectedChoice() { if (paramChoicesWindow!=null && paramChoicesWindow.isVisible()) { String choice = paramChoicesWindow.getSelectedChoice(); if (choice!=null) { JTextComponent tc = ac.getTextComponent(); Highlight h = getCurrentParameterHighlight(); if (h!=null) { // "+1" is a workaround for Java Highlight issues. tc.setSelectionStart(h.getStartOffset()+1); tc.setSelectionEnd(h.getEndOffset()); tc.replaceSelection(choice); moveToNextParam(); } else { UIManager.getLookAndFeel().provideErrorFeedback(tc); } return true; } } return false; }
JTextComponent comp = ac.getTextComponent(); InputMap im = comp.getInputMap(); ActionMap am = comp.getActionMap();
JTextComponent tc = ac.getTextComponent(); lastSelectedParam = -1;