-
Notifications
You must be signed in to change notification settings - Fork 44
/
CsvValidationInspection.java
177 lines (154 loc) · 7.1 KB
/
CsvValidationInspection.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package net.seesharpsoft.intellij.plugins.csv.inspection;
import com.intellij.codeInspection.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.TokenType;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.IncorrectOperationException;
import net.seesharpsoft.intellij.plugins.csv.CsvHelper;
import net.seesharpsoft.intellij.plugins.csv.CsvLanguage;
import net.seesharpsoft.intellij.plugins.csv.intention.CsvIntentionHelper;
import net.seesharpsoft.intellij.plugins.csv.psi.CsvTypes;
import net.seesharpsoft.intellij.plugins.csv.settings.CsvCodeStyleSettings;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* @author max
*/
public class CsvValidationInspection extends LocalInspectionTool {
private static final Logger LOG = Logger.getInstance("#net.seesharpsoft.intellij.plugins.csv.inspection.CsvSyntaxInspection");
private final LocalQuickFix fixUnescapedSequence = new UnescapedSequenceFix();
private final LocalQuickFix fixSeparatorMissing = new SeparatorMissingFix();
private final LocalQuickFix fixClosingQuoteMissing = new ClosingQuoteMissingFix();
@NonNls
private static final String UNESCAPED_SEQUENCE = "Unescaped sequence";
@NonNls
private static final String SEPARATOR_MISSING = "Separator missing";
@NonNls
private static final String CLOSING_QUOTE_MISSING = "Quote missing";
@NotNull
public String getDisplayName() {
return "Propose possible fixes";
}
@Nullable
public String getStaticDescription() {
return "Propose possible fixes to invalid syntax in CSV files.";
}
@NotNull
public String getGroupDisplayName() {
return "CSV";
}
@NotNull
public String getShortName() {
return "CsvValidation";
}
@Override
public boolean isEnabledByDefault() {
return true;
}
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new PsiElementVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element == null || !holder.getFile().getLanguage().isKindOf(CsvLanguage.INSTANCE)) {
return;
}
IElementType elementType = CsvHelper.getElementType(element);
PsiElement firstChild = element.getFirstChild();
PsiElement nextSibling = element.getNextSibling();
if (elementType == TokenType.ERROR_ELEMENT && CsvHelper.getElementType(firstChild) == TokenType.BAD_CHARACTER) {
CsvValidationInspection.this.registerError(holder, element, UNESCAPED_SEQUENCE, fixUnescapedSequence);
if (!"\"".equals(firstChild.getText())) {
CsvValidationInspection.this.registerError(holder, element, SEPARATOR_MISSING, fixSeparatorMissing);
}
} else if ((elementType == CsvTypes.TEXT || elementType == CsvTypes.ESCAPED_TEXT)
&& CsvHelper.getElementType(nextSibling) == TokenType.ERROR_ELEMENT
&& nextSibling.getFirstChild() == null) {
CsvValidationInspection.this.registerError(holder, element, CLOSING_QUOTE_MISSING, fixClosingQuoteMissing);
}
}
};
}
private boolean registerError(@NotNull final ProblemsHolder holder, @NotNull PsiElement element, @NotNull String descriptionTemplate, @Nullable LocalQuickFix fix) {
if (element != null && this.isSuppressedFor(element)) {
return false;
}
holder.registerProblem(element, descriptionTemplate, fix);
return true;
}
private static abstract class CsvLocalQuickFix implements LocalQuickFix {
@NotNull
public String getName() {
return this.getFamilyName();
}
}
private static class UnescapedSequenceFix extends CsvLocalQuickFix {
@NotNull
public String getFamilyName() {
return "Surround with quotes";
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
try {
PsiElement element = descriptor.getPsiElement();
Document document = PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile());
List<Integer> quotePositions = new ArrayList<>();
int quotePosition = CsvIntentionHelper.getOpeningQuotePosition(element);
if (quotePosition != -1) {
quotePositions.add(quotePosition);
}
PsiElement endSeparatorElement = CsvIntentionHelper.findQuotePositionsUntilSeparator(element, quotePositions);
if (endSeparatorElement == null) {
quotePositions.add(document.getTextLength());
} else {
quotePositions.add(endSeparatorElement.getTextOffset());
}
String text = CsvIntentionHelper.addQuotes(document.getText(), quotePositions);
document.setText(text);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
private static class SeparatorMissingFix extends CsvLocalQuickFix {
@NotNull
public String getFamilyName() {
return "Add separator";
}
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
try {
PsiElement element = descriptor.getPsiElement();
Document document = PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile());
String separator = CsvCodeStyleSettings.getCurrentSeparator(element.getProject(), element.getContainingFile().getLanguage());
String text = document.getText();
document.setText(text.substring(0, element.getTextOffset()) + separator + text.substring(element.getTextOffset()));
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
private static class ClosingQuoteMissingFix extends CsvLocalQuickFix {
@NotNull
public String getFamilyName() {
return "Add closing quote";
}
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
try {
PsiElement element = descriptor.getPsiElement();
Document document = PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile());
document.setText(document.getText() + "\"");
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
}