Java Code Examples for com.intellij.lang.Language#isKindOf()

The following examples show how to use com.intellij.lang.Language#isKindOf() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: bamboo-soy   File: SoyFileViewProvider.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected PsiFile createFile(@NotNull Language lang) {
  ParserDefinition parserDefinition = getDefinition(lang);
  if (parserDefinition == null) {
    return null;
  }

  if (lang.is(TEMPLATE_DATA_LANGUAGE)) {
    PsiFileImpl file = (PsiFileImpl) parserDefinition.createFile(this);
    file.setContentElementType(TEMPLATE_DATA_ELEMENT_TYPE);
    return file;
  } else if (lang.isKindOf(BASE_LANGUAGE)) {
    return parserDefinition.createFile(this);
  } else {
    return null;
  }
}
 
Example 2
@Override
public void setSelected(@NotNull AnActionEvent anActionEvent, boolean selected) {
    PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE);
    if (psiFile == null) {
        return;
    }
    Language language = psiFile.getLanguage();
    if (!language.isKindOf(CsvLanguage.INSTANCE) || language instanceof CsvSeparatorHolder) {
        return;
    }
    CsvFileAttributes csvFileAttributes = ServiceManager.getService(psiFile.getProject(), CsvFileAttributes.class);
    csvFileAttributes.setFileSeparator(psiFile, this.mySeparator);
    FileContentUtilCore.reparseFiles(psiFile.getVirtualFile());

    FileEditor fileEditor = anActionEvent.getData(PlatformDataKeys.FILE_EDITOR);
    if (fileEditor != null) {
        fileEditor.selectNotify();
    }
}
 
Example 3
Source Project: react-templates-plugin   File: RTJSBracesUtil.java    License: MIT License 6 votes vote down vote up
public static boolean hasConflicts(String start, String end, PsiElement element) {
    final Language elementLanguage = element.getLanguage();
    // JSP contains two roots that contain XmlText, don't inject anything in JSP root to prevent double injections
    if ("JSP".equals(elementLanguage.getDisplayName())) {
        return true;
    }

    PsiFile file = element.getContainingFile();
    if (DEFAULT_START.equals(start) || DEFAULT_END.equals(end)) {
        // JSX attributes don't contain AngularJS injections, {{}} is JSX injection with object inside
        if (elementLanguage.isKindOf(JavascriptLanguage.INSTANCE)) return true;

        for (Language language : file.getViewProvider().getLanguages()) {
            if (DEFAULT_CONFLICTS.contains(language.getDisplayName())) {
                return true;
            }
        }
    }
    return false;
}
 
Example 4
@Override
public boolean isInContext(@NotNull PsiFile file, int offset) {
    Language language = PsiUtilCore.getLanguageAtOffset(file, offset);
    if (language.isKindOf(BashFileType.BASH_LANGUAGE)) {
        PsiElement element = file.findElementAt(offset);
        if (element == null) {
            //if a user edits at the end of a comment at the end of a file then findElementAt returns null
            //(for yet unknown reasons)
            element = file.findElementAt(offset - 1);
        }

        return !BashPsiUtils.hasParentOfType(element, PsiComment.class, 3)
                && !BashPsiUtils.hasParentOfType(element, BashShebang.class, 3)
                && !BashPsiUtils.hasParentOfType(element, BashHereDoc.class, 1)
                && !BashPsiUtils.isCommandParameterWord(element);
    }

    return false;
}
 
Example 5
Source Project: react-templates-plugin   File: RTJSBracesUtil.java    License: MIT License 6 votes vote down vote up
public static boolean hasConflicts(String start, String end, PsiElement element) {
    final Language elementLanguage = element.getLanguage();
    // JSP contains two roots that contain XmlText, don't inject anything in JSP root to prevent double injections
    if ("JSP".equals(elementLanguage.getDisplayName())) {
        return true;
    }

    PsiFile file = element.getContainingFile();
    if (DEFAULT_START.equals(start) || DEFAULT_END.equals(end)) {
        // JSX attributes don't contain AngularJS injections, {{}} is JSX injection with object inside
        if (elementLanguage.isKindOf(JavascriptLanguage.INSTANCE)) return true;

        for (Language language : file.getViewProvider().getLanguages()) {
            if (DEFAULT_CONFLICTS.contains(language.getDisplayName())) {
                return true;
            }
        }
    }
    return false;
}
 
Example 6
Source Project: consulo   File: CodeEditUtil.java    License: Apache License 2.0 6 votes vote down vote up
private static void markToReformatBeforeOrInsertWhitespace(final ASTNode left, @Nonnull final ASTNode right) {
  final Language leftLang = left != null ? PsiUtilCore.getNotAnyLanguage(left) : null;
  final Language rightLang = PsiUtilCore.getNotAnyLanguage(right);

  ASTNode generatedWhitespace = null;
  if (leftLang != null && leftLang.isKindOf(rightLang)) {
    generatedWhitespace = LanguageTokenSeparatorGenerators.INSTANCE.forLanguage(leftLang).generateWhitespaceBetweenTokens(left, right);
  }
  else if (rightLang.isKindOf(leftLang)) {
    generatedWhitespace = LanguageTokenSeparatorGenerators.INSTANCE.forLanguage(rightLang).generateWhitespaceBetweenTokens(left, right);
  }

  if (generatedWhitespace != null) {
    final TreeUtil.CommonParentState parentState = new TreeUtil.CommonParentState();
    TreeUtil.prevLeaf((TreeElement)right, parentState);
    parentState.nextLeafBranchStart.getTreeParent().addChild(generatedWhitespace, parentState.nextLeafBranchStart);
  }
  else {
    markToReformatBefore(right, true);
  }
}
 
Example 7
Source Project: consulo   File: RenameUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean isValidName(final Project project, final PsiElement psiElement, final String newName) {
  if (newName == null || newName.length() == 0) {
    return false;
  }
  final Condition<String> inputValidator = RenameInputValidatorRegistry.getInputValidator(psiElement);
  if (inputValidator != null) {
    return inputValidator.value(newName);
  }
  if (psiElement instanceof PsiFile || psiElement instanceof PsiDirectory) {
    return newName.indexOf('\\') < 0 && newName.indexOf('/') < 0;
  }
  if (psiElement instanceof PomTargetPsiElement) {
    return !StringUtil.isEmptyOrSpaces(newName);
  }

  final PsiFile file = psiElement.getContainingFile();
  final Language elementLanguage = psiElement.getLanguage();

  final Language fileLanguage = file == null ? null : file.getLanguage();
  Language language = fileLanguage == null ? elementLanguage : fileLanguage.isKindOf(elementLanguage) ? fileLanguage : elementLanguage;

  return LanguageNamesValidation.INSTANCE.forLanguage(language).isIdentifier(newName.trim(), project);
}
 
Example 8
Source Project: consulo   File: PsiFileBase.java    License: Apache License 2.0 5 votes vote down vote up
private static Language findLanguage(Language baseLanguage, FileViewProvider viewProvider) {
  final Set<Language> languages = viewProvider.getLanguages();
  for (final Language actualLanguage : languages) {
    if (actualLanguage.isKindOf(baseLanguage)) {
      return actualLanguage;
    }
  }
  throw new AssertionError(
      "Language " + baseLanguage + " doesn't participate in view provider " + viewProvider + ": " + new ArrayList<Language>(languages));
}
 
Example 9
public boolean canChangeValueSeparator(@NotNull PsiFile psiFile) {
    Language language = psiFile.getLanguage();
    return language.isKindOf(CsvLanguage.INSTANCE) && !(language instanceof CsvSeparatorHolder);
}
 
Example 10
Source Project: consulo   File: PsiUtilCore.java    License: Apache License 2.0 4 votes vote down vote up
protected static Language narrowLanguage(final Language language, final Language candidate) {
  if (candidate.isKindOf(language)) return candidate;
  return language;
}
 
Example 11
Source Project: consulo   File: BlockSupportImpl.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Find ast node that could be reparsed incrementally
 *
 * @return Pair (target reparseable node, new replacement node)
 * or {@code null} if can't parse incrementally.
 */
@Nullable
public static Couple<ASTNode> findReparseableRoots(@Nonnull PsiFileImpl file, @Nonnull FileASTNode oldFileNode, @Nonnull TextRange changedPsiRange, @Nonnull CharSequence newFileText) {
  final FileElement fileElement = (FileElement)oldFileNode;
  final CharTable charTable = fileElement.getCharTable();
  int lengthShift = newFileText.length() - fileElement.getTextLength();

  if (fileElement.getElementType() instanceof ITemplateDataElementType || isTooDeep(file)) {
    // unable to perform incremental reparse for template data in JSP, or in exceptionally deep trees
    return null;
  }

  final ASTNode leafAtStart = fileElement.findLeafElementAt(Math.max(0, changedPsiRange.getStartOffset() - 1));
  final ASTNode leafAtEnd = fileElement.findLeafElementAt(Math.min(changedPsiRange.getEndOffset(), fileElement.getTextLength() - 1));
  ASTNode node = leafAtStart != null && leafAtEnd != null ? TreeUtil.findCommonParent(leafAtStart, leafAtEnd) : fileElement;
  Language baseLanguage = file.getViewProvider().getBaseLanguage();

  while (node != null && !(node instanceof FileElement)) {
    IElementType elementType = node.getElementType();
    if (elementType instanceof IReparseableElementTypeBase || elementType instanceof IReparseableLeafElementType) {
      final TextRange textRange = node.getTextRange();

      if (textRange.getLength() + lengthShift > 0 && (baseLanguage.isKindOf(elementType.getLanguage()) || !TreeUtil.containsOuterLanguageElements(node))) {
        final int start = textRange.getStartOffset();
        final int end = start + textRange.getLength() + lengthShift;
        if (end > newFileText.length()) {
          reportInconsistentLength(file, newFileText, node, start, end);
          break;
        }

        CharSequence newTextStr = newFileText.subSequence(start, end);

        ASTNode newNode;
        if (elementType instanceof IReparseableElementTypeBase) {
          newNode = tryReparseNode((IReparseableElementTypeBase)elementType, node, newTextStr, file.getManager(), baseLanguage, charTable);
        }
        else {
          newNode = tryReparseLeaf((IReparseableLeafElementType)elementType, node, newTextStr);
        }

        if (newNode != null) {
          if (newNode.getTextLength() != newTextStr.length()) {
            String details = ApplicationManager.getApplication().isInternal() ? "text=" + newTextStr + "; treeText=" + newNode.getText() + ";" : "";
            LOG.error("Inconsistent reparse: " + details + " type=" + elementType);
          }

          return Couple.of(node, newNode);
        }
      }
    }
    node = node.getTreeParent();
  }
  return null;
}
 
Example 12
Source Project: consulo   File: BaseRefactoringAction.java    License: Apache License 2.0 4 votes vote down vote up
protected boolean isAvailableForLanguage(Language language) {
  return language.isKindOf(InternalStdFileTypes.JAVA.getLanguage());
}