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

Possible to convert windows \ paths to / ? #8

Open
avario-cpu opened this issue Aug 25, 2024 · 2 comments
Open

Possible to convert windows \ paths to / ? #8

avario-cpu opened this issue Aug 25, 2024 · 2 comments

Comments

@avario-cpu
Copy link

avario-cpu commented Aug 25, 2024

Amazing plugin but afaik it will insert paths with \ on Windows which are basically useless, unless there's an option to replace \ with / I'm unaware of ?

@avario-cpu avario-cpu changed the title Anything feasible regarding to windows \ path ? Anything feasible regarding to windows \ paths ? Aug 25, 2024
@avario-cpu avario-cpu changed the title Anything feasible regarding to windows \ paths ? Possible to convert windows \ paths to / ? Aug 25, 2024
@kiyoon
Copy link
Owner

kiyoon commented Aug 25, 2024

Hi, currently there's no way to insert path with replacing, but I'm open to review a PR if you wish to implement it.

@avario-cpu
Copy link
Author

I don't think I'm anywhere near remotely capable for this now. But I did end up implementing a much simpler version of what you've done in my config.

If that's useful to anyone else looking for something similar:

code
local action_state = require("telescope.actions.state")
local actions = require("telescope.actions")
local telescope = require("telescope")

local function convert_path(path)
  -- Convert backslashes to forward slashes
  return path:gsub("\\", "/")
end

local function get_relative_path(path)
  local relative = vim.fn.fnamemodify(path, ":.")
  return convert_path(relative)
end

local function insert_path(prompt_bufnr, use_relative)
  local selection = action_state.get_selected_entry()
  if selection == nil then
    print("No selection")
    return
  end
  local path = selection.path
  if path == nil then
    print("No path")
    return
  end

  if use_relative then
    path = get_relative_path(path)
  else
    path = convert_path(path)
  end

  actions.close(prompt_bufnr)
  vim.api.nvim_put({ path }, "", false, true)
end

local insert_absolute_path = function(prompt_bufnr)
  insert_path(prompt_bufnr, false)
end

local insert_relative_path = function(prompt_bufnr)
  insert_path(prompt_bufnr, true)
end

telescope.setup({
  defaults = {
    mappings = {
      n = {
        ["="] = insert_absolute_path,
        ["-"] = insert_relative_path,
      },
    },
  },
})

-- Set up the Ctrl+t binding for insert mode Telescope call
vim.keymap.set("i", "<C-t>", function()
  require("telescope.builtin").find_files()
end, { noremap = true, silent = true, desc = "Telescope find files" })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants