Search in sources :

Example 1 with TodoItem

use of com.intellij.psi.search.TodoItem in project intellij-community by JetBrains.

the class TodoFileNode method createListForSingleFile.

private Collection<AbstractTreeNode> createListForSingleFile() {
    PsiFile psiFile = getValue();
    TodoItem[] items = findAllTodos(psiFile, myBuilder.getTodoTreeStructure().getSearchHelper());
    ArrayList<AbstractTreeNode> children = new ArrayList<>(items.length);
    Document document = PsiDocumentManager.getInstance(getProject()).getDocument(psiFile);
    if (document != null) {
        for (TodoItem todoItem : items) {
            if (todoItem.getTextRange().getEndOffset() < document.getTextLength() + 1) {
                SmartTodoItemPointer pointer = new SmartTodoItemPointer(todoItem, document);
                TodoFilter toDoFilter = getToDoFilter();
                if (toDoFilter != null) {
                    TodoItemNode itemNode = new TodoItemNode(getProject(), pointer, myBuilder);
                    if (toDoFilter.contains(todoItem.getPattern())) {
                        children.add(itemNode);
                    }
                } else {
                    children.add(new TodoItemNode(getProject(), pointer, myBuilder));
                }
            }
        }
    }
    Collections.sort(children, SmartTodoItemPointerComparator.ourInstance);
    return children;
}
Also used : TodoItem(com.intellij.psi.search.TodoItem) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Document(com.intellij.openapi.editor.Document)

Example 2 with TodoItem

use of com.intellij.psi.search.TodoItem in project intellij-community by JetBrains.

the class TodoFileNode method findAllTodos.

public static TodoItem[] findAllTodos(final PsiFile psiFile, final PsiTodoSearchHelper helper) {
    final List<TodoItem> todoItems = new ArrayList<>(Arrays.asList(helper.findTodoItems(psiFile)));
    psiFile.accept(new PsiRecursiveElementWalkingVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof PsiLanguageInjectionHost) {
                InjectedLanguageUtil.enumerate(element, new PsiLanguageInjectionHost.InjectedPsiVisitor() {

                    @Override
                    public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
                        if (places.size() == 1) {
                            Document document = PsiDocumentManager.getInstance(injectedPsi.getProject()).getCachedDocument(injectedPsi);
                            if (!(document instanceof DocumentWindow))
                                return;
                            for (TodoItem item : helper.findTodoItems(injectedPsi)) {
                                TextRange rangeInHost = ((DocumentWindow) document).injectedToHost(item.getTextRange());
                                todoItems.add(new TodoItemImpl(psiFile, rangeInHost.getStartOffset(), rangeInHost.getEndOffset(), item.getPattern()));
                            }
                        }
                    }
                });
            }
            super.visitElement(element);
        }
    });
    return todoItems.toArray(new TodoItem[todoItems.size()]);
}
Also used : TodoItem(com.intellij.psi.search.TodoItem) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) TodoItemImpl(com.intellij.psi.impl.search.TodoItemImpl) NotNull(org.jetbrains.annotations.NotNull) DocumentWindow(com.intellij.injected.editor.DocumentWindow)

Example 3 with TodoItem

use of com.intellij.psi.search.TodoItem in project intellij-community by JetBrains.

the class TodoItemNode method update.

@Override
public void update(PresentationData presentation) {
    TodoItem todoItem = getValue().getTodoItem();
    RangeMarker myRangeMarker = getValue().getRangeMarker();
    if (!todoItem.getFile().isValid() || !myRangeMarker.isValid() || myRangeMarker.getStartOffset() == myRangeMarker.getEndOffset()) {
        myRangeMarker.dispose();
        setValue(null);
        return;
    }
    myHighlightedRegions.clear();
    // Update name
    Document document = getValue().getDocument();
    CharSequence chars = document.getCharsSequence();
    int startOffset = myRangeMarker.getStartOffset();
    int endOffset = myRangeMarker.getEndOffset();
    LOG.assertTrue(startOffset > -1);
    LOG.assertTrue(startOffset <= document.getTextLength());
    LOG.assertTrue(endOffset > -1);
    LOG.assertTrue(endOffset < document.getTextLength() + 1);
    int lineNumber = document.getLineNumber(startOffset);
    LOG.assertTrue(lineNumber > -1);
    LOG.assertTrue(lineNumber < document.getLineCount());
    int lineStartOffset = document.getLineStartOffset(lineNumber);
    LOG.assertTrue(lineStartOffset > -1);
    LOG.assertTrue(lineStartOffset <= startOffset);
    LOG.assertTrue(lineStartOffset <= document.getTextLength());
    int columnNumber = startOffset - lineStartOffset;
    LOG.assertTrue(columnNumber > -1);
    while (lineStartOffset < document.getTextLength() && (chars.charAt(lineStartOffset) == '\t' || chars.charAt(lineStartOffset) == ' ')) {
        lineStartOffset++;
    }
    int lineEndOffset = document.getLineEndOffset(lineNumber);
    LOG.assertTrue(lineEndOffset >= 0);
    LOG.assertTrue(lineEndOffset <= document.getTextLength());
    String lineColumnPrefix = "(" + (lineNumber + 1) + ", " + (columnNumber + 1) + ") ";
    String highlightedText = chars.subSequence(lineStartOffset, Math.min(lineEndOffset, chars.length())).toString();
    String newName = lineColumnPrefix + highlightedText;
    // Update icon
    Icon newIcon = todoItem.getPattern().getAttributes().getIcon();
    // Update highlighted regions
    myHighlightedRegions.clear();
    EditorHighlighter highlighter = myBuilder.getHighlighter(todoItem.getFile(), document);
    HighlighterIterator iterator = highlighter.createIterator(lineStartOffset);
    while (!iterator.atEnd()) {
        int start = Math.max(iterator.getStart(), lineStartOffset);
        int end = Math.min(iterator.getEnd(), lineEndOffset);
        if (lineEndOffset < start || lineEndOffset < end) {
            break;
        }
        TextAttributes attributes = iterator.getTextAttributes();
        int fontType = attributes.getFontType();
        if ((fontType & Font.BOLD) != 0) {
            // suppress bold attribute
            attributes = attributes.clone();
            attributes.setFontType(fontType & ~Font.BOLD);
        }
        HighlightedRegion region = new HighlightedRegion(lineColumnPrefix.length() + start - lineStartOffset, lineColumnPrefix.length() + end - lineStartOffset, attributes);
        myHighlightedRegions.add(region);
        iterator.advance();
    }
    TextAttributes attributes = todoItem.getPattern().getAttributes().getTextAttributes();
    HighlightedRegion region = new HighlightedRegion(lineColumnPrefix.length() + startOffset - lineStartOffset, lineColumnPrefix.length() + endOffset - lineStartOffset, attributes);
    myHighlightedRegions.add(region);
    //
    presentation.setPresentableText(newName);
    presentation.setIcon(newIcon);
}
Also used : TodoItem(com.intellij.psi.search.TodoItem) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) HighlightedRegion(com.intellij.ui.HighlightedRegion) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 4 with TodoItem

use of com.intellij.psi.search.TodoItem in project intellij-community by JetBrains.

the class PsiTodoSearchHelperImpl method processTodoOccurences.

@NotNull
private static TodoItem[] processTodoOccurences(int startOffset, int endOffset, Collection<IndexPatternOccurrence> occurrences) {
    List<TodoItem> items = new ArrayList<>(occurrences.size());
    TextRange textRange = new TextRange(startOffset, endOffset);
    final TodoItemsCreator todoItemsCreator = new TodoItemsCreator();
    for (IndexPatternOccurrence occurrence : occurrences) {
        TextRange occurrenceRange = occurrence.getTextRange();
        if (textRange.contains(occurrenceRange)) {
            items.add(todoItemsCreator.createTodo(occurrence));
        }
    }
    return items.toArray(new TodoItem[items.size()]);
}
Also used : TodoItem(com.intellij.psi.search.TodoItem) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) IndexPatternOccurrence(com.intellij.psi.search.IndexPatternOccurrence) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with TodoItem

use of com.intellij.psi.search.TodoItem in project intellij-community by JetBrains.

the class TodoCommentInspection method checkFile.

@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
    final TodoItem[] todoItems = PsiTodoSearchHelper.SERVICE.getInstance(file.getProject()).findTodoItems(file);
    final List<ProblemDescriptor> result = new ArrayList<>();
    final THashSet<PsiComment> comments = new THashSet<>();
    for (TodoItem todoItem : todoItems) {
        final PsiComment comment = PsiTreeUtil.getParentOfType(file.findElementAt(todoItem.getTextRange().getStartOffset()), PsiComment.class, false);
        if (comment != null && comments.add(comment)) {
            result.add(manager.createProblemDescriptor(comment, InspectionsBundle.message("todo.comment.problem.descriptor"), isOnTheFly, null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
        }
    }
    return result.toArray(new ProblemDescriptor[result.size()]);
}
Also used : PsiComment(com.intellij.psi.PsiComment) TodoItem(com.intellij.psi.search.TodoItem) ArrayList(java.util.ArrayList) THashSet(gnu.trove.THashSet) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

TodoItem (com.intellij.psi.search.TodoItem)11 Document (com.intellij.openapi.editor.Document)4 TextRange (com.intellij.openapi.util.TextRange)3 ArrayList (java.util.ArrayList)3 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 Change (com.intellij.openapi.vcs.changes.Change)2 NotNull (org.jetbrains.annotations.NotNull)2 DocumentWindow (com.intellij.injected.editor.DocumentWindow)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)1 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)1 Computable (com.intellij.openapi.util.Computable)1 TodoCheckinHandlerWorker (com.intellij.openapi.vcs.checkin.TodoCheckinHandlerWorker)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiComment (com.intellij.psi.PsiComment)1 TodoItemImpl (com.intellij.psi.impl.search.TodoItemImpl)1 IndexPatternOccurrence (com.intellij.psi.search.IndexPatternOccurrence)1 PsiTodoSearchHelper (com.intellij.psi.search.PsiTodoSearchHelper)1 HighlightedRegion (com.intellij.ui.HighlightedRegion)1