Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added a file view provider for LSP files that yields fake PSI elements for words. #703

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright (c) 2020 Red Hat, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2024

* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/

package com.redhat.devtools.lsp4ij.features.documentSymbol;

import com.intellij.lang.Language;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.features.LSPPsiElement;
import org.jetbrains.annotations.NotNull;

/**
* File view provider factory for LSP files that yields simple PSI elements for the word under the caret. This helps
* with a number of plugin SDK EPs that need to find distinct elements.
*/
public class LSPDocumentFileViewProviderFactory implements FileViewProviderFactory {

@Override
@NotNull
public FileViewProvider createFileViewProvider(@NotNull VirtualFile virtualFile,
Language language,
@NotNull PsiManager manager,
boolean eventSystemEnabled) {
Project project = manager.getProject();
if (!project.isDisposed() && !virtualFile.isDirectory() && virtualFile.isValid()) {
// NOTE: We can't filter by LSP here, so we just have to make sure that this view provider behaves as the
// standard view provider for non-LSP files
return new LSPDocumentFileViewProvider(manager, virtualFile, eventSystemEnabled);
}

return new SingleRootFileViewProvider(manager, virtualFile, eventSystemEnabled);
}

private static class LSPDocumentFileViewProvider extends SingleRootFileViewProvider {
public LSPDocumentFileViewProvider(@NotNull PsiManager manager,
@NotNull VirtualFile virtualFile,
boolean eventSystemEnabled) {
super(manager, virtualFile, eventSystemEnabled);
}

@Override
public PsiElement findElementAt(int offset) {
VirtualFile virtualFile = getVirtualFile();
Project project = getManager().getProject();
PsiFile file = LSPIJUtils.getPsiFile(virtualFile, project);
// TODO: How can we quickly and simply check whether this is an LSP4IJ-managed file?
if (file != null) {
Document document = LSPIJUtils.getDocument(file);
if (document != null) {
TextRange wordTextRange = LSPIJUtils.getWordRangeAt(document, file, offset);
if (wordTextRange != null) {
return new LSPPsiElement(file, wordTextRange) {
@Override
// NOTE: We have to report this as physical even though it's not
public boolean isPhysical() {
return true;
}
};
}
}
}

return super.findElementAt(offset);
}
}
}
8 changes: 8 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ L
id="LSPDocumentLinkGotoDeclarationHandler"
implementation="com.redhat.devtools.lsp4ij.features.documentLink.LSPDocumentLinkGotoDeclarationHandler"/>

<!-- LSP file view provider to yield some PSI structure to otherwise flat files -->
<lang.fileViewProviderFactory
language="TEXT"
implementationClass="com.redhat.devtools.lsp4ij.features.documentSymbol.LSPDocumentFileViewProviderFactory"/>
<lang.fileViewProviderFactory
language="textmate"
implementationClass="com.redhat.devtools.lsp4ij.features.documentSymbol.LSPDocumentFileViewProviderFactory"/>

<!-- LSP textDocument/documentSymbol request support -->
<lang.psiStructureViewFactory
id="LSPDocumentSymbolStructureViewFactoryForText"
Expand Down
Loading