use of com.intellij.openapi.editor.markup.RangeHighlighter in project kotlin by JetBrains.
the class KotlinInplaceVariableIntroducer method runWriteActionAndRestartRefactoring.
protected final void runWriteActionAndRestartRefactoring(final Runnable runnable) {
final Ref<Boolean> greedyToRight = new Ref<Boolean>();
new WriteCommandAction(myProject, getCommandName(), getCommandName()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
PsiDocumentManager.getInstance(myProject).commitDocument(myEditor.getDocument());
ASTNode identifier = myDeclaration.getNode().findChildByType(KtTokens.IDENTIFIER);
if (identifier != null) {
TextRange range = identifier.getTextRange();
RangeHighlighter[] highlighters = myEditor.getMarkupModel().getAllHighlighters();
for (RangeHighlighter highlighter : highlighters) {
if (highlighter.getStartOffset() == range.getStartOffset()) {
if (highlighter.getEndOffset() == range.getEndOffset()) {
greedyToRight.set(highlighter.isGreedyToRight());
highlighter.setGreedyToRight(false);
}
}
}
}
runnable.run();
TemplateState templateState = TemplateManagerImpl.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(myEditor));
if (templateState != null) {
myEditor.putUserData(INTRODUCE_RESTART, true);
templateState.cancelTemplate();
}
}
}.execute();
ApplicationManager.getApplication().runReadAction(new Runnable() {
@Override
public void run() {
ASTNode identifier = myDeclaration.getNode().findChildByType(KtTokens.IDENTIFIER);
if (identifier != null) {
TextRange range = identifier.getTextRange();
RangeHighlighter[] highlighters = myEditor.getMarkupModel().getAllHighlighters();
for (RangeHighlighter highlighter : highlighters) {
if (highlighter.getStartOffset() == range.getStartOffset()) {
if (highlighter.getEndOffset() == range.getEndOffset()) {
highlighter.setGreedyToRight(greedyToRight.get());
}
}
}
}
}
});
if (myEditor.getUserData(INTRODUCE_RESTART) == Boolean.TRUE) {
myInitialName = myDeclaration.getName();
performInplaceRefactoring(getSuggestionsForNextRun());
}
}
use of com.intellij.openapi.editor.markup.RangeHighlighter in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method highlightOccurrences.
public static void highlightOccurrences(Project project, @Nullable Editor editor, PsiElement[] elements) {
if (editor == null)
return;
ArrayList<RangeHighlighter> highlighters = new ArrayList<>();
HighlightManager highlightManager = HighlightManager.getInstance(project);
EditorColorsManager colorsManager = EditorColorsManager.getInstance();
TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
if (elements.length > 0) {
highlightManager.addOccurrenceHighlights(editor, elements, attributes, false, highlighters);
}
}
use of com.intellij.openapi.editor.markup.RangeHighlighter in project intellij-community by JetBrains.
the class BaseIntroduceAction method extractFromExpression.
private void extractFromExpression(Editor e, final XPathExpression expression) {
final Editor editor = (e instanceof EditorWindow) ? ((EditorWindow) e).getDelegate() : e;
final HighlightManager highlightManager = HighlightManager.getInstance(expression.getProject());
final Set<XPathExpression> matchingExpressions = RefactoringUtil.collectMatchingExpressions(expression);
final List<XmlTag> otherMatches = new ArrayList<>(matchingExpressions.size());
final ArrayList<RangeHighlighter> highlighters = new ArrayList<>(matchingExpressions.size() + 1);
if (matchingExpressions.size() > 0) {
final SelectionModel selectionModel = editor.getSelectionModel();
highlightManager.addRangeHighlight(editor, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd(), EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
for (XPathExpression expr : matchingExpressions) {
final TextRange range = XsltCodeInsightUtil.getRangeInsideHostingFile(expr);
highlightManager.addRangeHighlight(editor, range.getStartOffset(), range.getEndOffset(), EditorColors.SEARCH_RESULT_ATTRIBUTES.getDefaultAttributes(), false, highlighters);
final XmlTag tag = PsiTreeUtil.getContextOfType(expr, XmlTag.class, true);
assert tag != null;
otherMatches.add(tag);
}
}
final Settings dlg = getSettings(expression, matchingExpressions);
if (dlg == null || dlg.isCanceled())
return;
if (getCommandName() != null) {
new WriteCommandAction.Simple(e.getProject(), getCommandName()) {
protected void run() throws Throwable {
if (extractImpl(expression, matchingExpressions, otherMatches, dlg)) {
for (RangeHighlighter highlighter : highlighters) {
highlighter.dispose();
}
}
}
}.execute();
} else {
extractImpl(expression, matchingExpressions, otherMatches, dlg);
}
}
use of com.intellij.openapi.editor.markup.RangeHighlighter in project intellij-community by JetBrains.
the class VariableInlineHandler method invoke.
public static void invoke(@NotNull final XPathVariable variable, Editor editor) {
final String type = LanguageFindUsages.INSTANCE.forLanguage(variable.getLanguage()).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, MessageFormat.format("{0} ''{1}'' has no value.", StringUtil.capitalize(type), variable.getName()), TITLE, null);
return;
}
final Collection<PsiReference> references = ReferencesSearch.search(variable, new LocalSearchScope(tag.getParentTag()), false).findAll();
if (references.size() == 0) {
CommonRefactoringUtil.showErrorHint(project, editor, MessageFormat.format("{0} ''{1}'' is never used.", variable.getName()), TITLE, 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<PsiReference>() {
int allRefs = 0;
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(new PsiReference[references.size()]);
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.getDefaultAttributes(), false, highlighters);
if (!hasExternalRefs) {
if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} occurrence{3})", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getQuestionIcon()) != Messages.YES) {
return;
}
} else {
if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(MessageFormat.format("Inline {0} ''{1}''? ({2} local occurrence{3})\n" + "\nWarning: It is being used in external files. Its declaration will not be removed.", type, variable.getName(), String.valueOf(references.size()), references.size() > 1 ? "s" : ""), TITLE, Messages.getWarningIcon()) != Messages.YES) {
return;
}
}
final boolean hasRefs = hasExternalRefs;
new WriteCommandAction.Simple(project, "XSLT.Inline", tag.getContainingFile()) {
@Override
protected void run() throws Throwable {
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 e) {
Logger.getInstance(VariableInlineHandler.class.getName()).error(e);
}
}
}.execute();
}
use of com.intellij.openapi.editor.markup.RangeHighlighter in project intellij-community by JetBrains.
the class XPathEvalAction method highlightElement.
private static int highlightElement(Editor editor, PsiElement element, Config cfg, int offset) {
final RangeHighlighter highlighter = HighlighterUtil.highlightNode(editor, element, cfg.getAttributes(), cfg);
HighlighterUtil.addHighlighter(editor, highlighter);
return Math.min(highlighter.getStartOffset(), offset);
}
Aggregations