use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project android by JetBrains.
the class OverrideResourceAction method forkResourceFile.
private static void forkResourceFile(@NotNull Project project, @NotNull final ResourceFolderType folderType, @NotNull final VirtualFile file, @Nullable final XmlFile xmlFile, @Nullable String myNewFolder, @Nullable Configuration configuration, boolean open) {
final FolderConfiguration folderConfig;
if (myNewFolder == null) {
// Open a file chooser to select the configuration to be created
VirtualFile parentFolder = file.getParent();
assert parentFolder != null;
VirtualFile res = parentFolder.getParent();
folderConfig = selectFolderConfig(project, res, folderType);
} else {
folderConfig = FolderConfiguration.getConfigForFolder(myNewFolder);
}
if (folderConfig == null) {
return;
}
final Computable<Pair<String, VirtualFile>> computable = new Computable<Pair<String, VirtualFile>>() {
@Override
public Pair<String, VirtualFile> compute() {
String folderName = folderConfig.getFolderName(folderType);
try {
VirtualFile parentFolder = file.getParent();
assert parentFolder != null;
VirtualFile res = parentFolder.getParent();
VirtualFile newParentFolder = res.findChild(folderName);
if (newParentFolder == null) {
newParentFolder = res.createChildDirectory(this, folderName);
if (newParentFolder == null) {
String message = String.format("Could not create folder %1$s in %2$s", folderName, res.getPath());
return Pair.of(message, null);
}
}
final VirtualFile existing = newParentFolder.findChild(file.getName());
if (existing != null && existing.exists()) {
String message = String.format("File 'res/%1$s/%2$s' already exists!", folderName, file.getName());
return Pair.of(message, null);
}
// Attempt to get the document from the PSI file rather than the file on disk: get edited contents too
String text;
if (xmlFile != null) {
text = xmlFile.getText();
} else {
text = StreamUtil.readText(file.getInputStream(), "UTF-8");
}
VirtualFile newFile = newParentFolder.createChildData(this, file.getName());
VfsUtil.saveText(newFile, text);
return Pair.of(null, newFile);
} catch (IOException e2) {
String message = String.format("Failed to create File 'res/%1$s/%2$s' : %3$s", folderName, file.getName(), e2.getMessage());
return Pair.of(message, null);
}
}
};
WriteCommandAction<Pair<String, VirtualFile>> action = new WriteCommandAction<Pair<String, VirtualFile>>(project, "Add Resource") {
@Override
protected void run(@NotNull Result<Pair<String, VirtualFile>> result) throws Throwable {
result.setResult(computable.compute());
}
};
Pair<String, VirtualFile> result = action.execute().getResultObject();
String error = result.getFirst();
VirtualFile newFile = result.getSecond();
if (error != null) {
Messages.showErrorDialog(project, error, "Create Resource");
} else {
// First create a compatible configuration based on the current configuration
if (configuration != null) {
ConfigurationManager configurationManager = configuration.getConfigurationManager();
configurationManager.createSimilar(newFile, file);
}
if (open) {
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, newFile, -1);
FileEditorManager.getInstance(project).openEditor(descriptor, true);
}
}
}
use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project android by JetBrains.
the class PsiClassNavigation method navigate.
@Override
public void navigate(boolean requestFocus) {
OpenFileDescriptor fileDescriptor = myOffset >= 0 ? new OpenFileDescriptor(myPsiFile.getProject(), myPsiFile.getVirtualFile(), myOffset) : new OpenFileDescriptor(myPsiFile.getProject(), myPsiFile.getVirtualFile(), myLine, 0);
fileDescriptor.navigate(requestFocus);
}
use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project android by JetBrains.
the class ManifestPanel method goToDeclaration.
private void goToDeclaration(Node element) {
List<? extends Actions.Record> records = ManifestUtils.getRecords(myManifest, element);
for (Actions.Record record : records) {
SourceFilePosition sourceFilePosition = ManifestUtils.getActionLocation(myFacet.getModule(), record);
SourceFile sourceFile = sourceFilePosition.getFile();
if (!SourceFile.UNKNOWN.equals(sourceFile)) {
File ioFile = sourceFile.getSourceFile();
if (ioFile != null) {
VirtualFile file = LocalFileSystem.getInstance().findFileByIoFile(ioFile);
assert file != null;
int line = -1;
int column = 0;
SourcePosition sourcePosition = sourceFilePosition.getPosition();
if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
line = sourcePosition.getStartLine();
column = sourcePosition.getStartColumn();
}
Project project = myFacet.getModule().getProject();
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, line, column);
FileEditorManager.getInstance(project).openEditor(descriptor, true);
break;
}
}
}
}
use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project android by JetBrains.
the class HtmlLinkManager method openEditor.
private static boolean openEditor(@NotNull Project project, @NotNull VirtualFile file, int line, int column) {
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, line, column);
FileEditorManager manager = FileEditorManager.getInstance(project);
// Attempt to prefer text editor if it's available for this file
if (manager.openTextEditor(descriptor, true) != null) {
return true;
}
return !manager.openEditor(descriptor, true).isEmpty();
}
use of com.intellij.openapi.fileEditor.OpenFileDescriptor in project android by JetBrains.
the class HtmlLinkManager method handleOpenStackUrl.
private static void handleOpenStackUrl(@NotNull String url, @NotNull Module module) {
assert url.startsWith(URL_OPEN) : url;
// Syntax: URL_OPEN + className + '#' + methodName + ';' + fileName + ':' + lineNumber;
int start = URL_OPEN.length();
int semi = url.indexOf(';', start);
String className;
String fileName;
int line;
if (semi != -1) {
className = url.substring(start, semi);
int colon = url.indexOf(':', semi + 1);
if (colon != -1) {
fileName = url.substring(semi + 1, colon);
line = Integer.decode(url.substring(colon + 1));
} else {
fileName = url.substring(semi + 1);
line = -1;
}
// Attempt to open file
} else {
className = url.substring(start);
fileName = null;
line = -1;
}
String method = null;
int hash = className.indexOf('#');
if (hash != -1) {
method = className.substring(hash + 1);
className = className.substring(0, hash);
}
Project project = module.getProject();
PsiClass clz = JavaPsiFacade.getInstance(project).findClass(className, GlobalSearchScope.allScope(project));
if (clz != null) {
PsiFile containingFile = clz.getContainingFile();
if (fileName != null && containingFile != null && line != -1) {
VirtualFile virtualFile = containingFile.getVirtualFile();
if (virtualFile != null) {
String name = virtualFile.getName();
if (fileName.equals(name)) {
// Use the line number rather than the methodName
openEditor(project, containingFile, line - 1, -1);
return;
}
}
}
if (method != null) {
PsiMethod[] methodsByName = clz.findMethodsByName(method, true);
for (PsiMethod m : methodsByName) {
PsiFile psiFile = m.getContainingFile();
if (psiFile != null) {
VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile != null) {
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile, m.getTextOffset());
FileEditorManager.getInstance(project).openEditor(descriptor, true);
return;
}
}
}
}
if (fileName != null) {
PsiFile[] files = FilenameIndex.getFilesByName(project, fileName, GlobalSearchScope.allScope(project));
for (PsiFile psiFile : files) {
if (openEditor(project, psiFile, line != -1 ? line - 1 : -1, -1)) {
break;
}
}
}
}
}
Aggregations