boolean (@NotNull Project project, @Nullable final PsiModifierListOwner modifierListOwner, @NotNull final PsiLanguageInjectionHost host, final String languageId, Processor<? super PsiLanguageInjectionHost> annotationFixer) { if (modifierListOwner == null) return false; final Task.WithResult<Boolean, RuntimeException> task = new Task.WithResult<>(project, IntelliLangBundle.message("progress.looking.for", AnnotationUtil.LANGUAGE), true) { @Override protected Boolean compute(@NotNull ProgressIndicator indicator) { return ReadAction.nonBlocking(() -> isAnnotationsJarInPath(ModuleUtilCore.findModuleForPsiElement(modifierListOwner))) .executeSynchronously(); } }; final boolean addAnnotation = ProgressManager.getInstance().run(task) && PsiUtil.isLanguageLevel5OrHigher(modifierListOwner) && modifierListOwner.getModifierList() != null; final PsiElement statement = PsiTreeUtil.getParentOfType(host, PsiStatement.class, PsiField.class); if (!addAnnotation && statement == null) return false; Configuration.AdvancedConfiguration configuration = Configuration.getProjectInstance(project).getAdvancedConfiguration(); if (!configuration.isSourceModificationAllowed()) { String fixText = addAnnotation ? IntelliLangBundle.message("intelliLang.suggest.insert.annotation") : IntelliLangBundle.message("intelliLang.suggest.insert.comment"); InjectLanguageAction.addFixer(host, annotationFixer, fixText); return false; } WriteCommandAction.writeCommandAction(project, modifierListOwner.getContainingFile()).run(() -> { PsiElementFactory javaFacade = JavaPsiFacade.getElementFactory(project); if (addAnnotation) { JVMElementFactory factory = ObjectUtils.chooseNotNull(JVMElementFactories.getFactory(modifierListOwner.getLanguage(), project), javaFacade); PsiAnnotation annotation = factory.createAnnotationFromText("@" + AnnotationUtil.LANGUAGE + "(\"" + languageId + "\")", modifierListOwner); PsiModifierList list = Objects.requireNonNull(modifierListOwner.getModifierList()); final PsiAnnotation existingAnnotation = list.findAnnotation(AnnotationUtil.LANGUAGE); if (existingAnnotation != null) { existingAnnotation.replace(annotation); } else { list.addAfter(annotation, null); } JavaCodeStyleManager.getInstance(project).shortenClassReferences(list); } else { statement.getParent().addBefore(javaFacade.createCommentFromText("//language=" + languageId, host), statement); } }); return true; }