From c1e1815fd5b296171ab347cbd49973c87c7f201a Mon Sep 17 00:00:00 2001 From: AnnatarHe Date: Fri, 2 Jan 2026 12:18:18 +0800 Subject: [PATCH] feat(system): use last 2 folder layers for project name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, get_project_name() only returned the last folder layer (e.g., "coding-extension-vim"). This caused context loss when multiple projects have similar subfolder names like "frontend". Now returns the last 2 layers (e.g., "malamtime/coding-extension-vim") for better project identification in heartbeat data. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- lua/shelltime/utils/system.lua | 9 +++++++-- tests/system_spec.lua | 8 +++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lua/shelltime/utils/system.lua b/lua/shelltime/utils/system.lua index 06ce6b2..3019ffe 100644 --- a/lua/shelltime/utils/system.lua +++ b/lua/shelltime/utils/system.lua @@ -80,11 +80,16 @@ function M.get_project_root(file_path) return vim.fn.fnamemodify(file_path, ':p:h') end ---- Get project name from root path +--- Get project name from root path (last 2 folder layers) ---@param project_root string Project root path ---@return string Project name function M.get_project_name(project_root) - return vim.fn.fnamemodify(project_root, ':t') + local tail = vim.fn.fnamemodify(project_root, ':t') + local parent = vim.fn.fnamemodify(project_root, ':h:t') + if parent and parent ~= '' and parent ~= '/' then + return parent .. '/' .. tail + end + return tail end --- Generate UUID v4 diff --git a/tests/system_spec.lua b/tests/system_spec.lua index 1a599eb..ce5d1e0 100644 --- a/tests/system_spec.lua +++ b/tests/system_spec.lua @@ -181,12 +181,12 @@ describe('shelltime.utils.system', function() end) describe('get_project_name', function() - it('should extract directory name from path', function() + it('should extract last 2 folder layers from path', function() local name = system.get_project_name('/home/user/projects/myapp') - assert.equals('myapp', name) + assert.equals('projects/myapp', name) end) - it('should handle simple path', function() + it('should handle simple path with only 1 layer', function() local name = system.get_project_name('/myapp') assert.equals('myapp', name) end) @@ -196,6 +196,8 @@ describe('shelltime.utils.system', function() local name = system.get_project_name(project_dir) assert.is_string(name) assert.is_true(#name > 0) + -- Should contain a slash for 2 layers + assert.is_true(name:find('/') ~= nil) end) end) end)