/** * 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; }
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; }
/** * 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; }
/** * 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; }
/** * We prefer parameter standing for table names better than column names * since the choice of the table determines the choice of the column * * there are completions containing only columns but no tables * * @return highlight chosen. not null. */ private Highlight getPreferredInitialHighlight() { Highlight result = null; int idxTableParam = -1; int idxColumnParam = -1; List<Highlight> highlights = getParameterHighlights(); for (int i = 0; i < pc.getParamCount(); i++) { if (pc.getParam(i).getName().equals(Const.paramNameTable)) idxTableParam = i; if (pc.getParam(i).getName().equals(Const.paramNameColumn)) idxColumnParam = i; } if (idxTableParam >= 0) result = highlights.get(idxTableParam); else if (idxColumnParam >= 0) result = highlights.get(idxColumnParam); if (result == null && pc.getParamCount() > 0) result = highlights.get(0); return result; }
/** * gft adaption: quote * @param choice */ private void insertChoice(String choice) { JTextComponent tc = ac.getTextComponent(); Highlight h = getCurrentParameterHighlight(); if (h != null) { String text = tc.getText(); // "+1" is a workaround for Java Highlight issues. tc.setSelectionStart(h.getStartOffset() + 1); tc.setSelectionEnd(h.getEndOffset()); tc.replaceSelection(AutoCompletion.quoteChoice(choice)); moveToNextParam(); } else { UIManager.getLookAndFeel().provideErrorFeedback(tc); } }
/** * Remove the AutoSpellChecker from the given JTextComponent. * * @param text * the JTextComponent */ static void disable( JTextComponent text ){ AbstractDocument doc = (AbstractDocument)text.getDocument(); for(DocumentListener listener : doc.getDocumentListeners()){ if(listener instanceof AutoSpellChecker){ AutoSpellChecker autoSpell = (AutoSpellChecker)listener; doc.removeDocumentListener( autoSpell ); Highlighter highlighter = text.getHighlighter(); for( Highlight highlight : highlighter.getHighlights() ) { if( highlight.getPainter() == painter ){ highlighter.removeHighlight( highlight ); } } } } }
/** * Returns the highlight from a list that comes "first" in a list. Even * though most parameter highlights are ordered, sometimes they aren't * (e.g. the "cursor" parameter in a template completion is always last, * even though it can be anywhere in the template). * * @param highlights The list of highlights. Assumed to be non-empty. * @return The highlight that comes first in the document. * @see #getLastHighlight(List) */ private static final int getFirstHighlight(List<Highlight> highlights) { int first = -1; Highlight firstH = null; for (int i=0; i<highlights.size(); i++) { Highlight h = highlights.get(i); if (firstH==null || h.getStartOffset()<firstH.getStartOffset()) { firstH = h; first = i; } } return first; }
/** * Returns the highlight from a list that comes "last" in that list. Even * though most parameter highlights are ordered, sometimes they aren't * (e.g. the "cursor" parameter in a template completion is always last, * even though it can be anywhere in the template. * * @param highlights The list of highlights. Assumed to be non-empty. * @return The highlight that comes last in the document. * @see #getFirstHighlight(List) */ private static final int getLastHighlight(List<Highlight> highlights) { int last = -1; Highlight lastH = null; for (int i=highlights.size()-1; i>=0; i--) { Highlight h = highlights.get(i); if (lastH==null || h.getStartOffset()>lastH.getStartOffset()) { lastH = h; last = i; } } return last; }
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; }
/** * Moves to and selects the next parameter. * * @see #moveToPreviousParam() */ private void moveToNextParam() { JTextComponent tc = ac.getTextComponent(); int dot = tc.getCaretPosition(); int tagCount = tags.size(); if (tagCount==0) { tc.setCaretPosition(maxPos.getOffset()); deactivate(); } Highlight currentNext = null; int pos = -1; List<Highlight> highlights = getParameterHighlights(); for (int i=0; i<highlights.size(); i++) { Highlight hl = highlights.get(i); // Check "< dot", not "<= dot" as OutlineHighlightPainter paints // starting at one char AFTER the highlight starts, to work around // Java issue. Thanks to Matthew Adereth! if (currentNext==null || currentNext.getStartOffset()</*=*/dot || (hl.getStartOffset()>dot && hl.getStartOffset()<=currentNext.getStartOffset())) { currentNext = hl; pos = i; } } // No params after caret - go to first one if (currentNext.getStartOffset()+1<=dot) { int nextIndex = getFirstHighlight(highlights); currentNext = highlights.get(nextIndex); pos = 0; } // "+1" is a workaround for Java Highlight issues. tc.setSelectionStart(currentNext.getStartOffset()+1); tc.setSelectionEnd(currentNext.getEndOffset()); updateToolTipText(pos); }
private void possiblyUpdateParamCopies(Document doc) { int index = getCurrentParameterIndex(); // FunctionCompletions add an extra param at end of inserted text if (index>-1 && index<pc.getParamCount()) { // Typing in an "end parameter" => stop parameter assistance. Parameter param = pc.getParam(index); if (param.isEndParam()) { deactivate(); return; } // Get the current value of the current parameter. List<Highlight> paramHighlights = getParameterHighlights(); Highlight h = paramHighlights.get(index); int start = h.getStartOffset() + 1; // param offsets are offset (!) by 1 int len = h.getEndOffset() - start; String replacement = null; try { replacement = doc.getText(start, len); } catch (BadLocationException ble) { ble.printStackTrace(); // Never happens } // Replace any param copies tracking this parameter with the // value of this parameter. for (ParamCopyInfo pci : paramCopyInfos) { if (pci.paramName.equals(param.getName())) { pci.h = replaceHighlightedText(doc, pci.h, replacement); } } } else { // Probably the "end parameter" for FunctionCompletions. deactivate(); } }
/** * Updates the text in the tool tip to have the current parameter * displayed in bold. The "current parameter" is determined from the * current caret position. * * @return The "prefix" of text in the caret's parameter before the caret. */ private String updateToolTipText() { JTextComponent tc = ac.getTextComponent(); int dot = tc.getSelectionStart(); int mark = tc.getSelectionEnd(); int index = -1; String paramPrefix = null; List<Highlight> paramHighlights = getParameterHighlights(); for (int i=0; i<paramHighlights.size(); i++) { Highlight h = paramHighlights.get(i); // "+1" because of param hack - see OutlineHighlightPainter int start = h.getStartOffset()+1; if (dot>=start && dot<=h.getEndOffset()) { try { // All text selected => offer all suggestions, otherwise // use prefix before selection if (dot!=start || mark!=h.getEndOffset()) { paramPrefix = tc.getText(start, dot-start); } } catch (BadLocationException ble) { ble.printStackTrace(); } index = i; break; } } updateToolTipText(index); return paramPrefix; }
public void setMarkupColor(Bounds bounds, Color color) { // Save the currently selected offsets int start = textCode.getSelectionStart(); int end = textCode.getSelectionEnd(); // Remove selection SwingUtilities.invokeLater(() -> { textCode.setSelectionStart(start); textCode.setSelectionEnd(start); }); Stream.of(highlighter.getHighlights()) // Obtain highlights which are within the requested bounds .filter(h -> Bounds.of(h.getStartOffset(), h.getEndOffset()).collidesWith(bounds)) // For each highlight, we need to remove it and add it again as another color. There is no // way to directly change the color of a highlight once set. .forEach(h -> { Bounds b = Bounds.of(h.getStartOffset(), h.getEndOffset()); // TODO: Race Condition!!! highlightMap is added to from UI Thread SwingUtilities.invokeLater(() -> { highlighter.removeHighlight(h); HighlightPainter hp = new DefaultHighlightPainter(ColorUtils.makeTransparent(color)); try { Highlight h1 = (Highlight) highlighter.addHighlight(b.getStart(), b.getEnd(), hp); highlightMap.put(b, h1); } catch (BadLocationException ex) { ex.printStackTrace(); } }); }); // Restore saved offsets if (start != 0 || end != 0) { SwingUtilities.invokeLater(() -> setSelection(Bounds.of(start, end))); } }
/** * Returns the highlight from a list that comes "first" in a list. Even * though most parameter highlights are ordered, sometimes they aren't (e.g. * the "cursor" parameter in a template completion is always last, even * though it can be anywhere in the template). * * @param highlights * The list of highlights. Assumed to be non-empty. * @return The highlight that comes first in the document. * @see #getLastHighlight(List) */ private static final int getFirstHighlight(List<Highlight> highlights) { int first = -1; Highlight firstH = null; for (int i = 0; i < highlights.size(); i++) { Highlight h = highlights.get(i); if (firstH == null || h.getStartOffset() < firstH.getStartOffset()) { firstH = h; first = i; } } return first; }
/** * Returns the highlight from a list that comes "last" in that list. Even * though most parameter highlights are ordered, sometimes they aren't (e.g. * the "cursor" parameter in a template completion is always last, even * though it can be anywhere in the template. * * @param highlights * The list of highlights. Assumed to be non-empty. * @return The highlight that comes last in the document. * @see #getFirstHighlight(List) */ private static final int getLastHighlight(List<Highlight> highlights) { int last = -1; Highlight lastH = null; for (int i = highlights.size() - 1; i >= 0; i--) { Highlight h = highlights.get(i); if (lastH == null || h.getStartOffset() > lastH.getStartOffset()) { lastH = h; last = i; } } return last; }
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 (!isGftErrorHighlighting(painter) && (painter == p || painter == endingP)) paramHighlights.add(highlights[i]); } return paramHighlights; }
/** * Updates the text in the tool tip to have the current parameter displayed * in bold. The "current parameter" is determined from the current caret * position. * * @return The "prefix" of text in the caret's parameter before the caret. */ private String updateToolTipText() { JTextComponent tc = ac.getTextComponent(); int dot = tc.getSelectionStart(); int mark = tc.getSelectionEnd(); int index = -1; String paramPrefix = null; List<Highlight> paramHighlights = getParameterHighlights(); for (int i = 0; i < paramHighlights.size(); i++) { Highlight h = paramHighlights.get(i); // "+1" because of param hack - see OutlineHighlightPainter int start = h.getStartOffset() + 1; if (dot >= start && dot <= h.getEndOffset()) { try { // All text selected => offer all suggestions, otherwise // use prefix before selection if (dot != start || mark != h.getEndOffset()) { paramPrefix = tc.getText(start, dot - start); } } catch (BadLocationException ble) { ble.printStackTrace(); } index = i; break; } } updateToolTipText(index); return paramPrefix; }
/** * Remove all SpellChecker highlights from the given JTextComponent. * * @param text * the JTextComponent */ private static void removeHighlights( JTextComponent text ) { Highlighter highlighter = text.getHighlighter(); for( Highlight highlight : highlighter.getHighlights() ) { if( highlight.getPainter() == painter ) { highlighter.removeHighlight( highlight ); } } }