我正在写一个插件,它会插入getter和setter方法到当前的java文件。我的问题是,我每次运行它好像没有变化发生在插件的时间。然而,变化确实发生了,但只有当我进入文件| 同步?他们在文件中显示出来。
我打开了自动同步设置,但这并不解决问题。我也试图寻找到的IntelliJ社区版的源代码,试图看代码隐藏文件是什么| 同步但没有管理。
public void makeGetters(AnActionEvent e, ArrayList<String> lines, String mainLine) {
Project project = e.getProject();
Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
Document document = FileEditorManager.getInstance(project).getSelectedTextEditor().getDocument();
SelectionModel selectionModel = editor.getSelectionModel();
VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
String fileName = virtualFile.getPath();
String method = "Error method not initiated";
String returnType = "Error return type not initiated";
for (String s : lines) {
String[] lineArray = s.split(" ");
if (s.contains(" String ")) {
returnType = "String";
}
else if (s.contains(" int ")) {
returnType = "int";
}
else if (s.contains(" double ")) {
returnType = "double";
}
else if (s.contains(" boolean ")) {
returnType = "boolean";
}
method = "\n\tpublic " + returnType + " getName () { return " + lineArray[5] + " }\n";
try {
insert(fileName, method, mainLine);
} catch (IOException exception) {
Messages.showInfoMessage("IOException in insert method", "IOException!");
} finally {
//Some line(s) of code which will synchronize the document.
}
public void insert(String fileName, String method, String mainLine) throws IOException {
Path path = Paths.get(fileName);
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
int line = lines.indexOf(" public static void main(String[] args) {") - 1;
lines.add(line,method);
Files.write(path, lines, StandardCharsets.UTF_8);
}
我想代码写入到文件,然后该文件在最后进行同步。
任何帮助或方向将是巨大的。
下面的代码行解决我的问题:
VirtualFileManager.getInstance().syncRefresh()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句