@Override public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement host) { if (!host.isValid() || !(host instanceof XmlText) || !HtmlUtil.isHtmlTagContainingFile(host)) { return; } XmlTag scriptTag = ((XmlText)host).getParentTag(); if (scriptTag == null) { return; } final Language language = getScriptLanguageToInject(scriptTag); if (language == null || HtmlScriptInjectionBlockerExtension.isInjectionBlocked(scriptTag, language)) { return; } if (LanguageUtil.isInjectableLanguage(language)) { registrar .startInjecting(language) .addPlace(null, null, (PsiLanguageInjectionHost)host, TextRange.create(0, host.getTextLength())) .doneInjecting(); } }
@Nullable private static SmartPointerElementInfo createAnchorInfo(@Nonnull PsiElement element, @Nonnull PsiFile containingFile) { if (element instanceof StubBasedPsiElement && containingFile instanceof PsiFileImpl) { IStubFileElementType stubType = ((PsiFileImpl)containingFile).getElementTypeForStubBuilder(); if (stubType != null && stubType.shouldBuildStubFor(containingFile.getViewProvider().getVirtualFile())) { StubBasedPsiElement stubPsi = (StubBasedPsiElement)element; int stubId = PsiAnchor.calcStubIndex(stubPsi); if (stubId != -1) { return new AnchorElementInfo(element, (PsiFileImpl)containingFile, stubId, stubPsi.getElementType()); } } } Pair<Identikit.ByAnchor, PsiElement> pair = Identikit.withAnchor(element, LanguageUtil.getRootLanguage(containingFile)); if (pair != null) { return new AnchorElementInfo(pair.second, containingFile, pair.first); } return null; }
public static void updateFileExtension(@Nonnull Project project, @Nullable VirtualFile file) throws IOException { ApplicationManager.getApplication().assertWriteAccessAllowed(); if (CommandProcessor.getInstance().getCurrentCommand() == null) { throw new AssertionError("command required"); } if (file == null) return; Language language = LanguageUtil.getLanguageForPsi(project, file); FileType expected = getFileTypeFromName(file); FileType actual = language == null ? null : language.getAssociatedFileType(); if (expected == actual || actual == null) return; String ext = actual.getDefaultExtension(); if (StringUtil.isEmpty(ext)) return; String newName = PathUtil.makeFileName(file.getNameWithoutExtension(), ext); VirtualFile parent = file.getParent(); newName = parent != null && parent.findChild(newName) != null ? PathUtil.makeFileName(file.getName(), ext) : newName; file.rename(ScratchUtil.class, newName); }
@Override public void injectLanguages(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement host) { if (!(host instanceof XmlText)) { return; } XmlTag scriptTag = ((XmlText)host).getParentTag(); if (!"script".equalsIgnoreCase(scriptTag.getLocalName())) { return; } String mimeType = scriptTag.getAttributeValue("type"); Collection<Language> languages = Language.findInstancesByMimeType(mimeType); Language language = languages.isEmpty() ? null : languages.iterator().next(); if (language != null && LanguageUtil.isInjectableLanguage(language)) { registrar .startInjecting(language) .addPlace(null, null, (PsiLanguageInjectionHost)host, TextRange.create(0, host.getTextLength())) .doneInjecting(); } }
public static TextWithImports fromXExpression(@Nullable XExpression expression) { if(expression == null) { return null; } if(expression.getCustomInfo() == null && expression.getLanguage() == null) { return new TextWithImportsImpl(getKind(expression.getMode()), expression.getExpression()); } else { return new TextWithImportsImpl(getKind(expression.getMode()), expression.getExpression(), StringUtil.notNullize(expression.getCustomInfo()), LanguageUtil.getLanguageFileType(expression.getLanguage())); } }
private void updateDialectsAndContexts() { final FileType fileType = (FileType)fileTypes.getSelectedItem(); if (fileType instanceof LanguageFileType) { Language language = ((LanguageFileType)fileType).getLanguage(); Language[] languageDialects = LanguageUtil.getLanguageDialects(language); Arrays.sort(languageDialects, new Comparator<Language>() { @Override public int compare(Language o1, Language o2) { return o1.getDisplayName().compareTo(o2.getDisplayName()); } }); Language[] variants = new Language[languageDialects.length + 1]; variants[0] = null; System.arraycopy(languageDialects, 0, variants, 1, languageDialects.length); dialects.setModel(new DefaultComboBoxModel(variants)); dialects.setEnabled(variants.length > 1); } final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(fileType); if (profile instanceof StructuralSearchProfileBase) { final String[] contextNames = ((StructuralSearchProfileBase)profile).getContextNames(); if (contextNames.length > 0) { contexts.setModel(new DefaultComboBoxModel(contextNames)); contexts.setSelectedItem(contextNames[0]); contexts.setEnabled(true); return; } } contexts.setSelectedItem(null); contexts.setEnabled(false); }
private void updateDialectsCombo(@Nullable final String lastUsed) { final Object source = getSource(); ArrayList<Language> items = new ArrayList<Language>(); if (source instanceof LanguageFileType) { final Language baseLang = ((LanguageFileType)source).getLanguage(); items.add(baseLang); Language[] dialects = LanguageUtil.getLanguageDialects(baseLang); Arrays.sort(dialects, LanguageUtil.LANGUAGE_COMPARATOR); items.addAll(Arrays.asList(dialects)); } myDialectComboBox.setModel(new CollectionComboBoxModel(items)); final int size = items.size(); final boolean visible = size > 1; myDialectLabel.setVisible(visible); myDialectComboBox.setVisible(visible); if (visible && (myCurrentFile != null || lastUsed != null)) { String curLanguage = myCurrentFile != null ? myCurrentFile.getLanguage().toString() : lastUsed; for (int i = 0; i < size; ++i) { if (curLanguage.equals(items.get(i).toString())) { myDialectComboBox.setSelectedIndex(i); return; } } myDialectComboBox.setSelectedIndex(size > 0 ? 0 : -1); } }
@NotNull public static ListPopup forFileLanguages(@NotNull Project project, @Nullable final Language selection, @NotNull final Consumer<Language> onChosen) { return languagePopupBuilder(project, "Languages"). forValues(LanguageUtil.getFileLanguages()). withSelection(selection). onChosen(onChosen). buildPopup(); }
@NotNull @Override protected Collection<Language> getAllFilterValues() { final Collection<Language> registeredLanguages = Language.getRegisteredLanguages(); List<Language> accepted = new ArrayList<Language>(); for (Language language : registeredLanguages) { if (language != Language.ANY && !(language instanceof DependentLanguage)) { accepted.add(language); } } Collections.sort(accepted, LanguageUtil.LANGUAGE_COMPARATOR); return accepted; }
public ASTNode generateWhitespaceBetweenTokens(ASTNode left, ASTNode right) { PsiManager manager = right.getTreeParent().getPsi().getManager(); if (left.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) { return createLineBreak(manager); } if (left.getPsi().isValid() && right.getPsi().isValid()) { final PsiElement commonParent = PsiTreeUtil.findCommonParent(left.getPsi(), right.getPsi()); if (commonParent == null) return null; final PsiElement leftPrevAncestor = PsiTreeUtil.findPrevParent(commonParent, left.getPsi()); final PsiElement rightPrevAncestor = PsiTreeUtil.findPrevParent(commonParent, right.getPsi()); if (isStatementOrFunction(leftPrevAncestor) && isStatementOrFunction(rightPrevAncestor)) { int leftIndent = PyPsiUtils.getElementIndentation(leftPrevAncestor); int rightIndent = PyPsiUtils.getElementIndentation(rightPrevAncestor); int maxIndent = Math.max(leftIndent, rightIndent); return createWhitespace(manager, "\n" + StringUtil.repeatSymbol(' ', maxIndent)); } } if (right.getElementType() == PyTokenTypes.DEF_KEYWORD || right.getElementType() == PyTokenTypes.CLASS_KEYWORD) { return createLineBreak(manager); } if (left.getElementType() == TokenType.WHITE_SPACE || right.getElementType() == TokenType.WHITE_SPACE) { return null; } final PyStatement leftStatement = PsiTreeUtil.getParentOfType(left.getPsi(), PyStatement.class); if (leftStatement != null && !PsiTreeUtil.isAncestor(leftStatement, right.getPsi(), false)) { return createLineBreak(manager); } final Lexer lexer = new PythonIndentingLexer(); if (LanguageUtil.canStickTokensTogetherByLexer(left, right, lexer) == ParserDefinition.SpaceRequirements.MUST) { return createSpace(manager); } return null; }
public IElementType getTokenType() { if (myTokenType!=null) return myTokenType; IElementType tokenType = super.getTokenType(); myTokenStart = super.getTokenStart(); myTokenEnd = super.getTokenEnd(); if (hasSeenStyle()) { if (hasSeenTag() && ourStyleElementType!=null && isStartOfEmbeddmentTagContent(tokenType)) { myTokenEnd = skipToTheEndOfTheEmbeddment(); tokenType = ourStyleElementType; } else if (ourInlineStyleElementType!=null && isStartOfEmbeddmentAttributeValue(tokenType) && hasSeenAttribute()) { tokenType = ourInlineStyleElementType; } } else if (hasSeenScript()) { Language scriptLanguage = getScriptLanguage(); boolean canInject = scriptLanguage == null || LanguageUtil.isInjectableLanguage(scriptLanguage); if (hasSeenTag() && isStartOfEmbeddmentTagContent(tokenType) && canInject) { myTokenEnd = skipToTheEndOfTheEmbeddment(); IElementType currentScriptElementType = getCurrentScriptElementType(); tokenType = currentScriptElementType == null ? XmlTokenType.XML_DATA_CHARACTERS : currentScriptElementType; } else if (hasSeenAttribute() && isStartOfEmbeddmentAttributeValue(tokenType) && ourInlineScriptElementType!=null) { myTokenEnd = skipToTheEndOfTheEmbeddment(); tokenType = ourInlineScriptElementType; } } return myTokenType = tokenType; }
private static void initLanguageCache() { ourLanguageCache = new HashMap<String, Language>(); Collection<Language> registeredLanguages; do { registeredLanguages = new ArrayList<Language>(Language.getRegisteredLanguages()); for (Language language : registeredLanguages) { if (LanguageUtil.isInjectableLanguage(language)) { ourLanguageCache.put(language.getID(), language); } } } while (Language.getRegisteredLanguages().size() != registeredLanguages.size()); ourLanguageCount = registeredLanguages.size(); }
@Override public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) { final IElementType lType = left.getElementType(); final IElementType rType = right.getElementType(); if (rType == GroovyTokenTypes.kIMPORT && lType != TokenType.WHITE_SPACE) { return SpaceRequirements.MUST_LINE_BREAK; } else if (lType == MODIFIERS && rType == MODIFIERS) { return SpaceRequirements.MUST; } if (lType == GroovyTokenTypes.mSEMI || lType == GroovyTokenTypes.mSL_COMMENT) { return SpaceRequirements.MUST_LINE_BREAK; } if (lType == GroovyTokenTypes.mNLS || lType == GroovyDocTokenTypes.mGDOC_COMMENT_START) { return SpaceRequirements.MAY; } if (lType == GroovyTokenTypes.mGT) return SpaceRequirements.MUST; if (rType == GroovyTokenTypes.mLT) return SpaceRequirements.MUST; final ASTNode parent = TreeUtil.findCommonParent(left, right); if (parent == null || ArrayUtil.contains(parent.getElementType(), STRINGS)) { return SpaceRequirements.MUST_NOT; } return LanguageUtil.canStickTokensTogetherByLexer(left, right, new GroovyLexer()); }
@NotNull public SpaceRequirements spaceExistanceTypeBetweenTokens(@NotNull final ASTNode left, @NotNull final ASTNode right) { if (left.getElementType() == OCamlElementTypes.LABEL_NAME && right.getElementType() == OCamlTokenTypes.COLON) { return SpaceRequirements.MUST_NOT; } return LanguageUtil.canStickTokensTogetherByLexer(left, right, doCreateLexer()); }
@Override public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement host) { if (!host.isValid() || !(host instanceof XmlText) || !HtmlUtil.isHtmlTagContainingFile(host)) { return; } XmlTag scriptTag = ((XmlText)host).getParentTag(); if (scriptTag == null || !"script".equalsIgnoreCase(scriptTag.getLocalName())) { return; } String mimeType = scriptTag.getAttributeValue("type"); Collection<Language> languages = Language.findInstancesByMimeType(mimeType); Language language; if (!languages.isEmpty()) { language = languages.iterator().next(); } else if (mimeType != null && mimeType.contains("template")) { language = StdLanguages.HTML; } else { language = StdLanguages.TEXT; } if (LanguageUtil.isInjectableLanguage(language)) { registrar .startInjecting(language) .addPlace(null, null, (PsiLanguageInjectionHost)host, TextRange.create(0, host.getTextLength())) .doneInjecting(); } }
public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) { final IElementType lType = left.getElementType(); final IElementType rType = right.getElementType(); if (rType == kIMPORT && lType != TokenType.WHITE_SPACE) { return MUST_LINE_BREAK; } else if (lType == MODIFIERS && rType == MODIFIERS) { return MUST; } if (lType == mSEMI || lType == mSL_COMMENT) { return MUST_LINE_BREAK; } if (lType == mNLS || lType == mGDOC_COMMENT_START) { return MAY; } final IElementType parentType = left.getTreeParent().getElementType(); if (parentType == GSTRING || parentType == REGEX || parentType == GSTRING_INJECTION || parentType == mREGEX_LITERAL || parentType == mDOLLAR_SLASH_REGEX_LITERAL) { return MUST_NOT; } return LanguageUtil.canStickTokensTogetherByLexer(left, right, new GroovyLexer()); }
private static Language calcBaseLanguage(@Nonnull VirtualFile file, @Nonnull Project project, @Nonnull final FileType fileType) { if (fileType.isBinary()) return Language.ANY; if (isTooLargeForIntelligence(file)) return PlainTextLanguage.INSTANCE; Language language = LanguageUtil.getLanguageForPsi(project, file); return language != null ? language : PlainTextLanguage.INSTANCE; }
@Nonnull private static SmartPointerElementInfo createElementInfo(@Nonnull PsiFile containingFile, @Nonnull ProperTextRange range, boolean forInjected) { Project project = containingFile.getProject(); if (containingFile.getViewProvider() instanceof FreeThreadedFileViewProvider) { PsiLanguageInjectionHost host = InjectedLanguageManager.getInstance(project).getInjectionHost(containingFile); if (host != null) { SmartPsiElementPointer<PsiLanguageInjectionHost> hostPointer = SmartPointerManager.getInstance(project).createSmartPsiElementPointer(host); return new InjectedSelfElementInfo(project, containingFile, range, containingFile, hostPointer); } } if (!forInjected && range.equals(containingFile.getTextRange())) return new FileElementInfo(containingFile); return new SelfElementInfo(project, range, Identikit.fromTypes(PsiElement.class, null, LanguageUtil.getRootLanguage(containingFile)), containingFile, forInjected); }
@Override @Nonnull public FileViewProvider createFileViewProvider(@Nonnull final VirtualFile file, boolean eventSystemEnabled) { FileType fileType = file.getFileType(); Language language = LanguageUtil.getLanguageForPsi(myManager.getProject(), file); FileViewProviderFactory factory = language == null ? FileTypeFileViewProviders.INSTANCE.forFileType(fileType) : LanguageFileViewProviders.INSTANCE.forLanguage(language); FileViewProvider viewProvider = factory == null ? null : factory.createFileViewProvider(file, language, myManager, eventSystemEnabled); return viewProvider == null ? new SingleRootFileViewProvider(myManager, file, eventSystemEnabled, fileType) : viewProvider; }
@Nonnull protected FileType getFileType(@Nonnull XExpression expression) { FileType fileType = LanguageUtil.getLanguageFileType(expression.getLanguage()); if (fileType != null) { return fileType; } return getEditorsProvider().getFileType(); }
@Nullable public Icon substituteIcon(@Nonnull Project project, @Nonnull VirtualFile file) { Language language = substituteLanguage(project, file); FileType fileType = LanguageUtil.getLanguageFileType(language); if (fileType == null) fileType = ScratchUtil.getFileTypeFromName(file); return fileType != null ? fileType.getIcon() : null; }
@Override @Nullable public SyntaxHighlighter create(@Nonnull FileType fileType, @Nullable Project project, @Nullable VirtualFile file) { if (project == null || file == null || !(fileType instanceof ScratchFileType)) return null; Language language = LanguageUtil.getLanguageForPsi(project, file); return language == null ? null : SyntaxHighlighterFactory.getSyntaxHighlighter(language, project, file); }
@Nonnull @Override protected Collection<Language> getAllFilterValues() { final Collection<Language> registeredLanguages = Language.getRegisteredLanguages(); List<Language> accepted = new ArrayList<Language>(); for (Language language : registeredLanguages) { if (language != Language.ANY && !(language instanceof DependentLanguage)) { accepted.add(language); } } Collections.sort(accepted, LanguageUtil.LANGUAGE_COMPARATOR); return accepted; }
@NotNull @Override public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) { PsiElement leftPsi = left.getPsi(); final Lexer lexer = createLexer(leftPsi.getLanguageVersion()); return LanguageUtil.canStickTokensTogetherByLexer(left, right, lexer); }
@Nullable public static XExpression toXExpression(@Nullable TextWithImports text) { if(text != null && !text.getText().isEmpty()) { return new XExpressionImpl(text.getText(), LanguageUtil.getFileTypeLanguage(text.getFileType()), StringUtil.nullize(text.getImports()), getMode(text.getKind())); } return null; }
@Override public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) { final Lexer lexer = createLexer(left.getPsi().getProject()); return LanguageUtil.canStickTokensTogetherByLexer(left, right, lexer); }
public FileElementInfo(@NotNull final PsiFile file) { myVirtualFile = file.getVirtualFile(); myProject = file.getProject(); myLanguage = LanguageUtil.getRootLanguage(file); }
public static boolean isInjectableLanguage(Language language) { return LanguageUtil.isInjectableLanguage(language); }
@Override protected List<Language> getAvailableValues() { return LanguageUtil.getFileLanguages(); }
@Override public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) { return LanguageUtil.canStickTokensTogetherByLexer(left, right, new DtdLexer(false)); }
@Override public boolean shouldInspect(PsiFile file) { return !LanguageUtil.isInTemplateLanguageFile(file); }
public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) { final Lexer lexer = createLexer(left.getPsi().getProject()); return LanguageUtil.canStickTokensTogetherByLexer(left, right, lexer); }
public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) { return LanguageUtil.canStickTokensTogetherByLexer(left, right, new DtdLexer(false)); }
public FileElementInfo(@Nonnull final PsiFile file) { myVirtualFile = file.getVirtualFile(); myProject = file.getProject(); myLanguage = LanguageUtil.getRootLanguage(file); myFileClass = file.getClass(); }
public static boolean hasMatchingExtension(@Nonnull Project project, @Nonnull VirtualFile file) { FileType expected = getFileTypeFromName(file); Language language = LanguageUtil.getLanguageForPsi(project, file); FileType actual = language == null ? null : language.getAssociatedFileType(); return expected == actual && actual != null; }