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

The following examples show how to use org.netbeans.editor.Utilities#getIdentifierBefore() . 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: CodeTemplateCompletionItem.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void defaultAction(JTextComponent component) {
    Completion.get().hideAll();
    // Remove the typed part
    Document doc = component.getDocument();
    int caretOffset = component.getSelectionStart();
    int prefixLength = 0;
    try {
        String ident = Utilities.getIdentifierBefore((BaseDocument)doc, caretOffset);
        if (ident != null) {
            prefixLength = ident.length();
        }
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
    if (prefixLength > 0) {
        try {
            // Remove the typed prefix
            doc.remove(caretOffset - prefixLength, prefixLength);
        } catch (BadLocationException ble) {
        }
    }
    codeTemplate.insert(component);
}
 
Example 2
Source File: AndroidStyleable.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
@Override
public void defaultAction(JTextComponent component) {
    if (component != null) {
        try {
            BaseDocument document = (BaseDocument) component.getDocument();
            int caretPosition = component.getCaretPosition();
            String text = Utilities.getIdentifierBefore(document, component.getCaretPosition());
            if (text == null) {
                text = "";
            }
            int startPosition = caretPosition - text.length();
            if (AndroidStyleableStore.ANDROID_NAMESPACE.equals(nameSpacePath)) {
                document.replace(startPosition, text.length(), name, null);
            } else {
                document.replace(startPosition, text.length(), fullClassName, null);
            }
            Completion.get().hideDocumentation();
            Completion.get().hideCompletion();
            RankingProvider.inserted(fullClassName.hashCode());
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
}
 
Example 3
Source File: AttrCompletionItem.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
@Override
public void defaultAction(JTextComponent component) {
    if (component != null) {
        try {
            BaseDocument document = (BaseDocument) component.getDocument();
            int caretPosition = component.getCaretPosition();
            String text = Utilities.getIdentifierBefore(document, component.getCaretPosition());
            if (text == null) {
                text = "";
            }
            int startPosition = caretPosition - text.length();
            document.replace(startPosition, text.length(), completionText + "=\"\"", null);
            Completion.get().hideAll();
            RankingProvider.inserted(attr.getName().hashCode());
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
}
 
Example 4
Source File: LayoutCompletionQuery.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private boolean canFilterElement(JTextComponent component) {
    try {
        typedCharsFilter = Utilities.getIdentifierBefore((BaseDocument) component.getDocument(), component.getCaretPosition());
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
    if (typedCharsFilter == null) {
        typedCharsFilter = "";
    }
    typedCharsFilter = typedCharsFilter.toLowerCase();
    return true;
}
 
Example 5
Source File: LayoutCompletionQuery.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private boolean canFilterAttribute(JTextComponent component) {
    try {
        typedCharsFilter = Utilities.getIdentifierBefore((BaseDocument) component.getDocument(), component.getCaretPosition());
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
    if (typedCharsFilter == null) {
        typedCharsFilter = "";
    }
    typedCharsFilter = typedCharsFilter.toLowerCase();
    return true;
}
 
Example 6
Source File: LayoutCompletionQuery.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private void makeAttribute(AndroidJavaPlatform findPlatform, CompletionContextImpl context, FileObject primaryFile, AndroidProject androidProject, CompletionResultSet resultSet, Document doc, int caretOffset) {
    HashMap<String, String> declaredNamespaces = context.getDeclaredNamespaces();
    String typedChars = null;
    //when is cursor at end of text context returns null
    try {
        typedChars = Utilities.getIdentifierBefore((BaseDocument) doc, caretOffset);
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
    List<QName> pathFromRoot = context.getPathFromRoot();
    String attributeRoot = null;
    if (!pathFromRoot.isEmpty()) {
        QName qname = pathFromRoot.get(pathFromRoot.size() - 1);
        attributeRoot = qname.getLocalPart();
        AndroidStyleableNamespace platformWidgetNamespaces = findPlatform.getPlatformWidgetNamespaces();
    }
    if (typedChars == null) {
        typedChars = "";
    }
    final String typed = typedChars.toLowerCase();
    if (attributeRoot == null || "".equals(attributeRoot)) {
        return;
    }
    findAttributes(styleableAttrs, primaryFile, attributeRoot, declaredNamespaces, pathFromRoot);
    if ("".equals(typed)) {
        resultSet.addAllItems(styleableAttrs);
    } else {
        resultSet.addAllItems(styleableAttrs.stream().filter(c -> c.getLowerCasecompletionText().startsWith(typed) || c.getLowerCaseSimpleCompletionText().startsWith(typed)).collect(Collectors.toList()));
    }
}
 
Example 7
Source File: LayoutCompletionQuery.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private void makeElement(AndroidJavaPlatform findPlatform, CompletionContextImpl context, FileObject primaryFile, AndroidProject androidProject, CompletionResultSet resultSet, Document doc, int caretOffset) {
    HashMap<String, String> declaredNamespaces = context.getDeclaredNamespaces();
    currentMode = CompletionContext.CompletionType.COMPLETION_TYPE_ELEMENT;
    String typedChars = context.getTypedChars();
    if (typedChars == null) {
        //when is cursor at end of text context returns null
        try {
            typedChars = Utilities.getIdentifierBefore((BaseDocument) doc, caretOffset);
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    if (typedChars == null) {
        //cursor is after <
        typedChars = "";
    }
    final String typed = typedChars.toLowerCase();
    Map<String, AndroidStyleableNamespace> namespacesIn = AndroidStyleableStore.findNamespaces(primaryFile);
    Map<String, AndroidStyleableNamespace> namespaces = new HashMap<>();
    for (Map.Entry<String, String> entry : declaredNamespaces.entrySet()) {
        String name = entry.getKey();
        String nameSpace = entry.getValue();
        AndroidStyleableNamespace tmp = namespacesIn.get(nameSpace);
        if (tmp != null) {
            namespaces.put(name, tmp);
            items.putAll(tmp.getLayouts());
            items.putAll(tmp.getWitgets());
        }
    }
    if (namespaces.isEmpty()) {
        return;
    }
    startChars = typed;
    if ("".equals(typed)) {
        resultSet.addAllItems(items.values());
    } else {
        resultSet.addAllItems(items.values().stream().filter(c -> c.getLowerCaseFullClassName().startsWith(typed) || c.getLowerCaseName().startsWith(typed) || c.getUpperCaseLetters().equals(typed)).collect(Collectors.toList()));
    }
}
 
Example 8
Source File: LayoutCompletionQuery.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
private void makeAttributeValue(AndroidResValuesProvider valuesProvider, AndroidJavaPlatform findPlatform, CompletionContextImpl context, FileObject primaryFile, AndroidProject androidProject, CompletionResultSet resultSet, Document doc, int caretOffset) {
    HashMap<String, String> declaredNamespaces = context.getDeclaredNamespaces();
    String attribute = context.getAttribute();
    String typedChars = context.getTypedChars();
    if (typedChars == null) {
        try {
            typedChars = Utilities.getIdentifierBefore((BaseDocument) doc, caretOffset);
        } catch (BadLocationException ex) {
        }
    }
    if (typedChars == null) {
        typedChars = "";
    }
    String attributeRoot = null;
    List<QName> pathFromRoot = context.getPathFromRoot();
    List<AttrCompletionItem> tmpAttrs = new ArrayList<>();
    if (!pathFromRoot.isEmpty()) {
        if (attrValues.isEmpty()) {
            QName qname = pathFromRoot.get(pathFromRoot.size() - 1);
            attributeRoot = qname.getLocalPart();
            findAttributes(tmpAttrs, primaryFile, attributeRoot, declaredNamespaces, pathFromRoot);
            for (AttrCompletionItem tmpAttr : tmpAttrs) {
                if (attribute.equals(tmpAttr.getCompletionText())) {
                    List<BasicValuesCompletionItem> forType = valuesProvider.forType(tmpAttr.getAttr().getAttrTypes(), typedChars, doc, caretOffset);
                    attrValues.addAll(forType);
                    AndroidStyleableAttrEnum[] enums = tmpAttr.getAttr().getEnums();
                    for (AndroidStyleableAttrEnum aEnum : enums) {
                        attrValues.add(BasicValuesCompletionItem.create(AndroidValueType.ENUM, aEnum.getName(), aEnum.getValue(), aEnum.getComment()));
                    }
                    AndroidStyleableAttrFlag[] flags = tmpAttr.getAttr().getFlags();
                    for (AndroidStyleableAttrFlag flag : flags) {
                        attrValues.add(BasicValuesCompletionItem.create(AndroidValueType.FLAG, flag.getName(), flag.getValue(), flag.getComment()));
                    }
                    break;
                }
            }
        }
        final String typed = typedChars;
        if ("".equals(typedChars)) {
            resultSet.addAllItems(attrValues);
        } else {
            resultSet.addAllItems(attrValues.stream().filter(c -> c.getCompletionText().startsWith(typed) || c.getName().startsWith(typed)).collect(Collectors.toList()));
        }
    }
}