Search code examples
javaintellij-ideaintellij-plugin

Intellij plugin development - Get access to internal stack of last activated editors


In an intellij plugin development environment I would like to access what should be available some kind of stack over what editors was last activated.

I believe this is use by "activate most recently opened file".

Building this index myself would lead to mistakes due to various ways to open a file/tab.

What is the best way to do this?


Solution

  •     /**
         * Note, most recent file is last in the list.
         */
        public List<VirtualFile> getRecentFiles() {
                return EditorHistoryManager.getInstance(project).getFileList();
        }
        public VirtualFile getMostRecentFile(int index) {
                return getMostRecentFile(index, true);
        }
        public VirtualFile getMostRecentFile(int index, boolean mustBeOpen) {
                List<VirtualFile> files = getRecentFiles();
    
                int i = files.size() - index - 1;
    
                if ( i < files.size() ) {
                        VirtualFile file = files.get(i);
    
                        if ( mustBeOpen && !isOpen(file) ) {
                                file = getMostRecentFile( index - 1, mustBeOpen );
                        }
    
                        return file;
                }
    
                return null;
        }
    

    getMostRecentFile(1) would get the file prior to currently active editor.

    isOpen and project is missing here but can be retrieved from AnActionEvent event in a couple of ways.