How to synchronize current file in intellij using code?
I'm writing a plugin which will insert getter and setter methods into the current java file. My issue is, every time I run the plugin it seems like no change happens. However, changes do happen but only after I go into File | Synchronize do they show up in the file.
I have automatic synchronize settings turned on but this doesn't fix the issue. I've also tried looking into the intellij community edition source code to try and see what code is behind File | Synchronize but didn't manage.
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 = "ntpublic " + 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);
}
I would like the code to write to the file and then for the file to be synchronized at the end.
Any help or direction would be great.
java intellij-idea
add a comment |
I'm writing a plugin which will insert getter and setter methods into the current java file. My issue is, every time I run the plugin it seems like no change happens. However, changes do happen but only after I go into File | Synchronize do they show up in the file.
I have automatic synchronize settings turned on but this doesn't fix the issue. I've also tried looking into the intellij community edition source code to try and see what code is behind File | Synchronize but didn't manage.
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 = "ntpublic " + 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);
}
I would like the code to write to the file and then for the file to be synchronized at the end.
Any help or direction would be great.
java intellij-idea
add a comment |
I'm writing a plugin which will insert getter and setter methods into the current java file. My issue is, every time I run the plugin it seems like no change happens. However, changes do happen but only after I go into File | Synchronize do they show up in the file.
I have automatic synchronize settings turned on but this doesn't fix the issue. I've also tried looking into the intellij community edition source code to try and see what code is behind File | Synchronize but didn't manage.
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 = "ntpublic " + 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);
}
I would like the code to write to the file and then for the file to be synchronized at the end.
Any help or direction would be great.
java intellij-idea
I'm writing a plugin which will insert getter and setter methods into the current java file. My issue is, every time I run the plugin it seems like no change happens. However, changes do happen but only after I go into File | Synchronize do they show up in the file.
I have automatic synchronize settings turned on but this doesn't fix the issue. I've also tried looking into the intellij community edition source code to try and see what code is behind File | Synchronize but didn't manage.
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 = "ntpublic " + 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);
}
I would like the code to write to the file and then for the file to be synchronized at the end.
Any help or direction would be great.
java intellij-idea
java intellij-idea
asked Jan 2 at 14:27
chesspriestchesspriest
357
357
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Looking at the documentation... perhaps you should try passing the last parameter(s) to the Files.write method, which is var args, on the last line of your insert method. Check this out:
"The options parameter specifies how the the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present."
Try passing SYNC in. It is a guess, but makes some sense.
add a comment |
The following line of code fixes my problem:
VirtualFileManager.getInstance().syncRefresh()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008084%2fhow-to-synchronize-current-file-in-intellij-using-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Looking at the documentation... perhaps you should try passing the last parameter(s) to the Files.write method, which is var args, on the last line of your insert method. Check this out:
"The options parameter specifies how the the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present."
Try passing SYNC in. It is a guess, but makes some sense.
add a comment |
Looking at the documentation... perhaps you should try passing the last parameter(s) to the Files.write method, which is var args, on the last line of your insert method. Check this out:
"The options parameter specifies how the the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present."
Try passing SYNC in. It is a guess, but makes some sense.
add a comment |
Looking at the documentation... perhaps you should try passing the last parameter(s) to the Files.write method, which is var args, on the last line of your insert method. Check this out:
"The options parameter specifies how the the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present."
Try passing SYNC in. It is a guess, but makes some sense.
Looking at the documentation... perhaps you should try passing the last parameter(s) to the Files.write method, which is var args, on the last line of your insert method. Check this out:
"The options parameter specifies how the the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present."
Try passing SYNC in. It is a guess, but makes some sense.
answered Jan 2 at 18:37


Caio Dornelles AntunesCaio Dornelles Antunes
986
986
add a comment |
add a comment |
The following line of code fixes my problem:
VirtualFileManager.getInstance().syncRefresh()
add a comment |
The following line of code fixes my problem:
VirtualFileManager.getInstance().syncRefresh()
add a comment |
The following line of code fixes my problem:
VirtualFileManager.getInstance().syncRefresh()
The following line of code fixes my problem:
VirtualFileManager.getInstance().syncRefresh()
answered Jan 2 at 22:37
chesspriestchesspriest
357
357
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008084%2fhow-to-synchronize-current-file-in-intellij-using-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown