Skip to content

Commit

Permalink
chore: Add type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewferrier committed May 25, 2024
1 parent 4c87a23 commit 70f7c21
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lua/wrapping/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local M = {}
local utils = require("wrapping.utils")
local treesitter = require("wrapping.treesitter")

---@type Options
local OPTION_DEFAULTS = {
set_nvim_opt_defaults = true,
softener = {
Expand Down Expand Up @@ -42,6 +43,8 @@ local OPTION_DEFAULTS = {
local VERY_LONG_TEXTWIDTH_FOR_SOFT = 999999
local opts

---@param str string
---@return nil
local function log(str)
if opts.log_path ~= nil then
local bufname = vim.fn.bufname()
Expand All @@ -58,6 +61,7 @@ local function log(str)
end
end

---@return boolean
local function soft_wrap_mode_quiet()
if vim.b.wrapmode == "soft" then
return false
Expand Down Expand Up @@ -95,6 +99,7 @@ local function soft_wrap_mode_quiet()
return true
end

---@return boolean
local function hard_wrap_mode_quiet()
if vim.b.wrapmode == "hard" then
return false
Expand Down Expand Up @@ -122,18 +127,21 @@ local function hard_wrap_mode_quiet()
return true
end

---@return nil
M.soft_wrap_mode = function()
if soft_wrap_mode_quiet() and opts.notify_on_switch then
vim.notify("Soft wrap mode.")
end
end

---@return nil
M.hard_wrap_mode = function()
if hard_wrap_mode_quiet() and opts.notify_on_switch then
vim.notify("Hard wrap mode.")
end
end

---@return nil
M.toggle_wrap_mode = function()
if M.get_current_mode() == "hard" then
M.soft_wrap_mode()
Expand All @@ -142,6 +150,7 @@ M.toggle_wrap_mode = function()
end
end

---@return number
local function get_softener()
local filetype = vim.api.nvim_get_option_value("filetype", { buf = 0 })
local value = vim.tbl_get(opts.softener, filetype)
Expand All @@ -153,6 +162,7 @@ local function get_softener()
end
end

---@return boolean
local function likely_nontextual_language()
-- If an LSP provider supports these capabilities it's almost certainly not
-- a textual language, and therefore we should use hard wrapping
Expand All @@ -177,6 +187,7 @@ local function likely_nontextual_language()
return false
end

---@return boolean
local function likely_textwidth_set_deliberately()
local textwidth_global =
vim.api.nvim_get_option_value("textwidth", { scope = "global" })
Expand All @@ -202,6 +213,7 @@ local function likely_textwidth_set_deliberately()
return false
end

---@return integer, integer
local function get_excluded_treesitter()
local filetype = vim.api.nvim_get_option_value("filetype", { buf = 0 })
local exclusions = opts.excluded_treesitter_queries[filetype]
Expand All @@ -222,6 +234,7 @@ local function get_excluded_treesitter()
end

---@param reason string
---@return nil
local function auto_heuristic(reason)
log("Testing for auto heuristic because of event " .. reason)

Expand All @@ -247,6 +260,7 @@ local function auto_heuristic(reason)
end
end

---@return nil
M.set_mode_heuristically = function()
local buftype = vim.api.nvim_get_option_value("buftype", { buf = 0 })

Expand Down Expand Up @@ -333,6 +347,7 @@ M.set_mode_heuristically = function()
end
end

---@return string|nil
M.get_current_mode = function()
if vim.b.wrapmode then
return vim.b.wrapmode
Expand All @@ -341,6 +356,8 @@ M.get_current_mode = function()
end
end

---@param o Options
---@return nil
M.setup = function(o)
opts = vim.tbl_deep_extend("force", OPTION_DEFAULTS, o or {})

Expand Down
6 changes: 6 additions & 0 deletions lua/wrapping/treesitter.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local M = {}

---@param module_name string
local function try_to_load(module_name)
local function requiref(module)
require(module)
Expand All @@ -16,6 +17,8 @@ end

-- FIXME: This is technically inaccurate right now as it only looks at lines and
-- not starting/ending chars
---@param start_line integer
---@param end_line integer
local function get_character_count(start_line, end_line)
local lines = vim.api.nvim_buf_get_lines(0, start_line, end_line, true)

Expand All @@ -27,6 +30,9 @@ local function get_character_count(start_line, end_line)
return count
end

---@param language string
---@param query string
---@return integer, integer
M.count_lines_of_query = function(language, query)
local total_lines = 0
local total_chars = 0
Expand Down
19 changes: 19 additions & 0 deletions lua/wrapping/types.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- See https://luals.github.io/wiki/annotations/

---@meta types

---@class Softener
---@field default number

---@class Options
---@field set_nvim_opt_defaults boolean
---@field softener Softener
---@field create_commands boolean
---@field create_keymaps boolean
---@field auto_set_mode_heuristically boolean
---@field auto_set_mode_filetype_allowlist string[]
---@field auto_set_mode_filetype_denylist string[]
---@field buftype_allowlist string[]
---@field excluded_treesitter_queries table[]
---@field notify_on_switch boolean
---@field log_path string
3 changes: 3 additions & 0 deletions lua/wrapping/utils.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

-- FIXME: There may be a more efficient way to do this
---@return integer
M.get_buf_size = function()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)

Expand All @@ -13,6 +14,7 @@ M.get_buf_size = function()
end

-- FIXME: There may be a more efficient way to do this
---@return integer
M.count_blank_lines = function()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)

Expand All @@ -26,6 +28,7 @@ M.count_blank_lines = function()
return count
end

---@return string
M.get_log_path = function()
return vim.fn.stdpath("log") .. "/wrapping.nvim.log"
end
Expand Down

0 comments on commit 70f7c21

Please sign in to comment.