update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInsight / daemon / impl / quickfix / QuickFixAction.java
blob3402d1c854eeb540e7261823d298d44f932ccb8b
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.codeInsight.daemon.impl.quickfix;
19 import com.intellij.codeInsight.daemon.HighlightDisplayKey;
20 import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl;
21 import com.intellij.codeInsight.daemon.impl.HighlightInfo;
22 import com.intellij.codeInsight.intention.IntentionAction;
23 import com.intellij.codeInspection.HintAction;
24 import com.intellij.openapi.editor.Editor;
25 import com.intellij.openapi.editor.RangeMarker;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.util.Condition;
28 import com.intellij.openapi.util.Pair;
29 import com.intellij.openapi.util.TextRange;
30 import com.intellij.psi.PsiFile;
31 import org.jetbrains.annotations.NotNull;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Iterator;
36 import java.util.List;
38 /**
39 * @author Alexey Kudravtsev
41 public final class QuickFixAction {
42 private QuickFixAction() {
45 public static void registerQuickFixAction(HighlightInfo info, IntentionAction action, HighlightDisplayKey key) {
46 registerQuickFixAction(info, null, action, key);
49 public static void registerQuickFixAction(HighlightInfo info, IntentionAction action) {
50 registerQuickFixAction(info, null, action, null);
53 @Deprecated
54 public static void registerQuickFixAction(HighlightInfo info, IntentionAction action, List<IntentionAction> options, String displayName) {
55 if (info == null || action == null) return;
56 final TextRange fixRange = new TextRange(info.startOffset, info.endOffset);
57 if (info.quickFixActionRanges == null) {
58 info.quickFixActionRanges = new ArrayList<Pair<HighlightInfo.IntentionActionDescriptor, TextRange>>();
60 info.quickFixActionRanges.add(Pair.create(new HighlightInfo.IntentionActionDescriptor(action, options, displayName), fixRange));
61 info.fixStartOffset = Math.min (info.fixStartOffset, fixRange.getStartOffset());
62 info.fixEndOffset = Math.max (info.fixEndOffset, fixRange.getEndOffset());
65 public static void registerQuickFixAction(HighlightInfo info, TextRange fixRange, IntentionAction action, final HighlightDisplayKey key) {
66 if (info == null || action == null) return;
67 if (fixRange == null) fixRange = new TextRange(info.startOffset, info.endOffset);
68 if (info.quickFixActionRanges == null) {
69 info.quickFixActionRanges = new ArrayList<Pair<HighlightInfo.IntentionActionDescriptor, TextRange>>();
71 info.quickFixActionRanges.add(Pair.create(new HighlightInfo.IntentionActionDescriptor(action, key), fixRange));
72 info.fixStartOffset = Math.min (info.fixStartOffset, fixRange.getStartOffset());
73 info.fixEndOffset = Math.max (info.fixEndOffset, fixRange.getEndOffset());
74 if (action instanceof HintAction) {
75 info.setHint(true);
79 public static void unregisterQuickFixAction(HighlightInfo info, Condition<IntentionAction> condition) {
80 for (Iterator<Pair<HighlightInfo.IntentionActionDescriptor, TextRange>> it = info.quickFixActionRanges.iterator(); it.hasNext();) {
81 Pair<HighlightInfo.IntentionActionDescriptor, TextRange> pair = it.next();
82 if (condition.value(pair.first.getAction())) {
83 it.remove();
88 /**
89 * Is invoked inside atomic action.
91 @NotNull
92 public static List<HighlightInfo.IntentionActionDescriptor> getAvailableActions(@NotNull Editor editor, @NotNull PsiFile file, final int passId) {
93 int offset = editor.getCaretModel().getOffset();
94 final Project project = file.getProject();
96 List<HighlightInfo.IntentionActionDescriptor> result = new ArrayList<HighlightInfo.IntentionActionDescriptor>();
97 List<HighlightInfo> infos = DaemonCodeAnalyzerImpl.getHighlightsAround(editor.getDocument(), project, offset);
98 int[] groups = passId == -1 ? null : new int[]{passId};
99 for (HighlightInfo info : infos) {
100 addAvailableActionsForGroups(info, editor, file, result, groups, offset);
102 return result;
105 private static void addAvailableActionsForGroups(HighlightInfo info, Editor editor, PsiFile file, List<HighlightInfo.IntentionActionDescriptor> outList,
106 int[] groups,
107 int offset) {
108 if (info == null || info.quickFixActionMarkers == null) return;
109 if (groups != null && Arrays.binarySearch(groups, info.group) < 0) return;
110 for (Pair<HighlightInfo.IntentionActionDescriptor, RangeMarker> pair : info.quickFixActionMarkers) {
111 HighlightInfo.IntentionActionDescriptor actionInGroup = pair.first;
112 RangeMarker range = pair.second;
113 if (range.isValid()) {
114 int start = range.getStartOffset();
115 int end = range.getEndOffset();
116 final Project project = file.getProject();
117 if (start <= offset && offset <= end && actionInGroup.getAction().isAvailable(project, editor, file)) {
118 outList.add(actionInGroup);