001 /* 002 * Copyright 2010-2013 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.jet.lang.psi; 018 019 import com.intellij.lang.ASTNode; 020 import com.intellij.psi.ElementManipulators; 021 import com.intellij.psi.LiteralTextEscaper; 022 import com.intellij.psi.PsiLanguageInjectionHost; 023 import com.intellij.psi.tree.TokenSet; 024 import org.jetbrains.annotations.NotNull; 025 import org.jetbrains.jet.JetNodeTypes; 026 import org.jetbrains.jet.lexer.JetTokens; 027 028 029 public class JetStringTemplateExpression extends JetExpressionImpl implements PsiLanguageInjectionHost { 030 private static final TokenSet TOKENS_SUITABLE_FOR_INJECTION = TokenSet.create(JetNodeTypes.LITERAL_STRING_TEMPLATE_ENTRY, JetNodeTypes.ESCAPE_STRING_TEMPLATE_ENTRY); 031 032 public JetStringTemplateExpression(@NotNull ASTNode node) { 033 super(node); 034 } 035 036 @Override 037 public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) { 038 return visitor.visitStringTemplateExpression(this, data); 039 } 040 041 @NotNull 042 public JetStringTemplateEntry[] getEntries() { 043 return findChildrenByClass(JetStringTemplateEntry.class); 044 } 045 046 @Override 047 public boolean isValidHost() { 048 ASTNode node = getNode(); 049 ASTNode child = node.getFirstChildNode().getTreeNext(); 050 while (child != null) { 051 if (child.getElementType() == JetTokens.CLOSING_QUOTE) return true; 052 if (!TOKENS_SUITABLE_FOR_INJECTION.contains(child.getElementType())) return false; 053 child = child.getTreeNext(); 054 } 055 return false; 056 } 057 058 @Override 059 public PsiLanguageInjectionHost updateText(@NotNull String text) { 060 return ElementManipulators.handleContentChange(this, text); 061 } 062 063 @NotNull 064 @Override 065 public LiteralTextEscaper<? extends PsiLanguageInjectionHost> createLiteralTextEscaper() { 066 return new KotlinStringLiteralTextEscaper(this); 067 } 068 }