summaryrefslogtreecommitdiff
path: root/neovim/lua/vimrc.lua
blob: fc40e80620e9baabf17a97e82863bacafecaa1ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
-- vim: fdm=marker
local log_warning = require 'vimrc.utils'.log_warning
local M = {}

M.cwd_save_session = function()
    vim.cmd([[
augroup vimrc_save_session
    au!
    au VimLeave * mksession! ]] .. os.getenv("PWD") .. [[/Session.vim
augroup end
    ]])
end

function M.setup_treesitter()
    if vim.o.loadplugins == false then
        return
    end

    assert(vim.fn.exists(":TSInstall") == 0, "TreeSitter is already configured.")

    -- vim.cmd([[packadd nvim-treesitter]])
    require 'nvim-treesitter.configs'.setup {
        highlight = {
            enable = true,
            -- Setting this to true will run `:h syntax` and tree-sitter at the same time.
            -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
            -- Using this option may slow down your editor, and you may see some duplicate highlights.
            -- Instead of true it can also be a list of languages
        },
    }
end

function M.setup_rest_nvim()
    require("rest-nvim").setup({
        result_split_horizontal = true,
        skip_ssl_verification = false,
        highlight = { enabled = true, timeout = 150 },
        jump_to_request = false,
    })

    local map = vim.api.nvim_set_keymap
    map(
        "n",
        "<leader>tt",
        "<Plug>RestNvim",
        { silent = true }
    )
    map(
        "n",
        "<leader>tp",
        "<Plug>RestNvimPreview",
        { silent = true }
    )
end

local jq_functions = {}
function M.setup_jq_function()
    jq_functions.filter_jq = function(path, first, last)
        P(path)
        local buf = vim.api.nvim_get_current_buf()
        first, last = tonumber(first), tonumber(last)
        first = first - 1
        local json_string = table.concat(vim.api.nvim_buf_get_lines(buf, first, last, true))
        local tmp = os.tmpname()
        local f = io.open(tmp, "w")
        if f then
            f:write(json_string)
            f:close()
        else
            return false
        end
        local cmd = {
            "/bin/sh",
            "-c",
            [[cat ]] .. tmp .. [[ | jq ']] .. path .. [[']]
        }
        require('firvish.job_control').start_job({
            cmd = cmd,
            filetype = "json",
            title = "jq",
            listed = true,
            output_qf = false,
            is_background_job = false,
        })
    end
    _G.jq_functions = jq_functions
    vim.cmd [[command! -nargs=* -range FJq :lua _G.jq_functions.filter_jq('<args>', "<line1>", "<line2>")]]
end

local build_functions = {}
function M.setup_build_function()
    build_functions.rebuild = function()
        local cmd = { "rebuild" }
        require('firvish.job_control').start_job({
            cmd = cmd,
            filetype = "job-output",
            title = "rebuild",
            listed = true,
            output_qf = true,
            is_background_job = true,
        })
    end
    _G.build_functions = build_functions
    vim.cmd [[command! Rebuild :lua _G.build_functions.rebuild()]]

    build_functions.azure_yaml = function(file)
        local cwd = os.getenv('PWD')
        local auth = os.getenv('AZURE_DEVOPS_AUTH')
        local ado_org = os.getenv('AZURE_DEVOPS_ORG')
        local ado_proj = os.getenv('AZURE_DEVOPS_PROJECT')
        local pipeline = os.getenv('AZURE_YAML_DEFINITION_ID')
        local branch = os.getenv('AZURE_YAML_BRANCH')

        local url = [[https://dev.azure.com/]] .. ado_org .. [[/]] .. ado_proj .. [[/_apis/pipelines/]] .. pipeline .. [[/preview?api-version=7.1-preview.1]]
        local auth_header = [[Authorization: Basic ]] .. auth
        local yaml_file = cwd .. "/" .. file

        local shell_script = string.format(
[[yaml_string="
$(cat ${PWD}/${AZURE_YAML_DEBUG_SNIPPET})
$(cat %s)
"
yaml_string="$(echo "$yaml_string" | yq '... style="single"' - | sed -re /\s*\#.*$/d)"
curl -s -X POST '%s' -H 'Content-Type: application/json' -H '%s' --data-binary @- << THEEND | jq '.finalYaml // .' | yq '... style="double"' -
{
    "previewRun":true,
    "templateParameters": {},
    "resources": {
        "repositories": {
            "self": {
                "refName": "refs/heads/%s"
            }
        }
    },
    "yamlOverride":"$yaml_string"
}
THEEND]],
            yaml_file, url, auth_header, branch, branch
        )
        local cmd = {
            "/bin/bash", "-c", shell_script
        }
        require('firvish.job_control').start_job({
            cmd = cmd,
            filetype = "yaml",
            title = "azureyaml",
            use_last_buffer = false,
            listed = true,
            output_qf = true,
            is_background_job = false,
            cwd = cwd
        })
    end
    vim.cmd [[command! -nargs=1 -complete=file AzureYaml :lua _G.build_functions.azure_yaml("<args>")]]
end

-- {{{ global lua debugging
_G.P = function(arg)
    print(vim.inspect(arg))
end

M.reload_package_complete = function(_, _, _)
    -- arg_lead, cmd_line, cursor_pos
    -- TODO: implement completion based on loaded packages
    return 'hi'
end

M.reload_package = function(name)
    P([[name=]] .. name)
    if package.loaded[name] == nil then
        log_warning("Package not loaded.", "[vimrc]")
        return
    end
    package.loaded[name] = nil
    return require(name)
end

vim.cmd(
    [[command! -complete=custom,reloadpackage#complete -nargs=1 LuaReload :lua require('vimrc').reload_package(<q-args>)]]
)

M.activate_reload_on_write = function(name)
    vim.cmd([[augroup package_reload_]] .. name)
    vim.cmd([[au!]])
    vim.cmd([[au BufWritePost <buffer> ]]
        .. string.format([[:lua require('vimrc').reload_package('%s')]], name))
    vim.cmd([[augroup END]])
end

vim.cmd(
    [[command! -complete=custom,reloadpackage#complete -nargs=1 ActivateReload :lua require('vimrc').activate_reload_on_write(<q-args>)]]
)

-- }}}
-- {{{ syntax tools
M.print_synstack_at = function(line, col)
    local syn = vim.fn.synstack(line, col)
    for _, id in pairs(syn) do
        P(vim.fn.synIDattr(id, 'name'))
    end
end

vim.cmd(
    string.format("command! SyntaxInspect :lua require('vimrc').print_synstack_at(%s, %s)",
        vim.fn.line("."),
        vim.fn.col(".")
    )
)
-- }}}

return M