删除velocity插件依赖,兼容更多IDE。

This commit is contained in:
makejava 2021-02-01 17:51:40 +08:00
parent 81243b45d5
commit 12072505dd
5 changed files with 50 additions and 76 deletions

View File

@ -1,47 +0,0 @@
package com.sjhy.plugin.provider;
import com.intellij.psi.CommonClassNames;
import com.intellij.velocity.VtlGlobalVariableProvider;
import com.intellij.velocity.psi.VtlLightVariable;
import com.intellij.velocity.psi.VtlVariable;
import com.intellij.velocity.psi.files.VtlFile;
import com.sjhy.plugin.ui.base.TemplateEditor;
import org.jetbrains.annotations.NotNull;
import java.util.*;
/**
* EasyCode 专用Velocity全局变量
*
* @author makejava
* @version 1.0.0
* @since 2018/8/14 11:07
*/
public class EasyCodeGlobalVariableProvider extends VtlGlobalVariableProvider {
@NotNull
@Override
public Collection<VtlVariable> getGlobalVariables(@NotNull VtlFile file) {
// 非EasyCode模板不提供支持
if (!Objects.equals(file.getName(), TemplateEditor.EASY_CODE_TEMPLATE)) {
//noinspection unchecked
return Collections.EMPTY_LIST;
}
final List<VtlVariable> result = new ArrayList<>();
result.add(new VtlLightVariable("author", file, CommonClassNames.JAVA_LANG_STRING));
result.add(new VtlLightVariable("encode", file, CommonClassNames.JAVA_LANG_STRING));
result.add(new VtlLightVariable("packageName", file, CommonClassNames.JAVA_LANG_STRING));
result.add(new VtlLightVariable("modulePath", file, CommonClassNames.JAVA_LANG_STRING));
result.add(new VtlLightVariable("importList", file, "java.util.Set<java.lang.String>"));
result.add(new VtlLightVariable("callback", file, "com.sjhy.plugin.entity.Callback"));
result.add(new VtlLightVariable("tool", file, "com.sjhy.plugin.tool.GlobalTool"));
result.add(new VtlLightVariable("time", file, "com.sjhy.plugin.tool.TimeUtils"));
result.add(new VtlLightVariable("tableInfo", file, "com.sjhy.plugin.entity.TableInfo"));
result.add(new VtlLightVariable("tableInfoList", file, "java.util.List<com.sjhy.plugin.entity.TableInfo>"));
result.add(new VtlLightVariable("generateService", file, "com.sjhy.plugin.tool.ExtraCodeGenerateUtils"));
result.add(new VtlLightVariable("dbUtil", file, "com.intellij.database.util.DbUtil"));
result.add(new VtlLightVariable("dasUtil", file, "com.intellij.database.util.DasUtil"));
return result;
}
}

View File

@ -4,9 +4,9 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.intellij.ide.fileTemplates.impl.UrlUtil;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.UnknownFileType;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.util.ExceptionUtil;
import com.sjhy.plugin.entity.GlobalConfig;
import com.sjhy.plugin.entity.GlobalConfigGroup;
@ -215,6 +215,10 @@ public class GlobalConfigSettingPanel implements Configurable {
// 如果编辑面板已经实例化需要选释放后再初始化
if (templateEditor == null) {
FileType velocityFileType = FileTypeManager.getInstance().getFileTypeByExtension("vm");
// 当IDE未安装velocity插件时使用txt类型处理数据
if (velocityFileType == UnknownFileType.INSTANCE) {
velocityFileType = FileTypeManager.getInstance().getFileTypeByExtension("txt");
}
templateEditor = new TemplateEditor(project, item.getName() + ".vm", item.getValue(), GLOBAL_CONFIG_DESCRIPTION_INFO, velocityFileType);
// 代码修改回调
templateEditor.setCallback(() -> onUpdate());

View File

@ -282,26 +282,33 @@ public class SelectSavePath extends JDialog {
refreshPath();
});
//添加包选择事件
packageChooseButton.addActionListener(e -> {
PackageChooserDialog dialog = new PackageChooserDialog("Package Chooser", project);
dialog.show();
PsiPackage psiPackage = dialog.getSelectedPackage();
if (psiPackage != null) {
packageField.setText(psiPackage.getQualifiedName());
// 刷新路径
refreshPath();
}
});
try {
Class.forName("com.intellij.ide.util.PackageChooserDialog");
//添加包选择事件
packageChooseButton.addActionListener(e -> {
PackageChooserDialog dialog = new PackageChooserDialog("Package Chooser", project);
dialog.show();
PsiPackage psiPackage = dialog.getSelectedPackage();
if (psiPackage != null) {
packageField.setText(psiPackage.getQualifiedName());
// 刷新路径
refreshPath();
}
});
// 添加包编辑框失去焦点事件
packageField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
// 刷新路径
refreshPath();
}
});
// 添加包编辑框失去焦点事件
packageField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
// 刷新路径
refreshPath();
}
});
} catch (ClassNotFoundException e) {
// 没有PackageChooserDialog并非支持Java的IDE禁用相关UI组件
packageField.setEnabled(false);
packageChooseButton.setEnabled(false);
}
//初始化路径
refreshPath();

View File

@ -19,6 +19,7 @@ import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.UnknownFileType;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
@ -246,8 +247,13 @@ public class TemplateSettingPanel implements Configurable {
protected void selectedItem(Template item) {
// 如果编辑面板已经实例化需要选释放后再初始化
if (templateEditor == null) {
FileType velocityFileType = FileTypeManager.getInstance().getFileTypeByExtension("vm");
templateEditor = new TemplateEditor(project, item.getName() + ".vm", item.getCode(), TEMPLATE_DESCRIPTION_INFO, velocityFileType);
// 针对不支持velocity的IDE特殊处理
String fileType = "vm";
if (FileTypeManager.getInstance().getFileTypeByExtension(fileType) == UnknownFileType.INSTANCE) {
fileType = "txt";
}
FileType velocityFileType = FileTypeManager.getInstance().getFileTypeByExtension(fileType);
templateEditor = new TemplateEditor(project, item.getName() + "." + fileType, item.getCode(), TEMPLATE_DESCRIPTION_INFO, velocityFileType);
// 代码修改回调
templateEditor.setCallback(() -> onUpdate());
baseItemSelectPanel.getRightPanel().add(templateEditor.createComponent(), BorderLayout.CENTER);
@ -358,6 +364,10 @@ public class TemplateSettingPanel implements Configurable {
PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(project);
String fileName = templateEditor.getName();
FileType velocityFileType = FileTypeManager.getInstance().getFileTypeByExtension("vm");
// 当IDE未安装velocity插件时使用txt类型处理数据
if (velocityFileType == UnknownFileType.INSTANCE) {
velocityFileType = FileTypeManager.getInstance().getFileTypeByExtension("txt");
}
PsiFile psiFile = psiFileFactory.createFileFromText("EasyCodeTemplateDebug.vm.ft", velocityFileType, code, 0, true);
// 标识为模板让velocity跳过语法校验
psiFile.getViewProvider().putUserData(FileTemplateManager.DEFAULT_TEMPLATE_PROPERTIES, FileTemplateManager.getInstance(project).getDefaultProperties());

View File

@ -204,10 +204,12 @@
<depends>com.intellij.modules.lang</depends>
<!--必须依赖Database Tool插件-->
<depends>com.intellij.database</depends>
<!--必须依赖Velocity插件-->
<depends>com.intellij.velocity</depends>
<!--java支持扩展-->
<depends>com.intellij.modules.java</depends>
<application-components>
<!--<component>-->
<!--<implementation-class>com.sjhy.plugin.tool.CloneUtils</implementation-class>-->
<!--</component>-->
</application-components>
<!--扩展点-->
<extensions defaultExtensionNs="com.intellij">
@ -219,8 +221,6 @@
<projectService serviceInterface="com.sjhy.plugin.service.CodeGenerateService" serviceImplementation="com.sjhy.plugin.service.impl.CodeGenerateServiceImpl"/>
<!--系统设置面板-->
<applicationConfigurable dynamic="true" instance="com.sjhy.plugin.ui.MainSetting"/>
<velocity.globalVariableProvider implementation="com.sjhy.plugin.provider.EasyCodeGlobalVariableProvider"/>
</extensions>
<actions>
@ -231,4 +231,4 @@
</group>
</actions>
</idea-plugin>
</idea-plugin>