aboutsummaryrefslogtreecommitdiff
path: root/nvim/lua/config/keymaps.lua
blob: 123e0391e5893aed9ac5ebf5e1f84d73c527561a (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
local M = {}

local map = vim.keymap.set

function M.lsp(buffer, client)
  local opts = {
    buffer = buffer,
    silent = true,
  }

  map("n", "gd", vim.lsp.buf.definition, opts)
  map("n", "gD", vim.lsp.buf.declaration, opts)
  map("n", "gr", vim.lsp.buf.references, opts)
  map("n", "gi", vim.lsp.buf.implementation, opts)
  map("n", "K", vim.lsp.buf.hover, opts)
  map("n", "<leader>rn", vim.lsp.buf.rename, opts)
  map("n", "<leader>ca", vim.lsp.buf.code_action, opts)
  map("n", "<leader>cf", function()
    require("conform").format({
      async = true,
      lsp_fallback = true,
    })
  end, opts)

  local function set_c_bindings()
    if client and client.name == "clangd" then map("n", "<leader>hs", "<cmd>ClangdSwitchSourceHeader<cr>", opts) end
  end

  local function set_python_bindings()
    if client and (client.name == "basedpyright" or client.name == "pyright" or client.name == "pylsp") then
      local function run_pytest()
        vim.cmd("botright split | terminal pytest")
      end

      map("n", "<F6>", run_pytest, {
        desc = "Run pytest (nur Python-Projekt)",
      })
    end
  end

  set_c_bindings()
  set_python_bindings()
end

function M.telescope()
  local opts = {
    silent = true,
  }

  map("n", "<leader>ff", function()
    require("telescope.builtin").find_files()
  end, opts)

  map("n", "<leader>fg", function()
    require("telescope.builtin").live_grep()
  end, opts)

  map("n", "<leader>fb", function()
    require("telescope.builtin").buffers()
  end, opts)

  map("n", "<leader>fh", function()
    require("telescope.builtin").help_tags()
  end, opts)

  map("n", "<leader>dd", function()
    require("telescope.builtin").diagnostics()
  end, opts)
end

function M.gitsigns()
  map("n", "<leader>tb", require("gitsigns").toggle_current_line_blame)
end

function M.copilot()
  if vim.fn.exists(":Copilot") == 0 then return end

  map("n", "<leader>ct", function()
    local status = vim.fn["copilot#Enabled"]()

    if status == 1 then
      vim.cmd("Copilot disable")
      print("Copilot disabled")
    else
      vim.cmd("Copilot enable")
      print("Copilot enabled")
    end
  end, { desc = "Copilot Toggle" })

  map("n", "<leader>ce", function()
    vim.cmd("Copilot enable")
    print("Copilot enabled")
  end, { desc = "Copilot Enable" })

  map("n", "<leader>cd", function()
    vim.cmd("Copilot disable")
    print("Copilot disabled")
  end, { desc = "Copilot Disable" })

  map("n", "<leader>cs", function()
    vim.cmd("Copilot status")
  end, { desc = "Copilot Status" })
end

function M.global()
  local opts = {
    silent = true,
  }

  map("n", "ö", "}", opts)
  map("n", "ä", "{", opts)

  -- besseres Verhalten von <PageUp und <PageDown>
  -- https://vimrc-dissection.blogspot.com/2009/02/fixing-pageup-and-pagedown.html
  map("n", "<PageUp>", "1000<C-u>", opts)
  map("n", "<PageDown>", "1000<C-d>", opts)
  map("i", "<PageUp>", "<C-o>1000<C-u>", opts)
  map("i", "<PageDown>", "<C-o>1000<C-d>", opts)

  -- Verschiebe Zeilen mit Alt+j / Alt+k
  -- MacOS: Terminal → Einstellungen → Profile → Tastatur → "Option als Meta-Taste verwenden"
  map("n", "<M-j>", ":m .+1<CR>==", opts)
  map("n", "<M-k>", ":m .-2<CR>==", opts)
  map("i", "<M-j>", "<Esc>:m .+1<CR>==gi", opts)
  map("i", "<M-k>", "<Esc>:m .-2<CR>==gi", opts)
  map("x", "<M-j>", ":m '>+1<CR>gv=gv", opts)
  map("x", "<M-k>", ":m '<-2<CR>gv=gv", opts)

  -- besseres Verhalten in umbrochenen Texten
  map({ "n", "x", "o" }, "j", function()
    return vim.v.count == 0 and "gj" or "j"
  end, { expr = true, silent = true })

  map({ "n", "x", "o" }, "k", function()
    return vim.v.count == 0 and "gk" or "k"
  end, { expr = true, silent = true })

  map("n", "<F5>", "<cmd>make<CR>", opts)

  -- Kombinationen mit Leader-Key
  map("n", "<leader>K", function()
    vim.diagnostic.open_float({ scope = "cursor", border = "rounded" })
  end, opts)

  map("n", "<leader>q", vim.diagnostic.setloclist, opts)

  map("n", "<leader>ts", function()
    vim.opt_local.spell = not vim.opt_local.spell:get()
  end, { desc = "Toggle spell checking" })

  map("n", "[d", function()
    vim.diagnostic.jump_prev({ wrap = false })
  end, opts)

  map("n", "]d", function()
    vim.diagnostic.jump_next({ wrap = false })
  end, opts)

  map("n", "[e", function()
    vim.diagnostic.jump_prev({
      severity = vim.diagnostic.severity.ERROR,
      wrap = false,
    })
  end, opts)

  map("n", "]e", function()
    vim.diagnostic.jump_next({
      severity = vim.diagnostic.severity.ERROR,
      wrap = false,
    })
  end, opts)
end

return M