com.intellij.lang.properties.psi.impl.PropertiesFileImpl Java Examples

The following examples show how to use com.intellij.lang.properties.psi.impl.PropertiesFileImpl. 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: PropertiesCompletionProvider.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@Override
protected void addCompletions(final CompletionParameters completionParameters,
        final ProcessingContext processingContext, final CompletionResultSet resultSet) {

    final PsiElement element = completionParameters.getPosition();
    if (!LeafPsiElement.class.isInstance(element)) {
        return; // ignore comment
    }

    final Project project = element.getProject();
    final Module module = findModule(element);
    final SuggestionService service = ServiceManager.getService(project, SuggestionService.class);
    if ((module == null || !service.isSupported(completionParameters))) { // limit suggestion to Messages
        return;
    }

    if (PropertyValueImpl.class.isInstance(element)) {
        ofNullable(PropertyValueImpl.class.cast(element).getPrevSibling())
                .map(PsiElement::getPrevSibling)
                .map(PsiElement::getText)
                .ifPresent(text -> resultSet.addAllElements(service.computeValueSuggestions(text)));
    } else if (PropertyKeyImpl.class.isInstance(element)) {
        final List<String> containerElements = PropertiesFileImpl.class
                .cast(element.getContainingFile())
                .getProperties()
                .stream()
                .filter(p -> !Objects.equals(p.getKey(), element.getText()))
                .map(IProperty::getKey)
                .collect(toList());
        resultSet
                .addAllElements(service
                        .computeKeySuggestions(project, module, getPropertiesPackage(module, completionParameters),
                                containerElements, truncateIdeaDummyIdentifier(element)));
    }
}
 
Example #2
Source File: NutzInjectConfUtil.java    From NutzCodeInsight with Apache License 2.0 5 votes vote down vote up
public static List<PsiElement> findPropertiesPsiElement(Project project, Collection<VirtualFile> virtualFiles, String key) {
    List<PsiElement> result = new ArrayList<>();
    for (VirtualFile virtualFile : virtualFiles) {
        PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
        List<IProperty> iProperties = ((PropertiesFileImpl) psiFile).getProperties();
        iProperties.forEach(iProperty -> {
            if (iProperty.getKey().equals(key)) {
                result.add(iProperty.getPsiElement());
            }
        });
    }
    return result;
}
 
Example #3
Source File: CamelSmartCompletionEndpointOptions.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * We need special logic to determine when it should insert "=" at the end of the options
 */
@NotNull
private static LookupElementBuilder addInsertHandler(final Editor editor, final LookupElementBuilder builder,
                                                     final String suffix) {
    return builder.withInsertHandler((context, item) -> {
        // enforce using replace select char as we want to replace any existing option
        if (context.getCompletionChar() == Lookup.NORMAL_SELECT_CHAR) {
            int endSelectOffBy = 0;
            if (context.getFile() instanceof PropertiesFileImpl) {
                //if it's a property file the PsiElement does not start and end with an quot
                endSelectOffBy = 1;
            }
            final char text = context
                    .getDocument()
                    .getCharsSequence()
                    .charAt(context.getSelectionEndOffset() - endSelectOffBy);
            if (text != '=') {
                EditorModificationUtil.insertStringAtCaret(editor, "=");
            }
        } else if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
            // we still want to keep the suffix because they are other options
            String value = suffix;
            final int pos = value.indexOf("&");
            if (pos > -1) {
                // strip out first part of suffix until next option
                value = value.substring(pos);
            }
            EditorModificationUtil.insertStringAtCaret(editor, "=" + value);
            // and move cursor back again
            final int offset = -1 * value.length();
            EditorModificationUtil.moveCaretRelatively(editor, offset);
        }

    });
}