From 1afa56764aaddbbc4253c8e61086aedec2340efd Mon Sep 17 00:00:00 2001 From: bh Date: Thu, 13 Nov 2025 19:55:39 +0800 Subject: LuaSnips Stuff --- init.lua | 26 +++++++++++ lazy-lock.json | 14 ++++-- lua/config/luasnip.lua | 20 +++++++++ lua/config/luasnips.lua | 21 --------- lua/plugins/init.lua | 50 ++++++++++++++++++--- lua/plugins/test.c | 114 +++++++++++++++++++++++++++++++++++++++++++++--- 6 files changed, 208 insertions(+), 37 deletions(-) create mode 100644 lua/config/luasnip.lua delete mode 100644 lua/config/luasnips.lua diff --git a/init.lua b/init.lua index 5c41b11..1a7365d 100644 --- a/init.lua +++ b/init.lua @@ -71,6 +71,28 @@ vim.g.mapleader = ' ' -- Tree vim.api.nvim_set_keymap('n', 't', ':NvimTreeToggle', { noremap = true, silent = true }) +-- vim.keymap.set('n', ';', function() +-- -- Append a semicolon at the end of the line without moving the cursor +-- local col = vim.fn.col('.') -- save current column +-- vim.cmd('normal! A;') -- go to end of line and append ; +-- vim.fn.cursor(vim.fn.line('.'), col) -- restore cursor +-- end, { noremap = true, silent = true }) + +-- ~/.config/nvim/lua/keymaps.lua +vim.keymap.set('n', ';', function() + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) -- save cursor + local line = vim.api.nvim_get_current_line() + + -- Only add semicolon if it doesn't exist + if not line:match(";$") then + vim.api.nvim_set_current_line(line .. ";") + end + + -- Restore cursor + vim.api.nvim_win_set_cursor(0, {row, col}) +end, { noremap = true, silent = true }) + + -- Telescope local builtin = require('telescope.builtin') vim.keymap.set('n', 'ff', builtin.find_files, {}) @@ -79,6 +101,7 @@ vim.keymap.set('n', 'fb', builtin.buffers, {}) + -------------------------------------------------------- -- 🪶 GUI and Colour Configuration -------------------------------------------------------- @@ -129,6 +152,9 @@ vim.cmd [[ highlight BufferCurrentIcon guifg=#8affff guibg=#003636 highlight BufferTabpageFill guibg=#003636 + + highlight Folded guifg=#74c4c4 guibg=#003636 + highlight FoldColumn guifg=#74c4c4 guibg=#003636 ]] -- desired icon background colour diff --git a/lazy-lock.json b/lazy-lock.json index dcbe927..ed685d0 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -1,13 +1,21 @@ { - "LuaSnip": { "branch": "master", "commit": "3732756842a2f7e0e76a7b0487e9692072857277" }, + "LuaSnip": { "branch": "master", "commit": "5a1e39223db9a0498024a77b8441169d260c8c25" }, "barbar.nvim": { "branch": "master", "commit": "53b5a2f34b68875898f0531032fbf090e3952ad7" }, - "cmp-nvim-lsp": { "branch": "main", "commit": "bd5a7d6db125d4654b50eeae9f5217f24bb22fd3" }, + "ccc.nvim": { "branch": "main", "commit": "9d1a256e006decc574789dfc7d628ca11644d4c2" }, + "cmp-nvim-lsp": { "branch": "main", "commit": "cbc7b02bb99fae35cb42f514762b89b5126651ef" }, "darkplus.nvim": { "branch": "main", "commit": "6101ffce60682208e9e076710299d189ce11c40d" }, "dashboard-nvim": { "branch": "master", "commit": "0775e567b6c0be96d01a61795f7b64c1758262f6" }, "gitsigns.nvim": { "branch": "main", "commit": "20ad4419564d6e22b189f6738116b38871082332" }, "lazy.nvim": { "branch": "main", "commit": "85c7ff3711b730b4030d03144f6db6375044ae82" }, + "lualine.nvim": { "branch": "master", "commit": "3946f0122255bc377d14a59b27b609fb3ab25768" }, + "neoscroll.nvim": { "branch": "master", "commit": "f957373912e88579e26fdaea4735450ff2ef5c9c" }, + "nvim-autopairs": { "branch": "master", "commit": "7a2c97cccd60abc559344042fefb1d5a85b3e33b" }, "nvim-cmp": { "branch": "main", "commit": "106c4bcc053a5da783bf4a9d907b6f22485c2ea0" }, - "nvim-lspconfig": { "branch": "master", "commit": "c8503e63c6afab3ed34b49865a4a4edbb1ebf4a8" }, + "nvim-lspconfig": { "branch": "master", "commit": "b34fbdffdcb6295c7a25df6ba375452a2e73c32e" }, + "nvim-surround": { "branch": "main", "commit": "fcfa7e02323d57bfacc3a141f8a74498e1522064" }, + "nvim-tree.lua": { "branch": "master", "commit": "1eda2569394f866360e61f590f1796877388cb8a" }, + "nvim-treesitter": { "branch": "master", "commit": "42fc28ba918343ebfd5565147a42a26580579482" }, + "nvim-treesitter-textobjects": { "branch": "master", "commit": "5ca4aaa6efdcc59be46b95a3e876300cfead05ef" }, "nvim-web-devicons": { "branch": "master", "commit": "8dcb311b0c92d460fac00eac706abd43d94d68af" }, "onedark.nvim": { "branch": "master", "commit": "6c10964f91321c6a0f09bcc41dd64e7a6602bc4f" }, "plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" }, diff --git a/lua/config/luasnip.lua b/lua/config/luasnip.lua new file mode 100644 index 0000000..f3b39b4 --- /dev/null +++ b/lua/config/luasnip.lua @@ -0,0 +1,20 @@ +return function() + local ls = require("luasnip") + + -- Expand snippet + vim.keymap.set("i", "", function() ls.expand() end, { silent = true }) + + -- Jump forward/backward + vim.keymap.set({ "i", "s" }, "", function() ls.jump(1) end, { silent = true }) + vim.keymap.set({ "i", "s" }, "", function() ls.jump(-1) end, { silent = true }) + + -- Change choice in choice node + vim.keymap.set({ "i", "s" }, "", function() + if ls.choice_active() then + ls.change_choice(1) + end + end, { silent = true }) + + -- Optionally, load snippets here if you have them + -- require("luasnip.loaders.from_vscode").lazy_load() +end diff --git a/lua/config/luasnips.lua b/lua/config/luasnips.lua deleted file mode 100644 index 22fc94f..0000000 --- a/lua/config/luasnips.lua +++ /dev/null @@ -1,21 +0,0 @@ -return function() - local ls = require("luasnip") - - -- Expand snippet - vim.keymap.set("i", "", function() ls.expand() end, { silent = true }) - - -- Jump forward/backward - vim.keymap.set({ "i", "s" }, "", function() ls.jump(1) end, { silent = true }) - vim.keymap.set({ "i", "s" }, "", function() ls.jump(-1) end, { silent = true }) - - -- Change choice in choice node - vim.keymap.set({ "i", "s" }, "", function() - if ls.choice_active() then - ls.change_choice(1) - end - end, { silent = true }) - - -- Optionally, load snippets here if you have them - -- require("luasnip.loaders.from_vscode").lazy_load() -end - diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua index 0f8fad0..f66d9c6 100644 --- a/lua/plugins/init.lua +++ b/lua/plugins/init.lua @@ -8,6 +8,49 @@ return { end, }, + -- TreeSitter + {"nvim-treesitter/nvim-treesitter", branch = 'master', lazy = false, build = ":TSUpdate", + config = function() + require("config.treesitter")() + end,}, + {"nvim-treesitter/nvim-treesitter-textobjects", lazy = false, }, + + -- Autopairs + { + 'windwp/nvim-autopairs', + event = "InsertEnter", + config = true + -- use opts = {} for passing setup options + -- this is equivalent to setup({}) function + }, + + { + "kylechui/nvim-surround", + version = "^3.0.0", -- Use for stability; omit to use `main` branch for the latest features + event = "VeryLazy", + config = function() + require("nvim-surround").setup({ + -- Configuration here, or leave empty to use defaults + }) + end + }, + + + -- Autocompletion + { "hrsh7th/nvim-cmp" }, + { "hrsh7th/cmp-nvim-lsp" }, + + -- Snippets + { + "L3MON4D3/LuaSnip", + -- follow latest release. + version = "v2.*", -- Replace by the latest released major (first number of latest release) + -- install jsregexp (optional!). + build = "make install_jsregexp", + config = function() + require("config.luasnip")() + end, + }, -- File Explorer { "nvim-tree/nvim-tree.lua", config = require("config.tree") }, @@ -33,13 +76,6 @@ return { -- LaTeX { "lervag/vimtex", ft = "tex", config = require("config.vimtex") }, - -- Autocompletion - { "hrsh7th/nvim-cmp" }, - { "hrsh7th/cmp-nvim-lsp" }, - - -- Snippets - { "L3MON4D3/LuaSnip" }, - -- Grammar & Spell Checking { "rhysd/vim-grammarous", ft = "tex" }, diff --git a/lua/plugins/test.c b/lua/plugins/test.c index 57028aa..bdba367 100644 --- a/lua/plugins/test.c +++ b/lua/plugins/test.c @@ -1,12 +1,114 @@ #include +#include +#include +#include -int main() -{ - int x = 5; - printf("Hello World!\n"); +#define MAX_NAME_LEN 50 +#define ARRAY_SIZE 5 - while(1){ - printf("Hello World!\n"); +typedef enum { + STUDENT, + TEACHER, + STAFF +} Role; + +typedef struct { + char name[MAX_NAME_LEN]; + int age; + Role role; + float grades[ARRAY_SIZE]; +} Person; + +// Function declarations +void print_person(const Person* p); +float average_grade(const Person* p); +Person* create_person(const char* name, int age, Role role); +void fill_random_grades(Person* p); +void print_all(Person** people, int count); +void free_all(Person** people, int count); +int find_oldest(Person** people, int count); + +int main() { + Person* people[ARRAY_SIZE]; + + people[0] = create_person("Alice", 20, STUDENT); + people[1] = create_person("Bob", 35, TEACHER); + people[2] = create_person("Charlie", 28, STAFF); + people[3] = create_person("Diana", 22, STUDENT); + people[4] = create_person("Ethan", 40, TEACHER); + + for (int i = 0; i < ARRAY_SIZE; i++) { + fill_random_grades(people[i]); } + print_all(people, ARRAY_SIZE); + + int oldest_index = find_oldest(people, ARRAY_SIZE); + printf("\nOldest person is: %s, Age: %d\n", people[oldest_index]->name, people[oldest_index]->age); + + free_all(people, ARRAY_SIZE); + + return 0; } + +void print_person(const Person* p) { + const char* role_names[] = {"Student", "Teacher", "Staff"}; + printf("Name: %s\n", p->name); + printf("Age: %d\n", p->age); + printf("Role: %s\n", role_names[p->role]); + printf("Grades: "); + for (int i = 0; i < ARRAY_SIZE; i++) { + printf("%.2f ", p->grades[i]); + } + printf("\nAverage Grade: %.2f\n\n", average_grade(p)); +} + +float average_grade(const Person* p) { + float sum = 0; + for (int i = 0; i < ARRAY_SIZE; i++) { + sum += p->grades[i]; + } + return sum / ARRAY_SIZE; +} + +Person* create_person(const char* name, int age, Role role) { + Person* p = (Person*)malloc(sizeof(Person)); + if (!p) { + fprintf(stderr, "Memory allocation failed\n"); + exit(1); + } + strncpy(p->name, name, MAX_NAME_LEN); + p->age = age; + p->role = role; + memset(p->grades, 0, sizeof(p->grades)); + return p; +} + +void fill_random_grades(Person* p) { + for (int i = 0; i < ARRAY_SIZE; i++) { + p->grades[i] = (float)(rand() % 101); // 0 to 100 + } +} + +void print_all(Person** people, int count) { + for (int i = 0; i < count; i++) { + print_person(people[i]); + } +} + +void free_all(Person** people, int count) { + for (int i = 0; i < count; i++) { + free(people[i]); + } +} + +int find_oldest(Person** people, int count) { + int oldest_index = 0; + for (int i = 1; i < count; i++) { + if (people[i]->age > people[oldest_index]->age) { + oldest_index = i; + } + } + return oldest_index; +} + -- cgit v1.2.3