From d45eac318460780e36c1a50479a91ce93b433bc0 Mon Sep 17 00:00:00 2001
From: tedmoyses <tmoyses@360insights.com>
Date: Sun, 7 May 2023 08:08:30 +0100
Subject: [PATCH] improved skeleton and moved to custom plugin

---
 .gitignore                                   |  1 +
 init.lua                                     | 65 +-------------------
 lua/custom/plugins/tmoyses/keymaps/init.lua  | 22 +++++++
 lua/custom/plugins/tmoyses/sessions/init.lua | 22 +++++++
 lua/custom/plugins/tmoyses/skeleton/init.lua | 27 ++++++++
 templates/skeleton.test.vue                  |  9 +++
 testing.lua                                  |  1 +
 7 files changed, 85 insertions(+), 62 deletions(-)
 create mode 100644 lua/custom/plugins/tmoyses/keymaps/init.lua
 create mode 100644 lua/custom/plugins/tmoyses/sessions/init.lua
 create mode 100644 lua/custom/plugins/tmoyses/skeleton/init.lua
 create mode 100644 templates/skeleton.test.vue
 create mode 100644 testing.lua

diff --git a/.gitignore b/.gitignore
index ea93edad..f4c2ac30 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@ test.sh
 .luarc.json
 nvim
 lazy-lock.json
+session.vim
diff --git a/init.lua b/init.lua
index b5425b92..2cfe3cdf 100644
--- a/init.lua
+++ b/init.lua
@@ -500,65 +500,6 @@ cmp.setup {
 -- The line beneath this is called `modeline`. See `:help modeline`
 -- vim: ts=2 sts=2 sw=2 et
 
-
--- my keymaps
-vim.keymap.set('i', 'jk', '<Esc>', { desc="Back to normal mode from insert mode" });
-vim.keymap.set('i', 'kj', '<Esc>', { desc="Back to normal mode from insert mode" });
-vim.keymap.set({'n', 'x'}, 'B', '0', { desc="Move to beginning of line" });
-vim.keymap.set({'n', 'x'}, 'E', '$', { desc="Move to end of line" });
-
--- move text up or down
-vim.keymap.set('n', '<A-j>', ':m .+1<CR>==', { desc="Move line down one line" });
-vim.keymap.set('n', '<A-k>', ':m .-2<CR>==', { desc="Move line up one line" });
-vim.keymap.set('i', '<A-j>', '<Esc>:m .+1<CR>==gi', { desc="Move line down one line" });
-vim.keymap.set('i', '<A-k>', '<Esc>:m .-2<CR>==gi', { desc="Move line up one line" });
-vim.keymap.set('x', '<A-j>', ":m '>+1<CR>gv=gv", { desc="Move selection down one line" });
-vim.keymap.set('x', '<A-k>', ":m '<-2<CR>gv=gv", { desc="Move selection up one line" });
-
--- move text across and back (indenting)
-vim.keymap.set('n', '<A-l>', '>>_', { desc="Move (indent) text right" });
-vim.keymap.set('n', '<A-h>', '<<_', { desc="Move (de-indent text left" });
-vim.keymap.set('i', '<A-l>', '<C-t>', { desc="Move (indent) text right" });
-vim.keymap.set('i', '<A-h>', '<C-d>', { desc="Move (de-indent) text left" });
-vim.keymap.set('x', '<A-l>', '>gv', { desc="Move (indent) text right" });
-vim.keymap.set('x', '<A-h>', '<gv', { desc="Move (de-indent) text left"});
-
-
--- my basic session management
-vim.api.nvim_create_autocmd({'VimEnter'}, {
-  callback = function ()
-    if vim.fn.filereadable('session.vim') then
-      vim.cmd('source session.vim')
-      vim.notify("Session Restored", vim.log.levels.info, {title = "Sessions"})
-    end
-  end
-})
-
-vim.api.nvim_create_autocmd({'VimLeave'}, {
-  callback = function ()
-    vim.cmd('mks! session.vim')
-  end
-})
-
--- my basic skeleton implementation
-vim.api.nvim_create_autocmd({'BufNewFile'}, {
-  callback = function (args)
-    local extension = string.match(args.file, '\\.([^\\.])+$')
-    local basename = string.match(args.file, '([^\\/])+$')
-
-    print(vim.inspect(args.file))
-    local function is_file(path)
-      local f=io.open(path, "r")
-      if f~=nil then io.close(f) return true else return false end
-    end
-    if is_file('templates/skeleton.' .. basename) then
-      -- we have a match to a skeleton file
-      vim.cmd(':so templates/skeleton.' .. basename)
-      return
-    end
-    if is_file('templates/skeletoni.' .. extension) then
-      vim.cmd(':so templates/skeleton.' .. extension)
-      return
-    end
-  end
-})
+require('custom.plugins.tmoyses.keymaps')
+require('custom.plugins.tmoyses.sessions')
+require('custom.plugins.tmoyses.skeleton')
diff --git a/lua/custom/plugins/tmoyses/keymaps/init.lua b/lua/custom/plugins/tmoyses/keymaps/init.lua
new file mode 100644
index 00000000..787e962b
--- /dev/null
+++ b/lua/custom/plugins/tmoyses/keymaps/init.lua
@@ -0,0 +1,22 @@
+-- my keymaps
+vim.keymap.set('i', 'jk', '<Esc>', { desc="Back to normal mode from insert mode" });
+vim.keymap.set('i', 'kj', '<Esc>', { desc="Back to normal mode from insert mode" });
+vim.keymap.set({'n', 'x'}, 'B', '0', { desc="Move to beginning of line" });
+vim.keymap.set({'n', 'x'}, 'E', '$', { desc="Move to end of line" });
+
+-- move text up or down
+vim.keymap.set('n', '<A-j>', ':m .+1<CR>==', { desc="Move line down one line" });
+vim.keymap.set('n', '<A-k>', ':m .-2<CR>==', { desc="Move line up one line" });
+vim.keymap.set('i', '<A-j>', '<Esc>:m .+1<CR>==gi', { desc="Move line down one line" });
+vim.keymap.set('i', '<A-k>', '<Esc>:m .-2<CR>==gi', { desc="Move line up one line" });
+vim.keymap.set('x', '<A-j>', ":m '>+1<CR>gv=gv", { desc="Move selection down one line" });
+vim.keymap.set('x', '<A-k>', ":m '<-2<CR>gv=gv", { desc="Move selection up one line" });
+
+-- move text across and back (indenting)
+vim.keymap.set('n', '<A-l>', '>>_', { desc="Move (indent) text right" });
+vim.keymap.set('n', '<A-h>', '<<_', { desc="Move (de-indent text left" });
+vim.keymap.set('i', '<A-l>', '<C-t>', { desc="Move (indent) text right" });
+vim.keymap.set('i', '<A-h>', '<C-d>', { desc="Move (de-indent) text left" });
+vim.keymap.set('x', '<A-l>', '>gv', { desc="Move (indent) text right" });
+vim.keymap.set('x', '<A-h>', '<gv', { desc="Move (de-indent) text left"});
+
diff --git a/lua/custom/plugins/tmoyses/sessions/init.lua b/lua/custom/plugins/tmoyses/sessions/init.lua
new file mode 100644
index 00000000..ccabd53c
--- /dev/null
+++ b/lua/custom/plugins/tmoyses/sessions/init.lua
@@ -0,0 +1,22 @@
+-- my basic session management
+
+local session_file = 'session.vim'
+
+vim.api.nvim_create_autocmd({'VimEnter'}, {
+  callback = function ()
+    if vim.fn.argc() == 0 and vim.fn.filereadable(session_file) == 1 then
+      vim.cmd('source ' .. session_file)
+      vim.notify("Session restored from " .. session_file, vim.log.levels.INFO, {title = "Sessions"})
+    end
+  end,
+  desc = "Load session file on start up - start vim with no args to load the session file in current working directory"
+})
+
+vim.api.nvim_create_autocmd({'VimLeave'}, {
+  callback = function ()
+    if vim.fn.argc() == 0 then
+      vim.cmd('mks! ' .. session_file)
+    end
+  end,
+  desc = "Write session file on close"
+})
diff --git a/lua/custom/plugins/tmoyses/skeleton/init.lua b/lua/custom/plugins/tmoyses/skeleton/init.lua
new file mode 100644
index 00000000..0c6ec329
--- /dev/null
+++ b/lua/custom/plugins/tmoyses/skeleton/init.lua
@@ -0,0 +1,27 @@
+-- my basic skeleton implementation
+vim.api.nvim_create_autocmd({'BufNewFile'}, {
+  callback = function (args)
+    local lines = vim.api.nvim_buf_get_lines(0, 0, vim.api.nvim_buf_line_count(0), false)
+    local skeleton_files = vim.api.nvim_get_runtime_file('templates/skeleton.*', true)
+    local skeleton_path = 'templates/skeleton.'
+
+    -- fist bail out if we have any lines in this file
+    if #lines ~= 1 or lines[1] ~= "" then return end
+
+    -- now bail if we don't have any skeleton files
+    if #skeleton_files == 0 then return end
+    table.sort(skeleton_files, function (a, b) return #a>#b end)
+
+    for i,v in pairs(skeleton_files) do
+      local start, finish = string.find(v, skeleton_path)
+      local match = string.sub(v, finish+1, -1)
+      if string.match(args.file, match .. '$') then
+        -- we have a matching skel file so read it in
+        vim.cmd('%!cat ' .. v)
+        vim.notify("Skeleton file used to create file: " .. v, vim.log.levels.INFO, { title = "Sessions" })
+        break
+      end
+    end
+  end,
+  desc = "Simple skeleton file function to match buffer name to a template and add the content to the new file"
+})
diff --git a/templates/skeleton.test.vue b/templates/skeleton.test.vue
new file mode 100644
index 00000000..97de9c50
--- /dev/null
+++ b/templates/skeleton.test.vue
@@ -0,0 +1,9 @@
+this is a test file!
+
+<script setup>
+
+</script>
+
+<template>
+
+</template>
diff --git a/testing.lua b/testing.lua
new file mode 100644
index 00000000..b99f8cfc
--- /dev/null
+++ b/testing.lua
@@ -0,0 +1 @@
+print(string.match('foo.bar', "%..$"))