How to programmatically determine new and modified files via plugin? Follow
Answered
Hello,
- I need to get the set of new and modified source files in a project. I looked into IdeDocumentHistory and ChangeList* as well as many other classes. I don't get the files I expect from the methods I found. It also appears that one needs to have a version control system in place to be able to identify the source files.
- I would like to be able to determine new and modified files in a project without requiring VCS. I expect there must be a way, since the compiler is incremental. I also expect that it is likely to be an aggregation of various file reporting mechanisms. Another reason I believe this must be possible is because Undo exists for each file, so modifications must be tracked.
- I would also like to be able to identify generated source files.
Any suggestions on how I go about getting the results I need?
Below is the code I have created which provides source files. I am concerned that in some situations, it will produce too many results.I am starting with a CompileContext object.
Thanks,
Todd
private static Set<VirtualFile> getFilesToProcess(final CompileContext compileContext)
{
final CompileScope compileScope = compileContext.getCompileScope();
final Set<VirtualFile> newFiles =
new HashSet<>(Arrays.asList(compileScope.getFiles(null, true)));
newFiles.addAll(getFilesToShow(compileContext.getProject()));
return Collections.unmodifiableSet(newFiles);
}
private static List<VirtualFile> getFilesToShow(@NotNull final Project project)
{
final List<VirtualFile> filesData = new ArrayList<>();
final Runnable filesToShowRunnable = () -> {
final FileEditorManagerImpl editorManager =
(FileEditorManagerImpl)FileEditorManager.getInstance(project);
final ArrayList<VirtualFile> editors = new ArrayList<>();
final Set<VirtualFile> addedFiles = new LinkedHashSet<>();
for (final Pair<VirtualFile, EditorWindow> pair :
editorManager.getSelectionHistory())
{
editors.add(pair.first);
}
for (final VirtualFile editor : editors)
{
addedFiles.add(editor);
filesData.add(editor);
}
if (filesData.size() <= 1
&& editors.size() == 1
&& (filesData.isEmpty() || !editors.get(0).equals(filesData.get(0)))
&& addedFiles.add(editors.get(0)))
{
filesData.add(0, editors.get(0));
}
};
if (EDT.isCurrentThreadEdt())
{
filesToShowRunnable.run();
}
else
{
SwingUtilities.invokeLater(filesToShowRunnable);
}
return filesData;
}
Please sign in to leave a comment.
Hi Todd,
Please take a look at https://plugins.jetbrains.com/docs/intellij/virtual-file.html#how-do-i-get-notified-when-vfs-changes
Thank you. I appreciate the hint. I'll check it out.