private static void registerForStringVarInitializer( @NotNull Disposable parent, @NotNull final Project project, final Language language, @NotNull @NonNls final String varName, @NonNls final String prefix, @NonNls final String suffix) { if (language == null) return; final ConcatenationAwareInjector injector = new ConcatenationAwareInjector() { @Override public void getLanguagesToInject( @NotNull MultiHostRegistrar injectionPlacesRegistrar, @NotNull PsiElement... operands) { PsiVariable variable = PsiTreeUtil.getParentOfType(operands[0], PsiVariable.class); if (variable == null) return; if (!varName.equals(variable.getName())) return; if (!(operands[0] instanceof PsiLiteralExpression)) return; boolean started = false; String prefixFromPrev = ""; for (int i = 0; i < operands.length; i++) { PsiElement operand = operands[i]; if (!(operand instanceof PsiLiteralExpression)) { continue; } Object value = ((PsiLiteralExpression) operand).getValue(); if (!(value instanceof String)) { prefixFromPrev += value; continue; } TextRange textRange = textRangeToInject((PsiLanguageInjectionHost) operand); if (!started) { injectionPlacesRegistrar.startInjecting(language); started = true; } injectionPlacesRegistrar.addPlace( prefixFromPrev + (i == 0 ? "" : prefix == null ? "" : prefix), i == operands.length - 1 ? null : suffix, (PsiLanguageInjectionHost) operand, textRange); prefixFromPrev = ""; } if (started) { injectionPlacesRegistrar.doneInjecting(); } } }; JavaConcatenationInjectorManager.getInstance(project).registerConcatenationInjector(injector); Disposer.register( parent, new Disposable() { @Override public void dispose() { boolean b = JavaConcatenationInjectorManager.getInstance(project) .unregisterConcatenationInjector(injector); assert b; } }); }
private static void registerForParameterValue( Disposable parent, final Project project, final Language language, final String paramName) { if (language == null) return; final ConcatenationAwareInjector injector = new ConcatenationAwareInjector() { @Override public void getLanguagesToInject( @NotNull MultiHostRegistrar injectionPlacesRegistrar, @NotNull PsiElement... operands) { PsiElement operand = operands[0]; if (!(operand instanceof PsiLiteralExpression)) return; if (!(operand.getParent() instanceof PsiExpressionList)) return; PsiExpressionList expressionList = (PsiExpressionList) operand.getParent(); int i = ArrayUtil.indexOf(expressionList.getExpressions(), operand); if (!(operand.getParent().getParent() instanceof PsiMethodCallExpression)) return; PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) operand.getParent().getParent(); PsiMethod method = methodCallExpression.resolveMethod(); if (method == null) return; PsiParameter[] parameters = method.getParameterList().getParameters(); if (i >= parameters.length) return; PsiParameter parameter = parameters[i]; if (!paramName.equals(parameter.getName())) return; TextRange textRange = textRangeToInject((PsiLanguageInjectionHost) operand); injectionPlacesRegistrar .startInjecting(language) .addPlace(null, null, (PsiLanguageInjectionHost) operand, textRange) .doneInjecting(); } }; JavaConcatenationInjectorManager.getInstance(project).registerConcatenationInjector(injector); Disposer.register( parent, new Disposable() { @Override public void dispose() { boolean b = JavaConcatenationInjectorManager.getInstance(project) .unregisterConcatenationInjector(injector); assert b; } }); }