let mapleader=','

" mit <CTRL>+<Page*> durch die Tabs schalten
nnoremap <silent> <C-PageUp>   :tabprevious<CR>
nnoremap <silent> <C-PageDown> :tabnext<CR>

" besseres Verhalten von <PageUp und <PageDown>
map  <silent> <PageUp>        1000<C-U>
map  <silent> <PageDown>      1000<C-D>
imap <silent> <PageUp>   <C-O>1000<C-U>
imap <silent> <PageDown> <C-O>1000<C-D>

" Verschiebe Zeilen mit ALT+j oder ALT+k
nnoremap <A-j> :m .+1<CR>==
nnoremap <A-k> :m .-2<CR>==
inoremap <A-j> <Esc>:m .+1<CR>==gi
inoremap <A-k> <Esc>:m .-2<CR>==gi
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m '<-2<CR>gv=gv

" Absatzweises springen auf einer deutsche Tastatur
nnoremap ö }
nnoremap ä {
xnoremap ö }
xnoremap ä {
onoremap ö }
onoremap ä {

" besseres Verhalten in umbrochenen Texten
map j gj
map k gk
"map $ g$
"map 0 g0

" Funktionstasten
nnoremap <silent> <F4> :NvimTreeToggle<CR>
nnoremap <silent> <F5> :make<CR>

" Telescope
nnoremap <leader>ff <cmd>Telescope find_files<cr>
nnoremap <leader>fg <cmd>Telescope live_grep<cr>
nnoremap <leader>fb <cmd>Telescope buffers<cr>
nnoremap <leader>fh <cmd>Telescope help_tags<cr>

" LSP Keybindings
lua << EOF
vim.keymap.set("i", "<c-space>", function() vim.lsp.completion.get() end)

-- map <cr> to <c-y> when the popup menu is visible
vim.keymap.set("i", "<cr>", "pumvisible() ? '<c-y>' : '<cr>'", { expr = true })

--[[
vim.keymap.set('n', '<space>e', vim.diagnostic.open_float)
vim.keymap.set('n', '[d', vim.diagnostic.goto_prev)
vim.keymap.set('n', ']d', vim.diagnostic.goto_next)
vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist)

vim.api.nvim_create_autocmd('LspAttach', {
  group = vim.api.nvim_create_augroup('UserLspConfig', {}),
  callback = function(ev)
    vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

    local opts = { buffer = ev.buf }
    vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
    vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
    vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
    vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
    vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
    vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts)
    vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts)
    vim.keymap.set('n', '<space>wl', function()
      print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
    end, opts)
    vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, opts)
    vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
    vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, opts)
    vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
    vim.keymap.set('n', '<space>f', function()
      vim.lsp.buf.format { async = true }
    end, opts)
  end,
})
]]
EOF
