Skip to content

Commit

Permalink
refactor(switcher): Remove unnecessary 'ProjectInfo' pseudoclass, use…
Browse files Browse the repository at this point in the history
… 'Project' instead
  • Loading branch information
Gazareth committed Feb 5, 2023
1 parent b8665a2 commit 62af93a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
14 changes: 14 additions & 0 deletions lua/projections/project.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ function Project.new(name, workspace)
return project
end

-- Alternate constructor (using a given project dir)
---@param dir_path string Full path for session file
---@return Project
---@nodiscard
function Project.new_from_dir(dir_path)
local path = Path.new(dir_path)
local name = path:basename()
local workspace = path:parent()
local project = setmetatable({}, Project)
project.name = name
project.workspace = workspace
return project
end

-- Returns the path to the project
---@return Path
---@nodiscard
Expand Down
3 changes: 3 additions & 0 deletions lua/projections/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ function Session.restore_from_session_file(spath)
-- TODO: correctly indicate errors here!
vim.cmd("silent! source " .. spath)
if config.restore_hooks.post ~= nil then config.restore_hooks.post() end
-- If successful, formally set project
local Switcher = require("projections.switcher")
Switcher:set_current()
return true
end

Expand Down
54 changes: 26 additions & 28 deletions lua/projections/switcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,24 @@ local Session = require("projections.session")
local Project = require("projections.project")
local utils = require("projections.utils")

---@alias ProjectInfo { path: Path, project: Project }
local initial_project_info = {
project = "",
path = "",
}
local initial_project_info = {}

local M = {}

M._current = initial_project_info;
M._current = Project;

-- Attempts to return the current active project
----@return ProjectInfo
function M:get_current()
return self._current;
end

-- Creates a table of all necessary information about a project
-- @param path string Path to the project's root directory
-- @param name string Name of the project
-- return nil | ProjectInfo
---@nodiscard
local create_project_info = function(path)
local name = utils.project_name_from_session_filepath(path)
return name and path and {
path = path,
project = name,
} or nil
-- Attempts to set the current active project, with no args passed, unsets current project
-- @param project_info ProjectInfo table of information about the project to set as the current one
----@return boolean
local M:set_current(project_info)
self._current = project_info or initial_project_info
return true
end

-- Attempts to switch to the last loaded project
Expand All @@ -37,33 +28,40 @@ function M:last()
local latest_session = Session.latest()
if latest_session ~= nil then
local project_dir = utils.project_dir_from_session_file(tostring(latest_session))
return self:switch(vim.fn.expand(project_dir))
-- "expand" for OS compatiblity (Windows)
project_dir = vim.fn.expand(project_dir)
return self:switch(Project.new_from_dir(project_dir))
end
return false
end

-- Attempts to switch projects and load the session file.
---@param spath string Path to project root
---@param new_project Project table describing project to switch to
---@return boolean
function M:switch(spath)
function M:switch(new_project)
local new_path = new_project:path()
if utils._unsaved_buffers_present() then
vim.notify("projections: Unsaved buffers. Unable to switch projects", vim.log.levels.WARN)
return false
end

local session_info = Session.info(spath)
if session_info == nil then return false end

if vim.loop.cwd() ~= spath then Session.store(vim.loop.cwd()) end
vim.cmd("noautocmd cd " .. spath)
-- Attempt to store current session before moving on to a new project
if new_path ~= self._current:path() then Session.store(vim.loop.cwd()) end
vim.cmd("noautocmd cd " .. new_path)
vim.cmd [[
silent! %bdelete
clearjumps
]]
self._current = create_project_info(spath)

if Session.restore(spath) then
vim.schedule(function() print("Restored session for project: ", spath) end)
-- Formally set the current project
self._current = new_project

-- If there's a session for the project we're switching to, attempt to restore it
local session_info = Session.info(new_path)
if session_info == nil then return false expand

if Session.restore(new_path) then
vim.schedule(function() print("Restored session for project: ", new_path) end)
end
return true
end
Expand Down
8 changes: 0 additions & 8 deletions lua/projections/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ local split = function(inputString, sep)
return fields
end

M.project_name_from_session_filepath = function(filepath)
local file_name = vim.fn.fnamemodify(filepath, ":p:t")
local split_name = split(file_name, "_")
-- Remove the session identifier (everyting beyond the last '_')
table.remove(split_name)
return table.concat(split_name)
end

-- Gets a project directory from a session file
---@param filepath string Filepath for session file
---@return string
Expand Down

0 comments on commit 62af93a

Please sign in to comment.