Skip to content

Instantly share code, notes, and snippets.

@josefaidt
Last active August 8, 2024 16:49
Show Gist options
  • Save josefaidt/1ad51ef5e657f017dae82ff756fce12c to your computer and use it in GitHub Desktop.
Save josefaidt/1ad51ef5e657f017dae82ff756fce12c to your computer and use it in GitHub Desktop.

Revisions

  1. josefaidt revised this gist Aug 8, 2024. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions init.lua
    Original 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>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 })
    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>')
    4 changes: 2 additions & 2 deletions vscode_amplify_docs_fold_filters_commands.lua
    Original 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(
    'SetFilter',
    '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(
    'ApplyFilter',
    'AmplifyDocsApplyFilter',
    function(opts)
    vim.api.nvim_command('UnfoldAll')
    if opts.args and #opts.args > 0 then
  2. josefaidt revised this gist Aug 8, 2024. 1 changed file with 8 additions and 11 deletions.
    19 changes: 8 additions & 11 deletions vscode_amplify_docs_fold_filters.lua
    Original 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 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
    @@ -40,29 +39,27 @@ function M.FoldNonMatchingFilters()
    end
    end
    if not match_found then
    fold_start = i - 1 -- Adjust for zero-based indexing
    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
    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]))
    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
    return M
  3. josefaidt created this gist Aug 6, 2024.
    17 changes: 17 additions & 0 deletions init.lua
    Original 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
    68 changes: 68 additions & 0 deletions vscode_amplify_docs_fold_filters.lua
    Original 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
    34 changes: 34 additions & 0 deletions vscode_amplify_docs_fold_filters_commands.lua
    Original 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 = '?' }
    )