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;
}
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()]);
}
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);
}
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()]);
}
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()]);
}
Aggregations