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
.editorActions
.smartEnter
;
18 import com
.intellij
.openapi
.editor
.Editor
;
19 import com
.intellij
.psi
.*;
20 import com
.intellij
.psi
.util
.PsiTreeUtil
;
21 import com
.intellij
.util
.IncorrectOperationException
;
24 * Created by IntelliJ IDEA.
28 * To change this template use Options | File Templates.
30 @SuppressWarnings({"HardCodedStringLiteral"})
31 public class MissingReturnExpressionFixer
implements Fixer
{
32 public void apply(Editor editor
, JavaSmartEnterProcessor processor
, PsiElement psiElement
)
33 throws IncorrectOperationException
{
34 if (psiElement
instanceof PsiReturnStatement
) {
35 PsiReturnStatement retStatement
= (PsiReturnStatement
) psiElement
;
36 if (retStatement
.getReturnValue() != null &&
37 startLine(editor
, retStatement
) == startLine(editor
, retStatement
.getReturnValue())) {
41 PsiElement parent
= PsiTreeUtil
.getParentOfType(psiElement
, PsiClassInitializer
.class, PsiMethod
.class);
42 if (parent
instanceof PsiMethod
) {
43 PsiMethod method
= (PsiMethod
) parent
;
44 final PsiType returnType
= method
.getReturnType();
45 if (returnType
!= null && returnType
!= PsiType
.VOID
) {
46 final int startOffset
= retStatement
.getTextRange().getStartOffset();
47 if (retStatement
.getReturnValue() != null) {
48 editor
.getDocument().insertString(startOffset
+ "return".length(), ";");
51 processor
.registerUnresolvedError(startOffset
+ "return".length());
57 private int startLine(Editor editor
, PsiElement psiElement
) {
58 return editor
.getDocument().getLineNumber(psiElement
.getTextRange().getStartOffset());