Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Suc authored and Kevin Suc committed Mar 24, 2024
2 parents 1cf1ed8 + 2afdaa7 commit cbe89ad
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 79 deletions.
49 changes: 17 additions & 32 deletions .github/workflows/docgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,29 @@ on:
- master

jobs:
build-sources:
docgen:
name: Generate docs
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- run: date +%F > todays-date
- name: Restore cache for today's nightly.
uses: actions/cache@v2
with:
path: build
key: ${{ runner.os }}-appimage-${{ hashFiles('todays-date') }}

- name: Prepare
run: |
test -d build || {
mkdir -p build
wget https://github.com/neovim/neovim/releases/download/nightly/nvim.appimage
chmod +x nvim.appimage
mv nvim.appimage ./build/nvim
}
mkdir -p ~/.local/share/nvim/site/pack/vendor/start
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
git clone --depth 1 https://github.com/tjdevries/tree-sitter-lua ~/.local/share/nvim/site/pack/vendor/start/tree-sitter-lua
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start
- uses: rhysd/action-setup-vim@v1
with:
neovim: true
version: stable

- name: Build parser
run: |
# We have to build the parser every single time to keep up with parser changes
cd ~/.local/share/nvim/site/pack/vendor/start/tree-sitter-lua
make dist
cd -
- uses: actions/cache@v3
with:
path: deps
key: ${{ runner.os }}-deps-${{ matrix.version }}-${{ hashFiles('todays-date') }}
restore-keys: |
${{ runner.os }}-deps-${{ matrix.version }}-
- name: Generating docs
run: |
export PATH="${PWD}/build/:${PATH}"
make docgen
- name: Generating
run: make docgen

# inspired by nvim-lspconfigs
- name: Update documentation
- name: Commit changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MSG: |
Expand All @@ -61,3 +45,4 @@ jobs:
git add doc/
# Only commit and push if we have changes
git diff --quiet && git diff --staged --quiet || (git commit -m "${COMMIT_MSG}"; git push origin HEAD:${GITHUB_REF})
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
name: stylua
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: JohnnyMorganz/stylua-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Tests
on: [push]
jobs:
test:
name: unit tests
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
version: [v0.9.0, nightly]

steps:
- uses: actions/checkout@v4
- run: date +%F > todays-date

- uses: rhysd/action-setup-vim@v1
with:
neovim: true
version: ${{ matrix.version }}

- uses: actions/cache@v3
with:
path: deps
key: ${{ runner.os }}-deps-${{ matrix.version }}-${{ hashFiles('todays-date') }}
restore-keys: |
${{ runner.os }}-deps-${{ matrix.version }}-
- name: Run tests
run: |
nvim --version
make test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
doc/tags
deps/
37 changes: 36 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
docgen:
.PHONY: docgen test clean

DEPS_DIR := deps
TS_DIR := $(DEPS_DIR)/tree-sitter-lua
PLENARY_DIR := $(DEPS_DIR)/plenary.nvim
TELESCOPE_DIR := $(DEPS_DIR)/telescope.nvim

define git_clone_or_pull
@mkdir -p $(dir $1)
@if [ ! -d "$1" ]; then \
git clone --depth 1 $2 $1; \
else \
git -C "$1" pull; \
fi
endef

$(DEPS_DIR):
@mkdir -p $@

plenary: | $(DEPS_DIR)
$(call git_clone_or_pull,$(PLENARY_DIR),https://github.com/nvim-lua/plenary.nvim)

docgen-deps: plenary | $(DEPS_DIR)
$(call git_clone_or_pull,$(TS_DIR),https://github.com/tjdevries/tree-sitter-lua)
cd "$(TS_DIR)" && make dist

test-deps: plenary | $(DEPS_DIR)
$(call git_clone_or_pull,$(TELESCOPE_DIR),https://github.com/nvim-telescope/telescope.nvim)

docgen: docgen-deps
nvim --headless --noplugin -u scripts/minimal_init.vim -c "luafile ./scripts/gendocs.lua" -c 'qa'

test: test-deps
nvim --headless --noplugin -u scripts/minimal_init.vim -c "PlenaryBustedDirectory lua/tests/ { minimal_init = './scripts/minimal_init.vim' }"

clean:
@rm -rf $(DEPS_DIR)
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ More demo examples can be found in the [showcase issue](https://github.com/nvim-

### Table of Contents

- [Requirements](#requirements)
- [Installation](#installation)
- [Setup and Configuration](#setup-and-configuration)
- [Usage](#usage)
Expand All @@ -20,22 +21,27 @@ More demo examples can be found in the [showcase issue](https://github.com/nvim-

---

## Requirements
- Neovim >= **0.9.0**
- [fd](https://github.com/sharkdp/fd) (optional, for faster browser)
- git (optional, for display git status)

## Installation

Install the plugin with your preferred package manager.

```lua
-- packer
use {
"nvim-telescope/telescope-file-browser.nvim",
requires = { "nvim-telescope/telescope.nvim", "nvim-lua/plenary.nvim" }
}

--lazy
{
"nvim-telescope/telescope-file-browser.nvim",
dependencies = { "nvim-telescope/telescope.nvim", "nvim-lua/plenary.nvim" }
}

-- packer
use {
"nvim-telescope/telescope-file-browser.nvim",
requires = { "nvim-telescope/telescope.nvim", "nvim-lua/plenary.nvim" }
}
```

<details>
Expand All @@ -49,12 +55,6 @@ Plug 'nvim-telescope/telescope-file-browser.nvim'

</details>

#### Optional Dependencies

- `telescope-file-browser` optionally leverages [fd](https://github.com/sharkdp/fd) if installed for faster, more async browsing
- [`nvim-web-devicons`](https://github.com/nvim-tree/nvim-web-devicons) for file icons
- `git` to show the status of files directly in the file browser.

## Setup and Configuration

You can configure the `telescope-file-browser` like any other `telescope.nvim` picker. Please see `:h telescope-file-browser.picker` for the full set of options dedicated to the picker. Unless otherwise stated, you can pass these options either to your configuration at extension setup or picker startup. For instance, you can map `theme` and [mappings](#remappings) as you are used to from `telescope.nvim`.
Expand Down Expand Up @@ -95,8 +95,8 @@ local fb_actions = require "telescope._extensions.file_browser.actions"
require("telescope").setup {
extensions = {
file_browser = {
path = vim.loop.cwd()
cwd = vim.loop.cwd()
path = vim.loop.cwd(),
cwd = vim.loop.cwd(),
cwd_to_path = false,
grouped = false,
files = true,
Expand All @@ -105,7 +105,7 @@ require("telescope").setup {
auto_depth = false,
select_buffer = false,
hidden = { file_browser = false, folder_browser = false },
respect_gitignore = vim.fn.executable "fd" == 1
respect_gitignore = vim.fn.executable "fd" == 1,
no_ignore = false,
follow_symlinks = false,
browse_files = require("telescope._extensions.file_browser.finders").browse_files,
Expand Down
2 changes: 1 addition & 1 deletion lua/telescope/_extensions/file_browser/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ end
local function newly_created_root(path, cwd)
local idx
local parents = path:parents()
cwd = fb_utils.trim_right_os_sep(cwd)
cwd = fb_utils.sanitize_path_str(cwd)
for i, p in ipairs(parents) do
if p == cwd then
idx = i
Expand Down
28 changes: 12 additions & 16 deletions lua/telescope/_extensions/file_browser/make_entry.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local fb_utils = require "telescope._extensions.file_browser.utils"
local Path = require "plenary.path"
local action_state = require "telescope.actions.state"
local entry_display = require "telescope.pickers.entry_display"
local fb_git = require "telescope._extensions.file_browser.git"
local fb_utils = require "telescope._extensions.file_browser.utils"
local fb_make_entry_utils = require "telescope._extensions.file_browser.make_entry_utils"
local fs_stat = require "telescope._extensions.file_browser.fs_stat"
local utils = require "telescope.utils"
local log = require "telescope.log"
local entry_display = require "telescope.pickers.entry_display"
local action_state = require "telescope.actions.state"
local os_sep = Path.path.sep
local state = require "telescope.state"
local strings = require "plenary.strings"
local Path = require "plenary.path"
local os_sep = Path.path.sep
local os_sep_len = #os_sep
local utils = require "telescope.utils"

local sep = " "

Expand Down Expand Up @@ -114,12 +114,9 @@ local make_entry = function(opts)
})

-- needed since Path:make_relative does not resolve parent dirs
local parent_dir = Path:new(opts.cwd):parent():absolute()
local parent_dir = fb_utils.sanitize_path_str(Path:new(opts.cwd):parent():absolute())
local mt = {}
mt.cwd = opts.cwd
-- +1 to start at first file char; cwd may or may not end in os_sep
local cwd_substr = #mt.cwd + 1
cwd_substr = mt.cwd:sub(-1, -1) ~= os_sep and cwd_substr + os_sep_len or cwd_substr
mt.cwd = fb_utils.sanitize_path_str(opts.cwd)

-- TODO(fdschmidt93): handle VimResized with due to variable width
mt.display = function(entry)
Expand All @@ -128,7 +125,7 @@ local make_entry = function(opts)
local display_array = {}
local icon, icon_hl

local tail = fb_utils.trim_right_os_sep(entry.ordinal)
local tail = fb_utils.sanitize_path_str(entry.ordinal)
local path_display = utils.transform_path(opts, tail)

if entry.is_dir then
Expand Down Expand Up @@ -238,14 +235,13 @@ local make_entry = function(opts)
end

return function(absolute_path)
absolute_path = fb_utils.trim_right_os_sep(absolute_path)
absolute_path = fb_utils.sanitize_path_str(absolute_path)
local path = Path:new(absolute_path)
local is_dir = path:is_dir()

local e = setmetatable({
absolute_path,
ordinal = (absolute_path == opts.cwd and ".")
or (absolute_path == parent_dir and ".." or absolute_path:sub(cwd_substr, -1)),
ordinal = fb_make_entry_utils.get_ordinal_path(absolute_path, opts.cwd, parent_dir),
Path = path,
path = absolute_path,
is_dir = is_dir,
Expand Down
29 changes: 29 additions & 0 deletions lua/telescope/_extensions/file_browser/make_entry_utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local Path = require "plenary.path"
local fb_utils = require "telescope._extensions.file_browser.utils"

local os_sep = Path.path.sep
local os_sep_len = #os_sep

local M = {}

--- compute ordinal path
--- accounts for `auto_depth` option
---@param path string
---@param cwd string
---@param parent string
---@return string
M.get_ordinal_path = function(path, cwd, parent)
path = fb_utils.sanitize_path_str(path)
if path == cwd then
return "."
elseif path == parent then
return ".."
end

local cwd_substr = #cwd + 1
cwd_substr = cwd:sub(-1, -1) ~= os_sep and cwd_substr + os_sep_len or cwd_substr

return path:sub(cwd_substr, -1)
end

return M
23 changes: 18 additions & 5 deletions lua/telescope/_extensions/file_browser/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ local truncate = require("plenary.strings").truncate

local fb_utils = {}

fb_utils.iswin = vim.loop.os_uname().sysname == "Windows_NT"

fb_utils.is_dir = function(path)
if Path.is_path(path) then
return path:is_dir()
Expand Down Expand Up @@ -181,17 +183,28 @@ fb_utils.notify = function(funname, opts)
end
end

-- trim the right most os separator from a path string
fb_utils.trim_right_os_sep = function(path)
return path:sub(-1, -1) ~= os_sep and path or path:sub(1, -1 - #os_sep)
--- de-dupe os_sep and right trim os_sep nearly everywhere
--- exception for root dir path (`/` or `C:\`)
---@param path string
---@return string
fb_utils.sanitize_path_str = function(path)
path = path:gsub(os_sep .. os_sep, os_sep)
if iswin then
if path:match "^%w:\\$" then
return path
else
return (path:gsub("(.)\\$", "%1"))
end
end
return (path:gsub("(.)/$", "%1"))
end

local _get_selection_index = function(path, dir, results)
local path_dir = Path:new(path):parent():absolute()
path = fb_utils.trim_right_os_sep(path)
path = fb_utils.sanitize_path_str(path)
if dir == path_dir then
for i, path_entry in ipairs(results) do
if fb_utils.trim_right_os_sep(path_entry.value) == path then
if fb_utils.sanitize_path_str(path_entry.value) == path then
return i
end
end
Expand Down

0 comments on commit cbe89ad

Please sign in to comment.