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
.impl
.source
.jsp
.jspJava
.JspMethodCall
;
21 import com
.intellij
.util
.IncorrectOperationException
;
22 import com
.intellij
.util
.text
.CharArrayUtil
;
25 * Created by IntelliJ IDEA.
29 * To change this template use Options | File Templates.
31 public class MethodCallFixer
implements Fixer
{
32 public void apply(Editor editor
, JavaSmartEnterProcessor processor
, PsiElement psiElement
) throws IncorrectOperationException
{
33 PsiExpressionList args
= null;
34 if (psiElement
instanceof PsiMethodCallExpression
&& !(psiElement
instanceof JspMethodCall
)) {
35 args
= ((PsiMethodCallExpression
) psiElement
).getArgumentList();
36 } else if (psiElement
instanceof PsiNewExpression
) {
37 args
= ((PsiNewExpression
) psiElement
).getArgumentList();
40 if (args
== null) return;
42 PsiElement parenth
= args
.getLastChild();
44 if (parenth
== null || !")".equals(parenth
.getText())) {
46 PsiElement child
= args
.getFirstChild();
47 while (child
!= null) {
48 if (child
instanceof PsiErrorElement
) {
49 final PsiErrorElement errorElement
= (PsiErrorElement
)child
;
50 if (errorElement
.getErrorDescription().indexOf("')'") >= 0) {
51 endOffset
= errorElement
.getTextRange().getStartOffset();
55 child
= child
.getNextSibling();
58 if (endOffset
== -1) {
59 endOffset
= args
.getTextRange().getEndOffset();
62 final PsiExpression
[] params
= args
.getExpressions();
63 if (params
.length
> 0 && startLine(editor
, args
) != startLine(editor
, params
[0])) {
64 endOffset
= args
.getTextRange().getStartOffset() + 1;
67 endOffset
= CharArrayUtil
.shiftBackward(editor
.getDocument().getCharsSequence(), endOffset
- 1, " \t\n") + 1;
68 editor
.getDocument().insertString(endOffset
, ")");
72 private int startLine(Editor editor
, PsiElement psiElement
) {
73 return editor
.getDocument().getLineNumber(psiElement
.getTextRange().getStartOffset());