99import com .intellij .openapi .util .Key ;
1010import com .intellij .openapi .util .TextRange ;
1111import com .intellij .psi .PsiElement ;
12+ import com .intellij .psi .tree .IElementType ;
1213import com .intellij .xml .util .XmlStringUtil ;
1314import net .seesharpsoft .intellij .plugins .csv .CsvColumnInfo ;
1415import net .seesharpsoft .intellij .plugins .csv .CsvHelper ;
16+ import net .seesharpsoft .intellij .plugins .csv .editor .CsvEditorSettingsExternalizable ;
1517import net .seesharpsoft .intellij .plugins .csv .psi .CsvFile ;
1618import net .seesharpsoft .intellij .plugins .csv .psi .CsvTypes ;
19+ import net .seesharpsoft .intellij .plugins .csv .settings .CsvCodeStyleSettings ;
1720import org .jetbrains .annotations .NotNull ;
1821
1922import java .awt .*;
@@ -27,12 +30,16 @@ public class CsvAnnotator implements Annotator {
2730
2831 protected static final Integer MAX_COLUMN_HIGHLIGHT_COLORS = 10 ;
2932 protected static final Key <Integer > MAX_NO_OF_DEFINED_COLUMN_HIGHLIGHT_COLORS = Key .create ("CSV_LAST_DEFINED_COLOR_INDEX_KEY" );
30-
33+ protected static final Key <TextAttributes > TAB_SEPARATOR_HIGHLIGHT_COLOR = Key .create ("CSV_TAB_SEPARATOR_HIGHLIGHT_COLOR" );
34+ protected static final Key <Boolean > TAB_SEPARATOR_HIGHLIGHT_COLOR_DETERMINED = Key .create ("CSV_TAB_SEPARATOR_HIGHLIGHT_COLOR_DETERMINED" );
35+
3136 public static final ColorDescriptor [] COLOR_DESCRIPTORS ;
37+
3238 static {
3339 List <ColorDescriptor > colorDescriptorList = new ArrayList ();
3440 for (int i = 0 ; i < MAX_COLUMN_HIGHLIGHT_COLORS ; ++i ) {
35- colorDescriptorList .add (new ColorDescriptor (String .format ("Column Highlighting Color %d" , i + 1 ), ColorKey .createColorKey (String .format ("CSV_COLUMN_COLOR_%d" , i + 1 ), (Color ) null ), ColorDescriptor .Kind .BACKGROUND ));
41+ colorDescriptorList .add (new ColorDescriptor (String .format ("Column Highlighting Color %d" , i + 1 ),
42+ ColorKey .createColorKey (String .format ("CSV_COLUMN_COLOR_%d" , i + 1 ), (Color ) null ), ColorDescriptor .Kind .BACKGROUND ));
3643 }
3744 COLOR_DESCRIPTORS = colorDescriptorList .toArray (new ColorDescriptor [MAX_COLUMN_HIGHLIGHT_COLORS ]);
3845 }
@@ -42,11 +49,16 @@ public class CsvAnnotator implements Annotator {
4249
4350 @ Override
4451 public void annotate (@ NotNull final PsiElement element , @ NotNull final AnnotationHolder holder ) {
45- if (CsvHelper .getElementType (element ) != CsvTypes .FIELD || !(element .getContainingFile () instanceof CsvFile )) {
52+ IElementType elementType = CsvHelper .getElementType (element );
53+ if ((elementType != CsvTypes .FIELD && elementType != CsvTypes .COMMA ) || !(element .getContainingFile () instanceof CsvFile )) {
4654 return ;
4755 }
4856
4957 CsvFile csvFile = (CsvFile ) element .getContainingFile ();
58+ if (handleSeparatorElement (element , holder , elementType , csvFile )) {
59+ return ;
60+ }
61+
5062 CsvColumnInfo <PsiElement > columnInfo = csvFile .getMyColumnInfoMap ().getColumnInfo (element );
5163
5264 if (columnInfo != null ) {
@@ -70,14 +82,39 @@ public void annotate(@NotNull final PsiElement element, @NotNull final Annotatio
7082 }
7183 }
7284
85+ protected boolean handleSeparatorElement (@ NotNull PsiElement element , @ NotNull AnnotationHolder holder , IElementType elementType , CsvFile csvFile ) {
86+ if (elementType == CsvTypes .COMMA ) {
87+ TextAttributes textAttributes = holder .getCurrentAnnotationSession ().getUserData (TAB_SEPARATOR_HIGHLIGHT_COLOR );
88+ if (!Boolean .TRUE .equals (holder .getCurrentAnnotationSession ().getUserData (TAB_SEPARATOR_HIGHLIGHT_COLOR_DETERMINED ))) {
89+ String separator = CsvCodeStyleSettings .getCurrentSeparator (csvFile .getProject (), csvFile .getLanguage ());
90+ if (CsvEditorSettingsExternalizable .getInstance ().isHighlightTabSeparator () && separator .equals (CsvCodeStyleSettings .TAB_SEPARATOR )) {
91+ textAttributes = new TextAttributes (null ,
92+ CsvEditorSettingsExternalizable .getInstance ().getTabHighlightColor (),
93+ null , null , 0 );
94+ holder .getCurrentAnnotationSession ().putUserData (TAB_SEPARATOR_HIGHLIGHT_COLOR , textAttributes );
95+ holder .getCurrentAnnotationSession ().putUserData (TAB_SEPARATOR_HIGHLIGHT_COLOR_DETERMINED , Boolean .TRUE );
96+ }
97+ }
98+ if (textAttributes != null ) {
99+ Annotation annotation = holder .createAnnotation (CSV_COLUMN_INFO_SEVERITY , element .getTextRange (), "<TAB>" );
100+ annotation .setEnforcedTextAttributes (textAttributes );
101+ annotation .setNeedsUpdateOnTyping (false );
102+ }
103+ return true ;
104+ }
105+ return false ;
106+ }
107+
73108 protected TextAttributes getTextAttributes (AnnotationSession annotationSession , CsvColumnInfo <PsiElement > columnInfo ) {
74109 EditorColorsScheme editorColorsScheme = EditorColorsManager .getInstance ().getGlobalScheme ();
75110 Integer maxNoOfDefinedColumnHighlightColors = annotationSession .getUserData (MAX_NO_OF_DEFINED_COLUMN_HIGHLIGHT_COLORS );
76111 if (maxNoOfDefinedColumnHighlightColors == null ) {
77112 maxNoOfDefinedColumnHighlightColors = 0 ;
78- for (int colorDescriptorIndex = 0 ; colorDescriptorIndex < COLOR_DESCRIPTORS .length ; ++colorDescriptorIndex ) {
79- if (editorColorsScheme .getColor (COLOR_DESCRIPTORS [colorDescriptorIndex ].getKey ()) != null ) {
80- maxNoOfDefinedColumnHighlightColors = colorDescriptorIndex + 1 ;
113+ if (CsvEditorSettingsExternalizable .getInstance ().isColumnHighlightingEnabled ()) {
114+ for (int colorDescriptorIndex = 0 ; colorDescriptorIndex < COLOR_DESCRIPTORS .length ; ++colorDescriptorIndex ) {
115+ if (editorColorsScheme .getColor (COLOR_DESCRIPTORS [colorDescriptorIndex ].getKey ()) != null ) {
116+ maxNoOfDefinedColumnHighlightColors = colorDescriptorIndex + 1 ;
117+ }
81118 }
82119 }
83120 annotationSession .putUserData (MAX_NO_OF_DEFINED_COLUMN_HIGHLIGHT_COLORS , maxNoOfDefinedColumnHighlightColors );
0 commit comments