Search in sources :

Example 11 with Language

use of com.intellij.lang.Language in project intellij-community by JetBrains.

the class SpellCheckerDictionaryGenerator method processLeafsNames.

protected void processLeafsNames(@NotNull final PsiElement leafElement, @NotNull final HashSet<String> seenNames) {
    final Language language = leafElement.getLanguage();
    SpellCheckingInspection.tokenize(leafElement, language, new TokenConsumer() {

        @Override
        public void consumeToken(PsiElement element, final String text, boolean useRename, int offset, TextRange rangeToCheck, Splitter splitter) {
            splitter.split(text, rangeToCheck, textRange -> {
                final String word = textRange.substring(text);
                addSeenWord(seenNames, word, language);
            });
        }
    });
}
Also used : TokenConsumer(com.intellij.spellchecker.tokenizer.TokenConsumer) Language(com.intellij.lang.Language) java.util(java.util) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NamesValidator(com.intellij.lang.refactoring.NamesValidator) SpellCheckingInspection(com.intellij.spellchecker.inspections.SpellCheckingInspection) PsiManager(com.intellij.psi.PsiManager) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) FileUtil(com.intellij.openapi.util.io.FileUtil) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) ProgressManager(com.intellij.openapi.progress.ProgressManager) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) TokenConsumer(com.intellij.spellchecker.tokenizer.TokenConsumer) FileWriter(java.io.FileWriter) IOException(java.io.IOException) TextRange(com.intellij.openapi.util.TextRange) LanguageNamesValidation(com.intellij.lang.LanguageNamesValidation) File(java.io.File) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) Splitter(com.intellij.spellchecker.inspections.Splitter) NotNull(org.jetbrains.annotations.NotNull) SpellCheckerManager(com.intellij.spellchecker.SpellCheckerManager) Consumer(com.intellij.util.Consumer) Splitter(com.intellij.spellchecker.inspections.Splitter) Language(com.intellij.lang.Language) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Example 12 with Language

use of com.intellij.lang.Language in project intellij-community by JetBrains.

the class XPathEvalAction method execute.

private void execute(Editor editor) {
    final Project project = editor.getProject();
    final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (psiFile == null) {
        return;
    }
    InputExpressionDialog.Context input;
    XmlElement contextNode = null;
    final Config cfg = XPathAppComponent.getInstance().getConfig();
    do {
        RangeHighlighter contextHighlighter = null;
        if (cfg.isUseContextAtCursor()) {
            // find out current context node
            contextNode = MyPsiUtil.findContextNode(psiFile, editor);
            if (contextNode != null) {
                contextHighlighter = HighlighterUtil.highlightNode(editor, contextNode, cfg.getContextAttributes(), cfg);
            }
        }
        if (contextNode == null) {
            // in XPath data model, / is the document itself, including comments, PIs and the root element
            contextNode = ((XmlFile) psiFile).getDocument();
            if (contextNode == null) {
                FileViewProvider fileViewProvider = psiFile.getViewProvider();
                if (fileViewProvider instanceof TemplateLanguageFileViewProvider) {
                    Language dataLanguage = ((TemplateLanguageFileViewProvider) fileViewProvider).getTemplateDataLanguage();
                    PsiFile templateDataFile = fileViewProvider.getPsi(dataLanguage);
                    if (templateDataFile instanceof XmlFile)
                        contextNode = ((XmlFile) templateDataFile).getDocument();
                }
            }
        }
        input = inputXPathExpression(project, contextNode);
        if (contextHighlighter != null) {
            contextHighlighter.dispose();
        }
        if (input == null) {
            return;
        }
        HighlighterUtil.clearHighlighters(editor);
    } while (contextNode != null && evaluateExpression(input, contextNode, editor, cfg));
}
Also used : Project(com.intellij.openapi.project.Project) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) XmlFile(com.intellij.psi.xml.XmlFile) XmlElement(com.intellij.psi.xml.XmlElement) PsiFile(com.intellij.psi.PsiFile) TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) InputExpressionDialog(org.intellij.plugins.xpathView.ui.InputExpressionDialog)

Example 13 with Language

use of com.intellij.lang.Language in project intellij-community by JetBrains.

the class PythonRegexpInjector method injectRegexpLanguage.

private static void injectRegexpLanguage(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context, boolean verbose) {
    final Language language = verbose ? PythonVerboseRegexpLanguage.INSTANCE : PythonRegexpLanguage.INSTANCE;
    registrar.startInjecting(language);
    final PyInjectionUtil.InjectionResult result = PyInjectionUtil.registerStringLiteralInjection(context, registrar);
    if (result.isInjected()) {
        registrar.doneInjecting();
        if (!result.isStrict()) {
            final PsiFile file = getInjectedFile(registrar);
            if (file != null) {
                file.putUserData(InjectedLanguageUtil.FRANKENSTEIN_INJECTION, Boolean.TRUE);
            }
        }
    }
}
Also used : PyInjectionUtil(com.jetbrains.python.codeInsight.PyInjectionUtil) Language(com.intellij.lang.Language) PsiFile(com.intellij.psi.PsiFile)

Example 14 with Language

use of com.intellij.lang.Language in project intellij-community by JetBrains.

the class XmlUnwrapDescriptor method collectUnwrappers.

@Override
public List<Pair<PsiElement, Unwrapper>> collectUnwrappers(Project project, Editor editor, PsiFile file) {
    int offset = editor.getCaretModel().getOffset();
    PsiElement e1 = file.findElementAt(offset);
    if (e1 != null) {
        Language language = e1.getParent().getLanguage();
        if (language != file.getLanguage()) {
            UnwrapDescriptor unwrapDescriptor = LanguageUnwrappers.INSTANCE.forLanguage(language);
            if (unwrapDescriptor != null && !(unwrapDescriptor instanceof XmlUnwrapDescriptor)) {
                return unwrapDescriptor.collectUnwrappers(project, editor, file);
            }
        }
    }
    List<Pair<PsiElement, Unwrapper>> result = new ArrayList<>();
    FileViewProvider viewProvider = file.getViewProvider();
    for (Language language : viewProvider.getLanguages()) {
        UnwrapDescriptor unwrapDescriptor = LanguageUnwrappers.INSTANCE.forLanguage(language);
        if (unwrapDescriptor instanceof XmlUnwrapDescriptor) {
            PsiElement e = viewProvider.findElementAt(offset, language);
            PsiElement tag = PsiTreeUtil.getParentOfType(e, XmlTag.class);
            while (tag != null) {
                if (XmlChildRole.START_TAG_NAME_FINDER.findChild(tag.getNode()) != null) {
                    // Exclude implicit tags suck as 'jsp:root'
                    result.add(new Pair<>(tag, new XmlEnclosingTagUnwrapper()));
                }
                tag = PsiTreeUtil.getParentOfType(tag, XmlTag.class);
            }
        }
    }
    Collections.sort(result, (o1, o2) -> o2.first.getTextOffset() - o1.first.getTextOffset());
    return result;
}
Also used : Language(com.intellij.lang.Language) FileViewProvider(com.intellij.psi.FileViewProvider) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) UnwrapDescriptor(com.intellij.codeInsight.unwrap.UnwrapDescriptor) Pair(com.intellij.openapi.util.Pair) XmlTag(com.intellij.psi.xml.XmlTag)

Example 15 with Language

use of com.intellij.lang.Language in project intellij-community by JetBrains.

the class BreadcrumbsXmlWrapper method findFirstBreadcrumbedElement.

@Nullable
private static PsiElement findFirstBreadcrumbedElement(final int offset, final VirtualFile file, final Project project, final BreadcrumbsInfoProvider defaultInfoProvider) {
    if (file == null || !file.isValid())
        return null;
    PriorityQueue<PsiElement> leafs = new PriorityQueue<>(3, (o1, o2) -> {
        TextRange range1 = o1.getTextRange();
        if (range1 == null) {
            LOG.error(o1 + " returned null range");
            return 1;
        }
        TextRange range2 = o2.getTextRange();
        if (range2 == null) {
            LOG.error(o2 + " returned null range");
            return -1;
        }
        return range2.getStartOffset() - range1.getStartOffset();
    });
    FileViewProvider viewProvider = findViewProvider(file, project);
    if (viewProvider == null)
        return null;
    for (final Language language : viewProvider.getLanguages()) {
        ContainerUtil.addIfNotNull(leafs, viewProvider.findElementAt(offset, language));
    }
    while (!leafs.isEmpty()) {
        final PsiElement element = leafs.remove();
        if (!element.isValid())
            continue;
        BreadcrumbsInfoProvider provider = findProviderForElement(element, defaultInfoProvider);
        if (provider != null && provider.acceptElement(element)) {
            return element;
        }
        if (!(element instanceof PsiFile)) {
            ContainerUtil.addIfNotNull(leafs, getParent(element, provider));
        }
    }
    return null;
}
Also used : Language(com.intellij.lang.Language) TextRange(com.intellij.openapi.util.TextRange) PriorityQueue(java.util.PriorityQueue) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Language (com.intellij.lang.Language)292 NotNull (org.jetbrains.annotations.NotNull)58 Nullable (org.jetbrains.annotations.Nullable)49 PsiElement (com.intellij.psi.PsiElement)46 PsiFile (com.intellij.psi.PsiFile)45 FileType (com.intellij.openapi.fileTypes.FileType)31 Project (com.intellij.openapi.project.Project)31 TextRange (com.intellij.openapi.util.TextRange)25 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)21 FileViewProvider (com.intellij.psi.FileViewProvider)21 HbLanguage (com.dmarcotte.handlebars.HbLanguage)20 JavaLanguage (com.intellij.lang.java.JavaLanguage)16 Editor (com.intellij.openapi.editor.Editor)14 XMLLanguage (com.intellij.lang.xml.XMLLanguage)12 TemplateLanguageFileViewProvider (com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider)11 List (java.util.List)11 HTMLLanguage (com.intellij.lang.html.HTMLLanguage)9 JavascriptLanguage (com.intellij.lang.javascript.JavascriptLanguage)9 Document (com.intellij.openapi.editor.Document)9