use of com.intellij.injected.editor.VirtualFileWindow in project intellij-community by JetBrains.
the class FileEditorManagerImpl method openEditor.
@Override
@NotNull
public List<FileEditor> openEditor(@NotNull final OpenFileDescriptor descriptor, final boolean focusEditor) {
assertDispatchThread();
if (descriptor.getFile() instanceof VirtualFileWindow) {
VirtualFileWindow delegate = (VirtualFileWindow) descriptor.getFile();
int hostOffset = delegate.getDocumentWindow().injectedToHost(descriptor.getOffset());
OpenFileDescriptor realDescriptor = new OpenFileDescriptor(descriptor.getProject(), delegate.getDelegate(), hostOffset);
realDescriptor.setUseCurrentWindow(descriptor.isUseCurrentWindow());
return openEditor(realDescriptor, focusEditor);
}
final List<FileEditor> result = new SmartList<>();
CommandProcessor.getInstance().executeCommand(myProject, () -> {
VirtualFile file = descriptor.getFile();
final FileEditor[] editors = openFile(file, focusEditor, !descriptor.isUseCurrentWindow());
ContainerUtil.addAll(result, editors);
boolean navigated = false;
for (final FileEditor editor : editors) {
if (editor instanceof NavigatableFileEditor && getSelectedEditor(descriptor.getFile()) == editor) {
// try to navigate opened editor
navigated = navigateAndSelectEditor((NavigatableFileEditor) editor, descriptor);
if (navigated)
break;
}
}
if (!navigated) {
for (final FileEditor editor : editors) {
if (editor instanceof NavigatableFileEditor && getSelectedEditor(descriptor.getFile()) != editor) {
// try other editors
if (navigateAndSelectEditor((NavigatableFileEditor) editor, descriptor)) {
break;
}
}
}
}
}, "", null);
return result;
}
use of com.intellij.injected.editor.VirtualFileWindow in project intellij-community by JetBrains.
the class DirectoryGroupingRule method groupUsage.
@Override
@Nullable
public UsageGroup groupUsage(@NotNull Usage usage) {
if (usage instanceof UsageInFile) {
UsageInFile usageInFile = (UsageInFile) usage;
VirtualFile file = usageInFile.getFile();
if (file != null) {
if (file instanceof VirtualFileWindow) {
file = ((VirtualFileWindow) file).getDelegate();
}
VirtualFile dir = file.getParent();
if (dir == null)
return null;
return getGroupForFile(dir);
}
}
return null;
}
use of com.intellij.injected.editor.VirtualFileWindow in project intellij-community by JetBrains.
the class JavaFxImportsOptimizer method processFile.
@NotNull
@Override
public Runnable processFile(final PsiFile file) {
VirtualFile vFile = file.getVirtualFile();
if (vFile instanceof VirtualFileWindow)
vFile = ((VirtualFileWindow) vFile).getDelegate();
final Project project = file.getProject();
if (vFile == null || !ProjectRootManager.getInstance(project).getFileIndex().isInSourceContent(vFile)) {
return EmptyRunnable.INSTANCE;
}
final List<Pair<String, Boolean>> names = new ArrayList<>();
final Set<String> demandedForNested = new HashSet<>();
collectNamesToImport(names, demandedForNested, (XmlFile) file);
Collections.sort(names, (o1, o2) -> StringUtil.compare(o1.first, o2.first, true));
final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(project);
final List<Pair<String, Boolean>> sortedNames = ImportHelper.sortItemsAccordingToSettings(names, settings);
final Map<String, Boolean> onDemand = new HashMap<>();
ImportHelper.collectOnDemandImports(sortedNames, settings, onDemand);
for (String s : demandedForNested) {
onDemand.put(s, false);
}
final Set<String> imported = new HashSet<>();
final List<String> imports = new ArrayList<>();
for (Pair<String, Boolean> pair : sortedNames) {
final String qName = pair.first;
final String packageName = StringUtil.getPackageName(qName);
if (imported.contains(packageName) || imported.contains(qName)) {
continue;
}
if (onDemand.containsKey(packageName)) {
imported.add(packageName);
imports.add("<?import " + packageName + ".*?>");
} else {
imported.add(qName);
imports.add("<?import " + qName + "?>");
}
}
final PsiFileFactory factory = PsiFileFactory.getInstance(file.getProject());
final XmlFile dummyFile = (XmlFile) factory.createFileFromText("_Dummy_.fxml", StdFileTypes.XML, StringUtil.join(imports, "\n"));
final XmlDocument document = dummyFile.getDocument();
final XmlProlog newImportList = document != null ? document.getProlog() : null;
if (newImportList == null)
return EmptyRunnable.getInstance();
return () -> {
final XmlDocument xmlDocument = ((XmlFile) file).getDocument();
final XmlProlog prolog = xmlDocument != null ? xmlDocument.getProlog() : null;
if (prolog != null) {
final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class);
for (final XmlProcessingInstruction instruction : instructions) {
final ASTNode node = instruction.getNode();
final ASTNode nameNode = node.findChildByType(XmlTokenType.XML_NAME);
if (nameNode != null && nameNode.getText().equals("import")) {
instruction.delete();
}
}
prolog.add(newImportList);
} else {
document.addBefore(newImportList, document.getRootTag());
}
};
}
use of com.intellij.injected.editor.VirtualFileWindow in project intellij-community by JetBrains.
the class LastSelectedPropertiesFileStore method saveLastSelectedPropertiesFile.
public void saveLastSelectedPropertiesFile(PsiFile context, PropertiesFile file) {
VirtualFile virtualFile = context.getVirtualFile();
if (virtualFile instanceof VirtualFileWindow) {
virtualFile = ((VirtualFileWindow) virtualFile).getDelegate();
}
assert virtualFile != null;
String contextUrl = virtualFile.getUrl();
final VirtualFile vFile = file.getVirtualFile();
if (vFile != null) {
String url = vFile.getUrl();
lastSelectedUrls.put(contextUrl, url);
VirtualFile containingDir = virtualFile.getParent();
lastSelectedUrls.put(containingDir.getUrl(), url);
lastSelectedFileUrl = url;
StatisticsManager.getInstance().incUseCount(new StatisticsInfo(PROPERTIES_FILE_STATISTICS_KEY, FileUtil.toSystemDependentName(VfsUtilCore.urlToPath(url))));
}
}
use of com.intellij.injected.editor.VirtualFileWindow in project intellij-community by JetBrains.
the class PyUtil method getLanguageLevelForVirtualFile.
/**
* Returns Python language level for a virtual file.
*
* @see {@link LanguageLevel#forElement}
*/
@NotNull
public static LanguageLevel getLanguageLevelForVirtualFile(@NotNull Project project, @NotNull VirtualFile virtualFile) {
if (virtualFile instanceof VirtualFileWindow) {
virtualFile = ((VirtualFileWindow) virtualFile).getDelegate();
}
// Most of the cases should be handled by this one, PyLanguageLevelPusher pushes folders only
final VirtualFile folder = virtualFile.getParent();
if (folder != null) {
final LanguageLevel folderLevel = folder.getUserData(LanguageLevel.KEY);
if (folderLevel != null) {
return folderLevel;
}
final LanguageLevel fileLevel = PythonLanguageLevelPusher.getFileLanguageLevel(project, virtualFile);
if (fileLevel != null) {
return fileLevel;
}
} else {
// However this allows us to setup language level per file manually
// in case when it is LightVirtualFile
final LanguageLevel level = virtualFile.getUserData(LanguageLevel.KEY);
if (level != null)
return level;
if (ApplicationManager.getApplication().isUnitTestMode()) {
final LanguageLevel languageLevel = LanguageLevel.FORCE_LANGUAGE_LEVEL;
if (languageLevel != null) {
return languageLevel;
}
}
}
return guessLanguageLevelWithCaching(project);
}
Aggregations