Search in sources :

Example 21 with VirtualFileWindow

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;
}
Also used : VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with VirtualFileWindow

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) UsageInFile(com.intellij.usages.rules.UsageInFile) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with VirtualFileWindow

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());
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(com.intellij.util.containers.HashMap) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) Project(com.intellij.openapi.project.Project) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) ASTNode(com.intellij.lang.ASTNode) Pair(com.intellij.openapi.util.Pair) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with VirtualFileWindow

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))));
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) StatisticsInfo(com.intellij.psi.statistics.StatisticsInfo) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow)

Example 25 with VirtualFileWindow

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow)

Aggregations

VirtualFileWindow (com.intellij.injected.editor.VirtualFileWindow)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 Nullable (org.jetbrains.annotations.Nullable)7 NotNull (org.jetbrains.annotations.NotNull)5 DocumentWindow (com.intellij.injected.editor.DocumentWindow)4 Project (com.intellij.openapi.project.Project)4 Document (com.intellij.openapi.editor.Document)3 Module (com.intellij.openapi.module.Module)3 TextRange (com.intellij.openapi.util.TextRange)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 Language (com.intellij.lang.Language)2 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)2 SmartList (com.intellij.util.SmartList)2 PsiDirectoryNode (com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode)1 DocumentWindowImpl (com.intellij.injected.editor.DocumentWindowImpl)1 ASTNode (com.intellij.lang.ASTNode)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)1 Pair (com.intellij.openapi.util.Pair)1