Search in sources :

Example 86 with Language

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

the class FormatterUtil method spacesOnly.

private static boolean spacesOnly(@Nullable TreeElement node) {
    if (node == null)
        return false;
    if (isWhitespaceOrEmpty(node))
        return true;
    PsiElement psi = node.getPsi();
    if (psi == null) {
        return false;
    }
    Language language = psi.getLanguage();
    return WhiteSpaceFormattingStrategyFactory.getStrategy(language).containsWhitespacesOnly(node);
}
Also used : Language(com.intellij.lang.Language) PsiElement(com.intellij.psi.PsiElement)

Example 87 with Language

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

the class TargetElementUtil method getReferenceOrReferencedElement.

@Nullable
private PsiElement getReferenceOrReferencedElement(PsiFile file, Editor editor, int flags, int offset) {
    PsiElement result = doGetReferenceOrReferencedElement(file, editor, flags, offset);
    PsiElement languageElement = file.findElementAt(offset);
    Language language = languageElement != null ? languageElement.getLanguage() : file.getLanguage();
    TargetElementEvaluatorEx2 evaluator = getElementEvaluatorsEx2(language);
    if (evaluator != null) {
        result = evaluator.adjustReferenceOrReferencedElement(file, editor, offset, flags, result);
    }
    return result;
}
Also used : Language(com.intellij.lang.Language) Nullable(org.jetbrains.annotations.Nullable)

Example 88 with Language

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

the class CompletionUtil method getCompletionDataByElement.

@Nullable
public static CompletionData getCompletionDataByElement(@Nullable final PsiElement position, @NotNull PsiFile originalFile) {
    if (position == null)
        return null;
    PsiElement parent = position.getParent();
    Language language = parent == null ? position.getLanguage() : parent.getLanguage();
    final FileType fileType = language.getAssociatedFileType();
    if (fileType != null) {
        final CompletionData mainData = getCompletionDataByFileType(fileType);
        if (mainData != null) {
            return mainData;
        }
    }
    final CompletionData mainData = getCompletionDataByFileType(originalFile.getFileType());
    return mainData != null ? mainData : ourGenericCompletionData;
}
Also used : Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) LookupValueWithPsiElement(com.intellij.codeInsight.lookup.LookupValueWithPsiElement) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 89 with Language

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

the class EnterAfterUnmatchedBraceHandler method getUnmatchedLBracesNumberBefore.

/**
   * Calculates number of unmatched left braces before the given offset.
   *
   * @param editor   target editor
   * @param offset   target offset
   * @param fileType target file type
   * @return number of unmatched braces before the given offset;
   * negative value if it's not possible to perform the calculation or if there are no unmatched left braces before
   * the given offset
   */
protected static int getUnmatchedLBracesNumberBefore(Editor editor, int offset, FileType fileType) {
    if (offset == 0) {
        return -1;
    }
    CharSequence chars = editor.getDocument().getCharsSequence();
    if (chars.charAt(offset - 1) != '{') {
        return -1;
    }
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(offset - 1);
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
    if (!braceMatcher.isLBraceToken(iterator, chars, fileType) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
        return -1;
    }
    Language language = iterator.getTokenType().getLanguage();
    iterator = highlighter.createIterator(0);
    int lBracesBeforeOffset = 0;
    int lBracesAfterOffset = 0;
    int rBracesBeforeOffset = 0;
    int rBracesAfterOffset = 0;
    for (; !iterator.atEnd(); iterator.advance()) {
        IElementType tokenType = iterator.getTokenType();
        if (!tokenType.getLanguage().equals(language) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
            continue;
        }
        boolean beforeOffset = iterator.getStart() < offset;
        if (braceMatcher.isLBraceToken(iterator, chars, fileType)) {
            if (beforeOffset) {
                lBracesBeforeOffset++;
            } else {
                lBracesAfterOffset++;
            }
        } else if (braceMatcher.isRBraceToken(iterator, chars, fileType)) {
            if (beforeOffset) {
                rBracesBeforeOffset++;
            } else {
                rBracesAfterOffset++;
            }
        }
    }
    return lBracesBeforeOffset - rBracesBeforeOffset - (rBracesAfterOffset - lBracesAfterOffset);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) EditorEx(com.intellij.openapi.editor.ex.EditorEx) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Language(com.intellij.lang.Language) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 90 with Language

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

the class CommentByLineCommentHandler method findCommenter.

@Nullable
private static Commenter findCommenter(Editor editor, PsiFile file, final int line) {
    final FileType fileType = file.getFileType();
    if (fileType instanceof AbstractFileType) {
        return ((AbstractFileType) fileType).getCommenter();
    }
    Document document = editor.getDocument();
    int lineStartOffset = document.getLineStartOffset(line);
    int lineEndOffset = document.getLineEndOffset(line) - 1;
    final CharSequence charSequence = document.getCharsSequence();
    lineStartOffset = Math.max(0, CharArrayUtil.shiftForward(charSequence, lineStartOffset, " \t"));
    lineEndOffset = Math.max(0, CharArrayUtil.shiftBackward(charSequence, lineEndOffset < 0 ? 0 : lineEndOffset, " \t"));
    final Language lineStartLanguage = PsiUtilCore.getLanguageAtOffset(file, lineStartOffset);
    final Language lineEndLanguage = PsiUtilCore.getLanguageAtOffset(file, lineEndOffset);
    return CommentByBlockCommentHandler.getCommenter(file, editor, lineStartLanguage, lineEndLanguage);
}
Also used : AbstractFileType(com.intellij.openapi.fileTypes.impl.AbstractFileType) Language(com.intellij.lang.Language) FileType(com.intellij.openapi.fileTypes.FileType) AbstractFileType(com.intellij.openapi.fileTypes.impl.AbstractFileType) CustomSyntaxTableFileType(com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType) 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