Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor. Maybe something from that will be helpful #42

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b62aaf7
refactor(utils): replace slash to constant, normalize module
AgatZan Sep 1, 2024
4acb0bb
fix(utils): depracated api_nvim_get_option
AgatZan Sep 1, 2024
5a7d13d
refactor(utils): using API instead get_option
AgatZan Sep 1, 2024
2f5d311
refactor(utils): comment lenght(#is default table len), simplify cont…
AgatZan Sep 1, 2024
752d57e
refactor(utils): scandir using luav
AgatZan Sep 1, 2024
98c7778
refactor!: remove vim.g.scratch think about plugin/
AgatZan Sep 1, 2024
4d840d4
revert(global)
AgatZan Sep 1, 2024
727d9a7
fix(utils): remove initDir
AgatZan Sep 1, 2024
facb472
refactor(init): remove vim.g
AgatZan Sep 2, 2024
bb1264b
fix(actor): self.manual_text
AgatZan Sep 2, 2024
a38b49f
fix(api): create using api
AgatZan Sep 3, 2024
1cd36eb
fix(type): compare with exist |\n feat(window): window_cmd -> window_…
AgatZan Sep 3, 2024
ce833e3
refactor! + feat(input,selector)
AgatZan Sep 3, 2024
8be1031
clenup
AgatZan Sep 3, 2024
6ae4390
refactor!, feat(input, select)!
AgatZan Sep 3, 2024
acbf979
fix(actor): miss self
AgatZan Sep 4, 2024
c018570
fix(actor): `scratchByType` miss filename generator name
AgatZan Sep 4, 2024
a04d0b9
fix(actor): `scratchByName` create all subdir
AgatZan Sep 4, 2024
e7e18c6
refactor(actor): beautify condition at scratchBy*
AgatZan Sep 4, 2024
1bec735
refactor(utils): one time get filename
AgatZan Sep 4, 2024
2b68c50
fix(default, types, bugs): default command
AgatZan Sep 6, 2024
36f7652
revert(type): base_dir -> scratch_file_dir,\nfeat(config): filetype_d…
AgatZan Sep 6, 2024
039765a
chore(type): add missed type anotation in `utils.gen_file_path`
AgatZan Sep 6, 2024
c7f6492
fix(utils): open exist file in buffer error
AgatZan Sep 6, 2024
356e87f
Revert "fix(utils): open exist file in buffer error"
AgatZan Sep 6, 2024
c773e9b
fix(actor): all scratch* func with now use opts.arg.
AgatZan Sep 7, 2024
8ed8acc
fix(cmd): correct with current api
AgatZan Sep 7, 2024
f7da5be
fix(utils): scandir \\(now \) before filename
AgatZan Sep 7, 2024
77e8b7f
refactor(config): now default cmd in setup
AgatZan Sep 7, 2024
a0866dc
refactor(api): now api-way actor-way
AgatZan Sep 7, 2024
d7c94e4
fix(config): error with vim.g
AgatZan Sep 7, 2024
bbd6741
fix(config): dumb way but work
AgatZan Sep 7, 2024
7395b8b
refactor(config): remove duplicate in `init.lua`
AgatZan Sep 7, 2024
a3cfb44
refactor(config): remove `plugin/`
AgatZan Sep 7, 2024
5ac8b30
refactor(config): remove log
AgatZan Sep 7, 2024
3b5045d
fix(type): Scratch.ActorConfig now optional
AgatZan Sep 7, 2024
45915ac
refactor(config): move default setup to `plugin/` move default confi…
AgatZan Sep 8, 2024
05eab84
feat-draft(perf): compilation file-finder
AgatZan Sep 8, 2024
b21b0c6
fix(utils):put_cursor not found fix
AgatZan Sep 9, 2024
fa67cfa
fix!: only actor
AgatZan Oct 10, 2024
bd27d16
fix(config): return default `win_cwd` and `file_picker` option at config
AgatZan Oct 10, 2024
36e8fe9
fix(settup): getSelectedText() now work correct
AgatZan Oct 16, 2024
bd21b33
fix(init): get_selected text problem
AgatZan Oct 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lua/scratch/api.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local config = require("scratch.config")
local slash = require("scratch.utils").Slash()
local utils = require("scratch.utils")
local telescope_status, telescope_builtin = pcall(require, "telescope.builtin")
local MANUAL_INPUT_OPTION = "MANUAL_INPUT"
Expand Down Expand Up @@ -34,7 +33,7 @@ local function createScratchFileByName(filename)
local scratch_file_dir = config_data.scratch_file_dir
utils.initDir(scratch_file_dir)

local fullpath = scratch_file_dir .. slash .. filename
local fullpath = scratch_file_dir .. utils.slash .. filename
create_and_edit_file(fullpath)
end

Expand Down Expand Up @@ -205,8 +204,8 @@ local function open_scratch_vim_ui()

-- sort the files by their last modified time in descending order
table.sort(files, function(a, b)
return vim.fn.getftime(scratch_file_dir .. slash .. a)
> vim.fn.getftime(scratch_file_dir .. slash .. b)
return vim.fn.getftime(scratch_file_dir .. utils.slash .. a)
> vim.fn.getftime(scratch_file_dir .. utils.slash .. b)
end)

vim.ui.select(files, {
Expand All @@ -216,7 +215,7 @@ local function open_scratch_vim_ui()
end,
}, function(chosenFile)
if chosenFile then
create_and_edit_file(scratch_file_dir .. slash .. chosenFile)
create_and_edit_file(scratch_file_dir .. utils.slash .. chosenFile)
register_local_key()
end
end)
Expand Down
5 changes: 2 additions & 3 deletions lua/scratch/config.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local slash = require("scratch.utils").Slash()
local utils = require("scratch.utils")

---@alias mode
Expand Down Expand Up @@ -44,7 +43,7 @@ local utils = require("scratch.utils")
---@field filetype_details Scratch.FiletypeDetails
---@field localKeys Scratch.LocalKeyConfig[]
local default_config = {
scratch_file_dir = vim.fn.stdpath("cache") .. slash .. "scratch.nvim", -- where your scratch files will be put
scratch_file_dir = vim.fn.stdpath("cache") .. utils.slash .. "scratch.nvim", -- where your scratch files will be put
filetypes = { "lua", "js", "py", "sh" }, -- you can simply put filetype here
window_cmd = "edit", -- 'vsplit' | 'split' | 'edit' | 'tabedit' | 'rightbelow vsplit'
file_picker = "fzflua",
Expand Down Expand Up @@ -74,7 +73,7 @@ local function get_abs_path(ft)
local parentDir = config_data.scratch_file_dir
local subdir = config_data.filetype_details[ft] and config_data.filetype_details[ft].subdir
if subdir ~= nil then
parentDir = parentDir .. slash .. subdir
parentDir = parentDir .. utils.slash .. subdir
end
vim.fn.mkdir(parentDir, "p")

Expand Down
2 changes: 1 addition & 1 deletion lua/scratch/telescope_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function M.delete_item(prompt_bufnr)
end
local config_data = config.getConfig()
local scratch_file_dir = config_data.scratch_file_dir
local p = Path:new({ scratch_file_dir, file_name, sep = utils.Slash() })
local p = Path:new({ scratch_file_dir, file_name, sep = utils.slash })
return _delelte(p)
end)
end
Expand Down
100 changes: 53 additions & 47 deletions lua/scratch/utils.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
local M = {}

local function Slash()
local slash = "/"
if vim.fn.has("win32") == 1 then
slash = "\\"
end
return slash
end
-- NOTE: no need
-- local function Slash()
-- local slash = "/"
-- if vim.fn.has("win32") == 1 then
-- slash = "\\"
-- end
-- return slash
-- end

local slash = Slash()
M.slash = vim.fn.has("win32") and "\\" or "/"

-- Initialize the scratch file directory if it does not exist
-- TODO: remove this function
local function initDir(scratch_file_dir)
function M.initDir(scratch_file_dir)
if vim.fn.filereadable(scratch_file_dir) == 0 then
vim.fn.mkdir(scratch_file_dir, "p")
else
Expand All @@ -23,14 +24,14 @@ local function initDir(scratch_file_dir)
end

-- Recursively list all files in the specified directory
local function listDirectoryRecursive(directory)
function M.listDirectoryRecursive(directory)
local files = {}
local dir_list = vim.fn.readdir(directory)

for _, file in ipairs(dir_list) do
local path = directory .. slash .. file
local path = directory .. M.slash .. file
if vim.fn.isdirectory(path) == 1 and file ~= "." and file ~= ".." then
local subfiles = listDirectoryRecursive(path)
local subfiles = M.listDirectoryRecursive(path)
for _, subfile in ipairs(subfiles) do
files[#files + 1] = subfile
end
Expand All @@ -47,18 +48,18 @@ end
---@param parentDir string
---@param requiresDir boolean
---@return string
local function genFilepath(filename, parentDir, requiresDir)
function M.genFilepath(filename, parentDir, requiresDir)
if requiresDir then
local dirName = vim.trim(vim.fn.system("uuidgen"))
vim.fn.mkdir(parentDir .. slash .. dirName, "p")
return parentDir .. slash .. dirName .. slash .. filename
vim.fn.mkdir(parentDir .. M.slash .. dirName, "p")
return parentDir .. M.slash .. dirName .. M.slash .. filename
else
return parentDir .. slash .. filename
return parentDir .. M.slash .. filename
end
end

---@param localKeys Scratch.LocalKey[]
local function setLocalKeybindings(localKeys)
function M.setLocalKeybindings(localKeys)
for _, localKey in ipairs(localKeys) do
vim.keymap.set(localKey.modes, localKey.key, localKey.cmd, {
noremap = true,
Expand All @@ -71,7 +72,7 @@ end

---@param substr string
---@return boolean
local function filenameContains(substr)
function M.filenameContains(substr)
local s = vim.fn.expand("%:t")
if string.find(s, substr) then
return true
Expand All @@ -80,22 +81,24 @@ local function filenameContains(substr)
end
end

local table_length = function(T)
local count = 0
for _ in pairs(T) do
count = count + 1
end
return count
end
-- local table_length = function(T)
-- local count = 0
-- for _ in pairs(T) do
-- count = count + 1
-- end
-- return count
-- end

---@return string[]
local function getSelectedText()
function M.getSelectedText()
local _, csrow, cscol, _ = unpack(vim.fn.getpos("'<"))
local _, cerow, cecol, _ = unpack(vim.fn.getpos("'>"))

--NOTE: perfomance eq but this always string[]
-- local lines = vim.api.nvim_buf_get_lines(0, csrow, cerow, false)
local lines = vim.fn.getline(csrow, cerow)
local n = table_length(lines)
if n <= 0 then
---@cast lines string[]
local n = #lines
if n == 0 then
return {}
end
lines[n] = string.sub(lines[n], 1, cecol)
Expand All @@ -104,24 +107,27 @@ local function getSelectedText()
end

---@param msg string
local function log_err(msg)
function M.log_err(msg)
vim.notify(msg, vim.log.levels.ERROR, { title = "easy-commands.nvim" })
end

---@param title string
---@return {buf: integer, win: integer}
local function new_popup_window(title)
function M.new_popup_window(title)
local popup_buf = vim.api.nvim_create_buf(false, false)

-- NOTE: vim.api.nvim_get_option -- depracated
-- local api_get_option = vim.api.nvim_get_option or vim.api.nvim_get_option_value
local opts = {
relative = "editor", -- Assuming you want the floating window relative to the editor
row = 2,
col = 5,
width = vim.api.nvim_get_option("columns") - 10, -- Get the screen width
height = vim.api.nvim_get_option("lines") - 5, -- Get the screen height
--api_get_option("columns") - 10, (row * col?)
width = vim.api.nvim_win_get_width(0) - 10, -- Get the screen width
--api_get_option("lines") - 5,
height = vim.api.nvim_win_get_height(0) - 5, -- Get the screen height
style = "minimal",
border = "single",
title = ""
title = title,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it was forgotten

}

local win = vim.api.nvim_open_win(popup_buf, true, opts)
Expand All @@ -130,15 +136,15 @@ local function new_popup_window(title)
win = win,
}
end

return {
Slash = Slash,
initDir = initDir,
listDirectoryRecursive = listDirectoryRecursive,
genFilepath = genFilepath,
setLocalKeybindings = setLocalKeybindings,
filenameContains = filenameContains,
getSelectedText = getSelectedText,
log_err = log_err,
new_popup_window = new_popup_window,
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't know what benefit there is in using a double method name that way. Maybe it's just following the public API contract? If I guess, it seems like it matters.

return M
-- return {
-- Slash = Slash,
-- initDir = initDir,
-- listDirectoryRecursive = listDirectoryRecursive,
-- genFilepath = genFilepath,
-- setLocalKeybindings = setLocalKeybindings,
-- filenameContains = filenameContains,
-- getSelectedText = getSelectedText,
-- log_err = log_err,
-- new_popup_window = new_popup_window,
-- }