Java Code Examples for com.intellij.lang.injection.InjectedLanguageManager#getInstance()
The following examples show how to use
com.intellij.lang.injection.InjectedLanguageManager#getInstance() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InjectedLanguageUtil.java From consulo with Apache License 2.0 | 6 votes |
public static String getUnescapedText(@Nonnull PsiFile file, @Nullable final PsiElement startElement, @Nullable final PsiElement endElement) { final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(file.getProject()); if (manager.getInjectionHost(file) == null) { return file.getText().substring(startElement == null ? 0 : startElement.getTextRange().getStartOffset(), endElement == null ? file.getTextLength() : endElement.getTextRange().getStartOffset()); } final StringBuilder sb = new StringBuilder(); file.accept(new PsiRecursiveElementWalkingVisitor() { Boolean myState = startElement == null ? Boolean.TRUE : null; @Override public void visitElement(PsiElement element) { if (element == startElement) myState = Boolean.TRUE; if (element == endElement) myState = Boolean.FALSE; if (Boolean.FALSE == myState) return; if (Boolean.TRUE == myState && element.getFirstChild() == null) { sb.append(getUnescapedLeafText(element, false)); } else { super.visitElement(element); } } }); return sb.toString(); }
Example 2
Source File: BladeInjectTypeProvider.java From idea-php-laravel-plugin with MIT License | 6 votes |
/** * Resolve PHP language injection in Blade * * @see BladeVariableTypeProvider#getHostPhpFileForInjectedIfExists */ @Nullable private static BladeFileImpl getHostBladeFileForInjectionIfExists(PsiElement element) { PsiFile file = element.getContainingFile(); InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(element.getProject()); if (injectedLanguageManager.isInjectedFragment(file)) { PsiLanguageInjectionHost host = injectedLanguageManager.getInjectionHost(element); if (host instanceof BladePsiLanguageInjectionHost && host.isValidHost()) { PsiFile bladeFile = host.getContainingFile(); if (bladeFile instanceof BladeFileImpl) { return (BladeFileImpl) bladeFile; } } } return null; }
Example 3
Source File: GraphQLPsiSearchHelper.java From js-graphql-intellij-plugin with MIT License | 5 votes |
public GraphQLPsiSearchHelper(@NotNull final Project project) { myProject = project; mySettings = GraphQLSettings.getSettings(project); psiManager = PsiManager.getInstance(myProject); graphQLInjectionSearchHelper = ServiceManager.getService(GraphQLInjectionSearchHelper.class); injectedLanguageManager = InjectedLanguageManager.getInstance(myProject); graphQLConfigManager = GraphQLConfigManager.getService(myProject); pluginDescriptor = PluginManager.getPlugin(PluginId.getId("com.intellij.lang.jsgraphql")); final PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(myProject); defaultProjectFile = (GraphQLFile) psiFileFactory.createFileFromText("Default schema file", GraphQLLanguage.INSTANCE, ""); GlobalSearchScope defaultProjectFileScope = GlobalSearchScope.fileScope(defaultProjectFile); GlobalSearchScope builtInSchemaScope = GlobalSearchScope.fileScope(project, getBuiltInSchema().getVirtualFile()); GlobalSearchScope builtInRelaySchemaScope = GlobalSearchScope.fileScope(project, getRelayModernDirectivesSchema().getVirtualFile()); allBuiltInSchemaScopes = builtInSchemaScope .union(new ConditionalGlobalSearchScope(builtInRelaySchemaScope, mySettings::isEnableRelayModernFrameworkSupport)) .union(defaultProjectFileScope) ; final FileType[] searchScopeFileTypes = GraphQLFindUsagesUtil.getService().getIncludedFileTypes().toArray(FileType.EMPTY_ARRAY); searchScope = GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.projectScope(myProject), searchScopeFileTypes).union(allBuiltInSchemaScopes); project.getMessageBus().connect().subscribe(PsiManagerImpl.ANY_PSI_CHANGE_TOPIC, new AnyPsiChangeListener.Adapter() { @Override public void beforePsiChanged(boolean isPhysical) { // clear the cache on each PSI change fileNameToSchemaScope.clear(); } }); }
Example 4
Source File: EditorFoldingInfo.java From consulo with Apache License 2.0 | 5 votes |
@Nullable TextRange getPsiElementRange(@Nonnull FoldRegion region) { PsiElement element = getPsiElement(region); if (element == null) return null; PsiFile containingFile = element.getContainingFile(); InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(containingFile.getProject()); boolean isInjected = injectedManager.isInjectedFragment(containingFile); TextRange range = element.getTextRange(); if (isInjected) { range = injectedManager.injectedToHost(element, range); } return range; }
Example 5
Source File: BackspaceHandler.java From consulo with Apache License 2.0 | 5 votes |
private static boolean isOffsetInsideInjected(Editor injectedEditor, int injectedOffset) { if (injectedOffset == 0 || injectedOffset >= injectedEditor.getDocument().getTextLength()) { return false; } PsiFile injectedFile = ((EditorWindow)injectedEditor).getInjectedFile(); InjectedLanguageManager ilm = InjectedLanguageManager.getInstance(injectedFile.getProject()); TextRange rangeToEdit = new TextRange(injectedOffset - 1, injectedOffset+1); List<TextRange> editables = ilm.intersectWithAllEditableFragments(injectedFile, rangeToEdit); return editables.size() == 1 && editables.get(0).equals(rangeToEdit); }
Example 6
Source File: LocalInspectionsPass.java From consulo with Apache License 2.0 | 5 votes |
private void addHighlightsFromResults(@Nonnull List<HighlightInfo> outInfos, @Nonnull ProgressIndicator indicator) { InspectionProfile inspectionProfile = InspectionProjectProfileManager.getInstance(myProject).getInspectionProfile(); PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject); InjectedLanguageManager ilManager = InjectedLanguageManager.getInstance(myProject); Set<Pair<TextRange, String>> emptyActionRegistered = new THashSet<>(); for (Map.Entry<PsiFile, List<InspectionResult>> entry : result.entrySet()) { indicator.checkCanceled(); PsiFile file = entry.getKey(); Document documentRange = documentManager.getDocument(file); if (documentRange == null) continue; List<InspectionResult> resultList = entry.getValue(); synchronized (resultList) { for (InspectionResult inspectionResult : resultList) { indicator.checkCanceled(); LocalInspectionToolWrapper tool = inspectionResult.tool; HighlightSeverity severity = inspectionProfile.getErrorLevel(HighlightDisplayKey.find(tool.getShortName()), file).getSeverity(); for (ProblemDescriptor descriptor : inspectionResult.foundProblems) { indicator.checkCanceled(); PsiElement element = descriptor.getPsiElement(); if (element != null) { createHighlightsForDescriptor(outInfos, emptyActionRegistered, ilManager, file, documentRange, tool, severity, descriptor, element); } } } } } }
Example 7
Source File: LocalInspectionsPass.java From consulo with Apache License 2.0 | 5 votes |
private void addDescriptorsFromInjectedResults(@Nonnull InspectionManager iManager, @Nonnull GlobalInspectionContextImpl context) { InjectedLanguageManager ilManager = InjectedLanguageManager.getInstance(myProject); PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject); for (Map.Entry<PsiFile, List<InspectionResult>> entry : result.entrySet()) { PsiFile file = entry.getKey(); if (file == getFile()) continue; // not injected DocumentWindow documentRange = (DocumentWindow)documentManager.getDocument(file); List<InspectionResult> resultList = entry.getValue(); for (InspectionResult inspectionResult : resultList) { LocalInspectionToolWrapper toolWrapper = inspectionResult.tool; for (ProblemDescriptor descriptor : inspectionResult.foundProblems) { PsiElement psiElement = descriptor.getPsiElement(); if (psiElement == null) continue; if (SuppressionUtil.inspectionResultSuppressed(psiElement, toolWrapper.getTool())) continue; List<TextRange> editables = ilManager.intersectWithAllEditableFragments(file, ((ProblemDescriptorBase)descriptor).getTextRange()); for (TextRange editable : editables) { TextRange hostRange = documentRange.injectedToHost(editable); QuickFix[] fixes = descriptor.getFixes(); LocalQuickFix[] localFixes = null; if (fixes != null) { localFixes = new LocalQuickFix[fixes.length]; for (int k = 0; k < fixes.length; k++) { QuickFix fix = fixes[k]; localFixes[k] = (LocalQuickFix)fix; } } ProblemDescriptor patchedDescriptor = iManager.createProblemDescriptor(getFile(), hostRange, descriptor.getDescriptionTemplate(), descriptor.getHighlightType(), true, localFixes); addDescriptors(toolWrapper, patchedDescriptor, context); } } } } }
Example 8
Source File: LineMarkersPass.java From consulo with Apache License 2.0 | 5 votes |
private static void queryLineMarkersForInjected(@Nonnull PsiElement element, @Nonnull final PsiFile containingFile, @Nonnull Set<? super PsiFile> visitedInjectedFiles, @Nonnull final PairConsumer<? super PsiElement, ? super LineMarkerInfo<PsiElement>> consumer) { final InjectedLanguageManager manager = InjectedLanguageManager.getInstance(containingFile.getProject()); if (manager.isInjectedFragment(containingFile)) return; InjectedLanguageManager.getInstance(containingFile.getProject()).enumerateEx(element, containingFile, false, (injectedPsi, places) -> { if (!visitedInjectedFiles.add(injectedPsi)) return; // there may be several concatenated literals making the one injected file final Project project = injectedPsi.getProject(); Document document = PsiDocumentManager.getInstance(project).getCachedDocument(injectedPsi); if (!(document instanceof DocumentWindow)) return; List<PsiElement> injElements = CollectHighlightsUtil.getElementsInRange(injectedPsi, 0, injectedPsi.getTextLength()); final List<LineMarkerProvider> providers = getMarkerProviders(injectedPsi.getLanguage(), project); queryProviders(injElements, injectedPsi, providers, (injectedElement, injectedMarker) -> { GutterIconRenderer gutterRenderer = injectedMarker.createGutterRenderer(); TextRange injectedRange = new TextRange(injectedMarker.startOffset, injectedMarker.endOffset); List<TextRange> editables = manager.intersectWithAllEditableFragments(injectedPsi, injectedRange); for (TextRange editable : editables) { TextRange hostRange = manager.injectedToHost(injectedPsi, editable); Image icon = gutterRenderer == null ? null : gutterRenderer.getIcon(); GutterIconNavigationHandler<PsiElement> navigationHandler = injectedMarker.getNavigationHandler(); LineMarkerInfo<PsiElement> converted = new LineMarkerInfo<>(injectedElement, hostRange, icon, injectedMarker.updatePass, e -> injectedMarker.getLineMarkerTooltip(), navigationHandler, GutterIconRenderer.Alignment.RIGHT); consumer.consume(injectedElement, converted); } }); }); }
Example 9
Source File: HighlightUsagesHandler.java From consulo with Apache License 2.0 | 5 votes |
@Nullable public static TextRange getNameIdentifierRange(PsiFile file, PsiElement element) { final InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(element.getProject()); if (element instanceof PomTargetPsiElement) { final PomTarget target = ((PomTargetPsiElement)element).getTarget(); if (target instanceof PsiDeclaredTarget) { final PsiDeclaredTarget declaredTarget = (PsiDeclaredTarget)target; final TextRange range = declaredTarget.getNameIdentifierRange(); if (range != null) { if (range.getStartOffset() < 0 || range.getLength() <= 0) { return null; } final PsiElement navElement = declaredTarget.getNavigationElement(); if (PsiUtilBase.isUnderPsiRoot(file, navElement)) { return injectedManager.injectedToHost(navElement, range.shiftRight(navElement.getTextRange().getStartOffset())); } } } } if (!PsiUtilBase.isUnderPsiRoot(file, element)) { return null; } PsiElement identifier = IdentifierUtil.getNameIdentifier(element); if (identifier != null && PsiUtilBase.isUnderPsiRoot(file, identifier)) { return injectedManager.injectedToHost(identifier, identifier.getTextRange()); } return null; }
Example 10
Source File: TemplateLanguageErrorFilter.java From consulo with Apache License 2.0 | 5 votes |
@Override public boolean shouldHighlightErrorElement(@Nonnull PsiErrorElement element) { if (isKnownSubLanguage(element.getParent().getLanguage())) { // // Immediately discard filters with non-matching template class if already known // Class templateClass = element.getUserData(TEMPLATE_VIEW_PROVIDER_CLASS_KEY); if (templateClass != null && (templateClass != myTemplateFileViewProviderClass)) return true; PsiFile psiFile = element.getContainingFile(); int offset = element.getTextOffset(); InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(element.getProject()); if (injectedLanguageManager.isInjectedFragment(psiFile)) { PsiElement host = injectedLanguageManager.getInjectionHost(element); if (host != null) { psiFile = host.getContainingFile(); offset = injectedLanguageManager.injectedToHost(element, offset); } } final FileViewProvider viewProvider = psiFile.getViewProvider(); element.putUserData(TEMPLATE_VIEW_PROVIDER_CLASS_KEY, viewProvider.getClass()); if (!(viewProvider.getClass() == myTemplateFileViewProviderClass)) { return true; } // // An error can occur at template element or before it. Check both. // if (shouldIgnoreErrorAt(viewProvider, offset) || shouldIgnoreErrorAt(viewProvider, offset + 1)) return false; } return true; }
Example 11
Source File: OffsetsInFile.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull public OffsetsInFile toTopLevelFile() { InjectedLanguageManager manager = InjectedLanguageManager.getInstance(file.getProject()); PsiFile hostFile = manager.getTopLevelFile(file); if (hostFile == file) { return this; } else { return new OffsetsInFile(hostFile, offsets.mapOffsets(hostFile.getViewProvider().getDocument(), it -> manager.injectedToHost(file, it))); } }
Example 12
Source File: PsiCachedValue.java From consulo with Apache License 2.0 | 5 votes |
private boolean isVeryPhysical(@Nonnull PsiElement dependency) { if (!dependency.isValid()) { return false; } if (!dependency.isPhysical()) { return false; } // injected files are physical but can sometimes (look at you, completion) // be inexplicably injected into non-physical element, in which case PSI_MODIFICATION_COUNT doesn't change and thus can't be relied upon InjectedLanguageManager manager = InjectedLanguageManager.getInstance(myManager.getProject()); PsiFile topLevelFile = manager.getTopLevelFile(dependency); return topLevelFile != null && topLevelFile.isPhysical(); }
Example 13
Source File: MemberInplaceRenamer.java From consulo with Apache License 2.0 | 5 votes |
@Override protected boolean notSameFile(@Nullable VirtualFile file, @Nonnull PsiFile containingFile) { final PsiFile currentFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument()); if (currentFile == null) return true; InjectedLanguageManager manager = InjectedLanguageManager.getInstance(containingFile.getProject()); return manager.getTopLevelFile(containingFile) != manager.getTopLevelFile(currentFile); }
Example 14
Source File: InjectionUtils.java From BashSupport with Apache License 2.0 | 5 votes |
public static boolean walkInjection(PsiElement host, @NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place, boolean walkOn) { //fixme does this work on the escaped or unescpaed text? InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(host.getProject()); List<Pair<PsiElement, TextRange>> injectedPsiFiles = injectedLanguageManager.getInjectedPsiFiles(host); if (injectedPsiFiles != null) { for (Pair<PsiElement, TextRange> psi_range : injectedPsiFiles) { //fixme check lastParent ? walkOn &= psi_range.first.processDeclarations(processor, state, lastParent, place); } } return walkOn; }
Example 15
Source File: BashPsiUtils.java From BashSupport with Apache License 2.0 | 5 votes |
/** * Returns the start text offset of the element in the toplevel file, i.e the PsiFile which containing the real document. If an element * is injected then the outer file is returned. * * @param element The element to work on * @return The start text offset in the physical PsiFile, injected virtual PsiFiles are not used for text offset calculation */ public static int getFileTextOffset(@NotNull PsiElement element) { int offset = element.getTextOffset(); if (isInjectedElement(element)) { //fixme languageManager is probably expensive InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(element.getProject()); PsiLanguageInjectionHost injectionHost = languageManager.getInjectionHost(element); if (injectionHost != null) { offset += injectionHost.getTextOffset(); } } return offset; }
Example 16
Source File: InplaceRefactoring.java From consulo with Apache License 2.0 | 5 votes |
private boolean checkRangeContainsOffset(int offset, final TextRange textRange, PsiElement element) { int startOffset = element.getTextRange().getStartOffset(); final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(myProject); final PsiLanguageInjectionHost injectionHost = injectedLanguageManager.getInjectionHost(element); if (injectionHost != null) { final PsiElement nameIdentifier = getNameIdentifier(); final PsiLanguageInjectionHost initialInjectedHost = nameIdentifier != null ? injectedLanguageManager.getInjectionHost(nameIdentifier) : null; if (initialInjectedHost != null && initialInjectedHost != injectionHost) { return false; } } return textRange.shiftRight(startOffset).containsOffset(offset); }
Example 17
Source File: GraphQLJavascriptInjectionSearchHelper.java From js-graphql-intellij-plugin with MIT License | 5 votes |
/** * Uses the {@link GraphQLInjectionIndex} to process injected GraphQL PsiFiles * * @param scopedElement the starting point of the enumeration settings the scopedElement of the processing * @param schemaScope the search scope to use for limiting the schema definitions * @param consumer a consumer that will be invoked for each injected GraphQL PsiFile */ public void processInjectedGraphQLPsiFiles(PsiElement scopedElement, GlobalSearchScope schemaScope, Consumer<PsiFile> consumer) { try { final PsiManager psiManager = PsiManager.getInstance(scopedElement.getProject()); final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(scopedElement.getProject()); FileBasedIndex.getInstance().getFilesWithKey(GraphQLInjectionIndex.NAME, Collections.singleton(GraphQLInjectionIndex.DATA_KEY), virtualFile -> { final PsiFile fileWithInjection = psiManager.findFile(virtualFile); if (fileWithInjection != null) { fileWithInjection.accept(new PsiRecursiveElementVisitor() { @Override public void visitElement(PsiElement element) { if (GraphQLLanguageInjectionUtil.isJSGraphQLLanguageInjectionTarget(element)) { injectedLanguageManager.enumerate(element, (injectedPsi, places) -> { consumer.accept(injectedPsi); }); } else { // visit deeper until injection found super.visitElement(element); } } }); } return true; }, schemaScope); } catch (IndexNotReadyException e) { // can't search yet (e.g. during project startup) } }
Example 18
Source File: BashPsiUtils.java From BashSupport with Apache License 2.0 | 4 votes |
public static boolean isInjectedElement(@NotNull PsiElement element) { InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(element.getProject()); return languageManager.isInjectedFragment(element.getContainingFile()) || hasInjectionHostParent(element); }
Example 19
Source File: GraphQLScopeEditorNotificationProvider.java From js-graphql-intellij-plugin with MIT License | 4 votes |
private boolean showNotification(@NotNull VirtualFile file) { if (ApplicationManager.getApplication().isUnitTestMode()) { // injection enumerate below causes missing JSON schemas in 2019.2 for eslint return false; } if (!isGraphQLRelatedFile(file) || DumbService.getInstance(myProject).isDumb()) { return false; } final GraphQLConfigManager graphQLConfigManager = GraphQLConfigManager.getService(myProject); if (!graphQLConfigManager.isInitialized()) { return false; } if (file.getFileType() != GraphQLFileType.INSTANCE) { // for injected files, must contain graphql before the warning is shown final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file); final Ref<Boolean> hasGraphQL = new Ref<>(false); if (psiFile != null) { final InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(myProject); psiFile.accept(new PsiRecursiveElementVisitor() { @Override public void visitElement(PsiElement element) { if (hasGraphQL.get()) { return; } if (element instanceof PsiLanguageInjectionHost) { injectedLanguageManager.enumerate(element, (injectedPsi, places) -> { if (injectedPsi instanceof GraphQLFile) { hasGraphQL.set(true); } }); } else { super.visitElement(element); } } }); } if (!hasGraphQL.get()) { return false; } } if (GlobalSearchScope.projectScope(myProject).accept(file)) { if (graphQLConfigManager.getClosestConfigFile(file) != null) { if (graphQLConfigManager.getClosestIncludingConfigFile(file) == null) { // has config, but is not included return true; } } } return false; }
Example 20
Source File: InjectedLanguageManagerImpl.java From consulo with Apache License 2.0 | 4 votes |
public static InjectedLanguageManagerImpl getInstanceImpl(Project project) { return (InjectedLanguageManagerImpl)InjectedLanguageManager.getInstance(project); }