generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aya: move
TreeBuilder
to intellij-aya
- Loading branch information
Showing
3 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2020-2023 Tesla (Yinsen) Zhang. | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file. | ||
package org.aya.intellij.ui; | ||
|
||
import kala.collection.mutable.MutableList; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
import java.util.Objects; | ||
|
||
/** @param <T> the tree, no longer used in Aya. */ | ||
public abstract class TreeBuilder<T extends TreeBuilder.Tree<T>> { | ||
public interface Tree<T extends Tree<T>> { | ||
@NotNull MutableList<T> children(); | ||
} | ||
|
||
protected final Deque<@NotNull MutableList<@NotNull T>> tops = new ArrayDeque<>(); | ||
|
||
public @NotNull MutableList<@NotNull T> root() { | ||
return tops.getFirst(); | ||
} | ||
|
||
{ | ||
tops.addLast(MutableList.create()); | ||
} | ||
|
||
public void append(@NotNull T trace) { | ||
shift(trace); | ||
reduce(); | ||
} | ||
|
||
public void shift(@NotNull T trace) { | ||
Objects.requireNonNull(tops.getLast()).append(trace); | ||
tops.addLast(trace.children()); | ||
} | ||
|
||
public void unshift() { | ||
var buffer = Objects.requireNonNull(tops.getLast()); | ||
buffer.removeAt(buffer.size() - 1); | ||
} | ||
public void reduce() { tops.removeLast(); } | ||
public void reduceAndUnshift() { | ||
tops.removeLast(); | ||
} | ||
} |