Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: annotate types to eliminiate diagnostics and speed up tests #338

Merged
merged 9 commits into from
Aug 4, 2024
30 changes: 13 additions & 17 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,30 @@ jobs:
args: --check . -g '*.lua' -g '!deps/'

test:
runs-on: ubuntu-latest
timeout-minutes: 2
strategy:
matrix:
neovim_version: ["v0.7.2", "v0.8.3", "v0.9.5", "v0.10.0", "nightly"]
os: [ubuntu-latest]
neovim_version: ["v0.7.2", "v0.8.3", "v0.9.5", "v0.10.1", "nightly"]
include:
- os: windows-latest
neovim_version: v0.10.1
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- run: date +%F > todays-date

- name: restore cache for today's nightly.
uses: actions/cache@v4
with:
path: _neovim
key: ${{ runner.os }}-x64-${{ hashFiles('todays-date') }}
# For sshing in to debug GH actions
# - name: Setup tmate session
# uses: mxschmitt/action-tmate@v3
# with:
# detached: true

- name: setup neovim
uses: rhysd/action-setup-vim@v1
with:
neovim: true
version: ${{ matrix.neovim_version }}

- name: install ripgrep
run: |
sudo apt-get update
sudo apt-get install -y ripgrep

- name: run tests
run: |
make test
- name: Run tests
run: make test
41 changes: 21 additions & 20 deletions doc/auto-session.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defaultConf *defaultConf*
{auto_restore_enabled?} (boolean) Enables/disables auto restoring session on start
{auto_session_suppress_dirs?} (table) Suppress auto session for directories
{auto_session_allowed_dirs?} (table) Allow auto session for directories, if empty then all directories are allowed except for suppressed ones
{auto_session_create_enabled} (boolean|function) Enables/disables auto creating new sessions. Can take a function that should return true/false if a session should be created or not
{auto_session_create_enabled?} (boolean|function) Enables/disables auto creating new sessions. Can take a function that should return true/false if a session should be created or not
{auto_session_enable_last_session?} (boolean) On startup, loads the last saved session if session for cwd does not exist
{auto_session_use_git_branch?} (boolean) Include git branch name in session name to differentiate between sessions for different git branches
{auto_restore_lazy_delay_enabled?} (boolean) Automatically detect if Lazy.nvim is being used and wait until Lazy is done to make sure session is restored correctly. Does nothing if Lazy isn't being used. Can be disabled if a problem is suspected or for debugging
Expand All @@ -19,22 +19,24 @@ luaOnlyConf *luaOnlyConf*
Lua Only Configs for Auto Session

Fields: ~
{cwd_change_handling} (CwdChangeHandling)
{bypass_session_save_file_types?} (table) List of file types to bypass auto save when the only buffer open is one of the file types listed
{close_unsupported_windows?} (boolean) Whether to close windows that aren't backed by a real file
{silent_restore} (boolean) Whether to restore sessions silently or not
{log_level?} (string|integer) "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR
{args_allow_single_directory?} (boolean) Follow normal sesion save/load logic if launched with a single directory as the only argument
Argv Handling
{args_allow_files_auto_save?} (boolean|function) Allow saving a session even when launched with a file argument (or multiple files/dirs). It does not load any existing session first. While you can just set this to true, you probably want to set it to a function that decides when to save a session when launched with file args. See documentation for more detail
{cwd_change_handling?} (boolean|CwdChangeHandling)
{bypass_session_save_file_types?} (table) List of file types to bypass auto save when the only buffer open is one of the file types listed
{close_unsupported_windows?} (boolean) Whether to close windows that aren't backed by a real file
{silent_restore?} (boolean) Whether to restore sessions silently or not
{log_level?} (string|integer) "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR
{args_allow_single_directory?} (boolean) Follow normal sesion save/load logic if launched with a single directory as the only argument
Argv Handling
{args_allow_files_auto_save?} (boolean|function) Allow saving a session even when launched with a file argument (or multiple files/dirs). It does not load any existing session first. While you can just set this to true, you probably want to set it to a function that decides when to save a session when launched with file args. See documentation for more detail
{session_lens?} (session_lens_config) Session lens configuration options


CwdChangeHandling *CwdChangeHandling*
CWD Change Handling Config

Fields: ~
{restore_upcoming_session} (boolean) {true} restore session for upcoming cwd on cwd change
{pre_cwd_changed_hook} (boolean)
{pre_cwd_changed_hook?} (boolean) {true} This is called after auto_session code runs for the DirChangedPre autocmd
{post_cwd_changed_hook?} (boolean) {true} This is called after auto_session code runs for the DirChanged autocmd


session_control *session_control*
Expand All @@ -46,11 +48,10 @@ session_control *session_control*


AutoSession.setup({config}) *AutoSession.setup*
Setup function for AutoSession. Config is not marked as defaultConf so it doesn't
create a diagnostic about missing fields.
Setup function for AutoSession

Parameters: ~
{config} (table|nil) Config for auto session
{config} (defaultConf|nil) Config for auto session


AutoSession.session_exists_for_cwd() *AutoSession.session_exists_for_cwd*
Expand Down Expand Up @@ -190,13 +191,13 @@ session_lens_config *session_lens_config*
Session Lens Config

Fields: ~
{shorten_path} (boolean) Deprecated, pass { 'shorten' } to path_display
{path_display} (table) An array that specifies how to handle paths. Read :h telescope.defaults.path_display
{theme_conf} (table)
{buftypes_to_ignore} (table) Deprecated, if you're using this please report your usage on github
{previewer} (boolean)
{session_control} (session_control)
{load_on_setup} (boolean)
{shorten_path?} (boolean) Deprecated, pass { 'shorten' } to path_display
{path_display?} (table) An array that specifies how to handle paths. Read :h telescope.defaults.path_display
{theme_conf?} (table)
{buftypes_to_ignore?} (table) Deprecated, if you're using this please report your usage on github
{previewer?} (boolean)
{session_control?} (session_control)
{load_on_setup?} (boolean)


SessionLens.setup() *SessionLens.setup*
Expand Down
28 changes: 12 additions & 16 deletions lua/auto-session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local AutoCmds = require "auto-session.autocmds"
----------- Setup ----------
local AutoSession = {
---@type luaOnlyConf
---@diagnostic disable-next-line: missing-fields
conf = {},

-- Hold on to the lib object here, useful to have the same Lib object for unit
Expand Down Expand Up @@ -48,7 +47,7 @@ end
---@field auto_restore_enabled? boolean Enables/disables auto restoring session on start
---@field auto_session_suppress_dirs? table Suppress auto session for directories
---@field auto_session_allowed_dirs? table Allow auto session for directories, if empty then all directories are allowed except for suppressed ones
---@field auto_session_create_enabled boolean|function Enables/disables auto creating new sessions. Can take a function that should return true/false if a session should be created or not
---@field auto_session_create_enabled? boolean|function Enables/disables auto creating new sessions. Can take a function that should return true/false if a session should be created or not
---@field auto_session_enable_last_session? boolean On startup, loads the last saved session if session for cwd does not exist
---@field auto_session_use_git_branch? boolean Include git branch name in session name to differentiate between sessions for different git branches
---@field auto_restore_lazy_delay_enabled? boolean Automatically detect if Lazy.nvim is being used and wait until Lazy is done to make sure session is restored correctly. Does nothing if Lazy isn't being used. Can be disabled if a problem is suspected or for debugging
Expand All @@ -72,14 +71,15 @@ local defaultConf = {

---Lua Only Configs for Auto Session
---@class luaOnlyConf
---@field cwd_change_handling CwdChangeHandling
---@field cwd_change_handling? boolean|CwdChangeHandling
---@field bypass_session_save_file_types? table List of file types to bypass auto save when the only buffer open is one of the file types listed
---@field close_unsupported_windows? boolean Whether to close windows that aren't backed by a real file
---@field silent_restore boolean Whether to restore sessions silently or not
---@field silent_restore? boolean Whether to restore sessions silently or not
---@field log_level? string|integer "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR
---Argv Handling
---@field args_allow_single_directory? boolean Follow normal sesion save/load logic if launched with a single directory as the only argument
---@field args_allow_files_auto_save? boolean|function Allow saving a session even when launched with a file argument (or multiple files/dirs). It does not load any existing session first. While you can just set this to true, you probably want to set it to a function that decides when to save a session when launched with file args. See documentation for more detail
---@field session_lens? session_lens_config Session lens configuration options

local luaOnlyConf = {
bypass_session_save_file_types = nil, -- Bypass auto save when only buffer open is one of these file types
Expand All @@ -89,17 +89,16 @@ local luaOnlyConf = {
---CWD Change Handling Config
---@class CwdChangeHandling
---@field restore_upcoming_session boolean {true} restore session for upcoming cwd on cwd change
---@field pre_cwd_changed_hook boolean? {true} This is called after auto_session code runs for the DirChangedPre autocmd
---@field post_cwd_changed_hook boolean? {true} This is called after auto_session code runs for the DirChanged autocmd
---@field pre_cwd_changed_hook? boolean {true} This is called after auto_session code runs for the DirChangedPre autocmd
---@field post_cwd_changed_hook? boolean {true} This is called after auto_session code runs for the DirChanged autocmd

---@type CwdChangeHandling this config can also be set to `false` to disable cwd change handling altogether.
---@type boolean|CwdChangeHandling this config can also be set to `false` to disable cwd change handling altogether.
---Can also be set to a table with any of the following keys:
--- {
--- restore_upcoming_session = true,
--- pre_cwd_changed_hook = nil, -- lua function hook. This is called after auto_session code runs for the `DirChangedPre` autocmd
--- post_cwd_changed_hook = nil, -- lua function hook. This is called after auto_session code runs for the `DirChanged` autocmd
--- }
---@diagnostic disable-next-line: assign-type-mismatch
cwd_change_handling = false,
---Session Control Config
---@class session_control
Expand Down Expand Up @@ -132,9 +131,8 @@ local function check_config()
end
end

---Setup function for AutoSession. Config is not marked as defaultConf so it doesn't
---create a diagnostic about missing fields.
---@param config table|nil Config for auto session
---Setup function for AutoSession
---@param config defaultConf|nil Config for auto session
function AutoSession.setup(config)
AutoSession.conf = vim.tbl_deep_extend("force", AutoSession.conf, config or {})
Lib.setup(AutoSession.conf)
Expand All @@ -146,7 +144,6 @@ function AutoSession.setup(config)

check_config()

---@diagnostic disable-next-line: undefined-field
if AutoSession.conf.session_lens.load_on_setup then
Lib.logger.debug "Loading session lens on setup"
AutoSession.setup_session_lens()
Expand Down Expand Up @@ -350,6 +347,9 @@ local function bypass_save_by_filetype()

for _, current_window in ipairs(windows) do
local buf = vim.api.nvim_win_get_buf(current_window)

-- Deprecated as 0.9.0, should update to following when we only want to support 0.9.0+
-- local buf_ft = vim.bo[buf].filetype
local buf_ft = vim.api.nvim_buf_get_option(buf, "filetype")

local local_return = false
Expand Down Expand Up @@ -651,10 +651,7 @@ function AutoSession.autosave_and_restore(session_name)
end

local function write_to_session_control_json(session_file_name)
--- FIXME: fix the types at some point
---@diagnostic disable-next-line: undefined-field
local control_dir = AutoSession.conf.session_lens.session_control.control_dir
---@diagnostic disable-next-line: undefined-field
local control_file = AutoSession.conf.session_lens.session_control.control_filename
session_file_name = Lib.expand(session_file_name)

Expand Down Expand Up @@ -742,7 +739,6 @@ local function auto_restore_session_at_vim_enter()
if AutoSession.conf.auto_session_enable_last_session then
Lib.logger.debug "Last session is enabled, checking for session"

---@diagnostic disable-next-line: cast-local-type
local last_session_name = Lib.get_latest_session(AutoSession.get_root_dir())
if last_session_name then
Lib.logger.debug("Found last session: " .. last_session_name)
Expand Down
2 changes: 2 additions & 0 deletions lua/auto-session/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ end
---@param file_or_dir string
---@return string
function Lib.expand(file_or_dir)
-- Deprecated as 0.9.0, should update to following when we only want to support 0.9.0+
-- local saved_wildignore = vim.o.wildignore
local saved_wildignore = vim.api.nvim_get_option "wildignore"
vim.api.nvim_set_option("wildignore", "")
---@diagnostic disable-next-line: param-type-mismatch
Expand Down
19 changes: 9 additions & 10 deletions lua/auto-session/session-lens/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ local SessionLens = {

---Session Lens Config
---@class session_lens_config
---@field shorten_path boolean Deprecated, pass { 'shorten' } to path_display
---@field path_display table An array that specifies how to handle paths. Read :h telescope.defaults.path_display
---@field theme_conf table
---@field buftypes_to_ignore table Deprecated, if you're using this please report your usage on github
---@field previewer boolean
---@field session_control session_control
---@field load_on_setup boolean
---@field shorten_path? boolean Deprecated, pass { 'shorten' } to path_display
---@field path_display? table An array that specifies how to handle paths. Read :h telescope.defaults.path_display
---@field theme_conf? table
---@field buftypes_to_ignore? table Deprecated, if you're using this please report your usage on github
---@field previewer? boolean
---@field session_control? session_control
---@field load_on_setup? boolean

---@type session_lens_config
---@diagnostic disable-next-line: missing-fields
local defaultConf = {
theme_conf = {},
previewer = false,
Expand Down Expand Up @@ -152,8 +151,8 @@ SessionLens.search_session = function(custom_opts)
return { "fd", "--type", "f", "--color", "never" }
elseif 1 == vim.fn.executable "fdfind" then
return { "fdfind", "--type", "f", "--color", "never" }
elseif 1 == vim.fn.executable "find" and vim.fn.has "win32" == 0 then
return { "find", ".", "-type", "f" }
elseif 1 == vim.fn.executable "ls" and vim.fn.has "win32" == 0 then
return { "ls" }
elseif 1 == vim.fn.executable "cmd" and vim.fn.has "win32" == 1 then
return { "cmd", "/C", "dir", "/b" }
end
Expand Down
3 changes: 2 additions & 1 deletion tests/git_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe("The git config", function()

-- init repo and make a commit
runCmdAndPrint "git init -b main"
runCmdAndPrint 'git config user.email "test@test.com"; git config user.name "test"'
runCmdAndPrint 'git config user.email "test@test.com"'
runCmdAndPrint 'git config user.name "test"'
runCmdAndPrint "git add test.txt"
runCmdAndPrint "git commit -m 'init'"

Expand Down
24 changes: 22 additions & 2 deletions tests/test_lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,31 @@ M.named_session_name = "mysession"
M.named_session_path = M.session_dir .. M.named_session_name .. ".vim"

function M.fileHasString(file_path, string)
return vim.fn.system('rg -c "' .. string .. '" "' .. file_path .. '"'):gsub("%s+", "") ~= ""
if vim.fn.has "win32" == 1 then
-- Have to make sure it's windows' native find (and not MinGW's find), so we use specify SystemRoot
-- This is necessary to make it work for the GH action
return vim.fn
.system('findstr /c:"' .. string .. '" "' .. (file_path:gsub("/", "\\")) .. '" | %SystemRoot%\\system32\\find /c /v ""')
:gsub("%s+", "") ~= "0"
end
return vim.fn.system('grep -c "' .. string .. '" "' .. file_path .. '"'):gsub("%s+", "") ~= "0"
end

function M.sessionHasFile(session_path, file)
return vim.fn.system('rg badd "' .. session_path .. '" | rg -c "' .. file .. '"'):gsub("%s+", "") == "1"
if vim.fn.has "win32" == 1 then
-- Have to make sure it's windows' native find (and not MinGW's find), so we use specify SystemRoot
-- This is necessary to make it work for the GH action
return vim.fn
.system(
'findstr badd "'
.. (session_path:gsub("/", "\\"))
.. '" | findstr /c:"'
.. file
.. '" | %SystemRoot%\\system32\\find /c /v ""'
)
:gsub("%s+", "") == "1"
end
return vim.fn.system('grep badd "' .. session_path .. '" | grep -c "' .. file .. '"'):gsub("%s+", "") == "1"
end

function M.assertSessionHasFile(session_path, file)
Expand Down