IDEA线路图标
IDEA线路图标,暂时知道的有两种
第一种用来跳转到对应的文件,或者某个地方.比如接口调被覆盖方法,mapper调对应的xml文件,官方文档:https://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/line_marker_provider.html
第二种用来执行对应的功能,比如执行main方法
我想实现什么功能
思路:在IDEA自带的插件中有一个HttpClient的插件,可以生成发送Spring接口的请求文档(自认为不方便),于是就像仿照swagger,点击线路图标生成swagger类型对应接口,填入参数就可以直接发起请求,测试接口
下面是HttpClient生成的文档
下面是自己写的插件XTools<gitee地址>,可以在IDEA插件仓库搜索到,参考Knife4j
下面的是第二种实现方式
另外IDEA插件开发很坑,各种的版本不兼容,相关文档少,源码还没注释…一阵无语
继承此类,覆盖getLineMarkerInfo
public class XHttpLineMarkerProvider extends LineMarkerProviderDescriptor {
创建需要的线路图标
获取指定element内容,筛选需要的内容
@Override
public LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element) {
if (!(element instanceof PsiMethod)) return null;
PsiMethod psiMethod = (PsiMethod) element;
// System.out.println("类内容 :"+psiMethod.getContext().getText()); //类内容
// System.out.println("注释内容 :"+psiMethod.getDocComment().getText()); //注释内容
// System.out.println("方法全部内容 :"+psiMethod.getOriginalElement().getText()); //方法全部内容
//System.out.println("内容 :"+psiMethod.getParameterList().getText()); (@PathVariable(value = "materialId") Integer materialId, @RequestBody List<Competing> competingList)
// System.out.println("类内容 :"+psiMethod.getBody().getText()); //方法内容 不带方法名
PsiAnnotation getMapping = psiMethod.getAnnotation("@PostMapping");
PsiAnnotation[] annotations = psiMethod.getAnnotations();
for (PsiAnnotation annotation : annotations) {
if (annotation.getText()
.contains("PostMapping")) {
//System.out.println("注解:"+annotation.getText()); //@PostMapping("resetCompeting/{materialId}")
getMapping = annotation;
}
}
if (getMapping == null) {
return null;
}
// System.out.println("+++"+getMapping.getText()); +++@PostMapping("resetCompeting/{materialId}")
// System.out.println("+++----"+getMapping.getQualifiedName()); +++----org.springframework.web.bind.annotation.PostMapping
PsiAnnotationParameterList parameterList = getMapping.getParameterList();
String text = parameterList.getText();
// System.out.println("+++"+text); +++("resetCompeting/{materialId}")
List<JvmAnnotationAttribute> attributes = getMapping.getAttributes();
PsiAnnotationMemberValue value = getMapping.findAttributeValue("value");
// System.out.println(value.getText()); //"resetCompeting/{materialId}"
for (JvmAnnotationAttribute
attribute : attributes) {
JvmAnnotationConstantValue attributeValue = (JvmAnnotationConstantValue) attribute.getAttributeValue();
//System.out.println("----"+attribute.getAttributeName()+"--"+attributeValue.getConstantValue()); // ----value--resetCompeting/{materialId}
}
Info info = new Info(new XHttpAction(psiMethod.getBody(), "GetMapping", "get请求", IconLoader.getIcon("/images/mapper_method.png")));
DefaultActionGroup actionGroup = new DefaultActionGroup();
actionGroup.add(new XHttpAction(psiMethod.getBody(), "PetMapping", "get请求", IconLoader.getIcon("/images/mapper_method.png")));
actionGroup.add(info.actions[0]);
actionGroup.add(new Separator());
Function<PsiElement, String> tooltipProvider = element1 -> {
final StringBuilder tooltip = new StringBuilder();
if (info.tooltipProvider != null) {
String string = info.tooltipProvider.apply(element1);
if (string == null) return null;
if (tooltip.length() != 0) {
tooltip.append("\n");
}
tooltip.append(string);
}
return tooltip.length() == 0 ? null : tooltip.toString();
};
return new RunLineMarkerInfo(psiMethod.getBody(), IconLoader.getIcon("/images/mapper_method.png"), tooltipProvider, actionGroup);
}
这我也不知道干嘛的!!!
大概就是重写了个构造器,传递图标组
static class RunLineMarkerInfo extends LineMarkerInfo<PsiElement> {
private final DefaultActionGroup myActionGroup;
RunLineMarkerInfo(PsiElement element, Icon icon, Function<PsiElement, String> tooltipProvider,
DefaultActionGroup actionGroup) {
super(element, element.getTextRange(), icon, tooltipProvider, null, GutterIconRenderer.Alignment.CENTER);
myActionGroup = actionGroup;
}
@Override
public GutterIconRenderer createGutterRenderer() {
return new LineMarkerGutterIconRenderer<PsiElement>(this) {
@Override
public AnAction getClickAction() {
return null;
}
@Override
public boolean isNavigateAction() {
return true;
}
@Override
public ActionGroup getPopupMenuActions() {
return myActionGroup;
}
};
}
@NotNull
@Override
public MarkupEditorFilter getEditorFilter() {
return MarkupEditorFilterFactory.createIsNotDiffFilter();
}
}
Action中处理需要做的事情
public class XHttpAction extends AnAction {
public XHttpAction() {
super();
}
public XHttpAction(PsiElement body, String getMapping, String get请求, Icon icon) {
}
@Override
public void actionPerformed(AnActionEvent e) {
Editor requiredData = e.getRequiredData(DataKeys.EDITOR);
SelectionModel selectionModel = requiredData.getSelectionModel();
String selectedText = selectionModel.getSelectedText();
String name = e.getRequiredData(DataKeys.PSI_FILE).getViewProvider().getVirtualFile().getName();
DataCenter.TEXT=selectedText;
DataCenter.FILE_NAME=name;
OutContent outContent = new OutContent();
outContent.show();
// TODO: insert action logic here
}
}