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.
16 package com
.intellij
.slicer
;
18 import com
.intellij
.analysis
.AnalysisScope
;
19 import com
.intellij
.analysis
.AnalysisUIOptions
;
20 import com
.intellij
.analysis
.BaseAnalysisActionDialog
;
21 import com
.intellij
.codeInsight
.CodeInsightActionHandler
;
22 import com
.intellij
.codeInsight
.TargetElementUtilBase
;
23 import com
.intellij
.codeInsight
.hint
.HintManager
;
24 import com
.intellij
.openapi
.editor
.Editor
;
25 import com
.intellij
.openapi
.module
.Module
;
26 import com
.intellij
.openapi
.module
.ModuleUtil
;
27 import com
.intellij
.openapi
.project
.Project
;
28 import com
.intellij
.psi
.*;
29 import com
.intellij
.psi
.util
.PsiTreeUtil
;
30 import org
.jetbrains
.annotations
.NotNull
;
31 import org
.jetbrains
.annotations
.Nullable
;
36 public class SliceHandler
implements CodeInsightActionHandler
{
37 private final boolean myDataFlowToThis
;
39 public SliceHandler(boolean dataFlowToThis
) {
40 myDataFlowToThis
= dataFlowToThis
;
43 public void invoke(@NotNull final Project project
, @NotNull final Editor editor
, @NotNull final PsiFile file
) {
44 PsiDocumentManager
.getInstance(project
).commitAllDocuments(); // prevents problems with smart pointers creation
45 PsiElement expression
= getExpressionAtCaret(editor
, file
);
46 if (expression
== null) {
47 HintManager
.getInstance().showErrorHint(editor
, "Cannot find what to analyze. Please stand on the expression or variable or method parameter and try again.");
51 SliceManager sliceManager
= SliceManager
.getInstance(project
);
52 sliceManager
.slice(expression
,myDataFlowToThis
, this);
55 public boolean startInWriteAction() {
60 public PsiElement
getExpressionAtCaret(final Editor editor
, final PsiFile file
) {
61 int offset
= TargetElementUtilBase
.adjustOffset(editor
.getDocument(), editor
.getCaretModel().getOffset());
65 PsiElement atCaret
= file
.findElementAt(offset
);
67 PsiElement element
= PsiTreeUtil
.getParentOfType(atCaret
, PsiExpression
.class, PsiVariable
.class);
68 if (myDataFlowToThis
&& element
instanceof PsiLiteralExpression
) return null;
72 public SliceAnalysisParams
askForParams(PsiElement element
, boolean dataFlowToThis
, SliceManager
.StoredSettingsBean storedSettingsBean
, String dialogTitle
) {
73 AnalysisScope analysisScope
= new AnalysisScope(element
.getContainingFile());
74 Module module
= ModuleUtil
.findModuleForPsiElement(element
);
75 String name
= module
== null ?
null : module
.getName();
77 Project myProject
= element
.getProject();
78 AnalysisUIOptions analysisUIOptions
= new AnalysisUIOptions();
79 analysisUIOptions
.save(storedSettingsBean
.analysisUIOptions
);
81 BaseAnalysisActionDialog dialog
= new BaseAnalysisActionDialog(dialogTitle
, "Analyze scope", myProject
, analysisScope
, name
, true, analysisUIOptions
,
84 if (!dialog
.isOK()) return null;
86 storedSettingsBean
.analysisUIOptions
.save(analysisUIOptions
);
88 AnalysisScope scope
= dialog
.getScope(analysisUIOptions
, analysisScope
, myProject
, module
);
90 SliceAnalysisParams params
= new SliceAnalysisParams();
92 params
.dataFlowToThis
= dataFlowToThis
;