@Override public String fromString(@Nullable @NonNls String s, final ConvertContext context) { if (s == null) return null; if (myAllowEmpty && s.trim().length() == 0) return s; if (s.trim().length() == 1) return s; if (isUnicodeCharacterSequence(s)) { try { Integer.parseInt(s.substring(UNICODE_PREFIX.length()), 16); return s; } catch (NumberFormatException e) {} } return null; }
@Override protected boolean doIsValid(MavenId id, MavenProjectIndicesManager manager, ConvertContext context) { if (StringUtil.isEmpty(id.getGroupId()) || StringUtil.isEmpty(id.getArtifactId())) return false; if (manager.hasArtifactId(id.getGroupId(), id.getArtifactId())) { return true; } // Check if artifact was found on importing. MavenProject mavenProject = findMavenProject(context); if (mavenProject != null) { for (MavenArtifact artifact : mavenProject.findDependencies(id.getGroupId(), id.getArtifactId())) { if (artifact.isResolved()) { return true; } } } return false; }
public TargetResolver.Result fromString(@Nullable @NonNls String s, ConvertContext context) { final AntDomProject project = context.getInvocationElement().getParentOfType(AntDomProject.class, false); if (project == null) { return null; } final AntDomTarget contextTarget = context.getInvocationElement().getParentOfType(AntDomTarget.class, false); if (contextTarget == null) { return null; } final List<String> refs; if (s == null) { refs = Collections.emptyList(); } else { refs = new ArrayList<String>(); final StringTokenizer tokenizer = new StringTokenizer(s, ",", false); while (tokenizer.hasMoreTokens()) { final String ref = tokenizer.nextToken(); refs.add(ref.trim()); } } final TargetResolver.Result result = TargetResolver.resolve(project.getContextAntProject(), contextTarget, refs); result.setRefsString(s); return result; }
@Nullable public TargetResolver.Result fromString(@Nullable @NonNls String s, ConvertContext context) { final AntDomElement element = AntSupport.getInvocationAntDomElement(context); if (element != null && s != null) { final AntDomProject project = element.getAntProject(); AntDomProject projectToSearchFrom; final AntDomAnt antDomAnt = element.getParentOfType(AntDomAnt.class, false); if (antDomAnt != null) { final PsiFileSystemItem antFile = antDomAnt.getAntFilePath().getValue(); projectToSearchFrom = antFile instanceof PsiFile? AntSupport.getAntDomProjectForceAntFile((PsiFile)antFile) : null; } else { projectToSearchFrom = project.getContextAntProject(); } if (projectToSearchFrom == null) { return null; } final TargetResolver.Result result = TargetResolver.resolve(projectToSearchFrom, null, s); result.setRefsString(s); return result; } return null; }
@Nullable @Override public ActionOrGroup fromString(@Nullable @NonNls final String value, ConvertContext context) { if (StringUtil.isEmptyOrSpaces(value)) return null; final ActionOrGroup[] result = {null}; PairProcessor<String, ActionOrGroup> findProcessor = new PairProcessor<String, ActionOrGroup>() { @Override public boolean process(String s, ActionOrGroup actionOrGroup) { if (isRelevant(actionOrGroup) && Comparing.strEqual(value, s)) { result[0] = actionOrGroup; return false; } return true; } }; processActionOrGroup(context, findProcessor); return result[0]; }
private static List<LanguageDefinition> collectLibraryLanguages(final ConvertContext context, final Collection<PsiClass> allLanguages) { return ContainerUtil.mapNotNull(Language.getRegisteredLanguages(), new NullableFunction<Language, LanguageDefinition>() { @Override public LanguageDefinition fun(Language language) { if (language.getID().isEmpty() || language instanceof DependentLanguage) { return null; } final PsiClass psiClass = DomJavaUtil.findClass(language.getClass().getName(), context.getInvocationElement()); if (psiClass == null) { return null; } if (!allLanguages.contains(psiClass)) { return null; } final LanguageFileType type = language.getAssociatedFileType(); final Icon icon = type != null ? type.getIcon() : null; return new LanguageDefinition(language.getID(), psiClass, icon, language.getDisplayName()); } }); }
@NotNull @Override public Collection<? extends String> getVariants(ConvertContext context) { final XmlElement element = context.getXmlElement(); if (element == null) { return Collections.emptyList(); } final String value = ResourceReferenceConverter.getValue(element); if (value == null) { return Collections.emptyList(); } final String intPrefix = getIntegerPrefix(value); if (intPrefix.isEmpty()) { return Collections.emptyList(); } final List<String> result = new ArrayList<String>(ourUnits.size()); for (String unit : ourUnits.keySet()) { result.add(intPrefix + unit); } return result; }
@Override protected boolean doIsValid(MavenId id, MavenProjectIndicesManager manager, ConvertContext context) { if (StringUtil.isEmpty(id.getGroupId())) return false; if (manager.hasGroupId(id.getGroupId())) return true; // Check if artifact was found on importing. MavenProject mavenProject = findMavenProject(context); if (mavenProject != null) { for (MavenArtifact artifact : mavenProject.findDependencies(id.getGroupId(), id.getArtifactId())) { if (artifact.isResolved()) { return true; } } } return false; }
@NotNull @Override public PsiReference[] createReferences(GenericDomValue value, final PsiElement element, final ConvertContext context) { return new PsiReference[]{new WebReference(element) { @NotNull @Override public Object[] getVariants() { Module module = context.getModule(); if (module != null) { String name = MavenRepositoriesProvider.getInstance().getRepositoryUrl(getRepositoryId(context)); if (!StringUtil.isEmptyOrSpaces(name)) return new Object[]{name}; } return super.getVariants(); } }}; }
@Nullable @Override public PsiField fromString(@Nullable final String text, final ConvertContext context) { DomElement host = context.getInvocationElement(); if (host instanceof GenericAttributeValue<?>) { DomElement domProperty = host.getParent(); return domProperty instanceof Property ? BeansUtils.resolveToPsiField((Property) domProperty, text) : null; } return null; }
@Override protected ItemType searchForName( @NotNull final String name, @NotNull final ConvertContext context, @NotNull final TSMetaModel meta ) { return Optional.ofNullable(meta.findMetaClassByName(name)) .map(TSMetaClass::retrieveAllDomsStream) .orElse(Stream.empty()) .findFirst() .orElse(null); }
@Override protected Collection<? extends ItemType> searchAll( @NotNull final ConvertContext context, @NotNull final TSMetaModel meta ) { return meta.getMetaClassesStream() .map(TSMetaClass::retrieveAllDomsStream) .map(Stream::findFirst) .filter(Optional::isPresent) .map(Optional::get) .collect(Collectors.toList()); }
@Nullable @Override public final DOM fromString( @Nullable @NonNls final String s, final ConvertContext context ) { if (StringUtil.isEmpty(s)) { return null; } return searchForName(s, context, TSMetaModelAccess.getInstance(context.getProject()). getTypeSystemMeta(context.getFile())); }
@Override protected AtomicType searchForName( @NotNull final String name, @NotNull final ConvertContext context, @NotNull final TSMetaModel meta ) { return Optional.ofNullable(meta.findMetaAtomicByName(name)) .map(TSMetaAtomic::retrieveDom) .orElse(null); }
@Override protected Collection<? extends AtomicType> searchAll( @NotNull final ConvertContext context, @NotNull final TSMetaModel meta ) { return meta.getMetaAtomicStream() .map(TSMetaAtomic::retrieveDom) .filter(Objects::nonNull) .collect(Collectors.toList()); }
@Override protected CollectionType searchForName( @NotNull final String name, @NotNull final ConvertContext context, @NotNull final TSMetaModel meta ) { return Optional.ofNullable(meta.findMetaCollectionByName(name)) .map(TSMetaCollection::retrieveDom) .orElse(null); }
@Override protected Collection<? extends CollectionType> searchAll( @NotNull final ConvertContext context, @NotNull final TSMetaModel meta ) { return meta.getMetaCollectionsStream() .map(TSMetaCollection::retrieveDom) .filter(Objects::nonNull) .collect(Collectors.toList()); }
@NotNull @Override public Collection<? extends DOM> getVariants(final ConvertContext context) { final List<DOM> result = new LinkedList<>(); for (TypeSystemConverterBase<? extends DOM> next : myDelegates) { result.addAll(next.getVariants(context)); } return result; }
@Nullable @Override public DOM fromString( @Nullable @NonNls final String s, final ConvertContext context ) { for (TypeSystemConverterBase<? extends DOM> next : myDelegates) { final DOM nextResult = next.fromString(s, context); if (nextResult != null) { return nextResult; } } return null; }
@Nullable @Override public String toString(@Nullable final DOM t, final ConvertContext context) { for (TypeSystemConverterBase<? extends DOM> next : myDelegates) { final String nextToString = next.tryToString(t, context); if (nextToString != null) { return nextToString; } } return null; }
@Override protected EnumType searchForName( @NotNull final String name, @NotNull final ConvertContext context, @NotNull final TSMetaModel meta ) { return Optional.ofNullable(meta.findMetaEnumByName(name)) .map(TSMetaEnum::retrieveDom) .orElse(null); }
@Override protected Collection<? extends EnumType> searchAll( @NotNull final ConvertContext context, @NotNull final TSMetaModel meta ) { return meta.getMetaEnumsStream() .map(TSMetaEnum::retrieveDom) .filter(Objects::nonNull) .collect(Collectors.toList()); }
public void bindReference(final GenericDomValue<PsiMethod> genericValue, final ConvertContext context, final PsiElement element) { assert element instanceof PsiMethod : "PsiMethod expected"; final PsiMethod psiMethod = (PsiMethod)element; final ParentType parent = getParent(context); final AbstractMethodParams methodParams = getMethodParams(parent); genericValue.setStringValue(psiMethod.getName()); if (methodParams != null) { methodParams.undefine(); for (PsiParameter parameter : psiMethod.getParameterList().getParameters()) { methodParams.addMethodParam().setValue(parameter.getType()); } } }
@Override public String fromString(@Nullable @NonNls final String stringValue, final ConvertContext context) { if (stringValue != null && ((myAllowEmpty && stringValue.trim().length() == 0) || Arrays.binarySearch(getAllValues(), stringValue) >= 0)) { return stringValue; } return null; }
public String toString(@Nullable Boolean aBoolean, ConvertContext context) { final GenericAttributeValue attribValue = context.getInvocationElement().getParentOfType(GenericAttributeValue.class, false); if (attribValue == null) { return null; } return attribValue.getRawText(); }
@Override public String fromString(@Nullable @NonNls final String s, final ConvertContext context) { if (s == null) return null; if (myAllowEmpty && s.trim().length() == 0) return s; return parseNumber(s, myNumberClass) == null ? null : s; }
@Override public String getErrorMessage(@Nullable final String s, final ConvertContext context) { if (s == null) return super.getErrorMessage(s, context); return s.trim().length() == 0 ? DomBundle.message("value.converter.format.exception.empty.string", myNumberClass.getName()) : DomBundle.message("value.converter.format.exception", s, myNumberClass.getName()); }
public IdeaPlugin fromString(@Nullable @NonNls final String s, final ConvertContext context) { for (IdeaPlugin ideaPlugin : getAllPluginsWithoutSelf(context)) { final String otherId = ideaPlugin.getPluginId(); if (otherId == null) continue; if (otherId.equals(s)) return ideaPlugin; for (PluginModule module : ideaPlugin.getModules()) { final String moduleName = module.getValue().getValue(); if (moduleName != null && moduleName.equals(s)) return ideaPlugin; } } return null; }
@NotNull @Override public PsiReference[] createReferences(GenericDomValue<ResourceValue> value, PsiElement element, ConvertContext context) { if (element instanceof XmlAttributeValue) { // parent="" is allowed; it's used to deliberately allow style A.B.C to not be a child of A.B XmlAttributeValue attributeValue = (XmlAttributeValue)element; if (attributeValue.isValid() && attributeValue.getValue().isEmpty()) { return PsiReference.EMPTY_ARRAY; } } final PsiReference[] refsFromSuper = super.createReferences(value, element, context); final ResourceValue resValue = value.getValue(); if (resValue == null || resValue.getPackage() != null) { return refsFromSuper; } final AndroidFacet facet = AndroidFacet.getInstance(context); if (facet != null) { final PsiReference[] refs = getReferencesInStyleName(value, facet); if (refs.length > 0) { return ArrayUtil.mergeArrays(refsFromSuper, refs); } } return refsFromSuper; }
private static List<LanguageDefinition> collectLanguageDefinitions(final ConvertContext context) { final PsiClass languageClass = DomJavaUtil.findClass(Language.class.getName(), context.getInvocationElement()); if (languageClass == null) { return Collections.emptyList(); } final Project project = context.getProject(); final GlobalSearchScope projectProductionScope = GlobalSearchScopesCore.projectProductionScope(project); final Collection<PsiClass> allLanguages = CachedValuesManager.getCachedValue(languageClass, new CachedValueProvider<Collection<PsiClass>>() { @Nullable @Override public Result<Collection<PsiClass>> compute() { GlobalSearchScope allScope = projectProductionScope.union(ProjectScope.getLibrariesScope(project)); return Result.create(ClassInheritorsSearch.search(languageClass, allScope, true).findAll(), PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT); } }); ; final List<LanguageDefinition> libraryDefinitions = collectLibraryLanguages(context, allLanguages); final Collection<PsiClass> projectLanguages = ContainerUtil.filter(allLanguages, new Condition<PsiClass>() { @Override public boolean value(PsiClass aClass) { return PsiSearchScopeUtil.isInScope(projectProductionScope, aClass); } }); final List<LanguageDefinition> projectDefinitions = collectProjectLanguages(projectLanguages, libraryDefinitions); final List<LanguageDefinition> all = new ArrayList<LanguageDefinition>(libraryDefinitions); all.addAll(projectDefinitions); return all; }
@Nullable @Override public PsiField fromString(@Nullable @NonNls String s, ConvertContext context) { if (s == null) return null; PsiClass value = getEPBeanClass(context); if (value == null) return null; PsiField field = value.findFieldByName(s, true); if (field != null) { return field; } return findFieldByAttributeValue(value, s); }
@NotNull @Override public Collection<? extends String> getVariants(ConvertContext context) { final List<String> result = new ArrayList<String>(SdkVersionInfo.HIGHEST_KNOWN_API); for (int i = 1; i <= SdkVersionInfo.HIGHEST_KNOWN_API; i++) { if (i > SdkVersionInfo.HIGHEST_KNOWN_STABLE_API) { result.add(SdkVersionInfo.getBuildCode(i)); } else { result.add(Integer.toString(i)); } } return result; }
@Override protected JavaClassReferenceProvider createClassReferenceProvider(GenericDomValue<PsiClass> genericDomValue, ConvertContext context, ExtendClass extendClass) { final JavaClassReferenceProvider provider = super.createClassReferenceProvider(genericDomValue, context, extendClass); provider.setOption(JavaClassReferenceProvider.JVM_FORMAT, Boolean.TRUE); provider.setOption(JavaClassReferenceProvider.ALLOW_DOLLAR_NAMES, Boolean.TRUE); return provider; }
@Override @NotNull public Collection<String> getVariants(ConvertContext context) { List<String> variants = new ArrayList<String>(); for (ResolvingConverter<String> converter : converters) { variants.addAll(converter.getVariants(context)); } return variants; }
@Nullable private static PsiClass getEPBeanClass(ConvertContext context) { ExtensionPoint ep = context.getInvocationElement().getParentOfType(ExtensionPoint.class, true); if (ep == null) return null; PsiClass value = ep.getBeanClass().getValue(); if (value == null) return null; return value; }
@Nullable @Override public String toString(@Nullable Object object, ConvertContext context) { if (object == null) { return null; } return object.toString(); }
@NotNull @Override public Collection<Object> getVariants(ConvertContext context) { if (isClassContext(context)) { return Collections.emptyList(); } ArrayList<Object> results = new ArrayList<Object>(); results.addAll(myResourceReferenceConverter.getVariants(context)); return results; }
@Nullable @Override public Object fromString(@Nullable @NonNls String s, ConvertContext context) { if (isClassContext(context)) { return myPsiClassConverter.fromString(s, context); } return myResourceReferenceConverter.fromString(s, context); }
public String resolveString(@Nullable String unresolvedText, @NotNull ConvertContext context) { // todo: speed optimization: disable string resolution in places where it is not applicable if (unresolvedText == null) { return null; } final DomElement element = context.getInvocationElement(); return AntStringResolver.computeString(element, unresolvedText); }
@NotNull @Override public Collection<? extends String> getVariants(ConvertContext context) { List<String> result = new ArrayList<String>(); ResourceManager manager = SystemResourceManager.getInstance(context); if (manager != null) { AttributeDefinitions attrDefs = manager.getAttributeDefinitions(); if (attrDefs != null) { for (String name : attrDefs.getAttributeNames()) { result.add("android:" + name); } } } return result; }