001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.psi;
018
019 import com.intellij.lang.ASTNode;
020 import com.intellij.psi.PsiElementVisitor;
021 import com.intellij.psi.PsiFile;
022 import com.intellij.psi.impl.source.tree.LazyParseablePsiElement;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.annotations.Nullable;
025 import org.jetbrains.kotlin.KtNodeTypes;
026 import org.jetbrains.kotlin.lexer.KtTokens;
027
028 import java.util.List;
029
030 public class KtLambdaExpression extends LazyParseablePsiElement implements KtExpression {
031 public KtLambdaExpression(CharSequence text) {
032 super(KtNodeTypes.LAMBDA_EXPRESSION, text);
033 }
034
035 @Override
036 public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
037 return visitor.visitLambdaExpression(this, data);
038 }
039
040 @NotNull
041 public KtFunctionLiteral getFunctionLiteral() {
042 return findChildByType(KtNodeTypes.FUNCTION_LITERAL).getPsi(KtFunctionLiteral.class);
043 }
044
045 @NotNull
046 public List<KtParameter> getValueParameters() {
047 return getFunctionLiteral().getValueParameters();
048 }
049
050 @Nullable
051 public KtBlockExpression getBodyExpression() {
052 return getFunctionLiteral().getBodyExpression();
053 }
054
055 public boolean hasDeclaredReturnType() {
056 return getFunctionLiteral().getTypeReference() != null;
057 }
058
059 @NotNull
060 public KtElement asElement() {
061 return this;
062 }
063
064 @NotNull
065 public ASTNode getLeftCurlyBrace() {
066 return getFunctionLiteral().getNode().findChildByType(KtTokens.LBRACE);
067 }
068
069 @Nullable
070 public ASTNode getRightCurlyBrace() {
071 return getFunctionLiteral().getNode().findChildByType(KtTokens.RBRACE);
072 }
073
074 @NotNull
075 @Override
076 public KtFile getContainingKtFile() {
077 PsiFile file = getContainingFile();
078 assert file instanceof KtFile : "KtElement not inside KtFile: " + file + " " + file.getText();
079 return (KtFile) file;
080 }
081
082 @Override
083 public <D> void acceptChildren(@NotNull KtVisitor<Void, D> visitor, D data) {
084 KtPsiUtil.visitChildren(this, visitor, data);
085 }
086
087 @Override
088 public final void accept(@NotNull PsiElementVisitor visitor) {
089 if (visitor instanceof KtVisitor) {
090 accept((KtVisitor) visitor, null);
091 }
092 else {
093 visitor.visitElement(this);
094 }
095 }
096
097 @Override
098 public String toString() {
099 return getNode().getElementType().toString();
100 }
101
102 @NotNull
103 @Override
104 public KtElement getPsiOrParent() {
105 return this;
106 }
107 }