-
Notifications
You must be signed in to change notification settings - Fork 43
/
CsvHighlightUsagesHandler.java
70 lines (59 loc) · 2.65 KB
/
CsvHighlightUsagesHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package net.seesharpsoft.intellij.plugins.csv.highlighter;
import com.intellij.codeInsight.highlighting.HighlightUsagesHandlerBase;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.util.Consumer;
import net.seesharpsoft.intellij.plugins.csv.CsvColumnInfo;
import net.seesharpsoft.intellij.plugins.csv.CsvColumnInfoMap;
import net.seesharpsoft.intellij.plugins.csv.CsvHelper;
import net.seesharpsoft.intellij.plugins.csv.psi.CsvFile;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class CsvHighlightUsagesHandler extends HighlightUsagesHandlerBase<PsiElement> {
protected CsvHighlightUsagesHandler(@NotNull Editor editor, @NotNull CsvFile file) {
super(editor, file);
}
protected CsvFile getCsvFile() {
return (CsvFile)this.myFile;
}
@Override
public List<PsiElement> getTargets() {
Caret primaryCaret = this.myEditor.getCaretModel().getPrimaryCaret();
PsiElement myFocusedElement = this.myFile.getViewProvider().findElementAt(primaryCaret.getOffset());
if (myFocusedElement == null) {
myFocusedElement = this.myFile.getLastChild();
}
myFocusedElement = CsvHelper.getParentFieldElement(myFocusedElement);
if (myFocusedElement == null) {
return Collections.emptyList();
}
CsvColumnInfo<PsiElement> columnInfo = getCsvFile().getMyColumnInfoMap().getColumnInfo(myFocusedElement);
return columnInfo == null ? Collections.emptyList() : Collections.unmodifiableList(columnInfo.getElements());
}
@Override
protected void selectTargets(List<PsiElement> list, Consumer<List<PsiElement>> consumer) {
consumer.consume(list);
}
@Override
public void computeUsages(List<PsiElement> list) {
CsvColumnInfoMap<PsiElement> columnInfoMap = getCsvFile().getMyColumnInfoMap();
list.forEach(element -> {
if (element != null && !element.getText().isEmpty()) { this.addOccurrence(columnInfoMap.getRowInfo(element)); }
});
}
protected void addOccurrence(CsvColumnInfo<PsiElement>.RowInfo rowInfo) {
if (rowInfo == null) {
return;
}
TextRange range = rowInfo.getTextRange();
if (range != null) {
PsiElement element = rowInfo.getElement();
range = InjectedLanguageManager.getInstance(element.getProject()).injectedToHost(element, range);
this.myReadUsages.add(range);
}
}
}