todo highlighting: simplify, cleanup

GitOrigin-RevId: c702c37698bf971a0a7003056db6aaea9db414e9
This commit is contained in:
Alexey Kudravtsev
2024-05-06 14:57:40 +02:00
committed by intellij-monorepo-bot
parent 882adcb324
commit 114a67f0cd
6 changed files with 30 additions and 54 deletions

View File

@@ -53,7 +53,7 @@ final class TodoHighlightVisitor implements HighlightVisitor {
@Override
public void visit(@NotNull PsiElement element) {
if (element instanceof PsiFile psiFile && psiFile.getViewProvider().getAllFiles().get(0) == psiFile) {
highlightTodos(psiFile, psiFile.getText(), 0, psiFile.getTextLength(), myHolder);
highlightTodos(psiFile, psiFile.getText(), myHolder);
}
}
@@ -65,12 +65,10 @@ final class TodoHighlightVisitor implements HighlightVisitor {
private static void highlightTodos(@NotNull PsiFile file,
@NotNull CharSequence text,
int startOffset,
int endOffset,
@NotNull HighlightInfoHolder holder) {
PsiTodoSearchHelper helper = PsiTodoSearchHelper.getInstance(file.getProject());
if (helper == null || !shouldHighlightTodos(helper, file)) return;
TodoItem[] todoItems = helper.findTodoItems(file, startOffset, endOffset);
TodoItem[] todoItems = helper.findTodoItems(file);
for (TodoItem todoItem : todoItems) {
ProgressManager.checkCanceled();
@@ -87,12 +85,12 @@ final class TodoHighlightVisitor implements HighlightVisitor {
String tooltip = XmlStringUtil.escapeString(StringUtil.shortenPathWithEllipsis(description, 1024)).replace("\n", "<br>");
TextAttributes attributes = todoPattern.getAttributes().getTextAttributes();
addTodoItem(startOffset, endOffset, holder, attributes, description, tooltip, textRange);
addTodoItem(holder, attributes, description, tooltip, textRange);
if (!additionalRanges.isEmpty()) {
TextAttributes attributesForAdditionalLines = attributes.clone();
attributesForAdditionalLines.setErrorStripeColor(null);
for (TextRange range: additionalRanges) {
addTodoItem(startOffset, endOffset, holder, attributesForAdditionalLines, description, tooltip, range);
addTodoItem(holder, attributesForAdditionalLines, description, tooltip, range);
}
}
}
@@ -107,14 +105,11 @@ final class TodoHighlightVisitor implements HighlightVisitor {
return joiner.toString();
}
private static void addTodoItem(int restrictStartOffset,
int restrictEndOffset,
@NotNull HighlightInfoHolder holder,
private static void addTodoItem(@NotNull HighlightInfoHolder holder,
@NotNull TextAttributes attributes,
@NotNull @NlsContexts.DetailedDescription String description,
@NotNull @NlsContexts.Tooltip String tooltip,
@NotNull TextRange range) {
if (range.getStartOffset() >= restrictEndOffset || range.getEndOffset() <= restrictStartOffset) return;
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.TODO)
.range(range)
.textAttributes(attributes)

View File

@@ -746,6 +746,3 @@ c:com.intellij.psi.impl.search.PsiTodoSearchHelperImpl
- getTodoItemsCount(com.intellij.psi.PsiFile,com.intellij.psi.search.TodoPattern):I
- processFilesWithTodoItems(com.intellij.util.Processor):Z
- shouldHighlightInEditor(com.intellij.psi.PsiFile):Z
c:com.intellij.psi.impl.search.TodoItemsCreator
- <init>():V
- createTodo(com.intellij.psi.search.IndexPatternOccurrence):com.intellij.psi.search.TodoItem

View File

@@ -3,7 +3,6 @@ package com.intellij.psi.impl.search;
import com.intellij.ide.todo.TodoConfiguration;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.cache.TodoCacheManager;
@@ -17,7 +16,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.*;
public class PsiTodoSearchHelperImpl implements PsiTodoSearchHelper {
private static final TodoItem[] EMPTY_TODO_ITEMS = new TodoItem[0];
private final Project myProject;
@@ -27,7 +25,7 @@ public class PsiTodoSearchHelperImpl implements PsiTodoSearchHelper {
@Override
public PsiFile @NotNull [] findFilesWithTodoItems() {
HashSet<PsiFile> files = new HashSet<>();
Set<PsiFile> files = new HashSet<>();
processFilesWithTodoItems(new CommonProcessors.CollectProcessor<>(files));
return PsiUtilCore.toPsiFileArray(files);
}
@@ -44,33 +42,16 @@ public class PsiTodoSearchHelperImpl implements PsiTodoSearchHelper {
@Override
public TodoItem @NotNull [] findTodoItems(@NotNull PsiFile file, int startOffset, int endOffset) {
final Collection<IndexPatternOccurrence> occurrences = new ArrayList<>();
List<TodoItem> occurrences = new ArrayList<>();
TodoItemCreator todoItemCreator = new TodoItemCreator();
for (IndexPatternProvider provider : IndexPatternProvider.EP_NAME.getExtensionList()) {
IndexPatternSearch.search(file, provider, TodoConfiguration.getInstance().isMultiLine()).forEach(occurrences::add);
IndexPatternSearch.search(file, provider, TodoConfiguration.getInstance().isMultiLine()).forEach(occurrence -> {
if (occurrence.getTextRange().intersects(startOffset, endOffset)) {
occurrences.add(todoItemCreator.createTodo(occurrence));
}
});
}
if (occurrences.isEmpty()) {
return EMPTY_TODO_ITEMS;
}
return processTodoOccurences(startOffset, endOffset, occurrences);
}
private static TodoItem @NotNull [] processTodoOccurences(int startOffset,
int endOffset,
Collection<? extends 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.intersectsStrict(occurrenceRange) ||
occurrence.getAdditionalTextRanges().stream().anyMatch(r -> textRange.intersectsStrict(r))) {
items.add(todoItemsCreator.createTodo(occurrence));
}
}
return items.toArray(new TodoItem[0]);
return occurrences.isEmpty() ? TodoItem.EMPTY_ARRAY : occurrences.toArray(TodoItem.EMPTY_ARRAY);
}
@Override
@@ -80,18 +61,18 @@ public class PsiTodoSearchHelperImpl implements PsiTodoSearchHelper {
@Override
public TodoItem @NotNull [] findTodoItemsLight(@NotNull PsiFile file, int startOffset, int endOffset) {
final Collection<IndexPatternOccurrence> occurrences = new ArrayList<>();
Collection<TodoItem> occurrences = new ArrayList<>();
TodoItemCreator todoItemCreator = new TodoItemCreator();
for (IndexPatternProvider provider : IndexPatternProvider.EP_NAME.getExtensionList()) {
LightIndexPatternSearch.SEARCH.createQuery(
new IndexPatternSearch.SearchParameters(file, provider, TodoConfiguration.getInstance().isMultiLine())
).forEach(occurrences::add);
).forEach(occurrence -> {
if (occurrence.getTextRange().intersects(startOffset, endOffset)) {
occurrences.add(todoItemCreator.createTodo(occurrence));
}
});
}
if (occurrences.isEmpty()) {
return EMPTY_TODO_ITEMS;
}
return processTodoOccurences(startOffset, endOffset, occurrences);
return occurrences.isEmpty() ? TodoItem.EMPTY_ARRAY : occurrences.toArray(TodoItem.EMPTY_ARRAY);
}
@Override
@@ -103,7 +84,7 @@ public class PsiTodoSearchHelperImpl implements PsiTodoSearchHelper {
int total = 0;
for (IndexPatternProvider provider : IndexPatternProvider.EP_NAME.getExtensionList()) {
final int count = TodoCacheManager.getInstance(myProject).getTodoCount(virtualFile, provider);
int count = TodoCacheManager.getInstance(myProject).getTodoCount(virtualFile, provider);
if (count == -1) {
return findTodoItems(file).length;
}

View File

@@ -13,15 +13,16 @@ import org.jetbrains.annotations.Nullable;
/**
* moved from PsiSearchHelperImpl
*/
public class TodoItemsCreator {
class TodoItemCreator {
private final TodoPattern[] myTodoPatterns;
public TodoItemsCreator() {
TodoItemCreator() {
myTodoPatterns = TodoConfiguration.getInstance().getTodoPatterns();
}
public TodoItem createTodo(IndexPatternOccurrence occurrence) {
final TextRange occurrenceRange = occurrence.getTextRange();
@NotNull
TodoItem createTodo(@NotNull IndexPatternOccurrence occurrence) {
TextRange occurrenceRange = occurrence.getTextRange();
return new TodoItemImpl(occurrence.getFile(), occurrenceRange.getStartOffset(), occurrenceRange.getEndOffset(),
mapPattern(occurrence.getPattern()), occurrence.getAdditionalTextRanges());
}

View File

@@ -453,6 +453,7 @@ f:com.intellij.psi.search.TodoAttributes
- writeExternal(org.jdom.Element):V
com.intellij.psi.search.TodoItem
- sf:BY_START_OFFSET:java.util.Comparator
- sf:EMPTY_ARRAY:com.intellij.psi.search.TodoItem[]
- getAdditionalTextRanges():java.util.List
- a:getFile():com.intellij.psi.PsiFile
- a:getPattern():com.intellij.psi.search.TodoPattern

View File

@@ -11,6 +11,7 @@ import java.util.Comparator;
import java.util.List;
public interface TodoItem {
TodoItem[] EMPTY_ARRAY = new TodoItem[0];
@NotNull
PsiFile getFile();