Skip to content

Commit 91f9049

Browse files
authored
Merge pull request #915 from coleleavitt/fix/psi-access-threading
Fix PSI access threading issues in CsvTableModelSwing
2 parents 6c18d0d + 5f59e72 commit 91f9049

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/table/swing/CsvTableModelSwing.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.seesharpsoft.intellij.plugins.csv.editor.table.swing;
22

33
import com.intellij.openapi.application.ApplicationManager;
4+
import com.intellij.openapi.util.Computable;
45
import com.intellij.psi.PsiElement;
56
import net.seesharpsoft.intellij.plugins.csv.CsvHelper;
67
import net.seesharpsoft.intellij.plugins.csv.editor.table.CsvTableEditor;
@@ -30,7 +31,7 @@ public class CsvTableModelSwing extends CsvTableModelBase<CsvTableEditor> implem
3031
*/
3132
protected EventListenerList listenerList = new EventListenerList();
3233

33-
protected ScheduledFuture delayedUpdate;
34+
protected ScheduledFuture<?> delayedUpdate;
3435

3536
protected ScheduledExecutorService executorService;
3637

@@ -95,19 +96,23 @@ public void removeTableModelListener(TableModelListener l) {
9596
listenerList.remove(TableModelListener.class, l);
9697
}
9798

99+
/**
100+
* Gets header name for a given column, with index formatting.
101+
* PSI access is wrapped in a read action for thread safety.
102+
*/
98103
@Override
99104
public String getColumnName(int column) {
100-
PsiElement headerField = PsiHelper.findFirst(getPsiFile(), CsvTypes.FIELD);
101-
if (headerField != null) {
102-
headerField = PsiHelper.getNextNthSiblingOfType(headerField, column, CsvField.class);
103-
}
104-
String headerText = headerField == null ? "" : CsvHelper.unquoteCsvValue(headerField.getText(), getEscapeCharacter()).trim();
105-
106-
Map<String, Object> params = new HashMap<>();
107-
params.put("header", headerText);
108-
params.put("index", CsvEditorSettings.getInstance().isZeroBasedColumnNumbering() ? column : column + 1);
109-
110-
return CsvHelper.formatString("${header} (${index})", params);
105+
return ApplicationManager.getApplication().runReadAction((Computable<String>) () -> {
106+
PsiElement headerField = PsiHelper.findFirst(getPsiFile(), CsvTypes.FIELD);
107+
if (headerField != null) {
108+
headerField = PsiHelper.getNextNthSiblingOfType(headerField, column, CsvField.class);
109+
}
110+
String headerText = headerField == null ? "" : CsvHelper.unquoteCsvValue(headerField.getText(), getEscapeCharacter()).trim();
111+
Map<String, Object> params = new HashMap<>();
112+
params.put("header", headerText);
113+
params.put("index", CsvEditorSettings.getInstance().isZeroBasedColumnNumbering() ? column : column + 1);
114+
return CsvHelper.formatString("${header} (${index})", params);
115+
});
111116
}
112117

113118
@Override
@@ -119,4 +124,4 @@ public Class<?> getColumnClass(int columnIndex) {
119124
public boolean isCellEditable(int rowIndex, int columnIndex) {
120125
return getPsiFileHolder().isEditable();
121126
}
122-
}
127+
}

0 commit comments

Comments
 (0)