All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aya.intellij.GenericNode Maven / Gradle / Ivy

The newest version!
package org.aya.intellij;

import com.intellij.openapi.util.TextRange;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import kala.collection.SeqView;
import kala.text.StringSlice;
import kotlin.sequences.Sequence;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;
import java.util.Objects;

/**
 * Generalized {@link com.jetbrains.lang.parsing.AstMarkersSyntaxNode}
 * for reusing psi interfaces in Producer,
 * based on {@link StringSlice}.
 *
 * @author kiva
 */
public interface GenericNode> {
  @NotNull IElementType elementType();
  @NotNull StringSlice tokenText();
  @NotNull TextRange range();
  default @NotNull SeqView childrenView() {
    return new SeqView<>() {
      @Override public @NotNull Iterator iterator() {
        return childrenSequence().iterator();
      }
    };
  }
  @NotNull Sequence childrenSequence();
  default @NotNull @NonNls String toDebugString() { return toString(); }
  default @NotNull @NonNls String pp() { return toString(); }
  default boolean is(@NotNull IElementType type) { return elementType() == type; }
  default boolean is(@NotNull TokenSet tokenSet) { return tokenSet.contains(elementType()); }
  default @NotNull SeqView childrenOfType(@NotNull IElementType type) {
    return childrenView().filter(c -> c.is(type));
  }

  default @NotNull SeqView childrenOfType(@NotNull TokenSet tokenSet) {
    return childrenView().filter(c -> c.is(tokenSet));
  }
  default N firstChild() { return childrenView().getFirst(); }
  default N lastChild() { return childrenView().getLast(); }
  default N firstChildOrNull() { return childrenView().getFirstOrNull(); }
  default @Nullable N peekChild(@NotNull IElementType type) { return childrenOfType(type).getFirstOrNull(); }
  default @Nullable N peekChild(@NotNull TokenSet tokenSet) { return childrenOfType(tokenSet).getFirstOrNull(); }
  default @NotNull N child(@NotNull IElementType type) { return Objects.requireNonNull(peekChild(type)); }
  default @NotNull N child(@NotNull TokenSet tokenSet) { return Objects.requireNonNull(peekChild(tokenSet)); }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy