From 1afa56764aaddbbc4253c8e61086aedec2340efd Mon Sep 17 00:00:00 2001 From: bh Date: Thu, 13 Nov 2025 19:55:39 +0800 Subject: LuaSnips Stuff --- lua/config/luasnip.lua | 20 +++++++++ lua/config/luasnips.lua | 21 --------- lua/plugins/init.lua | 50 ++++++++++++++++++--- lua/plugins/test.c | 114 +++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 171 insertions(+), 34 deletions(-) create mode 100644 lua/config/luasnip.lua delete mode 100644 lua/config/luasnips.lua (limited to 'lua') 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