Skip to content

Commit

Permalink
refactor: cleanup & re-organize init.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 24, 2023
1 parent d4db163 commit f461358
Showing 1 changed file with 53 additions and 88 deletions.
141 changes: 53 additions & 88 deletions lua/various-textobjs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ local u = require("various-textobjs.utils")
local function argConvert(arg)
if arg == false then return "outer" end
if arg == true then return "inner" end
if arg ~= "outer" and arg ~= "inner" then
u.notify(
"Invalid argument for textobject, only 'outer' and 'inner' accepted. Falling back to outer textobject.",
"warn"
)
return "outer"
end
return arg
if arg == "outer" or arg == "inner" then return arg end

u.notify(
"Invalid argument for textobject, only 'outer' and 'inner' accepted. Falling back to outer textobject.",
"warn"
)
return "outer"
end

--------------------------------------------------------------------------------
-- CONFIG
-- default values when not setup function is run
local lookForwardSmall = 5
local lookForwardBig = 15
---@type config
local defaultConfig = {
lookForwardSmall = 5,
lookForwardBig = 15,
useDefaultKeymaps = false,
disabledKeymaps = {},
}
local config = defaultConfig

---@class config
---@field lookForwardSmall? number
Expand All @@ -37,19 +40,10 @@ local lookForwardBig = 15
---@field disabledKeymaps? string[]

---optional setup function
---@param userConfig config
---@param userConfig? config
function M.setup(userConfig)
---@type config
local defaultConfig = {
lookForwardSmall = 5,
lookForwardBig = 15,
useDefaultKeymaps = false,
disabledKeymaps = {},
}
local config = vim.tbl_deep_extend("keep", userConfig, defaultConfig)

lookForwardSmall = config.lookForwardSmall
lookForwardBig = config.lookForwardBig
config = vim.tbl_deep_extend("force", defaultConfig, userConfig or {})

if config.useDefaultKeymaps then
require("various-textobjs.default-keymaps").setup(config.disabledKeymaps)
end
Expand All @@ -58,123 +52,94 @@ end
--------------------------------------------------------------------------------
-- LINEWISE

---rest of paragraph (linewise)
function M.restOfParagraph() linewise.restOfParagraph() end

---Textobject for the entire buffer content
function M.entireBuffer() linewise.entireBuffer() end

---indentation textobj
---@param startBorder "inner"|"outer" exclude the startline
---@param endBorder "inner"|"outer" exclude the endline
---@param blankLines? "withBlanks"|"noBlanks"
function M.indentation(startBorder, endBorder, blankLines)
linewise.indentation(argConvert(startBorder), argConvert(endBorder), blankLines)
end

---from cursor position down all lines with same or higher indentation;
---essentially `ii` downwards
function M.restOfIndentation() linewise.restOfIndentation() end

---outer indentation, expanded until the next blank lines in both directions
---@param scope "inner"|"outer" outer adds a blank, like ip/ap textobjs
function M.greedyOuterIndentation(scope) linewise.greedyOuterIndentation(argConvert(scope)) end

---@param scope "inner"|"outer" outer adds one line after the fold
function M.closedFold(scope) linewise.closedFold(argConvert(scope), lookForwardBig) end
function M.closedFold(scope) linewise.closedFold(argConvert(scope), config.lookForwardBig) end

---@param scope "inner"|"outer" inner excludes the backticks
function M.mdFencedCodeBlock(scope) linewise.mdFencedCodeBlock(argConvert(scope), lookForwardBig) end
function M.mdFencedCodeBlock(scope)
linewise.mdFencedCodeBlock(argConvert(scope), config.lookForwardBig)
end

---@param scope "inner"|"outer" inner excludes the `"""`
function M.pyTripleQuotes(scope) charwise.pyTripleQuotes(argConvert(scope)) end

---lines visible in window textobj
function M.visibleInWindow() linewise.visibleInWindow() end
---@param scope "inner"|"outer" outer includes bottom cell border
function M.notebookCell(scope) linewise.notebookCell(argConvert(scope)) end

-- from cursor line to last visible line in window
function M.restOfParagraph() linewise.restOfParagraph() end
function M.visibleInWindow() linewise.visibleInWindow() end
function M.restOfWindow() linewise.restOfWindow() end
function M.multiCommentedLines() linewise.multiCommentedLines(config.lookForwardBig) end
function M.entireBuffer() linewise.entireBuffer() end

function M.multiCommentedLines() linewise.multiCommentedLines(lookForwardBig) end

---for plugins like NotebookNavigator.nvim
---@param scope "inner"|"outer" outer includes bottom cell border
function M.notebookCell(scope) linewise.notebookCell(argConvert(scope)) end
--------------------------------------------------------------------------------
-- BLOCKWISE

---Column Textobj (blockwise down until indent or shorter line)
function M.column() blockwise.column() end

--------------------------------------------------------------------------------
-- CHARWISE

---field which includes a call
---see also https://github.com/chrisgrieser/nvim-various-textobjs/issues/26
---@param scope "inner"|"outer" inner excludes the leading dot
function M.chainMember(scope) charwise.chainMember(argConvert(scope), lookForwardSmall) end

---Subword
---@param scope "inner"|"outer" outer includes trailing -_
function M.subword(scope) charwise.subword(argConvert(scope)) end

---near end of the line, ignoring trailing whitespace (relevant for markdown)
function M.nearEoL() charwise.nearEoL() end

---till next closing bracket
function M.toNextClosingBracket() charwise.toNextClosingBracket(lookForwardSmall) end

---till next quotation mark (backtick counts as one)
function M.toNextQuotationMark() charwise.toNextQuotationMark(lookForwardSmall) end

function M.anyQuote(scope) charwise.anyQuote(argConvert(scope), lookForwardSmall) end

function M.anyBracket(scope) charwise.anyBracket(argConvert(scope), lookForwardSmall) end

---current line (but characterwise)
function M.lineCharacterwise(scope) charwise.lineCharacterwise(argConvert(scope)) end
function M.toNextClosingBracket() charwise.toNextClosingBracket(config.lookForwardSmall) end
function M.toNextQuotationMark() charwise.toNextQuotationMark(config.lookForwardSmall) end
function M.url() charwise.url(config.lookForwardBig) end
function M.diagnostic() charwise.diagnostic(config.lookForwardBig) end

---diagnostic text object
---similar to https://github.com/andrewferrier/textobj-diagnostic.nvim
---requires builtin lsp
function M.diagnostic() charwise.diagnostic(lookForwardBig) end
---@param scope "inner"|"outer"
function M.anyQuote(scope) charwise.anyQuote(argConvert(scope), config.lookForwardSmall) end

---@param scope "inner"|"outer"
function M.anyBracket(scope) charwise.anyBracket(argConvert(scope), config.lookForwardSmall) end

---@param scope "inner"|"outer" inner value excludes trailing commas or semicolons, outer includes them. Both exclude trailing comments.
function M.value(scope) charwise.value(argConvert(scope), lookForwardSmall) end
function M.value(scope) charwise.value(argConvert(scope), config.lookForwardSmall) end

---@param scope "inner"|"outer" outer key includes the `:` or `=` after the key
function M.key(scope) charwise.key(argConvert(scope), lookForwardSmall) end
function M.key(scope) charwise.key(argConvert(scope), config.lookForwardSmall) end

---number textobj
---@deprecated use corresponding treesitter-textobject instead
---@param scope "inner"|"outer" inner number consists purely of digits, outer number factors in decimal points and includes minus sign
function M.number(scope) charwise.number(argConvert(scope), lookForwardSmall) end
function M.number(scope) charwise.number(argConvert(scope), config.lookForwardSmall) end

---@param scope "inner"|"outer" outer includes trailing -_
function M.subword(scope) charwise.subword(argConvert(scope)) end

---URL textobj
function M.url() charwise.url(lookForwardBig) end
---see #26
---@param scope "inner"|"outer" inner excludes the leading dot
function M.chainMember(scope) charwise.chainMember(argConvert(scope), config.lookForwardSmall) end

--------------------------------------------------------------------------------
-- FILETYPE SPECIFIC TEXTOBJS

---md links textobj
---@param scope "inner"|"outer" inner link only includes the link title, outer link includes link, url, and the four brackets.
function M.mdlink(scope) charwise.mdlink(argConvert(scope), lookForwardSmall) end
function M.mdlink(scope) charwise.mdlink(argConvert(scope), config.lookForwardSmall) end

---double square brackets
---@param scope "inner"|"outer" inner double square brackets exclude the brackets themselves
function M.doubleSquareBrackets(scope) charwise.doubleSquareBrackets(argConvert(scope), lookForwardSmall) end
function M.doubleSquareBrackets(scope)
charwise.doubleSquareBrackets(argConvert(scope), config.lookForwardSmall)
end

---CSS Selector Textobj
---@param scope "inner"|"outer" outer selector includes trailing comma and whitespace
function M.cssSelector(scope) charwise.cssSelector(argConvert(scope), lookForwardSmall) end
function M.cssSelector(scope) charwise.cssSelector(argConvert(scope), config.lookForwardSmall) end

---HTML/XML Attribute Textobj
---@param scope "inner"|"outer" inner selector is only the value of the attribute inside the quotation marks.
function M.htmlAttribute(scope) charwise.htmlAttribute(argConvert(scope), lookForwardSmall) end
function M.htmlAttribute(scope) charwise.htmlAttribute(argConvert(scope), config.lookForwardSmall) end

---Shell Pipe Textobj
---@param scope "inner"|"outer" outer selector includes the front pipe
function M.shellPipe(scope) charwise.shellPipe(argConvert(scope), lookForwardSmall) end
function M.shellPipe(scope) charwise.shellPipe(argConvert(scope), config.lookForwardSmall) end

--------------------------------------------------------------------------------
return M

0 comments on commit f461358

Please sign in to comment.