Last active
August 8, 2024 16:49
-
-
Save josefaidt/1ad51ef5e657f017dae82ff756fce12c to your computer and use it in GitHub Desktop.
Revisions
-
josefaidt revised this gist
Aug 8, 2024 . 2 changed files with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -8,9 +8,9 @@ if vim.g.vscode then -- 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>') 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 charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ vim.api.nvim_create_user_command( -- set filter vim.api.nvim_create_user_command( 'AmplifyDocsSetFilter', function(opts) fold_filters.SetFilter(opts.args) vim.api.nvim_command('UnfoldAll') @@ -22,7 +22,7 @@ vim.api.nvim_create_user_command( -- 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 -
josefaidt revised this gist
Aug 8, 2024 . 1 changed file with 8 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,6 @@ function M.FoldNonMatchingFilters() 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 @@ -40,29 +39,27 @@ function M.FoldNonMatchingFilters() 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 -
josefaidt created this gist
Aug 6, 2024 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ 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>sf', ':SetFilter<Space>', { noremap = true, silent = false }) vim.keymap.set('n', '<leader>uf', ':UnfoldAll<CR>', { noremap = true, silent = false }) vim.keymap.set('n', '<leader>af', ':ApplyFilter<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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ 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 cursor_line = cursor_pos[1] - 1 -- Zero-based index 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 }) -- Move cursor if it's within the fold range if cursor_line >= fold_start and cursor_line <= fold_end then local new_cursor_line = fold_end + 1 vim.api.nvim_win_set_cursor(0, { new_cursor_line + 1, 0 }) -- Move to line after fold end cursor_line = new_cursor_line -- Update cursor_line to prevent multiple moves 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 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ 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( 'SetFilter', 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( 'ApplyFilter', 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 = '?' } )