void (@NotNull final XPathVariable variable, Editor editor) { final String type = LanguageFindUsages.getType(variable); final Project project = variable.getProject(); final XmlTag tag = ((XsltElement)variable).getTag(); final String expression = tag.getAttributeValue("select"); if (expression == null) { CommonRefactoringUtil.showErrorHint(project, editor, XPathBundle.message("dialog.message.x.has.no.value", StringUtil.capitalize(type), variable.getName()), XPathBundle.message("dialog.title.xslt.inline"), null); return; } final Collection<PsiReference> references = ReferencesSearch.search(variable, new LocalSearchScope(tag.getParentTag()), false).findAll(); if (references.size() == 0) { CommonRefactoringUtil.showErrorHint(project, editor, XPathBundle.message("dialog.message.never.used", StringUtil.capitalize(type),variable.getName()), XPathBundle.message("dialog.title.xslt.inline"), null); return; } boolean hasExternalRefs = false; if (XsltSupport.isTopLevelElement(tag)) { final Query<PsiReference> query = ReferencesSearch.search(variable, GlobalSearchScope.allScope(project), false); hasExternalRefs = !query.forEach(new Processor<>() { int allRefs = 0; @Override public boolean process(PsiReference psiReference) { if (++allRefs > references.size()) { return false; } else if (!references.contains(psiReference)) { return false; } return true; } }); } final HighlightManager highlighter = HighlightManager.getInstance(project); final ArrayList<RangeHighlighter> highlighters = new ArrayList<>(); final PsiReference[] psiReferences = references.toArray(PsiReference.EMPTY_ARRAY); TextRange[] ranges = ContainerUtil.map2Array(psiReferences, TextRange.class, s -> { final PsiElement psiElement = s.getElement(); final XmlAttributeValue context = PsiTreeUtil.getContextOfType(psiElement, XmlAttributeValue.class, true); if (psiElement instanceof XPathElement && context != null) { return XsltCodeInsightUtil.getRangeInsideHostingFile((XPathElement)psiElement).cutOut(s.getRangeInElement()); } return psiElement.getTextRange().cutOut(s.getRangeInElement()); }); final Editor e = editor instanceof EditorWindow ? ((EditorWindow)editor).getDelegate() : editor; for (TextRange range : ranges) { final TextAttributes textAttributes = EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(); final Color color = getScrollmarkColor(textAttributes); highlighter.addOccurrenceHighlight(e, range.getStartOffset(), range.getEndOffset(), textAttributes, HighlightManagerImpl.HIDE_BY_ESCAPE, highlighters, color); } highlighter.addOccurrenceHighlights(e, new PsiElement[]{((XsltVariable)variable).getNameIdentifier()}, EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES, false, highlighters); if (!hasExternalRefs) { @NlsContexts.DialogMessage String message = XPathBundle.message("dialog.message.inline.variable", type, variable.getName(), references.size()); if (!ApplicationManager.getApplication().isUnitTestMode() && !MessageDialogBuilder.yesNo(XPathBundle.message("dialog.title.xslt.inline"), message).ask(project)) { return; } } else { @NlsContexts.DialogMessage String message = XPathBundle.message("dialog.message.inline.local.variable", type, variable.getName(), references.size()); if (!ApplicationManager.getApplication().isUnitTestMode() && !MessageDialogBuilder.yesNo(XPathBundle.message("dialog.title.xslt.inline"), message).icon(Messages.getWarningIcon()).ask(project)) { return; } } final boolean hasRefs = hasExternalRefs; WriteCommandAction.writeCommandAction(project, tag.getContainingFile()).withName(XPathBundle.message("dialog.title.xslt.inline")).run(() -> { try { for (PsiReference psiReference : references) { final PsiElement element = psiReference.getElement(); if (element instanceof XPathElement) { final XPathElement newExpr = XPathChangeUtil.createExpression(element, expression); element.replace(newExpr); } else { assert false; } } if (!hasRefs) { tag.delete(); } } catch (IncorrectOperationException ex) { Logger.getInstance(VariableInlineHandler.class.getName()).error(ex); } }); }