Skip to content

Commit 6c0a1d1

Browse files
committed
FIX: Cannot invoke "com.intellij.openapi.editor.Document.insertString(int, java.lang.CharSequence)" because "document" is null #386
1 parent 7c031df commit 6c0a1d1

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/main/java/net/seesharpsoft/intellij/plugins/csv/inspection/CsvValidationInspection.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ public String getFamilyName() {
121121
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
122122
PsiElement element = descriptor.getPsiElement();
123123
Document document = PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile());
124-
List<Integer> quotePositions = new ArrayList<>();
124+
if (document == null) return;
125125

126+
List<Integer> quotePositions = new ArrayList<>();
126127
int quotePosition = CsvIntentionHelper.getOpeningQuotePosition(element);
127128
if (quotePosition != -1) {
128129
quotePositions.add(quotePosition);
@@ -147,6 +148,8 @@ public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descri
147148
try {
148149
PsiElement element = descriptor.getPsiElement();
149150
Document document = PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile());
151+
if (document == null) return;
152+
150153
CsvValueSeparator separator = CsvHelper.getValueSeparator(element.getContainingFile());
151154
String text = document.getText();
152155
document.setText(text.substring(0, element.getTextOffset()) + separator.getCharacter() + text.substring(element.getTextOffset()));
@@ -166,6 +169,8 @@ public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descri
166169
try {
167170
PsiElement element = descriptor.getPsiElement();
168171
Document document = PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile());
172+
if (document == null) return;
173+
169174
document.setText(document.getText() + "\"");
170175
} catch (IncorrectOperationException e) {
171176
LOG.error(e);

src/main/java/net/seesharpsoft/intellij/plugins/csv/intention/CsvIntentionHelper.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ public static Collection<PsiElement> getAllElements(PsiFile file) {
4747

4848
public static void quoteAll(@NotNull Project project, @NotNull PsiFile psiFile) {
4949
Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
50-
List<Integer> quotePositions = new ArrayList<>();
50+
if (document == null) return;
5151

52+
List<Integer> quotePositions = new ArrayList<>();
5253
PsiTreeUtil.processElements(psiFile, CsvField.class, field -> {
5354
if (PsiHelper.getElementType(field.getFirstChild()) != CsvTypes.QUOTE) {
5455
quotePositions.add(field.getTextRange().getStartOffset());
@@ -63,6 +64,8 @@ public static void quoteAll(@NotNull Project project, @NotNull PsiFile psiFile)
6364

6465
public static void quoteValue(@NotNull Project project, @NotNull final PsiElement field) {
6566
Document document = PsiDocumentManager.getInstance(project).getDocument(field.getContainingFile());
67+
if (document == null) return;
68+
6669
List<Integer> quotePositions = new ArrayList<>();
6770
if (PsiHelper.getElementType(field.getFirstChild()) != CsvTypes.QUOTE) {
6871
quotePositions.add(field.getTextRange().getStartOffset());
@@ -75,9 +78,9 @@ public static void quoteValue(@NotNull Project project, @NotNull final PsiElemen
7578

7679
public static void unquoteAll(@NotNull Project project, @NotNull PsiFile psiFile) {
7780
Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
81+
if (document == null) return;
7882

7983
final List<PsiElement> quotePositions = new ArrayList<>();
80-
8184
PsiTreeUtil.processElements(psiFile, CsvField.class, field -> {
8285
if (getChildren(field).stream().noneMatch(element -> PsiHelper.getElementType(element) == CsvTypes.ESCAPED_TEXT)) {
8386
Pair<PsiElement, PsiElement> positions = getQuotePositions(field);
@@ -115,7 +118,7 @@ private static Pair<PsiElement, PsiElement> getQuotePositions(PsiElement element
115118
return null;
116119
}
117120

118-
public static void addQuotes(final Document document, List<Integer> quotePositions) {
121+
public static void addQuotes(@NotNull final Document document, List<Integer> quotePositions) {
119122
int offset = 0;
120123
String quote = "\"";
121124
quotePositions.sort(Integer::compareTo);

0 commit comments

Comments
 (0)