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.PsiComment;
021 import com.intellij.psi.PsiElement;
022 import com.intellij.psi.PsiWhiteSpace;
023 import com.intellij.psi.util.PsiTreeUtil;
024 import com.intellij.util.IncorrectOperationException;
025 import org.jetbrains.annotations.NotNull;
026 import org.jetbrains.kotlin.lexer.KtTokens;
027 import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
028 import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes;
029
030 import java.util.Arrays;
031 import java.util.List;
032
033 public class KtSuperTypeList extends KtElementImplStub<KotlinPlaceHolderStub<KtSuperTypeList>> {
034 public KtSuperTypeList(@NotNull ASTNode node) {
035 super(node);
036 }
037
038 public KtSuperTypeList(@NotNull KotlinPlaceHolderStub<KtSuperTypeList> stub) {
039 super(stub, KtStubElementTypes.SUPER_TYPE_LIST);
040 }
041
042 @Override
043 public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) {
044 return visitor.visitSuperTypeList(this, data);
045 }
046
047 @NotNull
048 public KtSuperTypeListEntry addEntry(@NotNull KtSuperTypeListEntry entry) {
049 return EditCommaSeparatedListHelper.INSTANCE.addItem(this, getEntries(), entry);
050 }
051
052 public void removeEntry(@NotNull KtSuperTypeListEntry entry) {
053 EditCommaSeparatedListHelper.INSTANCE.removeItem(entry);
054 if (getEntries().isEmpty()) {
055 delete();
056 }
057 }
058
059 @Override
060 public void delete() throws IncorrectOperationException {
061 PsiElement left = PsiTreeUtil.skipSiblingsBackward(this, PsiWhiteSpace.class, PsiComment.class);
062 if (left == null || left.getNode().getElementType() != KtTokens.COLON) left = this;
063 getParent().deleteChildRange(left, this);
064 }
065
066 public List<KtSuperTypeListEntry> getEntries() {
067 return Arrays.asList(getStubOrPsiChildren(KtStubElementTypes.SUPER_TYPE_LIST_ENTRIES, KtSuperTypeListEntry.ARRAY_FACTORY));
068 }
069 }