first commit with KotlinBukkitAPI Menu Preview \o/

This commit is contained in:
Gabriel Souza committed 2020-07-08 19:27:11 -03:00
commit caef30856c
744 files changed
+1336

No files matched your search

@@ -0,0 +1,35 @@
package br.com.devsrsouza.kotlinbukkitapi.tooling
import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.lang.annotation.Annotator
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.fileEditor.TextEditorWithPreview
import com.intellij.openapi.wm.IdeFocusManager
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtCallExpression
import javax.swing.SwingUtilities
/*
* This annotator if the file has menu declaration, he set the file editor
* to the KotlinBukkitAPI one.
*
object TestAnnotator : Annotator {
override fun annotate(element: PsiElement, holder: AnnotationHolder) {
if(element is KtCallExpression && isMenuCall(element)) {
val project = element.project
val fileEditorManager = FileEditorManager.getInstance(project)
SwingUtilities.invokeLater {
val virtualFile = element.containingFile.virtualFile
if(fileEditorManager.selectedEditor is TextEditorWithPreview) return@invokeLater
fileEditorManager.setSelectedEditor(
virtualFile,
MenuPreviewFileEditorProvider.ID
)
}
}
}
}*/
@@ -0,0 +1,14 @@
package br.com.devsrsouza.kotlinbukkitapi.tooling
data class MenuDeclaration(
val displayname: String,
val lines: Int,
val slots: List<MenuSlotDeclaration>
)
data class MenuSlotDeclaration(
val line: Int,
val slot: Int,
val itemId: Int?,
val isSelected: Boolean = false
)
@@ -0,0 +1,108 @@
package br.com.devsrsouza.kotlinbukkitapi.tooling
import com.intellij.util.ImageLoader
import org.jetbrains.kotlin.utils.keysToMap
import java.awt.*
import javax.swing.JPanel
class InventoryComponent(
val declaration: MenuDeclaration
) : JPanel() {
companion object {
val CHEST_SCALE = 2
val CHEST_SINGLE_LINE_HEIGHT = 42
val CHEST_LINE_DIFF = 18
val CHEST_WIDTH = 176
private val minecraftFontResource = "/assets/fonts/Minecraftia-Regular.ttf"
private val spritesFolder = "/assets/sprites"
private val itemsSpriteFolder = "/assets/items"
// the selected-border is 36x36 and not 32x32
private val selectedBorderSizeDiff = 2
}
private val lines = when {
declaration.lines > 6 -> 6
declaration.lines < 1 -> 1
else -> declaration.lines
}
private val spriteImage = ImageLoader.loadFromResource(
"$spritesFolder/chest-${lines}.png"
)!!
private val itemsImage = declaration.slots.filter {
it.line in 1..declaration.lines && it.slot in 1..9
}.keysToMap {
ImageLoader.loadFromResource("$itemsSpriteFolder/${it.itemId}-0.png")
//?.getScaledInstance(16, 16, Image.SCALE_FAST)
}
private val selectionImage = if(declaration.slots.any { it.isSelected })
ImageLoader.loadFromResource("$spritesFolder/selected-border.png")
else null
private val minecraftFont = Font.createFont(
Font.TRUETYPE_FONT,
this::class.java.getResource(minecraftFontResource).openStream()
).deriveFont(18.5f)
init {
isVisible = true
minimumSize = calculateDimension()
}
override fun paintComponent(g: Graphics) {
val g = g as Graphics2D
val oldTransform = g.transform
// drawing menu
g.scale(CHEST_SCALE.toDouble(), CHEST_SCALE.toDouble())
g.drawImage(spriteImage, 0, 0,this)
g.transform = oldTransform
// drawing items
for ((slot, texture) in itemsImage) {
val (x, y) = calculateXYForSlot(slot)
g.drawImage(texture ?: continue, x, y, this)
// if is selected draw selection border
if(slot.isSelected) {
g.drawImage(
selectionImage ?: continue,
x- selectedBorderSizeDiff,
y- selectedBorderSizeDiff,
this
)
}
}
// drawing displayname
g.font = minecraftFont
g.color = Color.DARK_GRAY
g.drawString(declaration.displayname, 16, 40)
}
fun calculateXYForSlot(menuSlotDeclaration: MenuSlotDeclaration): Pair<Int, Int> {
val topXPadding = 18 * CHEST_SCALE
val leftYPadding = 8 * CHEST_SCALE
val itemSize = 16 * CHEST_SCALE
val rightYPadding = 2 * CHEST_SCALE
val bottomXPadding = 2 * CHEST_SCALE
val itemXPos = menuSlotDeclaration.line -1
val itemYPos = menuSlotDeclaration.slot -1
val x = leftYPadding + (itemYPos * rightYPadding) + (itemYPos * itemSize)
val y = topXPadding + (itemXPos * bottomXPadding) + (itemXPos * itemSize)
return x to y
}
private fun calculateDimension(): Dimension {
val height = CHEST_SINGLE_LINE_HEIGHT + (lines - 1) * CHEST_LINE_DIFF
return Dimension((CHEST_WIDTH * CHEST_SCALE).toInt(), (height * CHEST_SCALE).toInt())
}
}
@@ -0,0 +1,156 @@
package br.com.devsrsouza.kotlinbukkitapi.tooling
import com.intellij.openapi.editor.event.CaretEvent
import com.intellij.openapi.editor.event.CaretListener
import com.intellij.openapi.editor.event.DocumentEvent
import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.fileEditor.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.UserDataHolderBase
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.util.forEachDescendantOfType
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.layout.panel
import com.intellij.util.Alarm
import org.jetbrains.kotlin.psi.KtCallExpression
import java.awt.*
import java.beans.PropertyChangeListener
import javax.swing.*
class MenuPreviewFileEditor(
private val myProject: Project,
private val myFile: VirtualFile,
private val mainEditor: TextEditor
) : UserDataHolderBase(), FileEditor {
private val mySwingAlarm = Alarm(Alarm.ThreadToUse.SWING_THREAD, this)
private val myDocument = FileDocumentManager.getInstance().getDocument(myFile)!!
private val myUi = JPanel().apply {
layout = BorderLayout()
isVisible = true
minimumSize = Dimension(
InventoryComponent.CHEST_WIDTH * InventoryComponent.CHEST_SCALE + 10,
0
)
}
private lateinit var currentScroll: JBScrollPane
private var currentSelectedLine: Int? = null
init {
rebuild(null)
// listen to the source code changes to rebuild the UI
myDocument.addDocumentListener(object : DocumentListener {
override fun beforeDocumentChange(event: DocumentEvent) {
mySwingAlarm.cancelAllRequests()
}
override fun documentChanged(event: DocumentEvent) {
mySwingAlarm.addRequest({
rebuild(currentScroll.viewport.viewPosition)
}, 50)
}
})
// listen to cursor changes to make a slot selectable
mainEditor.editor.caretModel.addCaretListener(object : CaretListener {
override fun caretPositionChanged(event: CaretEvent) {
currentSelectedLine = event.newPosition.line
rebuild(currentScroll.viewport.viewPosition)
}
})
}
fun rebuild(currentViewPosition: Point?) {
val tree = PsiDocumentManager.getInstance(myProject).getPsiFile(myDocument) ?: return
// clear the old UI
myUi.removeAll()
// create the scroll pane with a centralized component with the inventories
var menuCount = 0
currentScroll = JBScrollPane(
panel {
tree.forEachDescendantOfType<KtCallExpression> {
val declaration = findMenuDeclaration(
it,
currentSelectedLine
)
if (declaration != null) {
row {
component(
InventoryComponent(
declaration
)
)
}
menuCount++
}
}
}.centerComponent(),
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
).apply {
if(currentViewPosition != null)
viewport.viewPosition = currentViewPosition
}
if(menuCount > 0) {
// add the scroll pane to the UI
myUi.add(
currentScroll,
BorderLayout.CENTER
)
} else {
myUi.add(
JLabel("Currently this source code has no menu declaration!")
.centerComponent(),
BorderLayout.CENTER
)
}
}
override fun isModified(): Boolean = false
override fun isValid(): Boolean = true
override fun addPropertyChangeListener(listener: PropertyChangeListener) {
}
override fun removePropertyChangeListener(listener: PropertyChangeListener) {
}
override fun getName(): String = "KotlinBukkitAPI Menu Viewer"
override fun setState(state: FileEditorState) {
}
override fun getComponent(): JComponent {
return myUi
}
override fun getPreferredFocusedComponent(): JComponent? {
return null
}
override fun getCurrentLocation(): FileEditorLocation? {
return null
}
override fun dispose() {
mySwingAlarm.cancelAllRequests()
}
private fun JComponent.centerComponent(): JPanel {
return JPanel().apply {
isVisible = true
layout = GridBagLayout()
add(this@centerComponent)
}
}
}
@@ -0,0 +1,41 @@
package br.com.devsrsouza.kotlinbukkitapi.tooling
import com.intellij.openapi.fileEditor.*
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.kotlin.idea.KotlinFileType
class MenuPreviewFileEditorProvider : WeighedFileEditorProvider() {
companion object {
const val ID = "kotlinbukkitapi-menu-preview"
}
override fun getEditorTypeId(): String =
ID
override fun accept(project: Project, file: VirtualFile): Boolean {
val type = file.fileType
if(type != KotlinFileType.INSTANCE) return false
return true
// return PsiManager.getInstance(project).findFile(file)?.findDescendantOfType<KtCallExpression> {
// findMenuDeclaration(it) != null
// } != null
}
override fun createEditor(project: Project, file: VirtualFile): FileEditor {
val editor: TextEditor = TextEditorProvider.getInstance().createEditor(project, file) as TextEditor
val preview =
MenuPreviewFileEditor(project, file, editor)
return TextEditorWithPreview(editor, preview, "KotlinBukkitAPI")
}
//override fun getPolicy(): FileEditorPolicy = FileEditorPolicy.HIDE_DEFAULT_EDITOR
override fun getPolicy(): FileEditorPolicy = FileEditorPolicy.PLACE_AFTER_DEFAULT_EDITOR
}
@@ -0,0 +1,390 @@
package br.com.devsrsouza.kotlinbukkitapi.tooling
enum class MinecraftItem(val id: Int) {
AIR(0),
STONE(1),
GRASS(2),
DIRT(3),
COBBLESTONE(4),
WOOD(5),
SAPLING(6),
BEDROCK(7),
WATER(8),
STATIONARY_WATER(9),
LAVA(10), STATIONARY_LAVA(11),
SAND(12),
GRAVEL(13),
GOLD_ORE(14),
IRON_ORE(15),
COAL_ORE(16),
LOG(17),
LEAVES(18),
SPONGE(19),
GLASS(20),
LAPIS_ORE(21),
LAPIS_BLOCK(22),
DISPENSER(23),
SANDSTONE(24),
NOTE_BLOCK(25),
BED_BLOCK(26),
POWERED_RAIL(27),
DETECTOR_RAIL(28),
PISTON_STICKY_BASE(29),
WEB(30),
LONG_GRASS(31),
DEAD_BUSH(32),
PISTON_BASE(33),
PISTON_EXTENSION(34),
WOOL(35),
PISTON_MOVING_PIECE(36),
YELLOW_FLOWER(37),
RED_ROSE(38),
BROWN_MUSHROOM(39),
RED_MUSHROOM(40),
GOLD_BLOCK(41),
IRON_BLOCK(42),
DOUBLE_STEP(43),
STEP(44),
BRICK(45),
TNT(46),
BOOKSHELF(47),
MOSSY_COBBLESTONE(48),
OBSIDIAN(49),
TORCH(50),
FIRE(51),
MOB_SPAWNER(52),
WOOD_STAIRS(53),
CHEST(54),
REDSTONE_WIRE(55),
DIAMOND_ORE(56),
DIAMOND_BLOCK(57),
WORKBENCH(58),
CROPS(59),
SOIL(60),
FURNACE(61),
BURNING_FURNACE(62),
SIGN_POST(63),
WOODEN_DOOR(64),
LADDER(65),
RAILS(66),
COBBLESTONE_STAIRS(67),
WALL_SIGN(68),
LEVER(69),
STONE_PLATE(70),
IRON_DOOR_BLOCK(71),
WOOD_PLATE(72),
REDSTONE_ORE(73),
GLOWING_REDSTONE_ORE(74),
REDSTONE_TORCH_OFF(75),
REDSTONE_TORCH_ON(76),
STONE_BUTTON(77),
SNOW(78),
ICE(79),
SNOW_BLOCK(80),
CACTUS(81),
CLAY(82),
SUGAR_CANE_BLOCK(83),
JUKEBOX(84),
FENCE(85),
PUMPKIN(86),
NETHERRACK(87),
SOUL_SAND(88),
GLOWSTONE(89),
PORTAL(90),
JACK_O_LANTERN(91),
CAKE_BLOCK(92),
DIODE_BLOCK_OFF(93),
DIODE_BLOCK_ON(94),
STAINED_GLASS(95),
TRAP_DOOR(96),
MONSTER_EGGS(97),
SMOOTH_BRICK(98),
HUGE_MUSHROOM_1(99),
HUGE_MUSHROOM_2(100),
IRON_FENCE(101),
THIN_GLASS(102),
MELON_BLOCK(103),
PUMPKIN_STEM(104),
MELON_STEM(105),
VINE(106),
FENCE_GATE(107),
BRICK_STAIRS(108),
SMOOTH_STAIRS(109),
MYCEL(110),
WATER_LILY(111),
NETHER_BRICK(112),
NETHER_FENCE(113),
NETHER_BRICK_STAIRS(114),
NETHER_WARTS(115),
ENCHANTMENT_TABLE(116),
BREWING_STAND(117),
CAULDRON(118),
ENDER_PORTAL(119),
ENDER_PORTAL_FRAME(120),
ENDER_STONE(121),
DRAGON_EGG(122),
REDSTONE_LAMP_OFF(123),
REDSTONE_LAMP_ON(124),
WOOD_DOUBLE_STEP(125),
WOOD_STEP(126),
COCOA(127),
SANDSTONE_STAIRS(128),
EMERALD_ORE(129),
ENDER_CHEST(130),
TRIPWIRE_HOOK(131),
TRIPWIRE(132),
EMERALD_BLOCK(133),
SPRUCE_WOOD_STAIRS(134),
BIRCH_WOOD_STAIRS(135),
JUNGLE_WOOD_STAIRS(136),
COMMAND(137),
BEACON(138),
COBBLE_WALL(139),
FLOWER_POT(140),
CARROT(141),
POTATO(142),
WOOD_BUTTON(143),
SKULL(144),
ANVIL(145),
TRAPPED_CHEST(146),
GOLD_PLATE(147),
IRON_PLATE(148),
REDSTONE_COMPARATOR_OFF(149),
REDSTONE_COMPARATOR_ON(150),
DAYLIGHT_DETECTOR(151),
REDSTONE_BLOCK(152),
QUARTZ_ORE(153),
HOPPER(154),
QUARTZ_BLOCK(155),
QUARTZ_STAIRS(156),
ACTIVATOR_RAIL(157),
DROPPER(158),
STAINED_CLAY(159),
STAINED_GLASS_PANE(160),
LEAVES_2(161),
LOG_2(162),
ACACIA_STAIRS(163),
DARK_OAK_STAIRS(164),
SLIME_BLOCK(165),
BARRIER(166),
IRON_TRAPDOOR(167),
PRISMARINE(168),
SEA_LANTERN(169),
HAY_BLOCK(170),
CARPET(171),
HARD_CLAY(172),
COAL_BLOCK(173),
PACKED_ICE(174),
DOUBLE_PLANT(175),
STANDING_BANNER(176),
WALL_BANNER(177),
DAYLIGHT_DETECTOR_INVERTED(178),
RED_SANDSTONE(179),
RED_SANDSTONE_STAIRS(180),
DOUBLE_STONE_SLAB2(181),
STONE_SLAB2(182),
SPRUCE_FENCE_GATE(183),
BIRCH_FENCE_GATE(184),
JUNGLE_FENCE_GATE(185),
DARK_OAK_FENCE_GATE(186),
ACACIA_FENCE_GATE(187),
SPRUCE_FENCE(188),
BIRCH_FENCE(189),
JUNGLE_FENCE(190),
DARK_OAK_FENCE(191),
ACACIA_FENCE(192),
SPRUCE_DOOR(193),
BIRCH_DOOR(194),
JUNGLE_DOOR(195),
ACACIA_DOOR(196),
DARK_OAK_DOOR(197),
// ----- Item Separator -----
IRON_SPADE(256),
IRON_PICKAXE(257),
IRON_AXE(258),
FLINT_AND_STEEL(259),
APPLE(260),
BOW(261),
ARROW(262), COAL(263),
DIAMOND(264),
IRON_INGOT(265),
GOLD_INGOT(266),
IRON_SWORD(267),
WOOD_SWORD(268),
WOOD_SPADE(269),
WOOD_PICKAXE(270),
WOOD_AXE(271),
STONE_SWORD(272),
STONE_SPADE(273),
STONE_PICKAXE(274),
STONE_AXE(275),
DIAMOND_SWORD(276),
DIAMOND_SPADE(277),
DIAMOND_PICKAXE(278),
DIAMOND_AXE(279),
STICK(280),
BOWL(281),
MUSHROOM_SOUP(282),
GOLD_SWORD(283),
GOLD_SPADE(284),
GOLD_PICKAXE(285),
GOLD_AXE(286),
STRING(287),
FEATHER(288),
SULPHUR(289),
WOOD_HOE(290),
STONE_HOE(291),
IRON_HOE(292),
DIAMOND_HOE(293),
GOLD_HOE(294),
SEEDS(295),
WHEAT(296),
BREAD(297),
LEATHER_HELMET(298),
LEATHER_CHESTPLATE(299),
LEATHER_LEGGINGS(300),
LEATHER_BOOTS(301),
CHAINMAIL_HELMET(302),
CHAINMAIL_CHESTPLATE(303),
CHAINMAIL_LEGGINGS(304),
CHAINMAIL_BOOTS(305),
IRON_HELMET(306),
IRON_CHESTPLATE(307),
IRON_LEGGINGS(308),
IRON_BOOTS(309),
DIAMOND_HELMET(310),
DIAMOND_CHESTPLATE(311),
DIAMOND_LEGGINGS(312),
DIAMOND_BOOTS(313),
GOLD_HELMET(314),
GOLD_CHESTPLATE(315),
GOLD_LEGGINGS(316),
GOLD_BOOTS(317),
FLINT(318),
PORK(319),
GRILLED_PORK(320),
PAINTING(321),
GOLDEN_APPLE(322),
SIGN(323),
WOOD_DOOR(324),
BUCKET(325),
WATER_BUCKET(326),
LAVA_BUCKET(327),
MINECART(328),
SADDLE(329),
IRON_DOOR(330),
REDSTONE(331),
SNOW_BALL(332),
BOAT(333),
LEATHER(334),
MILK_BUCKET(335),
CLAY_BRICK(336),
CLAY_BALL(337),
SUGAR_CANE(338),
PAPER(339),
BOOK(340),
SLIME_BALL(341),
STORAGE_MINECART(342),
POWERED_MINECART(343),
EGG(344),
COMPASS(345),
FISHING_ROD(346),
WATCH(347),
GLOWSTONE_DUST(348),
RAW_FISH(349),
COOKED_FISH(350),
INK_SACK(351),
BONE(352),
SUGAR(353),
CAKE(354),
BED(355),
DIODE(356),
COOKIE(357),
MAP(358),
SHEARS(359),
MELON(360),
PUMPKIN_SEEDS(361),
MELON_SEEDS(362),
RAW_BEEF(363),
COOKED_BEEF(364),
RAW_CHICKEN(365),
COOKED_CHICKEN(366),
ROTTEN_FLESH(367),
ENDER_PEARL(368),
BLAZE_ROD(369),
GHAST_TEAR(370),
GOLD_NUGGET(371),
NETHER_STALK(372),
POTION(373),
GLASS_BOTTLE(374),
SPIDER_EYE(375),
FERMENTED_SPIDER_EYE(376),
BLAZE_POWDER(377),
MAGMA_CREAM(378),
BREWING_STAND_ITEM(379),
CAULDRON_ITEM(380),
EYE_OF_ENDER(381),
SPECKLED_MELON(382),
MONSTER_EGG(383),
EXP_BOTTLE(384),
FIREBALL(385),
BOOK_AND_QUILL(386),
WRITTEN_BOOK(387),
EMERALD(388),
ITEM_FRAME(389),
FLOWER_POT_ITEM(390),
CARROT_ITEM(391),
POTATO_ITEM(392),
BAKED_POTATO(393),
POISONOUS_POTATO(394),
EMPTY_MAP(395),
GOLDEN_CARROT(396),
SKULL_ITEM(397),
CARROT_STICK(398),
NETHER_STAR(399),
PUMPKIN_PIE(400),
FIREWORK(401),
FIREWORK_CHARGE(402),
ENCHANTED_BOOK(403),
REDSTONE_COMPARATOR(404),
NETHER_BRICK_ITEM(405),
QUARTZ(406),
EXPLOSIVE_MINECART(407),
HOPPER_MINECART(408),
PRISMARINE_SHARD(409),
PRISMARINE_CRYSTALS(410),
RABBIT(411),
COOKED_RABBIT(412),
RABBIT_STEW(413),
RABBIT_FOOT(414),
RABBIT_HIDE(415),
ARMOR_STAND(416),
IRON_BARDING(417),
GOLD_BARDING(418),
DIAMOND_BARDING(419),
LEASH(420),
NAME_TAG(421),
COMMAND_MINECART(422),
MUTTON(423),
COOKED_MUTTON(424),
BANNER(425),
SPRUCE_DOOR_ITEM(427),
BIRCH_DOOR_ITEM(428),
JUNGLE_DOOR_ITEM(429),
ACACIA_DOOR_ITEM(430),
DARK_OAK_DOOR_ITEM(431),
GOLD_RECORD(2256),
GREEN_RECORD(2257),
RECORD_3(2258),
RECORD_4(2259),
RECORD_5(2260),
RECORD_6(2261),
RECORD_7(2262),
RECORD_8(2263),
RECORD_9(2264),
RECORD_10(2265),
RECORD_11(2266),
RECORD_12(2267);
}
@@ -0,0 +1,106 @@
package br.com.devsrsouza.kotlinbukkitapi.tooling
import com.intellij.psi.util.collectDescendantsOfType
import com.intellij.psi.util.findDescendantOfType
import org.jetbrains.kotlin.builtins.getReceiverTypeFromFunctionType
import org.jetbrains.kotlin.builtins.getReturnTypeFromFunctionType
import org.jetbrains.kotlin.builtins.isExtensionFunctionType
import org.jetbrains.kotlin.idea.core.util.getLineNumber
import org.jetbrains.kotlin.idea.debugger.sequence.psi.resolveType
import org.jetbrains.kotlin.idea.intentions.isMethodCall
import org.jetbrains.kotlin.idea.refactoring.fqName.fqName
import org.jetbrains.kotlin.idea.refactoring.fqName.getKotlinFqName
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.types.typeUtil.isUnit
fun findMenuDeclaration(
element: KtCallExpression,
currentSelectedLine: Int? = null
): MenuDeclaration? {
val menuFqName = "br.com.devsrsouza.kotlinbukkitapi.dsl.menu.menu"
val simpleName = "menu"
if (!element.isCallFrom(simpleName, menuFqName)) return null
val menuFunction = element.findDescendantOfType<KtCallExpression> {
isMenuCall(it)
} ?: return null
// TODO: Support named parameters
val displayName = menuFunction.valueArguments.getOrNull(0)?.text?.replace("\"", "") ?: return null
val lines = menuFunction.valueArguments.getOrNull(1)?.text?.toIntOrNull() ?: return null
val menuLambdaBlock = element.findDescendantOfType<KtLambdaExpression> {
it.isBuilderBlockFor("br.com.devsrsouza.kotlinbukkitapi.dsl.menu.MenuDSL")
} ?: return null
val slotFqName = "br.com.devsrsouza.kotlinbukkitapi.dsl.menu.slot"
val slotSimpleName = "slot"
val slotCalls = menuLambdaBlock.collectDescendantsOfType<KtCallExpression> {
it.isCallFrom(slotSimpleName, slotFqName)
}.mapNotNull {
val args = it.valueArguments
// TODO: support named parameters
val line = args.getOrNull(0)?.text?.toIntOrNull() ?: return@mapNotNull null
val slot = args.getOrNull(1)?.text?.toIntOrNull() ?: return@mapNotNull null
// TODO: support just slot: slot(15, item(...)) {}
val itemValue = args.getOrNull(2) ?: return@mapNotNull null
val expression = itemValue.getArgumentExpression() ?: return@mapNotNull null
val materialExpression = expression.findDescendantOfType<KtDotQualifiedExpression> {
it.text.startsWith("Material")
&& it.receiverExpression.mainReference?.resolve()?.getKotlinFqName()?.asString()
?.equals("org.bukkit.Material") == true
} ?: return@mapNotNull null
val typeExpression = materialExpression.selectorExpression as? KtNameReferenceExpression ?: return@mapNotNull null
val materialName = typeExpression.text ?: return@mapNotNull null
val item = runCatching { MinecraftItem.valueOf(materialName) }.getOrNull()
println("Slot $line, $slot using item: $materialName")
val isSelected = it.getLineNumber() == currentSelectedLine
MenuSlotDeclaration(
line,
slot,
item?.id,
isSelected
)
}
return MenuDeclaration(displayName, lines, slotCalls)
}
fun isMenuCall(it: KtCallExpression) =
it.isCallFrom("menu", "br.com.devsrsouza.kotlinbukkitapi.dsl.menu.menu")
fun KtLambdaExpression.isBuilderBlockFor(fqName: String): Boolean {
val type = resolveType()
val extensionFunctionType = type.isExtensionFunctionType
if(!extensionFunctionType) return false
val returnTypeFromFunctionType = type.getReturnTypeFromFunctionType()
val receiverTypeFromFunctionType = type.getReceiverTypeFromFunctionType() ?: return false
val receiverFqName = receiverTypeFromFunctionType.fqName ?: return false
return returnTypeFromFunctionType.isUnit() && receiverFqName.asString().equals(fqName)
}
fun KtCallExpression.isCallFrom(
simpleName: String,
fqName: String
): Boolean {
return text.startsWith(simpleName) && isMethodCall(fqName)
}
+19
View File
@@ -0,0 +1,19 @@
<idea-plugin>
<id>br.com.devsrsouza.kotlinbukkitapi.tooling</id>
<name>KotlinBukkitAPI Tooling</name>
<vendor>DevSrSouza</vendor>
<!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<depends>com.intellij.modules.platform</depends>
<depends>org.jetbrains.kotlin</depends>
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<fileEditorProvider implementation="br.com.devsrsouza.kotlinbukkitapi.tooling.MenuPreviewFileEditorProvider"/>
</extensions>
<actions>
<!-- Add your actions here -->
</actions>
</idea-plugin>
Binary file not shown.

After

Width:  |  Height:  |  Size: 80 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1009 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 943 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 593 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 908 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 222 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 586 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 209 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 482 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 558 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 937 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 918 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 802 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 328 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 530 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 548 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 563 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 541 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 981 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 432 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 433 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 997 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 802 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 871 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 707 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 491 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 974 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 561 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 634 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 518 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 187 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 422 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 726 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 934 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 364 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 311 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 561 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 414 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 385 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 796 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 830 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 680 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 785 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 287 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 736 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 978 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 826 B

Loaded 100 of 744 files, more files were not shown because too many files have changed in this diff. Show more