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);
  }
  @Override
  public boolean isInContext(@NotNull final PsiFile file, final int offset) {
    PsiElement at = file.findElementAt(offset);
    if (at == null && offset == file.getTextLength()) {
      at = file.findElementAt(offset - 1);
    }
    Language language = at != null ? at.getParent().getLanguage() : null;

    if (language instanceof XMLLanguage) {
      final PsiLanguageInjectionHost host =
          PsiTreeUtil.getParentOfType(at, PsiLanguageInjectionHost.class, false);

      if (host != null) {
        final Ref<Boolean> hasJsInjection = new Ref<Boolean>(Boolean.FALSE);

        InjectedLanguageUtil.enumerate(
            host,
            new JSResolveUtil.JSInjectedFilesVisitor() {
              @Override
              protected void process(final JSFile file) {
                hasJsInjection.set(Boolean.TRUE);
              }
            });

        if (hasJsInjection.get()) {
          language = JavaScriptSupportLoader.JAVASCRIPT.getLanguage();
        }
      }
    }
    return language != null && language.isKindOf(JavaScriptSupportLoader.JAVASCRIPT.getLanguage());
  }
Пример #3
0
 @Nullable
 public static Language findJavaOrLikeLanguage(@NotNull final PsiFile file) {
   final Set<Language> languages = file.getViewProvider().getLanguages();
   for (final Language language : languages) {
     if (language == StdLanguages.JAVA) return language;
   }
   for (final Language language : languages) {
     if (language.isKindOf(StdLanguages.JAVA)) return language;
   }
   return null;
 }
  /**
   * This method searches ast node that could be reparsed incrementally and returns pair of target
   * reparseable node and new replacement node. Returns null if there is no any chance to make
   * incremental parsing.
   */
  @Nullable
  public Couple<ASTNode> findReparseableRoots(
      @NotNull PsiFileImpl file,
      @NotNull TextRange changedPsiRange,
      @NotNull CharSequence newFileText) {
    Project project = file.getProject();
    final FileElement fileElement = file.getTreeElement();
    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 IReparseableElementType) {
        final TextRange textRange = node.getTextRange();
        final IReparseableElementType reparseable = (IReparseableElementType) elementType;

        if (baseLanguage.isKindOf(reparseable.getLanguage())
            && textRange.getLength() + lengthShift > 0) {
          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);

          if (reparseable.isParsable(node.getTreeParent(), newTextStr, baseLanguage, project)) {
            ASTNode chameleon = reparseable.createNode(newTextStr);
            if (chameleon != null) {
              DummyHolder holder =
                  DummyHolderFactory.createHolder(
                      file.getManager(), null, node.getPsi(), charTable);
              holder.getTreeElement().rawAddChildren((TreeElement) chameleon);

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

              return Couple.of(node, chameleon);
            }
          }
        }
      }
      node = node.getTreeParent();
    }
    return null;
  }
 @Override
 protected boolean isMyLanguage(Language language) {
   return language.isKindOf(HbLanguage.INSTANCE);
 }
Пример #6
0
 @Override
 public boolean isMyLanguage(@NotNull Language language) {
   return language.isKindOf(getFileType().getLanguage());
 }