Skip to content

Commit

Permalink
Issue 708 - Fresh branch due to merge conflicts.
Browse files Browse the repository at this point in the history
  • Loading branch information
SCWells72 authored and angelozerr committed Dec 17, 2024
1 parent 1783e36 commit 1200518
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.intellij.psi.PsiReference;
import com.intellij.psi.impl.source.resolve.reference.PsiReferenceUtil;
import com.intellij.psi.search.PsiSearchHelper;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.UsageSearchContext;
import com.intellij.util.Processor;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
Expand All @@ -48,25 +49,41 @@ private LSPExternalReferencesFinder() {
/**
* Processes all external references for the LSP4IJ element at the offset in the specified file.
*
* @param file the file for which the element at the specified offset should be processed for external references
* @param offset the offset of the element in the file
* @param processor the external reference processor
* @param file the file for which the element at the specified offset should be processed for external references
* @param offset the offset of the element in the file
* @param processor the external reference processor
*/
public static void processExternalReferences(@NotNull PsiFile file,
int offset,
@NotNull Processor<PsiReference> processor) {
processExternalReferences(file, offset, ReadAction.compute(file::getUseScope), processor);
}

/**
* Processes all external references for the LSP4IJ element at the offset in the specified file.
*
* @param file the file for which the element at the specified offset should be processed for external references
* @param offset the offset of the element in the file
* @param searchScope the search scope
* @param processor the external reference processor
*/
public static void processExternalReferences(@NotNull PsiFile file,
int offset,
@NotNull SearchScope searchScope,
@NotNull Processor<PsiReference> processor) {
VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile != null) {
Document document = LSPIJUtils.getDocument(virtualFile);
TextRange wordTextRange = document != null ? LSPIJUtils.getWordRangeAt(document, file, offset) : null;
if (wordTextRange != null) {
LSPPsiElement wordElement = new LSPPsiElement(file, wordTextRange);
String wordText = wordElement.getText();
String wordText = ReadAction.compute(wordElement::getText);
if (StringUtil.isNotEmpty(wordText)) {
processExternalReferences(
file,
wordText,
wordTextRange,
searchScope,
ProgressManager.getInstance().getProgressIndicator(),
processor
);
Expand All @@ -78,6 +95,7 @@ public static void processExternalReferences(@NotNull PsiFile file,
private static void processExternalReferences(@NotNull PsiFile file,
@NotNull String wordText,
@NotNull TextRange wordTextRange,
@NotNull SearchScope searchScope,
@Nullable ProgressIndicator progressIndicator,
@NotNull Processor<PsiReference> processor) {
VirtualFile virtualFile = file.getVirtualFile();
Expand All @@ -95,7 +113,7 @@ private static void processExternalReferences(@NotNull PsiFile file,
}

Set<String> externalReferenceKeys = new HashSet<>();
PsiSearchHelper.getInstance(project).processElementsWithWord(
ReadAction.run(() -> PsiSearchHelper.getInstance(project).processElementsWithWord(
(element, offsetInElement) -> {
PsiReference originalReference = element.findReferenceAt(offsetInElement);
List<PsiReference> references = originalReference != null ?
Expand Down Expand Up @@ -132,11 +150,11 @@ private static void processExternalReferences(@NotNull PsiFile file,
}
return true;
},
ReadAction.compute(file::getUseScope),
searchScope,
wordText,
UsageSearchContext.ANY,
caseSensitive
);
));
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
Expand All @@ -24,6 +25,7 @@
import com.intellij.usages.UsageInfo2UsageAdapter;
import com.intellij.util.Processor;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor;
import org.eclipse.lsp4j.Position;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -53,11 +55,30 @@ public class LSPUsageSearcher extends CustomUsageSearcher {

@Override
public void processElementUsages(@NotNull PsiElement element, @NotNull Processor<? super Usage> processor, @NotNull FindUsagesOptions options) {

PsiFile file = element.getContainingFile();
if (file == null) {
return;
}

VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null) {
return;
}

Project project = element.getProject();
if (!LanguageServiceAccessor.getInstance(project).hasAny(
virtualFile,
l -> l.getClientFeatures().getUsageFeature().isSupported(file)
)) {
return;
}

if (element instanceof LSPUsageTriggeredPsiElement elt) {
if (elt.getLSPReferences() != null) {
elt.getLSPReferences()
.forEach(ref -> {
var psiElement = LSPUsagesManager.toPsiElement(ref.location(), ref.languageServer().getClientFeatures(), LSPUsagePsiElement.UsageKind.references, element.getProject());
var psiElement = LSPUsagesManager.toPsiElement(ref.location(), ref.languageServer().getClientFeatures(), LSPUsagePsiElement.UsageKind.references, project);
if (psiElement != null) {
processor.process(ReadAction.compute(() -> new UsageInfo2UsageAdapter(new UsageInfo(psiElement))));
}
Expand All @@ -66,10 +87,6 @@ public void processElementUsages(@NotNull PsiElement element, @NotNull Processor
}
}

PsiFile file = element.getContainingFile();
if (file == null) {
return;
}
// Get position where the "Find Usages" has been triggered
Position position = getPosition(element, file);
if (position == null) {
Expand Down Expand Up @@ -98,7 +115,12 @@ public void processElementUsages(@NotNull PsiElement element, @NotNull Processor
}

// For completeness' sake, also collect external usages to LSP (pseudo-)elements
LSPExternalReferencesFinder.processExternalReferences(file, element.getTextOffset(), reference -> processor.process(new UsageInfo2UsageAdapter(new UsageInfo(reference))));
LSPExternalReferencesFinder.processExternalReferences(
file,
ReadAction.compute(element::getTextOffset),
options.searchScope,
reference -> processor.process(ReadAction.compute(() -> new UsageInfo2UsageAdapter(new UsageInfo(reference))))
);
}

@Nullable
Expand Down

0 comments on commit 1200518

Please sign in to comment.