Support CommandDeclarationLineMarkerProvider for Java methods

This commit is contained in:
Him188 2021-02-03 21:47:19 +08:00
parent 6d02aa72d9
commit 73f457b628
3 changed files with 47 additions and 5 deletions

View File

@ -1,10 +1,30 @@
package test;
import net.mamoe.mirai.console.command.Command;
import net.mamoe.mirai.console.command.CommandOwner;
import net.mamoe.mirai.console.command.descriptor.CommandSignatureFromKFunction;
import net.mamoe.mirai.console.command.java.JSimpleCommand;
import net.mamoe.mirai.console.permission.Permission;
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription;
import org.jetbrains.annotations.NotNull;
public class TestJavaPlugin extends net.mamoe.mirai.console.plugin.jvm.JavaPlugin {
import java.util.List;
public class TestJavaPlugin extends JavaPlugin {
public TestJavaPlugin(@NotNull JvmPluginDescription description) {
super(description);
}
}
class TestCommand extends JSimpleCommand {
public TestCommand(@NotNull CommandOwner owner, @NotNull String primaryName, @NotNull String[] secondaryNames, @NotNull Permission basePermission) {
super(owner, primaryName, secondaryNames, basePermission);
}
@Handler
public void test(String s) {
}
}

View File

@ -13,6 +13,7 @@ import com.intellij.codeInsight.daemon.LineMarkerInfo
import com.intellij.codeInsight.daemon.LineMarkerProvider
import com.intellij.openapi.editor.markup.GutterIconRenderer
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import net.mamoe.mirai.console.intellij.Icons
import net.mamoe.mirai.console.intellij.resolve.getElementForLineMark
import net.mamoe.mirai.console.intellij.resolve.isSimpleCommandHandlerOrCompositeCommandSubCommand
@ -21,12 +22,25 @@ import org.jetbrains.kotlin.psi.KtNamedFunction
class CommandDeclarationLineMarkerProvider : LineMarkerProvider {
override fun getLineMarkerInfo(element: PsiElement): LineMarkerInfo<*>? {
if (element !is KtNamedFunction) return null
when (element) {
is KtNamedFunction -> {
if (!element.isSimpleCommandHandlerOrCompositeCommandSubCommand()) return null
runIgnoringErrors { // not showing icons is better than throwing exception every time doing inspection
return Info(getElementForLineMark(element.funKeyword ?: element.identifyingElement ?: element))
}
}
is PsiMethod -> {
if (!element.isSimpleCommandHandlerOrCompositeCommandSubCommand()) return null
runIgnoringErrors { // not showing icons is better than throwing exception every time doing inspection
return Info(getElementForLineMark(element.identifyingElement ?: element))
}
}
else -> return null
}
}
@Suppress("DEPRECATION")
class Info(callElement: PsiElement) : LineMarkerInfo<PsiElement>(

View File

@ -12,6 +12,7 @@ package net.mamoe.mirai.console.intellij.resolve
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiDeclarationStatement
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiModifierListOwner
import com.intellij.psi.util.parentsWithSelf
import net.mamoe.mirai.console.compiler.common.castOrNull
import net.mamoe.mirai.console.compiler.common.resolve.*
@ -43,15 +44,22 @@ import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
* For CompositeCommand.SubCommand
*/
fun KtNamedFunction.isCompositeCommandSubCommand(): Boolean = this.hasAnnotation(COMPOSITE_COMMAND_SUB_COMMAND_FQ_NAME)
fun PsiModifierListOwner.isCompositeCommandSubCommand(): Boolean = this.hasAnnotation(COMPOSITE_COMMAND_SUB_COMMAND_FQ_NAME)
internal fun PsiModifierListOwner.hasAnnotation(fqName: FqName): Boolean = this.hasAnnotation(fqName.asString())
/**
* SimpleCommand.Handler
*/
fun KtNamedFunction.isSimpleCommandHandler(): Boolean = this.hasAnnotation(SIMPLE_COMMAND_HANDLER_COMMAND_FQ_NAME)
fun PsiModifierListOwner.isSimpleCommandHandler(): Boolean = this.hasAnnotation(SIMPLE_COMMAND_HANDLER_COMMAND_FQ_NAME)
fun KtNamedFunction.isSimpleCommandHandlerOrCompositeCommandSubCommand(): Boolean =
this.isSimpleCommandHandler() || this.isCompositeCommandSubCommand()
fun PsiModifierListOwner.isSimpleCommandHandlerOrCompositeCommandSubCommand(): Boolean =
this.isSimpleCommandHandler() || this.isCompositeCommandSubCommand()
val KtPureClassOrObject.allSuperTypes: Sequence<KtSuperTypeListEntry>
get() = sequence {