com.intellij.lang.FileASTNode Java Examples

The following examples show how to use com.intellij.lang.FileASTNode. 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 File: DuplicatesInspectionBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
private DuplicatedCodeProcessor<?> processLightDuplicates(FileASTNode node,
                                                          VirtualFile virtualFile,
                                                          LightDuplicateProfile profile,
                                                          Project project) {
  final Ref<DuplicatedCodeProcessor<LighterASTNode>> processorRef = new Ref<>();
  LighterAST lighterAST = node.getLighterAST();

  profile.process(lighterAST, (hash, hash2, ast, nodes) -> {
    DuplicatedCodeProcessor<LighterASTNode> processor = processorRef.get();
    if (processor == null) {
      processorRef.set(processor = new LightDuplicatedCodeProcessor((TreeBackedLighterAST)ast, virtualFile, project));
    }
    processor.process(hash, hash2, nodes[0]);
  });
  return processorRef.get();
}
 
Example #2
Source File: BlockSupportImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
static ReparseResult reparse(@Nonnull final PsiFile file,
                             @Nonnull FileASTNode oldFileNode,
                             @Nonnull TextRange changedPsiRange,
                             @Nonnull final CharSequence newFileText,
                             @Nonnull final ProgressIndicator indicator,
                             @Nonnull CharSequence lastCommittedText) {
  PsiFileImpl fileImpl = (PsiFileImpl)file;

  final Couple<ASTNode> reparseableRoots = findReparseableRoots(fileImpl, oldFileNode, changedPsiRange, newFileText);
  if (reparseableRoots == null) {
    return makeFullParse(fileImpl, oldFileNode, newFileText, indicator, lastCommittedText);
  }
  ASTNode oldRoot = reparseableRoots.first;
  ASTNode newRoot = reparseableRoots.second;
  DiffLog diffLog = mergeTrees(fileImpl, oldRoot, newRoot, indicator, lastCommittedText);
  return new ReparseResult(diffLog, oldRoot, newRoot);
}
 
Example #3
Source File: PsiInvalidElementAccessException.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static Object findInvalidationTrace(@Nullable ASTNode element) {
  while (element != null) {
    Object trace = element.getUserData(INVALIDATION_TRACE);
    if (trace != null) {
      return trace;
    }
    ASTNode parent = element.getTreeParent();
    if (parent == null && element instanceof FileASTNode) {
      PsiElement psi = element.getPsi();
      trace = psi == null ? null : psi.getUserData(INVALIDATION_TRACE);
      if (trace != null) {
        return trace;
      }
    }
    element = parent;
  }
  return null;
}
 
Example #4
Source File: BlockSupportImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public DiffLog reparseRange(@Nonnull final PsiFile file,
                            @Nonnull FileASTNode oldFileNode,
                            @Nonnull TextRange changedPsiRange,
                            @Nonnull final CharSequence newFileText,
                            @Nonnull final ProgressIndicator indicator,
                            @Nonnull CharSequence lastCommittedText) {
  try (ReparseResult result = reparse(file, oldFileNode, changedPsiRange, newFileText, indicator, lastCommittedText)) {
    return result.log;
  }
}
 
Example #5
Source File: SelectWordUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void processRanges(@Nullable PsiElement element,
                                 CharSequence text,
                                 int cursorOffset,
                                 Editor editor,
                                 Processor<TextRange> consumer) {
  if (element == null) return;

  PsiFile file = element.getContainingFile();

  FileViewProvider viewProvider = file.getViewProvider();

  processInFile(element, consumer, text, cursorOffset, editor);

  for (PsiFile psiFile : viewProvider.getAllFiles()) {
    if (psiFile == file) continue;

    FileASTNode fileNode = psiFile.getNode();
    if (fileNode == null) continue;

    ASTNode nodeAt = fileNode.findLeafElementAt(element.getTextOffset());
    if (nodeAt == null) continue;

    PsiElement elementAt = nodeAt.getPsi();

    while (!(elementAt instanceof PsiFile) && elementAt != null) {
      if (elementAt.getTextRange().contains(element.getTextRange())) break;

      elementAt = elementAt.getParent();
    }

    if (elementAt == null) continue;

    processInFile(elementAt, consumer, text, cursorOffset, editor);
  }
}
 
Example #6
Source File: FileContentImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public LighterAST getLighterAST() {
  LighterAST lighterAST = getUserData(IndexingDataKeys.LIGHTER_AST_NODE_KEY);
  if (lighterAST == null) {
    FileASTNode node = getPsiFile().getNode();
    lighterAST = myLighterASTShouldBeThreadSafe ? new TreeBackedLighterAST(node) : node.getLighterAST();
    putUserData(IndexingDataKeys.LIGHTER_AST_NODE_KEY, lighterAST);
  }
  return lighterAST;
}
 
Example #7
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static FileASTNode findFileElement(@Nonnull ASTNode element) {
  ASTNode parent = element.getTreeParent();
  while (parent != null) {
    element = parent;
    parent = parent.getTreeParent();
  }

  if (element instanceof FileASTNode) {
    return (FileASTNode)element;
  }
  return null;
}
 
Example #8
Source File: ProtoFoldingBuilder.java    From protobuf-jetbrains-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isRegionCollapsedByDefault(@NotNull ASTNode node) {
    // This code should collapse header comments.
    // However, in most of the cases it will not work,
    // as when file is open caret is at the beginning of the document,
    // thus preventing collapsing it.
    // TODO: Collapse header comment when file is opened
    return node.getTreeParent() instanceof FileASTNode
            && findPreviousNonWhitespaceOrCommentNode(node) == null;
}
 
Example #9
Source File: DocumentCommitThread.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static List<Pair<PsiFileImpl, FileASTNode>> getAllFileNodes(@Nonnull PsiFile file) {
  if (!file.isValid()) {
    throw new PsiInvalidElementAccessException(file, "File " + file + " is invalid, can't commit");
  }
  if (file instanceof PsiCompiledFile) {
    throw new IllegalArgumentException("Can't commit ClsFile: " + file);
  }

  return ContainerUtil.map(file.getViewProvider().getAllFiles(), root -> Pair.create((PsiFileImpl)root, root.getNode()));
}
 
Example #10
Source File: BlockSupport.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public abstract DiffLog reparseRange(@Nonnull PsiFile file,
                                     @Nonnull FileASTNode oldFileNode,
                                     @Nonnull TextRange changedPsiRange,
                                     @Nonnull CharSequence newText,
                                     @Nonnull ProgressIndicator progressIndicator,
                                     @Nonnull CharSequence lastCommittedText) throws IncorrectOperationException;
 
Example #11
Source File: HXMLData.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
static private FileASTNode getRootNode(Project project, String hxmlPath) throws HXMLDataException {
  VirtualFile path = LocalFileSystem.getInstance().findFileByPath(hxmlPath);
  if(path == null) {
    throw new HXMLDataException(new FileNotFoundException(hxmlPath));
  }
  if(project == null) {
    project = DefaultProjectFactory.getInstance().getDefaultProject();
  }
  PsiFile file = PsiManager.getInstance(project).findFile(path);
  if(file == null) {
    throw new HXMLDataException("PsiFile not available for " + hxmlPath);
  }
  return file.getNode();
}
 
Example #12
Source File: IgnoreHLint.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@NotNull final Project project, Editor editor, final PsiFile file) throws IncorrectOperationException {
    PsiWhiteSpace newline = HaskellElementFactory.createNewLine(project);
    HaskellPpragma ppragma = HaskellElementFactory.createPpragmaFromText(
            project, "{-# ANN module (\"HLint: ignore " + hint + "\"::String) #-}");
    FileASTNode fileNode = file.getNode();

    ASTNode[] nodes;
    ASTNode node;

    // If the user has imports, place the pragma after them.
    node = fileNode.findChildByType(HaskellTypes.BODY);
    if (node != null) {
        nodes = node.getChildren(TokenSet.create(HaskellTypes.IMPDECL));
        if (nodes.length > 0) {
            PsiElement element = node.getPsi();
            element.addBefore(newline, element.addAfter(ppragma, nodes[nodes.length - 1].getPsi()));
            return;
        }
    }

    // If the user has a module declaration, place the pragma after it.
    node = fileNode.findChildByType(HaskellTypes.MODULEDECL);
    if (node != null) {
        file.addBefore(newline, file.addAfter(ppragma, node.getPsi()));
        return;
    }

    // If the user has any existing pragmas, place the pragma after them.
    nodes = fileNode.getChildren(TokenSet.create(HaskellTypes.PPRAGMA));
    if (nodes.length > 0) {
        file.addBefore(newline, file.addAfter(ppragma, nodes[nodes.length - 1].getPsi()));
        return;
    }

    // Otherwise, just insert the pragma at the top of the file.
    file.addAfter(newline, file.addBefore(ppragma, file.getFirstChild()));
}
 
Example #13
Source File: BuildParserTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
private void nodeToString(ASTNode node, StringBuilder builder) {
  if (node instanceof LeafElement || node.getPsi() == null) {
    return;
  }
  PsiElement[] childPsis = getChildBuildPsis(node);
  if (node instanceof FileASTNode) {
    appendChildren(childPsis, builder, false);
    return;
  }
  builder.append(node.getElementType());
  appendChildren(childPsis, builder, true);
}
 
Example #14
Source File: PsiUtilCore.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public FileASTNode getNode() {
  throw createException();
}
 
Example #15
Source File: PsiFile.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
FileASTNode getNode();
 
Example #16
Source File: MockPsiFile.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public FileASTNode getNode() {
  return null;
}
 
Example #17
Source File: BlockSupportImpl.java    From consulo with 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 #18
Source File: SharedImplUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static PsiFile getContainingFile(ASTNode thisElement) {
  FileASTNode element = findFileElement(thisElement);
  PsiElement psiElement = element == null ? null : element.getPsi();
  if (psiElement == null) return null;
  return psiElement.getContainingFile();
}
 
Example #19
Source File: HaxePreprocessorInspection.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
  if (!(file instanceof HaxeFile)) return null;
  final List<ProblemDescriptor> result = new ArrayList<ProblemDescriptor>();
  final ProblemReporter reporter = new ProblemReporter() {
    @Override
    public void reportProblem(ASTNode node, String message) {
      result.add( manager.createProblemDescriptor( node.getPsi(),
                                                   message,
                                                   (LocalQuickFix)null,
                                                   ProblemHighlightType.ERROR,
                                                   isOnTheFly));
    }
  };

  FileASTNode node1 = file.getNode();
  LeafElement firstLeaf = TreeUtil.findFirstLeaf(node1);

  Stack<List<ASTNode>> levels = new Stack<List<ASTNode>>();
  List<ASTNode> nodes = new ArrayList<ASTNode>();
  // Push the root node, just in case there is no #if to start it off.
  levels.push(nodes);

  ASTNode leaf = firstLeaf;
  while (leaf != null) {
    IElementType leafElementType = leaf.getElementType();

    if (leafElementType.equals(HaxeTokenTypes.PPIF)) {
      nodes = new ArrayList<ASTNode>();
      levels.push(nodes);
      nodes.add(leaf);
    }
    else if (leafElementType.equals(HaxeTokenTypes.PPEND)) {
      nodes.add(leaf);
      // Leave the base level in place, even if there are extra ends.
      if (levels.size() > 1) {
        validateLevel(nodes, reporter);
        levels.pop();
        nodes = levels.peek();
      }
    }
    else if (leafElementType.equals(HaxeTokenTypes.PPELSEIF)) {
      nodes.add(leaf);
    }
    else if (leafElementType.equals(HaxeTokenTypes.PPELSE)) {
      nodes.add(leaf);
    }
    leaf = TreeUtil.nextLeaf(leaf);
  }

  // Any levels that are still left need to be validated.
  for (List<ASTNode> level : levels) {
    validateLevel(level, reporter);
  }

  return ArrayUtil.toObjectArray(result, ProblemDescriptor.class);
}
 
Example #20
Source File: PsiBinaryFileImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public FileASTNode getNode() {
  return null; // TODO[max] throw new UnsupportedOperationException()
}
 
Example #21
Source File: CompletionAssertions.java    From consulo with Apache License 2.0 4 votes vote down vote up
static void assertCommitSuccessful(Editor editor, PsiFile psiFile) {
  Document document = editor.getDocument();
  int docLength = document.getTextLength();
  int psiLength = psiFile.getTextLength();
  PsiDocumentManager manager = PsiDocumentManager.getInstance(psiFile.getProject());
  boolean committed = !manager.isUncommited(document);
  if (docLength == psiLength && committed) {
    return;
  }

  FileViewProvider viewProvider = psiFile.getViewProvider();

  String message = "unsuccessful commit:";
  message += "\nmatching=" + (psiFile == manager.getPsiFile(document));
  message += "\ninjectedEditor=" + (editor instanceof EditorWindow);
  message += "\ninjectedFile=" + InjectedLanguageManager.getInstance(psiFile.getProject()).isInjectedFragment(psiFile);
  message += "\ncommitted=" + committed;
  message += "\nfile=" + psiFile.getName();
  message += "\nfile class=" + psiFile.getClass();
  message += "\nfile.valid=" + psiFile.isValid();
  message += "\nfile.physical=" + psiFile.isPhysical();
  message += "\nfile.eventSystemEnabled=" + viewProvider.isEventSystemEnabled();
  message += "\nlanguage=" + psiFile.getLanguage();
  message += "\ndoc.length=" + docLength;
  message += "\npsiFile.length=" + psiLength;
  String fileText = psiFile.getText();
  if (fileText != null) {
    message += "\npsiFile.text.length=" + fileText.length();
  }
  FileASTNode node = psiFile.getNode();
  if (node != null) {
    message += "\nnode.length=" + node.getTextLength();
    String nodeText = node.getText();
    message += "\nnode.text.length=" + nodeText.length();
  }
  VirtualFile virtualFile = viewProvider.getVirtualFile();
  message += "\nvirtualFile=" + virtualFile;
  message += "\nvirtualFile.class=" + virtualFile.getClass();
  message += "\n" + DebugUtil.currentStackTrace();

  throw new RuntimeExceptionWithAttachments("Commit unsuccessful", message, new Attachment(virtualFile.getPath() + "_file.txt", StringUtil.notNullize(fileText)), createAstAttachment(psiFile, psiFile),
                                            new Attachment("docText.txt", document.getText()));
}
 
Example #22
Source File: LightPsiFileImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public FileASTNode getNode() {
  return null;
}
 
Example #23
Source File: DuplicatesInspectionBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public ProblemDescriptor[] checkFile(@Nonnull final PsiFile psiFile, @Nonnull final InspectionManager manager, final boolean isOnTheFly) {
  final VirtualFile virtualFile = psiFile.getVirtualFile();
  if (!(virtualFile instanceof VirtualFileWithId) || /*!isOnTheFly || */!DuplicatesIndex.ourEnabled) return ProblemDescriptor.EMPTY_ARRAY;
  final DuplicatesProfile profile = DuplicatesIndex.findDuplicatesProfile(psiFile.getFileType());
  if (profile == null) return ProblemDescriptor.EMPTY_ARRAY;


  final FileASTNode node = psiFile.getNode();
  boolean usingLightProfile = profile instanceof LightDuplicateProfile &&
                              node.getElementType() instanceof ILightStubFileElementType &&
                              DuplicatesIndex.ourEnabledLightProfiles;
  final Project project = psiFile.getProject();
  DuplicatedCodeProcessor<?> processor;
  if (usingLightProfile) {
    processor = processLightDuplicates(node, virtualFile, (LightDuplicateProfile)profile, project);
  }
  else {
    processor = processPsiDuplicates(psiFile, virtualFile, profile, project);
  }
  if (processor == null) return null;

  final SmartList<ProblemDescriptor> descriptors = new SmartList<>();
  final VirtualFile baseDir = project.getBaseDir();
  for (Map.Entry<Integer, TextRange> entry : processor.reportedRanges.entrySet()) {
    final Integer offset = entry.getKey();
    if (!usingLightProfile && processor.fragmentSize.get(offset) < MIN_FRAGMENT_SIZE) continue;
    final VirtualFile file = processor.reportedFiles.get(offset);
    String path = null;

    if (file.equals(virtualFile)) {
      path = "this file";
    }
    else if (baseDir != null) {
      path = VfsUtilCore.getRelativePath(file, baseDir);
    }
    if (path == null) {
      path = file.getPath();
    }
    String message = "Found duplicated code in " + path;

    PsiElement targetElement = processor.reportedPsi.get(offset);
    TextRange rangeInElement = entry.getValue();
    final int offsetInOtherFile = processor.reportedOffsetInOtherFiles.get(offset);

    LocalQuickFix fix = isOnTheFly ? createNavigateToDupeFix(file, offsetInOtherFile) : null;
    long hash = processor.fragmentHash.get(offset);

    int hash2 = (int)(hash >> 32);
    LocalQuickFix viewAllDupesFix = isOnTheFly && hash != 0 ? createShowOtherDupesFix(virtualFile, offset, (int)hash, hash2) : null;

    boolean onlyExtractable = Registry.is("duplicates.inspection.only.extractable");
    LocalQuickFix extractMethodFix =
      (isOnTheFly || onlyExtractable) && hash != 0 ? createExtractMethodFix(targetElement, rangeInElement, (int)hash, hash2) : null;
    if (onlyExtractable) {
      if (extractMethodFix == null) return null;
      if (!isOnTheFly) extractMethodFix = null;
    }

    ProblemDescriptor descriptor = manager
      .createProblemDescriptor(targetElement, rangeInElement, message, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly, fix,
                               viewAllDupesFix, extractMethodFix);
    descriptors.add(descriptor);
  }

  return descriptors.isEmpty() ? null : descriptors.toArray(ProblemDescriptor.EMPTY_ARRAY);
}