Skip to content

Commit

Permalink
fix: remove usage of VfsUtil.findFileByURL
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon authored and angelozerr committed May 12, 2023
1 parent f126735 commit 98a3901
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDocumentManager;
Expand All @@ -45,7 +44,6 @@

import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.util.Scanner;

/**
Expand Down Expand Up @@ -129,8 +127,7 @@ public Location toLocation(PsiElement psiMember) {

@Override
public VirtualFile findFile(String uri) throws IOException {
//return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(Paths.get(new URI(uri)).toFile());
return VirtualFileManager.getInstance().findFileByUrl(VfsUtilCore.fixURLforIDEA(uri));
return LSPIJUtils.findResourceFor(uri);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.*;
import com.intellij.psi.PsiElement;
import org.apache.commons.lang.StringUtils;
import org.eclipse.lsp4j.CompletionParams;
Expand Down Expand Up @@ -160,29 +157,25 @@ public static void applyWorkspaceEdit(WorkspaceEdit edit, String label) {
} else if (change.isRight()) {
ResourceOperation resourceOperation = change.getRight();
if (resourceOperation instanceof CreateFile) {
try {
CreateFile createOperation = (CreateFile) resourceOperation;
URI targetURI = URI.create(createOperation.getUri());
VirtualFile targetFile = VfsUtil.findFileByURL(targetURI.toURL());
if (targetFile != null && createOperation.getOptions() != null) {
if (!createOperation.getOptions().getIgnoreIfExists()) {
Document document = getDocument(targetFile);
if (document != null) {
TextEdit textEdit = new TextEdit(new Range(toPosition(0, document), toPosition(document.getTextLength(), document)), "");
applyWorkspaceEdit(document, Collections.singletonList(textEdit));
}
}
} else {
try {
File f = new File(targetURI);
f.createNewFile();
VfsUtil.findFileByIoFile(f, true);
} catch (IOException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
CreateFile createOperation = (CreateFile) resourceOperation;
VirtualFile targetFile = findResourceFor(createOperation.getUri());
if (targetFile != null && createOperation.getOptions() != null) {
if (!createOperation.getOptions().getIgnoreIfExists()) {
Document document = getDocument(targetFile);
if (document != null) {
TextEdit textEdit = new TextEdit(new Range(toPosition(0, document), toPosition(document.getTextLength(), document)), "");
applyWorkspaceEdit(document, Collections.singletonList(textEdit));
}
}
} catch (MalformedURLException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
} else {
try {
URI targetURI = URI.create(createOperation.getUri());
File f = new File(targetURI);
f.createNewFile();
VfsUtil.findFileByIoFile(f, true);
} catch (IOException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
}
}
} else if (resourceOperation instanceof DeleteFile) {
try {
Expand Down Expand Up @@ -248,11 +241,7 @@ public static VirtualFile findResourceFor(URI uri) {
}

public static VirtualFile findResourceFor(String uri) {
try {
return VfsUtil.findFileByURL(new URL(uri));
} catch (MalformedURLException e) {
return null;
}
return VirtualFileManager.getInstance().findFileByUrl(VfsUtilCore.fixURLforIDEA(uri));
}

public static Editor[] editorsForFile(VirtualFile file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public boolean handleExternal(PsiElement element, PsiElement originalElement) {

@Override
public boolean handleExternalLink(PsiManager psiManager, String link, PsiElement context) {
VirtualFile file = getFile(link);
VirtualFile file = LSPIJUtils.findResourceFor(link);
if (file != null) {
FileEditorManager.getInstance(psiManager.getProject()).openFile(file, true, true);
return true;
Expand All @@ -276,14 +276,6 @@ public boolean canFetchDocumentationLink(String link) {
return false;
}

private VirtualFile getFile(String link) {
try {
return VfsUtil.findFileByURL(new URL(link));
} catch (MalformedURLException e) {
return null;
}
}

@Override
public @NotNull String fetchExternalDocumentation(@NotNull String link, @Nullable PsiElement element) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.IPsiUtils;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.core.ls.PsiUtilsLSImpl;
import com.redhat.devtools.intellij.quarkus.lsp4ij.LSPIJUtils;
import com.redhat.devtools.intellij.quarkus.lsp4ij.operations.codelens.LSPCodelensInlayProvider;
Expand All @@ -27,6 +29,7 @@
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Location;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
Expand All @@ -46,18 +49,20 @@ public void actionPerformed(AnActionEvent e) {
Command command = e.getData(LSPCodelensInlayProvider.LSP_COMMAND);
QuteJavaDefinitionParams params = getQuteJavaDefinitionParams(command.getArguments());
if (params != null) {
Location location = QuteSupportForTemplate.getInstance().getJavaDefinition(params, PsiUtilsLSImpl.getInstance(e.getProject()), new EmptyProgressIndicator());
VirtualFile f = VfsUtil.findFileByURL(new URL(location.getUri()));
Project project = e.getProject();
IPsiUtils utils = PsiUtilsLSImpl.getInstance(project);
Location location = QuteSupportForTemplate.getInstance().getJavaDefinition(params, utils, new EmptyProgressIndicator());
VirtualFile f = (location == null) ? null : utils.findFile(location.getUri());
if (f != null) {
Document document = FileDocumentManager.getInstance().getDocument(f);
if (document != null) {
OpenFileDescriptor desc = new OpenFileDescriptor(e.getProject(), f, LSPIJUtils.toOffset(location.getRange().getStart(), document));
FileEditorManager.getInstance(e.getProject()).openTextEditor(desc, true);
OpenFileDescriptor desc = new OpenFileDescriptor(project, f, LSPIJUtils.toOffset(location.getRange().getStart(), document));
FileEditorManager.getInstance(project).openTextEditor(desc, true);
}
}

}
} catch (MalformedURLException ex) {
} catch (IOException ex) {
LOGGER.log(System.Logger.Level.WARNING, ex.getLocalizedMessage(), ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,30 @@

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.core.ls.PsiUtilsLSImpl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class QuteOpenURIAction extends QuteAction {
private static System.Logger LOGGER = System.getLogger(QuteOpenURIAction.class.getName());
private static final System.Logger LOGGER = System.getLogger(QuteOpenURIAction.class.getName());

@Override
public void actionPerformed(AnActionEvent e) {
try {
String url = getURL(e);
if (url != null) {
VirtualFile f = VfsUtil.findFileByURL(new URL(url));
Project project = e.getProject();
if (url != null && project != null) {
VirtualFile f = PsiUtilsLSImpl.getInstance(project).findFile(url);
if (f != null) {
FileEditorManager.getInstance(e.getProject()).openFile(f, true);
FileEditorManager.getInstance(project).openFile(f, true);
}
}
} catch (MalformedURLException ex) {
} catch (IOException ex) {
LOGGER.log(System.Logger.Level.WARNING, ex.getLocalizedMessage(), ex);
}
}
Expand Down

0 comments on commit 98a3901

Please sign in to comment.