-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from stefanwatt/feature/telescope
Telescope support & Refactor
- Loading branch information
Showing
11 changed files
with
204 additions
and
107 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
local function cd_project() | ||
local adapter = vim.g.cd_project_config.adapter | ||
if adapter == "telescope" then | ||
return require("cd-project.adapter.telescope").cd_project() | ||
end | ||
|
||
require("cd-project.adapter.vim-ui").cd_project() | ||
end | ||
|
||
return { | ||
cd_project = cd_project, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
local project = require("cd-project.project-repo") | ||
local api = require("cd-project.api") | ||
|
||
---@param opts? table | ||
local cd_project = function(opts) | ||
local utils = require("cd-project.utils") | ||
local success, picker = pcall(require, "telescope.pickers") | ||
if not success then | ||
utils.log_error("telescope not installed") | ||
return | ||
end | ||
local pickers = require("telescope.pickers") | ||
local finders = require("telescope.finders") | ||
local conf = require("telescope.config").values | ||
local actions = require("telescope.actions") | ||
local action_state = require("telescope.actions.state") | ||
opts = opts or {} | ||
pickers | ||
.new(opts, { | ||
attach_mappings = function(prompt_bufnr, map) | ||
actions.select_default:replace(function() | ||
actions.close(prompt_bufnr) | ||
---@type CdProject.Project | ||
local selected_project = action_state.get_selected_entry().value | ||
api.cd_project(selected_project.path) | ||
end) | ||
return true | ||
end, | ||
prompt_title = "cd to project", | ||
finder = finders.new_table({ | ||
results = project.get_projects(), | ||
---@param project_entry CdProject.Project | ||
entry_maker = function(project_entry) | ||
return { | ||
value = project_entry, | ||
display = project_entry.path, | ||
ordinal = project_entry.path, | ||
} | ||
end, | ||
}), | ||
sorter = conf.generic_sorter(opts), | ||
}) | ||
:find() | ||
end | ||
|
||
return { | ||
cd_project = cd_project, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
---@class CdProject.Hook | ||
---@field callback fun(param: string) | ||
---@field name? string | ||
---@field order? number | ||
---@field trigger_point? string | ||
---@field pattern? string | ||
---@field match_rule? fun(dir: string): boolean | ||
--- | ||
---@param hooks CdProject.Hook[] | ||
---@param dir string | ||
---@param point string | ||
---@return function[] | ||
local get_hooks = function(hooks, dir, point) | ||
local matching_hooks = {} | ||
for _, hook in ipairs(hooks) do | ||
local matches = false | ||
local trigger_point = hook.trigger_point or "AFTER_CD" | ||
|
||
-- Check if match_rule exists and returns true | ||
if hook.match_rule == nil and hook.pattern == nil then | ||
matches = true | ||
elseif hook.match_rule and hook.match_rule(dir) and trigger_point == point then | ||
matches = true | ||
-- If no match_rule, check if pattern exists in dir | ||
elseif hook.pattern and dir:find(hook.pattern) and trigger_point == point then | ||
matches = true | ||
end | ||
|
||
-- Add hook to matching_hooks if it matches | ||
if matches then | ||
table.insert(matching_hooks, hook) | ||
end | ||
end | ||
|
||
-- Sort hooks by order if order is defined | ||
table.sort(matching_hooks, function(a, b) | ||
return (a.order or 0) < (b.order or 0) | ||
end) | ||
|
||
-- Extract and return the callback functions from the matching hooks | ||
local callbacks = {} | ||
for _, hook in ipairs(matching_hooks) do | ||
table.insert(callbacks, hook.callback) | ||
end | ||
|
||
return callbacks | ||
end | ||
|
||
return { | ||
get_hooks = get_hooks, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
---@param tbl table | ||
---@param path string | ||
local write_json_file = function(tbl, path) | ||
local content = vim.fn.json_encode(tbl) -- Encoding table to JSON string | ||
|
||
local file, err = io.open(path, "w") | ||
if not file then | ||
error("Could not open file: " .. err) | ||
return nil | ||
end | ||
|
||
file:write(content) | ||
file:close() | ||
end | ||
|
||
---@param path string | ||
---@return table | ||
local read_or_init_json_file = function(path) | ||
local file, _ = io.open(path, "r") | ||
if not file then | ||
write_json_file({}, path) | ||
return {} | ||
end | ||
|
||
local content = file:read("*a") -- Read the entire content | ||
file:close() | ||
|
||
return vim.fn.json_decode(content) or {} | ||
end | ||
|
||
return { | ||
write_json_file = write_json_file, | ||
read_or_init_json_file = read_or_init_json_file, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
local json = require("cd-project.json") | ||
---@class CdProject.Project | ||
---@field path string | ||
---@field name string | ||
---@field desc string|nil | ||
|
||
---@return CdProject.Project[] | ||
local get_projects = function() | ||
return json.read_or_init_json_file(vim.g.cd_project_config.projects_config_filepath) | ||
end | ||
|
||
---@param projects CdProject.Project[] | ||
local write_projects = function(projects) | ||
json.write_json_file(projects, vim.g.cd_project_config.projects_config_filepath) | ||
end | ||
|
||
return { | ||
get_projects = get_projects, | ||
write_projects = write_projects, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
local M = {} | ||
|
||
---@param msg string | ||
local function log_error(msg) | ||
vim.notify(msg, vim.log.levels.ERROR, { title = "cd-project.nvim" }) | ||
end | ||
|
||
return { | ||
log_error = log_error, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters