├── lua ├── session-lens.lua └── telescope │ └── _extensions │ ├── session-lens.lua │ └── session-lens │ ├── session-lens-actions.lua │ ├── session-lens-library.lua │ └── main.lua ├── .github └── FUNDING.yml ├── plugin └── session-lens.vim ├── LICENSE └── README.md /lua/session-lens.lua: -------------------------------------------------------------------------------- 1 | return require('telescope._extensions.session-lens.main') 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [rmagatti] 4 | patreon: rmagatti 5 | ko_fi: rmagatti 6 | custom: ['buymeacoffee.com/rmagatti'] 7 | -------------------------------------------------------------------------------- /plugin/session-lens.vim: -------------------------------------------------------------------------------- 1 | if exists('g:loaded_session_lens') | finish | endif " prevent loading file twice 2 | 3 | let s:save_cpo = &cpo " save user coptions 4 | set cpo&vim " reset them to defaults 5 | 6 | let LuaSearchSession = luaeval('require("telescope._extensions.session-lens.main").search_session') 7 | 8 | " Available commands 9 | command! -nargs=0 SearchSession call LuaSearchSession() 10 | 11 | let &cpo = s:save_cpo " and restore after 12 | unlet s:save_cpo 13 | 14 | let g:loaded_session_lens = 1 15 | -------------------------------------------------------------------------------- /lua/telescope/_extensions/session-lens.lua: -------------------------------------------------------------------------------- 1 | local has_telescope, telescope = pcall(require, "telescope") 2 | local has_auto_session = pcall(require, "auto-session") 3 | local SessionLens = require('telescope._extensions.session-lens.main') 4 | 5 | 6 | if not has_telescope then 7 | error("This plugin requires telescope.nvim (https://github.com/nvim-telescope/telescope.nvim)") 8 | end 9 | 10 | if not has_auto_session then 11 | error("This plugin requires auto-session (https://github.com/rmagatti/auto-session)") 12 | end 13 | 14 | return telescope.register_extension { 15 | setup = SessionLens.setup, 16 | exports = { 17 | search_session = SessionLens.search_session 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /lua/telescope/_extensions/session-lens/session-lens-actions.lua: -------------------------------------------------------------------------------- 1 | local AutoSession = require('auto-session') 2 | local action_state = require('telescope.actions.state') 3 | local actions = require('telescope.actions') 4 | 5 | local SessionLensActions = {} 6 | 7 | SessionLensActions.source_session = function(prompt_bufnr) 8 | local selection = action_state.get_selected_entry() 9 | actions.close(prompt_bufnr) 10 | vim.defer_fn(function () 11 | AutoSession.AutoSaveSession() 12 | vim.cmd("%bd!") 13 | AutoSession.RestoreSession(selection.path) 14 | end, 50) 15 | end 16 | 17 | SessionLensActions.delete_session = function(prompt_bufnr) 18 | local current_picker = action_state.get_current_picker(prompt_bufnr) 19 | current_picker:delete_selection(function(selection) 20 | AutoSession.DeleteSession(selection.path) 21 | end) 22 | end 23 | 24 | 25 | return SessionLensActions 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Ronnie Magatti 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lua/telescope/_extensions/session-lens/session-lens-library.lua: -------------------------------------------------------------------------------- 1 | local path = require('plenary.path') 2 | local AutoSession = require('auto-session') 3 | local AutoSessionLib = require('auto-session.lib') 4 | 5 | local Config = {} 6 | local Lib = { 7 | make_entry = {}, 8 | logger = {}, 9 | conf = { 10 | logLevel = false 11 | }, 12 | Config = Config, 13 | _VIM_FALSE = 0, 14 | _VIM_TRUE = 1, 15 | ROOT_DIR = "~/.config/nvim/sessions/" 16 | } 17 | 18 | 19 | 20 | -- Setup ====================================================== 21 | function Lib.setup(config) 22 | Lib.conf = Config.normalize(config) 23 | end 24 | 25 | function Config.normalize(config, existing) 26 | local conf = existing 27 | if Lib.isEmptyTable(config) then 28 | return conf 29 | end 30 | 31 | for k, v in pairs(config) do 32 | conf[k] = v 33 | end 34 | 35 | return conf 36 | end 37 | -- ==================================================== 38 | 39 | -- Helper functions =============================================================== 40 | local function hasValue (tab, val) 41 | for _, value in ipairs(tab) do 42 | if value == val then 43 | return true 44 | end 45 | end 46 | 47 | return false 48 | end 49 | 50 | 51 | function Lib.isEmptyTable(t) 52 | if t == nil then return true end 53 | return next(t) == nil 54 | end 55 | 56 | function Lib.isEmpty(s) 57 | return s == nil or s == '' 58 | end 59 | 60 | function Lib.endsWith(str, ending) 61 | return ending == "" or str:sub(-#ending) == ending 62 | end 63 | 64 | function Lib.appendSlash(str) 65 | if not Lib.isEmpty(str) then 66 | if not Lib.endsWith(str, "/") then 67 | str = str.."/" 68 | end 69 | end 70 | return str 71 | end 72 | -- =================================================================================== 73 | 74 | 75 | -- ==================== SessionLens ========================== 76 | function Lib.make_entry.gen_from_file(opts) 77 | local root = AutoSession.get_root_dir() 78 | return function(line) 79 | return { 80 | ordinal = line, 81 | value = line, 82 | filename = line, 83 | cwd = root, 84 | display = function(_) 85 | local out = AutoSessionLib.unescape_dir(line):match("(.+)%.vim") 86 | if opts.path_display and vim.tbl_contains(opts.path_display, "shorten") then 87 | out = path:new(out):shorten() 88 | end 89 | if out then return out end 90 | return line 91 | end, 92 | path = path:new(root, line):absolute() 93 | } 94 | end 95 | end 96 | -- =================================================================================== 97 | 98 | -- Logger ========================================================= 99 | function Lib.logger.debug(...) 100 | if Lib.conf.logLevel == 'debug' then 101 | print(...) 102 | end 103 | end 104 | 105 | function Lib.logger.info(...) 106 | local valid_values = {'info', 'debug'} 107 | if hasValue(valid_values, Lib.conf.logLevel) then 108 | print(...) 109 | end 110 | end 111 | 112 | function Lib.logger.error(...) 113 | error(...) 114 | end 115 | -- ========================================================= 116 | 117 | 118 | return Lib 119 | -------------------------------------------------------------------------------- /lua/telescope/_extensions/session-lens/main.lua: -------------------------------------------------------------------------------- 1 | local Lib = require('telescope._extensions.session-lens.session-lens-library') 2 | local AutoSession = require('auto-session') 3 | local SessionLensActions = require("telescope._extensions.session-lens.session-lens-actions") 4 | 5 | ----------- Setup ---------- 6 | local SessionLens = { 7 | conf = {} 8 | } 9 | 10 | local default_theme = "dropdown" 11 | 12 | local defaultConf = { 13 | theme = default_theme, 14 | theme_conf = { winblend = 10, border = true }, 15 | previewer = false 16 | } 17 | 18 | -- Set default config on plugin load 19 | SessionLens.conf = defaultConf 20 | 21 | function SessionLens.setup(config) 22 | SessionLens.conf = Lib.Config.normalize(config, SessionLens.conf) 23 | end 24 | 25 | 26 | local themes = require('telescope.themes') 27 | local actions = require('telescope.actions') 28 | 29 | SessionLens.search_session = function(custom_opts) 30 | custom_opts = (Lib.isEmptyTable(custom_opts) or custom_opts == nil) and SessionLens.conf or custom_opts 31 | 32 | -- Use auto_session_root_dir from the Auto Session plugin 33 | local cwd = AutoSession.conf.auto_session_root_dir 34 | 35 | if custom_opts.shorten_path ~= nil then 36 | Lib.logger.error('`shorten_path` config is deprecated, use the new `path_display` config instead') 37 | if custom_opts.shorten_path then 38 | custom_opts.path_display = {'shorten'} 39 | else 40 | custom_opts.path_display = nil 41 | end 42 | 43 | custom_opts.shorten_path = nil 44 | end 45 | 46 | -- error if there is no corresponding function for the user-specified theme in telescope.themes 47 | if not themes["get_" .. custom_opts.theme] then 48 | Lib.logger.error("invalid theme specified") 49 | return 50 | end 51 | 52 | local theme_opts = themes["get_" .. custom_opts.theme](custom_opts.theme_conf) 53 | 54 | -- Ignore last session dir on finder if feature is enabled 55 | if AutoSession.conf.auto_session_enable_last_session then 56 | if AutoSession.conf.auto_session_last_session_dir then 57 | local last_session_dir = AutoSession.conf.auto_session_last_session_dir:gsub(cwd, "") 58 | custom_opts["file_ignore_patterns"] = {last_session_dir} 59 | end 60 | end 61 | 62 | -- Use default previewer config by setting the value to nil if some sets previewer to true in the custom config. 63 | -- Passing in the boolean value errors out in the telescope code with the picker trying to index a boolean instead of a table. 64 | -- This fixes it but also allows for someone to pass in a table with the actual preview configs if they want to. 65 | if custom_opts.previewer ~= false and custom_opts.previewer == true then 66 | custom_opts["previewer"] = nil 67 | end 68 | 69 | local opts = { 70 | prompt_title = 'Sessions', 71 | entry_maker = Lib.make_entry.gen_from_file(custom_opts), 72 | cwd = cwd, 73 | -- TOOD: support custom mappings? 74 | attach_mappings = function(_, map) 75 | actions.select_default:replace(SessionLensActions.source_session) 76 | map("i", "", SessionLensActions.delete_session) 77 | return true 78 | end, 79 | } 80 | 81 | local find_files_conf = vim.tbl_deep_extend("force", opts, theme_opts, custom_opts or {}) 82 | require("telescope.builtin").find_files(find_files_conf) 83 | end 84 | 85 | return SessionLens 86 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # ⚠️ DEPRECATED - Session Lens is now part of auto-session. This repository is no longer needed. ⚠️ 3 | 4 | > # Session Lens 5 | > Session Lens extends [auto-session](https://github.com/rmagatti/auto-session) through Telescope.nvim, creating a simple session switcher with fuzzy finding capabilities. 6 | > 7 | > 8 | > 9 | > 10 | > # Usage 11 | > You can call the switcher from telescope 12 | > ```viml 13 | > :Telescope session-lens search_session 14 | > ``` 15 | > 16 | > Or straight from the plugin's path with lua 17 | > ```viml 18 | > :lua require('session-lens').search_session() 19 | > ``` 20 | > 21 | > # Installation 22 | > Any plugin manager should do. 23 | > 24 | > Plug 25 | > ```viml 26 | > " Plugin dependencies 27 | > Plug 'nvim-telescope/telescope.nvim' 28 | > Plug 'rmagatti/auto-session' 29 | > 30 | > Plug 'rmagatti/session-lens' 31 | > ``` 32 | > See https://github.com/nvim-telescope/telescope.nvim for it's dependencies 33 | > 34 | > Packer 35 | > ```lua 36 | > use { 37 | > 'rmagatti/session-lens', 38 | > requires = {'rmagatti/auto-session', 'nvim-telescope/telescope.nvim'}, 39 | > config = function() 40 | > require('session-lens').setup({--[[your custom config--]]}) 41 | > end 42 | > } 43 | > ``` 44 | > 45 | > The plugin is lazy loaded when calling it for the first time but you can pre-load it with Telescope like this if you'd rather have autocomplete for it off the bat. 46 | > ```lua 47 | > require("telescope").load_extension("session-lens") 48 | > ``` 49 | > 50 | > # Configuration 51 | > 52 | > ### Custom 53 | > Options can be set by calling the setup function, a common option is changing the shorten path behaviour. 54 | > ```lua 55 | > require('session-lens').setup { 56 | > path_display={'shorten'}, 57 | > } 58 | > ``` 59 | > 60 | > Another example would be changing how the dropdown looks, this can be done by setting the `theme` and `theme_conf` in the setup options. 61 | > The options in `theme_conf` get passed into `require('telescope.themes').get_dropdown(theme_conf)`, so anything supported by `get_dropdown` (or the function that corresponds to the specified `theme`) can be used here as well. 62 | > ```lua 63 | > require('session-lens').setup { 64 | > path_display = {'shorten'}, 65 | > theme = 'ivy', -- default is dropdown 66 | > theme_conf = { border = false }, 67 | > previewer = true 68 | > } 69 | > ``` 70 | > 👇 71 | > Screen Shot 2021-04-17 at 9 17 43 PM 72 | > 73 | > In addition to the above configs, since everything gets passed into `telescope.builtin.find_files`, any configs passed to the `setup` if supported by `find_files` will override the default behaviour, for example: 74 | > ```lua 75 | > require('session-lens').setup { 76 | > prompt_title = 'YEAH SESSIONS', 77 | > } 78 | > ``` 79 | > 👇 80 | > Screen Shot 2021-04-17 at 8 16 49 PM 81 | > 82 | > 83 | > # Commands 84 | > Session Lens exposes one command 85 | > - `:SearchSession` triggers the customized session picker 86 | > 87 | > # Compatibility 88 | > Neovim > 0.5 89 | > 90 | > Tested with: 91 | > ``` 92 | > NVIM v0.5.0-dev+a1ec36f 93 | > Build type: Release 94 | > LuaJIT 2.1.0-beta3 95 | > ``` 96 | > 97 | --------------------------------------------------------------------------------