-
Notifications
You must be signed in to change notification settings - Fork 10
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
Closed
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 4acb0bb
fix(utils): depracated api_nvim_get_option
AgatZan 5a7d13d
refactor(utils): using API instead get_option
AgatZan 2f5d311
refactor(utils): comment lenght(#is default table len), simplify cont…
AgatZan 752d57e
refactor(utils): scandir using luav
AgatZan 98c7778
refactor!: remove vim.g.scratch think about plugin/
AgatZan 4d840d4
revert(global)
AgatZan 727d9a7
fix(utils): remove initDir
AgatZan facb472
refactor(init): remove vim.g
AgatZan bb1264b
fix(actor): self.manual_text
AgatZan a38b49f
fix(api): create using api
AgatZan 1cd36eb
fix(type): compare with exist |\n feat(window): window_cmd -> window_…
AgatZan ce833e3
refactor! + feat(input,selector)
AgatZan 8be1031
clenup
AgatZan 6ae4390
refactor!, feat(input, select)!
AgatZan acbf979
fix(actor): miss self
AgatZan c018570
fix(actor): `scratchByType` miss filename generator name
AgatZan a04d0b9
fix(actor): `scratchByName` create all subdir
AgatZan e7e18c6
refactor(actor): beautify condition at scratchBy*
AgatZan 1bec735
refactor(utils): one time get filename
AgatZan 2b68c50
fix(default, types, bugs): default command
AgatZan 36f7652
revert(type): base_dir -> scratch_file_dir,\nfeat(config): filetype_d…
AgatZan 039765a
chore(type): add missed type anotation in `utils.gen_file_path`
AgatZan c7f6492
fix(utils): open exist file in buffer error
AgatZan 356e87f
Revert "fix(utils): open exist file in buffer error"
AgatZan c773e9b
fix(actor): all scratch* func with now use opts.arg.
AgatZan 8ed8acc
fix(cmd): correct with current api
AgatZan f7da5be
fix(utils): scandir \\(now \) before filename
AgatZan 77e8b7f
refactor(config): now default cmd in setup
AgatZan a0866dc
refactor(api): now api-way actor-way
AgatZan d7c94e4
fix(config): error with vim.g
AgatZan bbd6741
fix(config): dumb way but work
AgatZan 7395b8b
refactor(config): remove duplicate in `init.lua`
AgatZan a3cfb44
refactor(config): remove `plugin/`
AgatZan 5ac8b30
refactor(config): remove log
AgatZan 3b5045d
fix(type): Scratch.ActorConfig now optional
AgatZan 45915ac
refactor(config): move default setup to `plugin/` move default confi…
AgatZan 05eab84
feat-draft(perf): compilation file-finder
AgatZan b21b0c6
fix(utils):put_cursor not found fix
AgatZan fa67cfa
fix!: only actor
AgatZan bd27d16
fix(config): return default `win_cwd` and `file_picker` option at config
AgatZan 36e8fe9
fix(settup): getSelectedText() now work correct
AgatZan bd21b33
fix(init): get_selected text problem
AgatZan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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, | ||
} | ||
|
||
local win = vim.api.nvim_open_win(popup_buf, true, opts) | ||
|
@@ -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, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
-- } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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