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
.codeInsight
.generation
.surroundWith
;
18 import com
.intellij
.codeInsight
.CodeInsightBundle
;
19 import com
.intellij
.codeInsight
.guess
.GuessManager
;
20 import com
.intellij
.codeInsight
.lookup
.LookupElement
;
21 import com
.intellij
.codeInsight
.lookup
.PsiTypeLookupItem
;
22 import com
.intellij
.codeInsight
.template
.*;
23 import com
.intellij
.openapi
.editor
.Editor
;
24 import com
.intellij
.openapi
.editor
.ScrollType
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.openapi
.util
.TextRange
;
27 import com
.intellij
.psi
.PsiExpression
;
28 import com
.intellij
.psi
.PsiType
;
29 import com
.intellij
.util
.IncorrectOperationException
;
30 import org
.jetbrains
.annotations
.NonNls
;
32 import java
.util
.LinkedHashSet
;
35 class JavaWithCastSurrounder
extends JavaExpressionSurrounder
{
36 @NonNls private static final String TYPE_TEMPLATE_VARIABLE
= "type";
38 public boolean isApplicable(PsiExpression expr
) {
42 public TextRange
surroundExpression(final Project project
, final Editor editor
, PsiExpression expr
) throws IncorrectOperationException
{
43 PsiType
[] types
= GuessManager
.getInstance(project
).guessTypeToCast(expr
);
44 final Template template
= generateTemplate(project
, expr
.getText(), types
);
45 TextRange range
= expr
.getTextRange();
46 editor
.getDocument().deleteString(range
.getStartOffset(), range
.getEndOffset());
47 editor
.getCaretModel().moveToOffset(range
.getStartOffset());
48 editor
.getScrollingModel().scrollToCaret(ScrollType
.RELATIVE
);
49 TemplateManager
.getInstance(project
).startTemplate(editor
, template
);
53 private static Template
generateTemplate(Project project
, String exprText
, final PsiType
[] suggestedTypes
) {
54 final TemplateManager templateManager
= TemplateManager
.getInstance(project
);
55 final Template template
= templateManager
.createTemplate("", "");
56 template
.setToReformat(true);
58 Set
<LookupElement
> itemSet
= new LinkedHashSet
<LookupElement
>();
59 for (PsiType type
: suggestedTypes
) {
60 itemSet
.add(PsiTypeLookupItem
.createLookupItem(type
));
62 final LookupElement
[] lookupItems
= itemSet
.toArray(new LookupElement
[itemSet
.size()]);
64 final Result result
= suggestedTypes
.length
> 0 ?
new PsiTypeResult(suggestedTypes
[0], project
) : null;
66 Expression expr
= new Expression() {
67 public LookupElement
[] calculateLookupItems(ExpressionContext context
) {
68 return lookupItems
.length
> 1 ? lookupItems
: null;
71 public Result
calculateResult(ExpressionContext context
) {
75 public Result
calculateQuickResult(ExpressionContext context
) {
79 template
.addTextSegment("((");
80 template
.addVariable(TYPE_TEMPLATE_VARIABLE
, expr
, expr
, true);
81 template
.addTextSegment(")" + exprText
+ ")");
82 template
.addEndVariable();
87 public String
getTemplateDescription() {
88 return CodeInsightBundle
.message("surround.with.cast.template");