Java Code Examples for org.netbeans.editor.Utilities#getIdentifierBlock()

The following examples show how to use org.netbeans.editor.Utilities#getIdentifierBlock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtKit.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public boolean gotoDeclaration(JTextComponent target) {
    BaseDocument doc = Utilities.getDocument(target);
    if (doc == null)
        return false;
    try {
        Caret caret = target.getCaret();
        int dotPos = caret.getDot();
        int[] idBlk = Utilities.getIdentifierBlock(doc, dotPos);
        ExtSyntaxSupport extSup = (ExtSyntaxSupport)doc.getSyntaxSupport();
        if (idBlk != null) {
            int decPos = extSup.findDeclarationPosition(doc.getText(idBlk), idBlk[1]);
            if (decPos >= 0) {
                caret.setDot(decPos);
                return true;
            }
        }
    } catch (BadLocationException e) {
    }
    return false;
}
 
Example 2
Source File: HyperlinkProviderImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public int[] getHyperlinkSpan(Document doc, int offset, HyperlinkType type) {
    if (doc.getProperty(HyperlinkProviderImpl.class) != Boolean.TRUE) {
        //not handled by a LSP handler
        return null;
    }

    try {
        //XXX: not really using the server, are we?
        int[] ident = Utilities.getIdentifierBlock((BaseDocument) doc, offset);
        TokenSequence<?> ts = TokenHierarchy.get(doc).tokenSequence();
        ts.move(offset);
        if (ts.moveNext() && ts.token().id() == TextmateTokenId.TEXTMATE) {
            if (ident != null) {
                return new int[] {ts.offset(), ts.offset() + ts.token().length()};
            } else {
                return null;
            }
        }
        return ident;
    } catch (BadLocationException ex) {
        return null;
    }
}
 
Example 3
Source File: AddCaretSelectNextAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        Caret caret = target.getCaret();
        if (Utilities.isSelectionShowing(caret)) {
            EditorFindSupport findSupport = EditorFindSupport.getInstance();
            HashMap<String, Object> props = new HashMap<>(findSupport.createDefaultFindProperties());
            String searchWord = target.getSelectedText();
            int n = searchWord.indexOf('\n');
            if (n >= 0) {
                searchWord = searchWord.substring(0, n);
            }
            props.put(EditorFindSupport.FIND_WHAT, searchWord);
            props.put(EditorFindSupport.ADD_MULTICARET, Boolean.TRUE);
            EditorUI eui = org.netbeans.editor.Utilities.getEditorUI(target);
            if (eui.getComponent().getClientProperty("AsTextField") == null) { //NOI18N
                findSupport.setFocusedTextComponent(eui.getComponent());
            }
            findSupport.putFindProperties(props);
            findSupport.find(null, false);
            props.put(EditorFindSupport.ADD_MULTICARET, Boolean.FALSE);
            findSupport.putFindProperties(props);
        } else {
            try {
                int[] identifierBlock = Utilities.getIdentifierBlock((BaseDocument) target.getDocument(), caret.getDot());
                if (identifierBlock != null) {
                    caret.setDot(identifierBlock[0]);
                    caret.moveDot(identifierBlock[1]);
                }
            } catch (BadLocationException e) {
                LOGGER.log(Level.WARNING, null, e);
            }
        }
    }
}
 
Example 4
Source File: GotoAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    String actionName = actionName();
    if (EditorActionNames.gotoDeclaration.equals(actionName)) {
        resetCaretMagicPosition(target);
        if (target != null) {
            if (hyperlinkGoTo(target)) {
                return;
            }
            
            BaseDocument doc = Utilities.getDocument(target);
            if (doc != null) {
                try {
                    Caret caret = target.getCaret();
                    int dotPos = caret.getDot();
                    int[] idBlk = Utilities.getIdentifierBlock(doc, dotPos);
                    ExtSyntaxSupport extSup = (ExtSyntaxSupport) doc.getSyntaxSupport();
                    if (idBlk != null) {
                        int decPos = extSup.findDeclarationPosition(doc.getText(idBlk), idBlk[1]);
                        if (decPos >= 0) {
                            caret.setDot(decPos);
                        }
                    }
                } catch (BadLocationException e) {
                }
            }
        }
    }
}
 
Example 5
Source File: NbEditorUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** This method is a composition of <tt>Utilities.getIdentifierBlock()</tt>
* and <tt>SyntaxSupport.getFunctionBlock()</tt>.
* @return null if there's no identifier at the given position.
*   identifier block if there's identifier but it's not a function call.
*   three member array for the case that there is an identifier followed
*   by the function call character. The first two members are members
*   of the identifier block and the third member is the second member
*   of the function block.
*/
public static int[] getIdentifierAndMethodBlock(BaseDocument doc, int offset)
throws BadLocationException {
    int[] idBlk = Utilities.getIdentifierBlock(doc, offset);
    if (idBlk != null) {
        int[] funBlk = ((ExtSyntaxSupport)doc.getSyntaxSupport()).getFunctionBlock(idBlk);
        if (funBlk != null) {
            return new int[] { idBlk[0], idBlk[1], funBlk[1] };
        }
    }
    return idBlk;
}
 
Example 6
Source File: AddCaretSelectAllAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        Caret caret = target.getCaret();
        if (!Utilities.isSelectionShowing(caret)) {
            try {
                int[] identifierBlock = Utilities.getIdentifierBlock((BaseDocument) target.getDocument(), caret.getDot());
                if (identifierBlock != null) {
                    caret.setDot(identifierBlock[0]);
                    caret.moveDot(identifierBlock[1]);
                }
            } catch (BadLocationException e) {
                LOGGER.log(Level.WARNING, null, e);
            }
        }

        EditorFindSupport findSupport = EditorFindSupport.getInstance();
        HashMap<String, Object> props = new HashMap<>(findSupport.createDefaultFindProperties());
        String searchWord = target.getSelectedText();
        if (searchWord != null) {
            int n = searchWord.indexOf('\n');
            if (n >= 0) {
                searchWord = searchWord.substring(0, n);
            }
            props.put(EditorFindSupport.FIND_WHAT, searchWord);
            Document doc = target.getDocument();
            EditorUI eui = org.netbeans.editor.Utilities.getEditorUI(target);
            if (eui.getComponent().getClientProperty("AsTextField") == null) { //NOI18N
                findSupport.setFocusedTextComponent(eui.getComponent());
            }
            findSupport.putFindProperties(props);
            findSupport.find(null, false);

            if (caret instanceof EditorCaret) {
                EditorCaret editorCaret = (EditorCaret) caret;
                try {
                    int[] blocks = findSupport.getBlocks(new int[]{-1, -1}, doc, 0, doc.getLength());
                    if (blocks[0] >= 0 && blocks.length % 2 == 0) {
                        List<Position> newCarets = new ArrayList<>();

                        for (int i = 0; i < blocks.length; i += 2) {
                            int start = blocks[i];
                            int end = blocks[i + 1];
                            if (start == -1 || end == -1) {
                                break;
                            }
                            Position startPos = doc.createPosition(start);
                            Position endPos = doc.createPosition(end);
                            newCarets.add(endPos);
                            newCarets.add(startPos);
                        }

                        editorCaret.replaceCarets(newCarets, null); // TODO handle biases
                    }
                } catch (BadLocationException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
    }
}
 
Example 7
Source File: CompletionImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private int getCompletionPreSelectionIndex(List<CompletionItem> items) {
    String prefix = null;
    if(getActiveDocument() instanceof BaseDocument) {
        BaseDocument doc = (BaseDocument)getActiveDocument();
        int caretOffset = getActiveComponent().getSelectionStart();
        try {
            int[] block = Utilities.getIdentifierBlock(doc, caretOffset);
            if (block != null) {
                block[1] = caretOffset;
                prefix = doc.getText(block);
            }
        } catch (BadLocationException ble) {
        }
    }
    int closestIdx = 0;
    if (prefix != null && prefix.length() > 0) {
        int distance = Integer.MAX_VALUE;
        int idx = 0;
        String prefLC = prefix.toLowerCase();
        for (CompletionItem item : items) {
            CharSequence text = item.getInsertPrefix();
            int prio = item.getSortPriority() * 1000;
            boolean isSmart = prio < 0;
            String name = text != null ? text.toString() : null;
            if (name != null) {
                for (String part : name.split("\\.")) {
                    if (part.startsWith(prefix) && (!(item instanceof LazyCompletionItem) || ((LazyCompletionItem)item).accept())) {
                        return idx;
                    }
                    int d = prio + getDistance(part.toLowerCase(), prefLC);
                    if (part.toLowerCase().startsWith(prefLC)) {
                        d -= 500;
                    }
                    if (d < distance) {
                        distance = d;
                        closestIdx = idx;
                    }
                    if (!isSmart) {
                        break;
                    }
                }
            }
            idx++;
        }
    }
    return closestIdx;
}