Search in sources :

Example 76 with PsiFile

use of com.intellij.psi.PsiFile in project intellij-community by JetBrains.

the class PyiResolveTest method testModuleAttributePyiOverPy.

// PY-21231
public void testModuleAttributePyiOverPy() {
    final PsiElement result = doResolve();
    assertInstanceOf(result, PyTargetExpression.class);
    final PyTargetExpression target = (PyTargetExpression) result;
    assertEquals("foo", target.getName());
    final PsiFile containingFile = target.getContainingFile();
    assertInstanceOf(containingFile, PyiFile.class);
}
Also used : PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 77 with PsiFile

use of com.intellij.psi.PsiFile in project intellij-community by JetBrains.

the class HtmlUnknownTagInspectionBase method checkTag.

@Override
protected void checkTag(@NotNull final XmlTag tag, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    if (!(tag instanceof HtmlTag) || !XmlHighlightVisitor.shouldBeValidated(tag)) {
        return;
    }
    XmlElementDescriptor descriptorFromContext = XmlUtil.getDescriptorFromContext(tag);
    PsiElement parent = tag.getParent();
    XmlElementDescriptor parentDescriptor = parent instanceof XmlTag ? ((XmlTag) parent).getDescriptor() : null;
    XmlElementDescriptor ownDescriptor = isAbstractDescriptor(descriptorFromContext) ? tag.getDescriptor() : descriptorFromContext;
    if (isAbstractDescriptor(ownDescriptor) || (parentDescriptor instanceof HtmlElementDescriptorImpl && ownDescriptor instanceof HtmlElementDescriptorImpl && isAbstractDescriptor(descriptorFromContext))) {
        final String name = tag.getName();
        if (!isCustomValuesEnabled() || !isCustomValue(name)) {
            final AddCustomHtmlElementIntentionAction action = new AddCustomHtmlElementIntentionAction(TAG_KEY, name, XmlBundle.message("add.custom.html.tag", name));
            // todo: support "element is not allowed" message for html5
            // some tags in html5 cannot be found in xhtml5.xsd if they are located in incorrect context, so they get any-element descriptor (ex. "canvas: tag)
            final String message = isAbstractDescriptor(ownDescriptor) ? XmlErrorMessages.message("unknown.html.tag", name) : XmlErrorMessages.message("element.is.not.allowed.here", name);
            final PsiElement startTagName = XmlTagUtil.getStartTagNameElement(tag);
            assert startTagName != null;
            final PsiElement endTagName = XmlTagUtil.getEndTagNameElement(tag);
            List<LocalQuickFix> quickfixes = new ArrayList<>();
            quickfixes.add(action);
            if (isOnTheFly) {
                PsiFile file = startTagName.getContainingFile();
                if (file instanceof XmlFile) {
                    quickfixes.add(XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(startTagName, "", null));
                }
                // People using non-HTML as their template data language (but having not changed this in the IDE)
                // will most likely see 'unknown html tag' error, because HTML is usually the default.
                // So if they check quick fixes for this error they'll discover Change Template Data Language feature.
                ContainerUtil.addIfNotNull(quickfixes, createChangeTemplateDataFix(file));
            }
            if (HtmlUtil.isHtml5Tag(name) && !HtmlUtil.hasNonHtml5Doctype(tag)) {
                quickfixes.add(new SwitchToHtml5WithHighPriorityAction());
            }
            ProblemHighlightType highlightType = tag.getContainingFile().getContext() == null ? ProblemHighlightType.GENERIC_ERROR_OR_WARNING : ProblemHighlightType.INFORMATION;
            if (startTagName.getTextLength() > 0) {
                holder.registerProblem(startTagName, message, highlightType, quickfixes.toArray(new LocalQuickFix[quickfixes.size()]));
            }
            if (endTagName != null) {
                holder.registerProblem(endTagName, message, highlightType, quickfixes.toArray(new LocalQuickFix[quickfixes.size()]));
            }
        }
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) ArrayList(java.util.ArrayList) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) HtmlTag(com.intellij.psi.html.HtmlTag) HtmlElementDescriptorImpl(com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl) PsiFile(com.intellij.psi.PsiFile) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 78 with PsiFile

use of com.intellij.psi.PsiFile in project intellij-community by JetBrains.

the class XmlSyncTagCommunityTest method testSave.

public void testSave() {
    doTest("<div>     \n    \n</div><caret>", "\n", "<div>     \n    \n</div>\n");
    final PsiFile file = myFixture.getFile();
    myFixture.getEditor().getCaretModel().moveToOffset(myFixture.getDocument(file).getTextLength() - 2);
    file.getVirtualFile().putUserData(TrailingSpacesStripper.OVERRIDE_STRIP_TRAILING_SPACES_KEY, EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE);
    FileDocumentManager.getInstance().saveDocument(myFixture.getDocument(file));
    myFixture.checkResult("<div>\n\n</div>\n");
}
Also used : PsiFile(com.intellij.psi.PsiFile)

Example 79 with PsiFile

use of com.intellij.psi.PsiFile in project intellij-community by JetBrains.

the class HtmlFileReferenceHelper method getContexts.

@NotNull
@Override
public Collection<PsiFileSystemItem> getContexts(Project project, @NotNull VirtualFile vFile) {
    final PsiFile file = PsiManager.getInstance(project).findFile(vFile);
    final Module module = file != null ? ModuleUtilCore.findModuleForPsiElement(file) : null;
    if (module == null || !(file instanceof XmlFile))
        return Collections.emptyList();
    final String basePath = HtmlUtil.getHrefBase((XmlFile) file);
    if (basePath != null && !HtmlUtil.hasHtmlPrefix(basePath)) {
        for (VirtualFile virtualFile : getBaseRoots(module)) {
            final VirtualFile base = virtualFile.findFileByRelativePath(basePath);
            final PsiDirectory result = base != null ? PsiManager.getInstance(project).findDirectory(base) : null;
            if (result != null) {
                return Collections.<PsiFileSystemItem>singletonList(result);
            }
        }
    }
    return Collections.emptyList();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) NotNull(org.jetbrains.annotations.NotNull)

Example 80 with PsiFile

use of com.intellij.psi.PsiFile in project intellij-community by JetBrains.

the class HtmlUnknownTargetInspection method notRemoteBase.

static boolean notRemoteBase(PsiReference reference) {
    final PsiFile file = reference.getElement().getContainingFile();
    final String basePath = file instanceof XmlFile ? HtmlUtil.getHrefBase((XmlFile) file) : null;
    return basePath == null || !HtmlUtil.hasHtmlPrefix(basePath);
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile)

Aggregations

PsiFile (com.intellij.psi.PsiFile)1785 VirtualFile (com.intellij.openapi.vfs.VirtualFile)496 PsiElement (com.intellij.psi.PsiElement)468 Project (com.intellij.openapi.project.Project)267 Nullable (org.jetbrains.annotations.Nullable)267 NotNull (org.jetbrains.annotations.NotNull)248 Document (com.intellij.openapi.editor.Document)181 Editor (com.intellij.openapi.editor.Editor)168 XmlFile (com.intellij.psi.xml.XmlFile)126 PsiDirectory (com.intellij.psi.PsiDirectory)114 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)109 Module (com.intellij.openapi.module.Module)108 TextRange (com.intellij.openapi.util.TextRange)88 ArrayList (java.util.ArrayList)84 XmlTag (com.intellij.psi.xml.XmlTag)68 File (java.io.File)58 PsiManager (com.intellij.psi.PsiManager)56 PsiClass (com.intellij.psi.PsiClass)51 List (java.util.List)46 Language (com.intellij.lang.Language)45