From d8072fbc492a97dcbc7a90c139bba089eb08ee11 Mon Sep 17 00:00:00 2001 From: azerr Date: Tue, 23 Jun 2026 12:19:18 +0200 Subject: [PATCH] fix: Read access is allowed from inside read-action only in LSPUsageSearcher Fixes #1605 Signed-off-by: azerr --- .../lsp4ij/usages/LSPUsageSearcher.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/redhat/devtools/lsp4ij/usages/LSPUsageSearcher.java b/src/main/java/com/redhat/devtools/lsp4ij/usages/LSPUsageSearcher.java index 75b1f8237..1c05c4fc5 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/usages/LSPUsageSearcher.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/usages/LSPUsageSearcher.java @@ -77,9 +77,6 @@ public class LSPUsageSearcher extends CustomUsageSearcher { private static final Logger LOGGER = LoggerFactory.getLogger(LSPUsageSearcher.class); - record ElementContext(Project project, Position position, PsiFile file) { - } - /** * Processes all usages of the given element by querying the language server. *

@@ -113,17 +110,26 @@ record ElementContext(Project project, Position position, PsiFile file) { @Override public void processElementUsages(@NotNull PsiElement element, @NotNull Processor processor, @NotNull FindUsagesOptions options) { // 1. Collect only what we need from the element inside a narrow ReadAction - ElementContext ctx = runCancellableReadAction(() -> { + record ElementData(Project project, Position position, PsiFile file, int textOffset) {} + ElementData data = runCancellableReadAction(() -> { PsiFile file = element.getContainingFile(); - return new ElementContext(file.getProject(), getPosition(element, file), file); + if (file == null) { + return null; + } + Position position = getPosition(element, file); + if (position == null) { + return null; + } + return new ElementData(element.getProject(), position, file, element.getTextOffset()); }, ApplicationManager.getApplication()); - Project project = ctx.project(); - Position position = ctx.position(); - PsiFile file = ctx.file(); - - if (position == null || file == null) { + + if (data == null) { return; } + + Project project = data.project(); + PsiFile file = data.file(); + Position position = data.position(); if (!isUsageSupportedByLanguageServer(file)) { return; } @@ -200,7 +206,7 @@ public void processElementUsages(@NotNull PsiElement element, @NotNull Processor LSPExternalReferencesFinder.processExternalReferences( file, - element.getTextOffset(), + data.textOffset(), searchScope, reference -> processor.process(new UsageInfo2UsageAdapter(new UsageInfo(reference))) );