Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chip committed Feb 26, 2022
0 parents commit 3fb8b98
Show file tree
Hide file tree
Showing 10 changed files with 558 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"Lua.completion.autoRequire": false,
"Lua.workspace.checkThirdParty": false
}
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) 2021 Chip Castle Dot Com, Inc. - http://chipcastle.com

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.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: test

test:
nvim --headless -c "PlenaryBustedDirectory lua/tests/"
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# telescope-software-licenses.nvim

![telescope-software-licenses.nvim DEMO](assets/telescope-software-licenses.gif "telescope-software-licenses.nvim DEMO")

This [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim)
extension

## Requirements

- Neovim (v0.6.0)
- [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) (required)
- *Only tested on MacOS 11.6.1*

## Install

You can install the extension by using your plugin manager of choice or by
cloning this repository somewhere on your filepath, and then adding the
following somewhere after telescope in your configuration file (`init.vim` or
`init.lua`).

### Using [Paq](https://github.com/savq/paq-nvim)
```lua
require "paq" {
{ 'nvim-lua/plenary.nvim' };
{ 'nvim-telescope/telescope.nvim' };
{ "chip/telescope-software-licenses.nvim" };
}
require('telescope').load_extension('telescope-software-licenses');
```

### Using [packer.nvim](https://github.com/wbthomason/packer.nvim)
```lua
use 'nvim-lua/plenary.nvim'
use 'nvim-telescope/telescope.nvim'
use { "chip/telescope-software-licenses.nvim" }
require('telescope').load_extension('telescope-software-licenses')
```
## Setup

### Commands

```vim
" Prompts user for Github user/repo
" Prompts for file argument, but uses README.md as default
:Telescope telescope-software-licenses list
```

### Bind to Keys:

```vim
" Replace <Leader>cf with whatever you prefer
nnoremap <Leader>sl <cmd>Telescope telescope-software-licenses list
```

### Development

```zsh
$ git clone git@github.com:chip/telescope-software-licenses.nvim.git
$ cd telescope-software-licenses.nvim/lua/telescope/_extensions
$ nvim --cmd "set rtp+=$(pwd)" -u plugin/dev.vim
```
23 changes: 23 additions & 0 deletions lua/telescope/_extensions/plugin/dev.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
" FOR DEVELOPMENT
" nvim --cmd "set rtp+=$(pwd)" -u lua/telescope/_extensions/plugin/dev.vim
set rtp+=.

function! ReloadPlugin()
lua << EOF
for k in pairs(package.loaded) do
if k:match("software%-licenses") then
package.loaded[k] = nil
end
end
EOF
endfunction

" Reload the plugin
nnoremap <Leader>rp :call ReloadPlugin()<CR>
" Test the plugin
nnoremap <Leader>sl :Telescope software-licenses find<CR>
" Inital load
lua <<EOF
require('telescope').load_extension('software-licenses')
EOF
55 changes: 55 additions & 0 deletions lua/telescope/_extensions/software-licenses/find.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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"
local previewers = require "telescope.previewers"
local licenses = require('telescope._extensions.software-licenses.licenses')
local results = {}

function string:split(sep)
sep = sep or "\n"
local fields = {}
local pattern = string.format("([^%s]+)", sep)
for match, _ in self:gmatch(pattern) do table.insert(fields, match) end
return fields
end

for _, license in ipairs(licenses) do
local name = license.name
local text = license.text:split()
table.insert(results, {name, text})
end

local M = {}

M.licenses = function(telescope_opts)
telescope_opts = vim.tbl_extend("keep", telescope_opts or {},
require("telescope.themes").get_dropdown {})
pickers.new(telescope_opts, {
prompt_title = "telescope-software-licenses",
finder = finders.new_table {
results = results,
entry_maker = function(entry)
return {value = entry, display = entry[1], ordinal = entry[1]}
end
},
sorter = conf.generic_sorter(telescope_opts),
attach_mappings = function(prompt_bufnr, _)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
vim.api.nvim_put(selection.value[2], "l", false, true)
end)
return true
end,
previewer = previewers.new_buffer_previewer({
define_preview = function(self, entry)
local output = entry.value[2]
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, output)
end
})
}):find()
end

return M
34 changes: 34 additions & 0 deletions lua/telescope/_extensions/software-licenses/health.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local health = require("health")
local M = {}

local vim_installed = function()
local vim_version = vim.version()
local version = tonumber(string.format("%s.%s", vim_version.major,
vim_version.minor))
if version >= 0.6 then
return true
else
return false
end
end

M.check = function()
health.report_start("Start health check for telescope-software-licenses.nvim")
if vim_installed() then
health.report_ok("nvim version >= 0.6 installed")
else
health.report_error("please install nvim > 0.6")
end

local packages = {"telescope", "plenary.curl"}
for _, package in pairs(packages) do
local has_package, _ = pcall(require, package)
if has_package then
health.report_ok(string.format("%s is installed", package))
else
health.report_error(string.format("This plugin requires %s", package))
end
end
end

return M
11 changes: 11 additions & 0 deletions lua/telescope/_extensions/software-licenses/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local has_telescope, telescope = pcall(require, "telescope")
if not has_telescope then
error("This plugin requires nvim-telescope/telescope.nvim")
end

local find = require("telescope._extensions.software-licenses.find")
local health = require("telescope._extensions.software-licenses.health")
return telescope.register_extension {
setup = function(opts) return opts end,
exports = {find = find.licenses, health = health.check}
}
Loading

0 comments on commit 3fb8b98

Please sign in to comment.