Skip to content

Commit

Permalink
add components support
Browse files Browse the repository at this point in the history
- Added support for OpenAPI's components.
- Refactored some variable names.
- Updated `README.md` file.
  • Loading branch information
mpinta committed Oct 14, 2023
1 parent 60c6ab6 commit 8e7c4d5
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 60 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# outln.nvim
An outline plugin for Neovim, kinda.

## What and why?
## What and how?
Plugin provides an interface to easily and quickly jump to a language-specific definition within an open file. Under the hood it uses [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) to find the necessary nodes and [dressing.nvim](https://github.com/stevearc/dressing.nvim) to provide the UI.

## Install
Expand Down Expand Up @@ -36,7 +36,8 @@ outln.setup({
interfaces = true
},
openapi = {
endpoints = true
endpoints = true,
components = true
},
python = {
functions = true,
Expand All @@ -62,6 +63,7 @@ outln.setup({
| `structs` | Enables searching for struct definitions. | `go` |
| `interfaces` | Enables searching for interface definitions. | `go` |
| `endpoints` | Enables searching for endpoint definitions. | `openapi` |
| `components` | Enables searching for component definitions. | `openapi` |
| `classes` | Enables searching for class definitions. | `python` |

### Commands
Expand Down
64 changes: 64 additions & 0 deletions lua/outln/cleaner.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local utils = require("outln.utils")

local M = {}

-- Cleans OpenAPI query captures of endpoints.
local function clean_endpoints(qc)
local qc_clean = {}

for _, capture in pairs(qc) do
local name = capture[1]
local line = capture[2]

if name:sub(1, 1) == "/" then
qc_clean[line] = {}

table.insert(
qc_clean[line],
name
)
end
end

return qc_clean
end

-- Cleans OpenAPI's query captures of components.
local function clean_components(qc)
local qc_clean = {}

for _, capture in pairs(qc) do
local name = capture[1]
local line = capture[2]

local char = name:sub(1, 1)

if utils.is_alphabetical(char) and utils.is_uppercase(char) then
qc_clean[line] = {}

table.insert(
qc_clean[line],
name
)
end
end

return qc_clean
end

-- Cleans query captures based on provided language type and option.
function M.clean_query_captures(qc, lang_type, option)
if lang_type == "openapi" then
if option == "endpoints" then
return clean_endpoints(qc)
end

if option == "components" then
return clean_components(qc)
end
end

return qc
end

return M
29 changes: 16 additions & 13 deletions lua/outln/handler.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
local querier = require("outln.querier")
local queries = require("outln.queries")
local utils = require("outln.utils")
local querier = require("outln.querier")
local cleaner = require("outln.cleaner")
local sorter = require("outln.sorter")

local M = {}

-- Handles getting language-specific query captures.
function M.handle(lang, lang_type, options)
local qc = {}
local t = {}

for k, v in pairs(options) do
if v == true then
local query = queries[lang_type][k]
for definition, enabled in pairs(options) do
if enabled then
local query = queries[lang_type][definition]

if query ~= nil then
qc[k] = querier.get_query_captures(
local qc = querier.get_query_captures(
lang,
query
)

qc[k] = utils.sort_query_captures(qc[k])
qc = cleaner.clean_query_captures(
qc,
lang_type,
definition
)

t[definition] = sorter.sort_query_captures(qc)
end
end
end

if lang_type == "openapi" then
qc = utils.clean_openapi_query_captures(qc)
end

return qc
return t
end

return M
3 changes: 3 additions & 0 deletions lua/outln/icons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ M.methods = "󰫺"
-- Defines icon for endpoint definition.
M.endpoints = ""

-- Defines icon for component definition.
M.components = ""

return M
27 changes: 17 additions & 10 deletions lua/outln/outln.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ local defaults = {
interfaces = true
},
openapi = {
endpoints = true
endpoints = true,
components = true
},
python = {
functions = true,
Expand Down Expand Up @@ -55,19 +56,22 @@ local function get_captures(lang)
lang_type = "openapi"
end

local c = handler.handle(
local captures = handler.handle(
lang,
lang_type,
M.options[lang_type]
)

return c
return captures
end

-- Prepares captures for display on UI.
local function prepare_captures(c)
local n, m = preparer.prepare(c)
return n, m
local function prepare_captures(captures)
local names, metadata = preparer.prepare(
captures
)

return names, metadata
end

-- Sets user configured options.
Expand All @@ -87,14 +91,17 @@ function M.open_outln()
error("Language is not supported.")
end

local c = get_captures(lang)
local n, m = prepare_captures(c)
local captures = get_captures(lang)

local names, metadata = prepare_captures(
captures
)

vim.ui.select(n, {
vim.ui.select(names, {
prompt = "Outln Results",
}, function(selected)
if selected ~= nil then
vim.cmd(":" .. m[selected])
vim.cmd(":" .. metadata[selected])
vim.cmd("norm! _")
vim.cmd(M.options.after)
end
Expand Down
26 changes: 12 additions & 14 deletions lua/outln/preparer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@ local definitions = {
"classes",
"functions",
"methods",
"endpoints"
"endpoints",
"components"
}

-- Prepares names and metadata of captures.
function M.prepare(c)
local n, m = {}, {}
-- Prepares names and metadata of query captures.
function M.prepare(qc)
local names, metadata = {}, {}

for _, v in pairs(definitions) do
if c[v] ~= nil and #c[v] ~= 0 then
for _, j in pairs(c[v]) do
local name = j[1]
local line = j[2]
for _, definition in pairs(definitions) do
if qc[definition] ~= nil then
for line, name in pairs(qc[definition]) do
local new_name = icons[definition] .. " " .. name[1]

name = icons[v] .. " " .. name

table.insert(n, name)
m[name] = line
table.insert(names, new_name)
metadata[new_name] = line
end
end
end

return n, m
return names, metadata
end

return M
4 changes: 2 additions & 2 deletions lua/outln/querier.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ end
-- Gets given query's captures and their metadata.
function M.get_query_captures(lang, query)
local bufnr = vim.api.nvim_get_current_buf()
local queryp = parse_query(lang, query)
local parsed_query = parse_query(lang, query)

local qc = {}

for _, captures, metadata in queryp:iter_matches(
for _, captures, metadata in parsed_query:iter_matches(
get_root_node(lang),
bufnr
) do
Expand Down
5 changes: 5 additions & 0 deletions lua/outln/queries.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ M.openapi = {
(block_mapping_pair
key: (flow_node) @annotation (#offset! @annotation)
)
]],
components = [[
(block_mapping_pair
key: (flow_node) @annotation (#offset! @annotation)
)
]]
}

Expand Down
12 changes: 12 additions & 0 deletions lua/outln/sorter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local M = {}

-- Sorts query captures based on line numbers.
function M.sort_query_captures(qc)
table.sort(qc, function(x, y)
return x[2] < y[2]
end)

return qc
end

return M
30 changes: 11 additions & 19 deletions lua/outln/utils.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
local M = {}

-- Cleans OpenAPI query captures.
function M.clean_openapi_query_captures(qc)
local qc_clean = {}

for k, v in pairs(qc) do
qc_clean[k] = {}

for _, j in pairs(v) do
if j[1]:sub(1, 1) == "/" then
table.insert(qc_clean[k], j)
end
end
-- Checks if the character is alphabetical.
function M.is_alphabetical(c)
if string.match(c, "%a") then
return true
end

return qc_clean
return false
end

-- Sorts query captures based on line numbers.
function M.sort_query_captures(qc)
table.sort(qc, function(a, b)
return a[2] < b[2]
end)
-- Checks if the character is uppercase.
function M.is_uppercase(c)
if c == string.upper(c) then
return true
end

return qc
return false
end

return M

0 comments on commit 8e7c4d5

Please sign in to comment.