Last active
August 8, 2024 16:49
-
-
Save josefaidt/1ad51ef5e657f017dae82ff756fce12c to your computer and use it in GitHub Desktop.
lua scripts to fold InlineFilters in aws amplify docs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vim.api.nvim_set_keymap('n', '<SPACE>', '<NOP>', { noremap = true }) | |
vim.g.mapleader = " " | |
if vim.g.vscode then | |
require("vscode_amplify_docs_fold_filters_commands") | |
-- VSCode extension | |
local vscode = require('vscode-neovim') | |
-- set vscode as default notifier | |
vim.notify = vscode.notify | |
-- amplify docs filters | |
vim.keymap.set('n', '<leader>uf', ':UnfoldAll<CR>', { noremap = true, silent = false }) | |
vim.keymap.set('n', '<leader>sf', ':AmplifyDocsSetFilter<Space>', { noremap = true, silent = false }) | |
vim.keymap.set('n', '<leader>af', ':AmplifyDocsApplyFilter<Space>', { noremap = true, silent = false } | |
else | |
-- ordinary Neovim | |
vim.keymap.set('n', '<leader>w', ':w<CR>') | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local vscode = require('vscode') | |
local M = {} | |
-- Function to set the current filter | |
function M.SetFilter(new_filter) | |
vim.g.vscode_amplify_docs_current_filter = new_filter | |
print("Filter set to: " .. new_filter) | |
end | |
-- Function to fold regions where the filter is not the specified filter | |
function M.FoldNonMatchingFilters() | |
local current_filter = vim.g.vscode_amplify_docs_current_filter | |
if not current_filter then | |
print("No filter set.") | |
return | |
end | |
local start_pattern = '<InlineFilter filters={' | |
local end_pattern = '</InlineFilter>' | |
local markers = {} | |
local cursor_pos = vim.api.nvim_win_get_cursor(0) | |
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) | |
local fold_start = nil | |
for i, line in ipairs(lines) do | |
if string.find(line, start_pattern) then | |
local filters = string.match(line, 'filters={%[(.-)%]}') | |
if filters then | |
local filter_list = {} | |
for filter in string.gmatch(filters, '"(.-)"') do | |
table.insert(filter_list, filter) | |
end | |
local match_found = false | |
for _, filter in ipairs(filter_list) do | |
if filter == current_filter then | |
match_found = true | |
break | |
end | |
end | |
if not match_found then | |
fold_start = i - 1 -- Adjust for zero-based indexing | |
end | |
end | |
elseif string.find(line, end_pattern) then | |
if fold_start then | |
local fold_end = i - 1 -- Adjust for zero-based indexing | |
table.insert(markers, { fold_start, fold_end }) | |
fold_start = nil | |
end | |
end | |
end | |
-- Use VSCode commands to fold regions | |
for _, range in ipairs(markers) do | |
print(string.format("Folding range: %d-%d", range[1], range[2])) | |
vscode.call('editor.fold', { range = { range[1], range[2] } }) | |
end | |
-- Restore cursor position and bring it into view | |
vim.api.nvim_win_set_cursor(0, cursor_pos) | |
vim.cmd('normal zz') | |
end | |
return M |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local fold_filters = require('vscode_amplify_docs_fold_filters') | |
-- command to unfold all | |
vim.api.nvim_create_user_command( | |
'UnfoldAll', | |
function() | |
require('vscode').call('editor.unfoldAll') | |
end, | |
{} | |
) | |
-- set filter | |
vim.api.nvim_create_user_command( | |
'AmplifyDocsSetFilter', | |
function(opts) | |
fold_filters.SetFilter(opts.args) | |
vim.api.nvim_command('UnfoldAll') | |
fold_filters.FoldNonMatchingFilters() | |
end, | |
{ nargs = 1 } | |
) | |
-- Define a command to apply the existing filter in a new file | |
vim.api.nvim_create_user_command( | |
'AmplifyDocsApplyFilter', | |
function(opts) | |
vim.api.nvim_command('UnfoldAll') | |
if opts.args and #opts.args > 0 then | |
fold_filters.SetFilter(opts.args) | |
end | |
fold_filters.FoldNonMatchingFilters() | |
end, | |
{ nargs = '?' } | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment