Skip to content

Commit 70f7c21

Browse files
committed
chore: Add type annotations
1 parent 4c87a23 commit 70f7c21

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

lua/wrapping/init.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local M = {}
33
local utils = require("wrapping.utils")
44
local treesitter = require("wrapping.treesitter")
55

6+
---@type Options
67
local OPTION_DEFAULTS = {
78
set_nvim_opt_defaults = true,
89
softener = {
@@ -42,6 +43,8 @@ local OPTION_DEFAULTS = {
4243
local VERY_LONG_TEXTWIDTH_FOR_SOFT = 999999
4344
local opts
4445

46+
---@param str string
47+
---@return nil
4548
local function log(str)
4649
if opts.log_path ~= nil then
4750
local bufname = vim.fn.bufname()
@@ -58,6 +61,7 @@ local function log(str)
5861
end
5962
end
6063

64+
---@return boolean
6165
local function soft_wrap_mode_quiet()
6266
if vim.b.wrapmode == "soft" then
6367
return false
@@ -95,6 +99,7 @@ local function soft_wrap_mode_quiet()
9599
return true
96100
end
97101

102+
---@return boolean
98103
local function hard_wrap_mode_quiet()
99104
if vim.b.wrapmode == "hard" then
100105
return false
@@ -122,18 +127,21 @@ local function hard_wrap_mode_quiet()
122127
return true
123128
end
124129

130+
---@return nil
125131
M.soft_wrap_mode = function()
126132
if soft_wrap_mode_quiet() and opts.notify_on_switch then
127133
vim.notify("Soft wrap mode.")
128134
end
129135
end
130136

137+
---@return nil
131138
M.hard_wrap_mode = function()
132139
if hard_wrap_mode_quiet() and opts.notify_on_switch then
133140
vim.notify("Hard wrap mode.")
134141
end
135142
end
136143

144+
---@return nil
137145
M.toggle_wrap_mode = function()
138146
if M.get_current_mode() == "hard" then
139147
M.soft_wrap_mode()
@@ -142,6 +150,7 @@ M.toggle_wrap_mode = function()
142150
end
143151
end
144152

153+
---@return number
145154
local function get_softener()
146155
local filetype = vim.api.nvim_get_option_value("filetype", { buf = 0 })
147156
local value = vim.tbl_get(opts.softener, filetype)
@@ -153,6 +162,7 @@ local function get_softener()
153162
end
154163
end
155164

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

190+
---@return boolean
180191
local function likely_textwidth_set_deliberately()
181192
local textwidth_global =
182193
vim.api.nvim_get_option_value("textwidth", { scope = "global" })
@@ -202,6 +213,7 @@ local function likely_textwidth_set_deliberately()
202213
return false
203214
end
204215

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

224236
---@param reason string
237+
---@return nil
225238
local function auto_heuristic(reason)
226239
log("Testing for auto heuristic because of event " .. reason)
227240

@@ -247,6 +260,7 @@ local function auto_heuristic(reason)
247260
end
248261
end
249262

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

@@ -333,6 +347,7 @@ M.set_mode_heuristically = function()
333347
end
334348
end
335349

350+
---@return string|nil
336351
M.get_current_mode = function()
337352
if vim.b.wrapmode then
338353
return vim.b.wrapmode
@@ -341,6 +356,8 @@ M.get_current_mode = function()
341356
end
342357
end
343358

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

lua/wrapping/treesitter.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local M = {}
22

3+
---@param module_name string
34
local function try_to_load(module_name)
45
local function requiref(module)
56
require(module)
@@ -16,6 +17,8 @@ end
1617

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

@@ -27,6 +30,9 @@ local function get_character_count(start_line, end_line)
2730
return count
2831
end
2932

33+
---@param language string
34+
---@param query string
35+
---@return integer, integer
3036
M.count_lines_of_query = function(language, query)
3137
local total_lines = 0
3238
local total_chars = 0

lua/wrapping/types.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- See https://luals.github.io/wiki/annotations/
2+
3+
---@meta types
4+
5+
---@class Softener
6+
---@field default number
7+
8+
---@class Options
9+
---@field set_nvim_opt_defaults boolean
10+
---@field softener Softener
11+
---@field create_commands boolean
12+
---@field create_keymaps boolean
13+
---@field auto_set_mode_heuristically boolean
14+
---@field auto_set_mode_filetype_allowlist string[]
15+
---@field auto_set_mode_filetype_denylist string[]
16+
---@field buftype_allowlist string[]
17+
---@field excluded_treesitter_queries table[]
18+
---@field notify_on_switch boolean
19+
---@field log_path string

lua/wrapping/utils.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local M = {}
22

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

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

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

@@ -26,6 +28,7 @@ M.count_blank_lines = function()
2628
return count
2729
end
2830

31+
---@return string
2932
M.get_log_path = function()
3033
return vim.fn.stdpath("log") .. "/wrapping.nvim.log"
3134
end

0 commit comments

Comments
 (0)