Search in sources :

Example 16 with VirtualFileWindow

use of com.intellij.injected.editor.VirtualFileWindow in project intellij-community by JetBrains.

the class ReadonlyStatusHandlerImpl method ensureFilesWritable.

@Override
public OperationStatus ensureFilesWritable(@NotNull VirtualFile... files) {
    if (files.length == 0) {
        return new OperationStatusImpl(VirtualFile.EMPTY_ARRAY);
    }
    checkThreading();
    Set<VirtualFile> realFiles = new THashSet<>(files.length);
    for (VirtualFile file : files) {
        if (file instanceof LightVirtualFile) {
            VirtualFile originalFile = ((LightVirtualFile) file).getOriginalFile();
            if (originalFile != null) {
                file = originalFile;
            }
        }
        if (file instanceof VirtualFileWindow)
            file = ((VirtualFileWindow) file).getDelegate();
        if (file != null) {
            realFiles.add(file);
        }
    }
    files = VfsUtilCore.toVirtualFileArray(realFiles);
    for (final WritingAccessProvider accessProvider : myAccessProviders) {
        Collection<VirtualFile> denied = ContainerUtil.filter(files, virtualFile -> !accessProvider.isPotentiallyWritable(virtualFile));
        if (denied.isEmpty()) {
            denied = accessProvider.requestWriting(files);
        }
        if (!denied.isEmpty()) {
            return new OperationStatusImpl(VfsUtilCore.toVirtualFileArray(denied));
        }
    }
    final FileInfo[] fileInfos = createFileInfos(files);
    if (fileInfos.length == 0) {
        // if all files are already writable
        return createResultStatus(files);
    }
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        if (myClearReadOnlyInTests) {
            processFiles(new ArrayList<>(Arrays.asList(fileInfos)), null);
        }
        return createResultStatus(files);
    }
    // This event count hack is necessary to allow actions that called this stuff could still get data from their data contexts.
    // Otherwise data manager stuff will fire up an assertion saying that event count has been changed (due to modal dialog show-up)
    // The hack itself is safe since we guarantee that focus will return to the same component had it before modal dialog have been shown.
    final int savedEventCount = IdeEventQueue.getInstance().getEventCount();
    if (myState.SHOW_DIALOG) {
        new ReadOnlyStatusDialog(myProject, fileInfos).show();
    } else {
        // the collection passed is modified
        processFiles(new ArrayList<>(Arrays.asList(fileInfos)), null);
    }
    IdeEventQueue.getInstance().setEventCount(savedEventCount);
    return createResultStatus(files);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) THashSet(gnu.trove.THashSet) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) WritingAccessProvider(com.intellij.openapi.vfs.WritingAccessProvider)

Example 17 with VirtualFileWindow

use of com.intellij.injected.editor.VirtualFileWindow in project intellij-community by JetBrains.

the class DescriptorComposer method composeDescription.

protected void composeDescription(@NotNull CommonProblemDescriptor description, int i, @NotNull StringBuffer buf, @NotNull RefEntity refElement) {
    PsiElement expression = description instanceof ProblemDescriptor ? ((ProblemDescriptor) description).getPsiElement() : null;
    StringBuilder anchor = new StringBuilder();
    VirtualFile vFile = null;
    if (expression != null) {
        vFile = expression.getContainingFile().getVirtualFile();
        if (vFile instanceof VirtualFileWindow)
            vFile = ((VirtualFileWindow) vFile).getDelegate();
        //noinspection HardCodedStringLiteral
        anchor.append("<a HREF=\"");
        //noinspection HardCodedStringLiteral
        anchor.append(appendURL(vFile, "descr:" + i));
        anchor.append("\">");
        anchor.append(ProblemDescriptorUtil.extractHighlightedText(description, expression).replaceAll("\\$", "\\\\\\$"));
        //noinspection HardCodedStringLiteral
        anchor.append("</a>");
    } else {
        //noinspection HardCodedStringLiteral
        anchor.append("<font style=\"font-weight:bold; color:#FF0000\";>");
        anchor.append(InspectionsBundle.message("inspection.export.results.invalidated.item"));
        //noinspection HardCodedStringLiteral
        anchor.append("</font>");
    }
    String descriptionTemplate = description.getDescriptionTemplate();
    if (XmlStringUtil.isWrappedInHtml(descriptionTemplate)) {
        descriptionTemplate = XmlStringUtil.stripHtml(descriptionTemplate);
    } else {
        descriptionTemplate = StringUtil.replace(descriptionTemplate, "<code>", "'");
        descriptionTemplate = StringUtil.replace(descriptionTemplate, "</code>", "'");
        descriptionTemplate = XmlStringUtil.escapeString(descriptionTemplate);
    }
    //noinspection HardCodedStringLiteral
    final String reference = "#ref";
    final boolean containsReference = descriptionTemplate.contains(reference);
    String res = descriptionTemplate.replaceAll(reference, anchor.toString());
    final int lineNumber = description instanceof ProblemDescriptor ? ((ProblemDescriptor) description).getLineNumber() : -1;
    StringBuffer lineAnchor = new StringBuffer();
    if (expression != null && lineNumber >= 0) {
        Document doc = FileDocumentManager.getInstance().getDocument(vFile);
        lineAnchor.append(InspectionsBundle.message("inspection.export.results.at.line")).append(" ");
        //noinspection HardCodedStringLiteral
        lineAnchor.append("<a HREF=\"");
        int offset = doc.getLineStartOffset(lineNumber);
        offset = CharArrayUtil.shiftForward(doc.getCharsSequence(), offset, " \t");
        lineAnchor.append(appendURL(vFile, String.valueOf(offset)));
        lineAnchor.append("\">");
        lineAnchor.append(Integer.toString(lineNumber + 1));
        //noinspection HardCodedStringLiteral
        lineAnchor.append("</a>");
        //noinspection HardCodedStringLiteral
        final String location = "#loc";
        if (!res.contains(location)) {
            res += " (" + location + ")";
        }
        res = res.replaceAll(location, lineAnchor.toString());
    }
    buf.append(res.replace("#end", "").replace("#treeend", ""));
    buf.append(BR).append(BR);
    composeAdditionalDescription(buf, refElement);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement)

Example 18 with VirtualFileWindow

use of com.intellij.injected.editor.VirtualFileWindow in project intellij-community by JetBrains.

the class UsageInfo method offset.

private Pair<VirtualFile, Integer> offset() {
    VirtualFile containingFile0 = getVirtualFile();
    int shift0 = 0;
    if (containingFile0 instanceof VirtualFileWindow) {
        shift0 = ((VirtualFileWindow) containingFile0).getDocumentWindow().injectedToHost(0);
        containingFile0 = ((VirtualFileWindow) containingFile0).getDelegate();
    }
    Segment range = myPsiFileRange == null ? mySmartPointer.getPsiRange() : myPsiFileRange.getPsiRange();
    if (range == null)
        return null;
    return Pair.create(containingFile0, range.getStartOffset() + shift0);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow)

Example 19 with VirtualFileWindow

use of com.intellij.injected.editor.VirtualFileWindow in project intellij-community by JetBrains.

the class LanguageSubstitutors method processLanguageSubstitution.

private static void processLanguageSubstitution(@NotNull final VirtualFile file, @NotNull Language originalLang, @NotNull final Language substitutedLang, @NotNull Project project) {
    if (file instanceof VirtualFileWindow) {
        //   com.intellij.psi.impl.source.tree.injected.MultiHostRegistrarImpl#doneInjecting
        return;
    }
    Key<Language> projectKey = getOrCreateProjectKey(project);
    Language prevSubstitutedLang = projectKey.get(file);
    final Language prevLang = ObjectUtils.notNull(prevSubstitutedLang, originalLang);
    if (!prevLang.is(substitutedLang)) {
        if (file.replace(projectKey, prevSubstitutedLang, substitutedLang)) {
            if (prevSubstitutedLang == null) {
                // no need to reparse for the first language substitution
                return;
            }
            if (ApplicationManager.getApplication().isUnitTestMode()) {
                return;
            }
            requestReparsing(file, prevLang, substitutedLang);
        }
    }
}
Also used : Language(com.intellij.lang.Language) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow)

Example 20 with VirtualFileWindow

use of com.intellij.injected.editor.VirtualFileWindow in project intellij-community by JetBrains.

the class ResolveScopeManagerImpl method getUseScope.

@Override
@NotNull
public GlobalSearchScope getUseScope(@NotNull PsiElement element) {
    VirtualFile vDirectory;
    final VirtualFile virtualFile;
    final PsiFile containingFile;
    final GlobalSearchScope allScope = GlobalSearchScope.allScope(myManager.getProject());
    if (element instanceof PsiDirectory) {
        vDirectory = ((PsiDirectory) element).getVirtualFile();
        virtualFile = null;
        containingFile = null;
    } else {
        containingFile = element.getContainingFile();
        if (containingFile == null)
            return allScope;
        virtualFile = containingFile.getVirtualFile();
        if (virtualFile == null)
            return allScope;
        if (virtualFile instanceof VirtualFileWindow) {
            return GlobalSearchScope.fileScope(myProject, ((VirtualFileWindow) virtualFile).getDelegate());
        }
        vDirectory = virtualFile.getParent();
    }
    if (vDirectory == null)
        return allScope;
    final ProjectFileIndex projectFileIndex = myProjectRootManager.getFileIndex();
    final Module module = projectFileIndex.getModuleForFile(vDirectory);
    if (module == null) {
        VirtualFile notNullVFile = virtualFile != null ? virtualFile : vDirectory;
        final List<OrderEntry> entries = projectFileIndex.getOrderEntriesForFile(notNullVFile);
        if (entries.isEmpty() && (myAdditionalIndexableFileSet.isInSet(notNullVFile) || isFromAdditionalLibraries(notNullVFile))) {
            return allScope;
        }
        GlobalSearchScope result = LibraryScopeCache.getInstance(myProject).getLibraryUseScope(entries);
        return containingFile == null || virtualFile.isDirectory() || result.contains(virtualFile) ? result : GlobalSearchScope.fileScope(containingFile).uniteWith(result);
    }
    boolean isTest = TestSourcesFilter.isTestSources(vDirectory, myProject);
    GlobalSearchScope scope = isTest ? GlobalSearchScope.moduleTestsWithDependentsScope(module) : GlobalSearchScope.moduleWithDependentsScope(module);
    RefResolveService resolveService;
    if (virtualFile instanceof VirtualFileWithId && RefResolveService.ENABLED && (resolveService = RefResolveService.getInstance(myProject)).isUpToDate()) {
        return resolveService.restrictByBackwardIds(virtualFile, scope);
    }
    return scope;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) Module(com.intellij.openapi.module.Module) VirtualFileWithId(com.intellij.openapi.vfs.VirtualFileWithId) NotNull(org.jetbrains.annotations.NotNull)

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