Skip to content

Commit

Permalink
Initial commit of marlin
Browse files Browse the repository at this point in the history
  • Loading branch information
desdic committed Feb 16, 2024
0 parents commit e1f9af4
Show file tree
Hide file tree
Showing 14 changed files with 598 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# top-most EditorConfig file
root = true

[Makefile]
indent_style = tab

# Unix-style newlines with a newline ending every file
[{*.lua,*.md}]
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true

41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Tests

on: [push, pull_request]

jobs:
unit_tests:
name: unit tests
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-22.04
rev: nightly/nvim-linux64.tar.gz
- os: ubuntu-22.04
rev: v0.9.0/nvim-linux64.tar.gz
steps:
- uses: actions/checkout@v3
- run: date +%F > todays-date
- name: Restore from todays cache
uses: actions/cache@v3
with:
path: _neovim
key: ${{ runner.os }}-${{ matrix.rev }}-${{ hashFiles('todays-date') }}

- name: Prepare
run: |
test -d _neovim || {
mkdir -p _neovim
curl -sL "https://github.com/neovim/neovim/releases/download/${{ matrix.rev }}" | tar xzf - --strip-components=1 -C "${PWD}/_neovim"
}
- name: Dependencies
run: |
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
ln -s "$(pwd)" ~/.local/share/nvim/site/pack/vendor/start
- name: Run tests
run: |
export PATH="${PWD}/_neovim/bin:${PATH}"
nvim --version
make test
26 changes: 26 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Format

on: [push, pull_request]

jobs:
format:
name: Stylua
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: date +%W > weekly

- name: Restore cache
id: cache
uses: actions/cache@v2
with:
path: |
~/.cargo/bin
key: ${{ runner.os }}-cargo-${{ hashFiles('weekly') }}

- name: Install
if: steps.cache.outputs.cache-hit != 'true'
run: cargo install stylua

- name: Format
run: stylua --check lua/ --config-path=.stylua.toml
18 changes: 18 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Lint

on: [push, pull_request]

jobs:
lint:
name: Luacheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup
run: |
sudo apt-get update
sudo apt-get install luarocks
sudo luarocks install luacheck
- name: Lint
run: luacheck lua/ --globals vim
4 changes: 4 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"Lua.workspace.checkThirdParty": false
}
5 changes: 5 additions & 0 deletions .stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
column_width = 80
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 4
quote_style = "AutoPreferDouble"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023-2024 marlin.nvim

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.PHONY: test

test:
nvim --headless --noplugin -u scripts/test/minimal.vim \
-c "PlenaryBustedDirectory lua/marlin/test/ {minimal_init = 'scripts/tests/minimal.vim'}"
130 changes: 130 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# marlin.nvim
##### Smooth sailing between buffers

Persistent and extensible jumps across project buffers with ease.

### Setup

Example using the lazy plugin manager

```lua
{
"desdic/marlin.nvim",
opts = {},
config = function(_, opts)
local marlin = require("marlin")
marlin.setup(opts)
local keymap = vim.keymap.set

keymap("n", "<Leader>fa", function() marlin.add_file() end, { desc = "add file" })
keymap("n", "<Leader>fd", function() marlin.remove_file() end, { desc = "remove file" })

for index = 1,4 do
keymap("n", "<Leader>"..index, function() marlin.open_index(index) end, { desc = "goto "..index })
end
end
}
```

### Default configuration

```lua
local default = {
patterns = { ".git", ".svn" }, -- look for root of project
datafile = vim.fn.stdpath("data") .. "/marlin.json", -- location of data file
open_callback = function(bufnr, opts) -- default way to change to indexed buffer
vim.api.nvim_set_current_buf(bufnr)
end,
}
```

### Easy integration with most status lines

Example with [lualine](https://github.com/nvim-lualine/lualine.nvim)

```lua
return {
"nvim-lualine/lualine.nvim",
config = function()
local marlin = require("marlin")

local marlin_component = function()
local indexes = marlin.get_number_of_indexes()
if indexes == 0 then
return ""
end
local cur_index = marlin.get_index_number()

return "" .. cur_index .. "/" .. indexes
end

require("lualine").setup({
...
sections = {
...
lualine_c = { marlin_component },
...
},
})
end
```

### Extending behaviour

Default behaviour is to replace the current buffer with the indexes one with

```lua
open_callback = function(bufnr)
vim.api.nvim_set_current_buf(bufnr)
end,
```

But its possible to change the open_call function to get the behaviour you want. If you want to open new buffers in a vsplit you can

```lua
open_callback = function(bufnr, _)
vim.cmd("vsplit")
vim.api.nvim_set_current_buf(bufnr)
end,
```

Or if want to add an options to open_index that switches to the buffer if already open in a split

```lua
open_callback = function(bufnr, opts)
if opts.use_split then
local wins = vim.api.nvim_tabpage_list_wins(0)
for _, win in ipairs(wins) do
local winbufnr = vim.api.nvim_win_get_buf(win)

if winbufnr == bufnr then
vim.api.nvim_set_current_win(win)
return
end
end
end

vim.api.nvim_set_current_buf(bufnr)
end,
```

Choice is yours

### TODO

- Function to align indexes with buffers (vim.api.nvim_list_bufs)
- Function delete all indexes
- Currently no GUI and there might not be one

### Why yet another ..

When I first saw harpoon I was immediately hooked but I missed a few key features.

- I use splits and wanted to have it jump to the buffer and not replace the current one.
- I wanted persistent jumps per project and not per directory.

Like anyone else missing a feature I created a patch but it seems that many other did the same.

### Credits

Credit goes to [ThePrimeagen](https://github.com/ThePrimeagen/harpoon/) for the idea.
35 changes: 35 additions & 0 deletions lua/marlin/datafile.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
local M = {}

M.read_config = function(datafile)
if vim.fn.filereadable(datafile) ~= 0 then
local fd = io.open(datafile, "r")
if fd then
local content = fd:read("*a")
io.close(fd)
return vim.fn.json_decode(content)
end
end
return {}
end

M.save_data = function(datafile, project, localdata)
local data = M.read_config(datafile)
data[project] = localdata

-- If we have no more files we remove the project
if #data[project]["files"] == 0 then
data[project] = nil
end

local content = vim.fn.json_encode(data)
local fd = io.open(datafile, "w")
if not fd then
vim.notify("Unable to open " .. datafile .. " for write")
return
end

fd:write(content)
io.close(fd)
end

return M
Loading

0 comments on commit e1f9af4

Please sign in to comment.