diff --git a/README.md b/README.md index decb5a3..108e640 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,7 @@ lua require'telescope'.extensions.project.project{ display_type = 'full' } | `hidden_files` | Show hidden files in selected project | bool (default: false) | | `order_by` | Order projects by `asc`, `desc`, `recent` | string (default: recent) | | `sync_with_nvim_tree` | Sync projects with nvim tree plugin | bool (default: false) | +| `enable_osc7` | Emit OSC7 directory change notifications | bool (default: false) | | `search_by` | Telescope finder search by field (title/path) | string or table (default: title). Can also be a table {"title", "path"} to search by both title and path | | `on_project_selected` | Custom handler when project is selected | function(prompt_bufnr) (default: find project files) | | `cd_scope` | Array of cd scopes: `tab`, `window`, `global` | table (default: {"tab", "window"}) | @@ -144,6 +145,7 @@ require('telescope').setup { order_by = "asc", search_by = "title", sync_with_nvim_tree = true, -- default false + enable_osc7 = true, -- default false -- default for on_project_selected = find project files on_project_selected = function(prompt_bufnr) -- Do anything you want in here. For example: diff --git a/lua/telescope/_extensions/project/main.lua b/lua/telescope/_extensions/project/main.lua index 0ee02e0..ddedffd 100644 --- a/lua/telescope/_extensions/project/main.lua +++ b/lua/telescope/_extensions/project/main.lua @@ -65,6 +65,7 @@ M.setup = function(setup_config) on_project_selected = setup_config.on_project_selected search_by = setup_config.search_by or "title" sync_with_nvim_tree = setup_config.sync_with_nvim_tree or false + _utils.set_osc7_enabled(setup_config.enable_osc7 or false) local cd_scope = setup_config.cd_scope or { "tab", "window" } _actions.set_cd_scope(cd_scope) _git.update_git_repos(base_dirs, ignore_missing_dirs) diff --git a/lua/telescope/_extensions/project/utils.lua b/lua/telescope/_extensions/project/utils.lua index 62d04b2..040476a 100644 --- a/lua/telescope/_extensions/project/utils.lua +++ b/lua/telescope/_extensions/project/utils.lua @@ -1,8 +1,12 @@ local Path = require('plenary.path') local Project = require("telescope._extensions.project.project") +local uv = vim.uv or vim.loop + local M = {} +local osc7_enabled = false + -- The file path to telescope projects M.telescope_projects_file = vim.fn.stdpath('data') .. '/telescope-projects.txt' -- The file path to telescope workspaces @@ -101,6 +105,39 @@ M.store_project = function(file, project) file:write(line_contents .. '\n') end +-- Notify supporting terminals of the directory change via OSC7. +local notify_osc7 = function(project_path) + if not osc7_enabled then + return + end + + local abs_path = vim.fn.fnamemodify(project_path, ':p') + if abs_path == '' then + return + end + + local success, uri = pcall(vim.uri_from_fname, abs_path) + if not success then + return + end + + local hostname = '' + if uv and uv.os_gethostname then + hostname = uv.os_gethostname() or '' + end + + if hostname ~= '' then + uri = uri:gsub('^file://', 'file://' .. hostname) + end + + local osc7_sequence = string.format('\027]7;%s\007', uri) + pcall(vim.fn.chansend, vim.v.stderr, osc7_sequence) +end + +M.set_osc7_enabled = function(enabled) + osc7_enabled = not not enabled +end + ---Trim whitespace for strings ---@param s string ---@return string @@ -174,6 +211,7 @@ M.change_project_dir = function(project_path, cd_scope) if Path:new(project_path):exists() then M.update_last_accessed_project_time(project_path) vim.fn.execute(cd_scope .. " " .. project_path, "silent") + notify_osc7(project_path) if sync_with_nvim_tree then M.open_in_nvim_tree(project_path) end