Search in sources :

Example 16 with Trinity

use of com.intellij.openapi.util.Trinity in project intellij-community by JetBrains.

the class StubTreeBuilder method getStubbedRoots.

/** Order is deterministic. First element matches {@link FileViewProvider#getStubBindingRoot()} */
@NotNull
public static List<Pair<IStubFileElementType, PsiFile>> getStubbedRoots(@NotNull FileViewProvider viewProvider) {
    final List<Trinity<Language, IStubFileElementType, PsiFile>> roots = new SmartList<>();
    final PsiFile stubBindingRoot = viewProvider.getStubBindingRoot();
    for (Language language : viewProvider.getLanguages()) {
        final PsiFile file = viewProvider.getPsi(language);
        if (file instanceof PsiFileImpl) {
            final IElementType type = ((PsiFileImpl) file).getElementTypeForStubBuilder();
            if (type != null) {
                roots.add(Trinity.create(language, (IStubFileElementType) type, file));
            }
        }
    }
    ContainerUtil.sort(roots, (o1, o2) -> {
        if (o1.third == stubBindingRoot)
            return o2.third == stubBindingRoot ? 0 : -1;
        else if (o2.third == stubBindingRoot)
            return 1;
        else
            return StringUtil.compare(o1.first.getID(), o2.first.getID(), false);
    });
    return ContainerUtil.map(roots, trinity -> Pair.create(trinity.second, trinity.third));
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IStubFileElementType(com.intellij.psi.tree.IStubFileElementType) Trinity(com.intellij.openapi.util.Trinity) Language(com.intellij.lang.Language) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFile(com.intellij.psi.PsiFile) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with Trinity

use of com.intellij.openapi.util.Trinity in project intellij-community by JetBrains.

the class XmlLanguageInjector method getInjectedLanguage.

void getInjectedLanguage(final PsiElement place, final Ref<Boolean> unparsableRef, final PairProcessor<Language, List<Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange>>> processor) {
    if (place instanceof XmlTag) {
        final XmlTag xmlTag = (XmlTag) place;
        List<BaseInjection> injections = myConfiguration.getInjections(XmlLanguageInjectionSupport.XML_SUPPORT_ID);
        //noinspection ForLoopReplaceableByForEach
        for (int i = 0, injectionsSize = injections.size(); i < injectionsSize; i++) {
            final BaseInjection injection = injections.get(i);
            if (injection.acceptsPsiElement(xmlTag)) {
                final Language language = InjectedLanguage.findLanguageById(injection.getInjectedLanguageId());
                if (language == null)
                    continue;
                final boolean separateFiles = !injection.isSingleFile() && StringUtil.isNotEmpty(injection.getValuePattern());
                final List<Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange>> result = ContainerUtil.newArrayList();
                xmlTag.acceptChildren(new PsiElementVisitor() {

                    @Override
                    public void visitElement(final PsiElement element) {
                        if (element instanceof XmlText) {
                            if (!(element instanceof PsiLanguageInjectionHost) || element.getTextLength() == 0)
                                return;
                            final List<TextRange> list = injection.getInjectedArea(element);
                            final InjectedLanguage l = InjectedLanguage.create(injection.getInjectedLanguageId(), injection.getPrefix(), injection.getSuffix(), false);
                            for (TextRange textRange : list) {
                                result.add(Trinity.create((PsiLanguageInjectionHost) element, l, textRange));
                            }
                        } else if (element instanceof XmlTag) {
                            if (!separateFiles)
                                unparsableRef.set(Boolean.TRUE);
                            if (injection instanceof AbstractTagInjection && ((AbstractTagInjection) injection).isApplyToSubTags()) {
                                element.acceptChildren(this);
                            }
                        }
                    }
                });
                if (!result.isEmpty()) {
                    if (separateFiles) {
                        for (Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange> trinity : result) {
                            processor.process(language, Collections.singletonList(trinity));
                        }
                    } else {
                        processor.process(language, result);
                    }
                }
                if (injection.isTerminal()) {
                    break;
                }
            }
        }
    } else if (place instanceof XmlAttributeValue && place.getParent() instanceof XmlAttribute) {
        final XmlAttribute attribute = (XmlAttribute) place.getParent();
        final XmlAttributeValue value = (XmlAttributeValue) place;
        //if (value == null) return;
        // Check that we don't inject anything into embedded (e.g. JavaScript) content:
        // XmlToken: "
        // JSEmbeddedContent
        // XmlToken "
        // Actually IDEA shouldn't ask for injected languages at all in this case.
        final PsiElement[] children = value.getChildren();
        if (children.length < 3 || !(children[1] instanceof XmlToken) || ((XmlToken) children[1]).getTokenType() != XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN) {
            return;
        }
        List<BaseInjection> injections = myConfiguration.getInjections(XmlLanguageInjectionSupport.XML_SUPPORT_ID);
        //noinspection ForLoopReplaceableByForEach
        for (int i = 0, size = injections.size(); i < size; i++) {
            BaseInjection injection = injections.get(i);
            if (injection.acceptsPsiElement(attribute)) {
                final Language language = InjectedLanguage.findLanguageById(injection.getInjectedLanguageId());
                if (language == null)
                    continue;
                final boolean separateFiles = !injection.isSingleFile() && StringUtil.isNotEmpty(injection.getValuePattern());
                final List<TextRange> ranges = injection.getInjectedArea(value);
                if (ranges.isEmpty())
                    continue;
                final List<Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange>> result = new ArrayList<>();
                final InjectedLanguage l = InjectedLanguage.create(injection.getInjectedLanguageId(), injection.getPrefix(), injection.getSuffix(), false);
                for (TextRange textRange : ranges) {
                    result.add(Trinity.create((PsiLanguageInjectionHost) value, l, textRange));
                }
                if (separateFiles) {
                    for (Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange> trinity : result) {
                        processor.process(language, Collections.singletonList(trinity));
                    }
                } else {
                    processor.process(language, result);
                }
                if (injection.isTerminal()) {
                    break;
                }
            }
        }
    }
}
Also used : Trinity(com.intellij.openapi.util.Trinity) TextRange(com.intellij.openapi.util.TextRange) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) Language(com.intellij.lang.Language) InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) InjectedLanguage(org.intellij.plugins.intelliLang.inject.InjectedLanguage) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) AbstractTagInjection(org.intellij.plugins.intelliLang.inject.config.AbstractTagInjection) PsiElement(com.intellij.psi.PsiElement)

Example 18 with Trinity

use of com.intellij.openapi.util.Trinity in project intellij-community by JetBrains.

the class TemporaryPlacesInjector method getLanguagesToInject.

public void getLanguagesToInject(@NotNull final MultiHostRegistrar registrar, @NotNull final PsiElement context) {
    if (!(context instanceof PsiLanguageInjectionHost) || !((PsiLanguageInjectionHost) context).isValidHost())
        return;
    PsiLanguageInjectionHost host = (PsiLanguageInjectionHost) context;
    PsiFile containingFile = context.getContainingFile();
    InjectedLanguage injectedLanguage = myRegistry.getLanguageFor(host, containingFile);
    Language language = injectedLanguage != null ? injectedLanguage.getLanguage() : null;
    if (language == null)
        return;
    final ElementManipulator<PsiLanguageInjectionHost> manipulator = ElementManipulators.getManipulator(host);
    if (manipulator == null)
        return;
    List<Trinity<PsiLanguageInjectionHost, InjectedLanguage, TextRange>> trinities = Collections.singletonList(Trinity.create(host, injectedLanguage, manipulator.getRangeInElement(host)));
    InjectorUtils.registerInjection(language, trinities, containingFile, registrar);
    InjectorUtils.registerSupport(myRegistry.getLanguageInjectionSupport(), false, registrar);
}
Also used : Language(com.intellij.lang.Language) Trinity(com.intellij.openapi.util.Trinity)

Example 19 with Trinity

use of com.intellij.openapi.util.Trinity in project intellij-community by JetBrains.

the class SvnVcs method getTimestampForPropertiesChange.

private static Trinity<Long, Long, Long> getTimestampForPropertiesChange(final File ioFile, final boolean isDir) {
    final File dir = isDir ? ioFile : ioFile.getParentFile();
    final String relPath = SVNAdminUtil.getPropPath(ioFile.getName(), isDir ? SVNNodeKind.DIR : SVNNodeKind.FILE, false);
    final String relPathBase = SVNAdminUtil.getPropBasePath(ioFile.getName(), isDir ? SVNNodeKind.DIR : SVNNodeKind.FILE, false);
    final String relPathRevert = SVNAdminUtil.getPropRevertPath(ioFile.getName(), isDir ? SVNNodeKind.DIR : SVNNodeKind.FILE, false);
    return new Trinity<>(new File(dir, relPath).lastModified(), new File(dir, relPathBase).lastModified(), new File(dir, relPathRevert).lastModified());
}
Also used : Trinity(com.intellij.openapi.util.Trinity) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File)

Example 20 with Trinity

use of com.intellij.openapi.util.Trinity in project intellij-community by JetBrains.

the class SvnVcs method getPropertyWithCaching.

@Nullable
public PropertyValue getPropertyWithCaching(final VirtualFile file, final String propName) throws VcsException {
    Map<String, Pair<PropertyValue, Trinity<Long, Long, Long>>> cachedMap = myPropertyCache.get(keyForVf(file));
    final Pair<PropertyValue, Trinity<Long, Long, Long>> cachedValue = cachedMap == null ? null : cachedMap.get(propName);
    final File ioFile = virtualToIoFile(file);
    final Trinity<Long, Long, Long> tsTrinity = getTimestampForPropertiesChange(ioFile, file.isDirectory());
    if (cachedValue != null) {
        // zero means that a file was not found
        if (trinitiesEqual(cachedValue.getSecond(), tsTrinity)) {
            return cachedValue.getFirst();
        }
    }
    PropertyClient client = getFactory(ioFile).createPropertyClient();
    final PropertyValue value = client.getProperty(SvnTarget.fromFile(ioFile, SVNRevision.WORKING), propName, false, SVNRevision.WORKING);
    if (cachedMap == null) {
        cachedMap = new HashMap<>();
        myPropertyCache.put(keyForVf(file), cachedMap);
    }
    cachedMap.put(propName, Pair.create(value, tsTrinity));
    return value;
}
Also used : PropertyClient(org.jetbrains.idea.svn.properties.PropertyClient) Trinity(com.intellij.openapi.util.Trinity) PropertyValue(org.jetbrains.idea.svn.properties.PropertyValue) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Trinity (com.intellij.openapi.util.Trinity)24 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 Pair (com.intellij.openapi.util.Pair)6 ArrayList (java.util.ArrayList)6 NotNull (org.jetbrains.annotations.NotNull)6 Nullable (org.jetbrains.annotations.Nullable)5 Project (com.intellij.openapi.project.Project)4 File (java.io.File)4 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)3 Language (com.intellij.lang.Language)3 FileType (com.intellij.openapi.fileTypes.FileType)3 TextRange (com.intellij.openapi.util.TextRange)3 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)2 ProcessHandler (com.intellij.execution.process.ProcessHandler)2 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)2 Module (com.intellij.openapi.module.Module)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2