Search in sources :

Example 31 with Language

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

the class XmlTagRenameHandler method getElement.

@Nullable
private static PsiElement getElement(@Nullable final DataContext context) {
    if (context != null) {
        final Editor editor = getEditor(context);
        if (editor != null) {
            final int offset = editor.getCaretModel().getOffset();
            final PsiFile file = CommonDataKeys.PSI_FILE.getData(context);
            if (file instanceof XmlFile) {
                return file.getViewProvider().findElementAt(offset);
            }
            if (file != null) {
                final Language language = PsiUtilCore.getLanguageAtOffset(file, offset);
                if (language != file.getLanguage()) {
                    final PsiFile psiAtOffset = file.getViewProvider().getPsi(language);
                    if (psiAtOffset instanceof XmlFile) {
                        return psiAtOffset.findElementAt(offset);
                    }
                }
            }
        }
    }
    return null;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) Language(com.intellij.lang.Language) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with Language

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

the class CheckRegExpForm method isMatchingText.

static RegExpMatchResult isMatchingText(@NotNull final PsiFile regexpFile, @NotNull String sampleText) {
    final String regExp = regexpFile.getText();
    final Language regexpFileLanguage = regexpFile.getLanguage();
    final RegExpMatcherProvider matcherProvider = RegExpMatcherProvider.EP.forLanguage(regexpFileLanguage);
    if (matcherProvider != null) {
        final RegExpMatchResult result = ReadAction.compute(() -> {
            final PsiLanguageInjectionHost host = InjectedLanguageUtil.findInjectionHost(regexpFile);
            if (host != null) {
                return matcherProvider.matches(regExp, regexpFile, host, sampleText, 1000L);
            }
            return null;
        });
        if (result != null) {
            return result;
        }
    }
    final Integer patternFlags = ReadAction.compute(() -> {
        final PsiLanguageInjectionHost host = InjectedLanguageUtil.findInjectionHost(regexpFile);
        int flags = 0;
        if (host != null) {
            for (RegExpModifierProvider provider : RegExpModifierProvider.EP.allForLanguage(host.getLanguage())) {
                flags = provider.getFlags(host, regexpFile);
                if (flags > 0)
                    break;
            }
        }
        return flags;
    });
    try {
        //noinspection MagicConstant
        return Pattern.compile(regExp, patternFlags).matcher(StringUtil.newBombedCharSequence(sampleText, 1000)).matches() ? RegExpMatchResult.MATCHES : RegExpMatchResult.NO_MATCH;
    } catch (ProcessCanceledException pc) {
        return RegExpMatchResult.TIMEOUT;
    } catch (Exception ignore) {
    }
    return RegExpMatchResult.BAD_REGEXP;
}
Also used : Language(com.intellij.lang.Language) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 33 with Language

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

the class CheckRegExpIntentionAction method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    Pair<PsiElement, TextRange> pair = getRangePair(file, editor);
    if (pair != null && pair.first != null) {
        Language language = pair.first.getLanguage();
        return language.isKindOf(RegExpLanguage.INSTANCE);
    }
    PsiFile baseFile = InjectedLanguageManager.getInstance(project).getTopLevelFile(file);
    return baseFile != null && baseFile.getLanguage().isKindOf(RegExpLanguage.INSTANCE);
}
Also used : Language(com.intellij.lang.Language) RegExpLanguage(org.intellij.lang.regexp.RegExpLanguage) TextRange(com.intellij.openapi.util.TextRange) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 34 with Language

use of com.intellij.lang.Language in project smali by JesusFreke.

the class LightCodeInsightParsingTestCase method doCheckResult.

public static void doCheckResult(String myFullDataPath, PsiFile file, boolean checkAllPsiRoots, String targetDataName, boolean skipSpaces, boolean printRanges) throws IOException {
    FileViewProvider provider = file.getViewProvider();
    Set<Language> languages = provider.getLanguages();
    if (!checkAllPsiRoots || languages.size() == 1) {
        doCheckResult(myFullDataPath, targetDataName + ".txt", toParseTreeText(file, skipSpaces, printRanges).trim());
        return;
    }
    for (Language language : languages) {
        PsiFile root = provider.getPsi(language);
        String expectedName = targetDataName + "." + language.getID() + ".txt";
        doCheckResult(myFullDataPath, expectedName, toParseTreeText(root, skipSpaces, printRanges).trim());
    }
}
Also used : Language(com.intellij.lang.Language)

Example 35 with Language

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

the class SearchDialog method buildOptions.

protected void buildOptions(JPanel searchOptions) {
    recursiveMatching = new JCheckBox(SSRBundle.message("recursive.matching.checkbox"), true);
    if (isRecursiveSearchEnabled()) {
        searchOptions.add(UIUtil.createOptionLine(recursiveMatching));
    }
    caseSensitiveMatch = new JCheckBox(FindBundle.message("find.options.case.sensitive"), true);
    searchOptions.add(UIUtil.createOptionLine(caseSensitiveMatch));
    final List<FileType> types = new ArrayList<>();
    for (FileType fileType : StructuralSearchUtil.getSuitableFileTypes()) {
        if (StructuralSearchUtil.getProfileByFileType(fileType) != null) {
            types.add(fileType);
        }
    }
    Collections.sort(types, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
    final DefaultComboBoxModel<FileType> comboBoxModel = new DefaultComboBoxModel<>(types.toArray(new FileType[types.size()]));
    fileTypes = new ComboBox<>(comboBoxModel);
    fileTypes.setRenderer(new FileTypeRenderer());
    new ComboboxSpeedSearch(fileTypes) {

        @Override
        protected String getElementText(Object element) {
            return ((FileType) element).getName();
        }
    };
    contexts = new ComboBox<>();
    contexts.setPreferredSize(new Dimension(60, -1));
    dialects = new ComboBox<>();
    dialects.setRenderer(new ListCellRendererWrapper<Language>() {

        @Override
        public void customize(JList list, Language value, int index, boolean selected, boolean hasFocus) {
            if (value == null) {
                setText("None");
            } else {
                setText(value.getDisplayName());
            }
        }
    });
    dialects.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            updateEditor();
        }
    });
    new ComboboxSpeedSearch(dialects);
    dialects.setPreferredSize(new Dimension(120, -1));
    final JLabel jLabel = new JLabel(SSRBundle.message("search.dialog.file.type.label"));
    final JLabel jLabel2 = new JLabel(SSRBundle.message("search.dialog.context.label"));
    final JLabel jLabel3 = new JLabel(SSRBundle.message("search.dialog.file.dialect.label"));
    searchOptions.add(UIUtil.createOptionLine(new JComponent[] { jLabel, fileTypes, (JComponent) Box.createHorizontalStrut(8), jLabel2, contexts, (JComponent) Box.createHorizontalStrut(8), jLabel3, dialects }));
    jLabel.setLabelFor(fileTypes);
    jLabel2.setLabelFor(contexts);
    jLabel3.setLabelFor(dialects);
    detectFileTypeAndDialect();
    fileTypes.setSelectedItem(ourFtSearchVariant);
    fileTypes.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                updateDialectsAndContexts();
                updateEditor();
                initiateValidation();
            }
        }
    });
    dialects.setSelectedItem(ourDialect);
    contexts.setSelectedItem(ourContext);
    updateDialectsAndContexts();
}
Also used : ItemEvent(java.awt.event.ItemEvent) Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) LanguageFileType(com.intellij.openapi.fileTypes.LanguageFileType) FileTypeRenderer(com.intellij.openapi.fileTypes.impl.FileTypeRenderer) ItemListener(java.awt.event.ItemListener) ComboboxSpeedSearch(com.intellij.ui.ComboboxSpeedSearch)

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