Skip to content

Commit da14746

Browse files
authored
Merge pull request #120 from bew/add-inline-docs
Add inline docs to most functions based on vimdoc
2 parents de94b61 + b56c48e commit da14746

File tree

10 files changed

+396
-204
lines changed

10 files changed

+396
-204
lines changed

.editorconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
root = true
2+
3+
[*.lua]
4+
indent_style = space
5+
indent_size = 4

doc/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tags

doc/multicursor.txt

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,40 @@ Continue reading for a guide of how to use the default config.
4949
config = function()
5050
local mc = require("multicursor-nvim")
5151
mc.setup()
52-
52+
5353
local set = vim.keymap.set
54-
54+
5555
-- Add or skip cursor above/below the main cursor.
5656
set({"n", "x"}, "<up>", function() mc.lineAddCursor(-1) end)
5757
set({"n", "x"}, "<down>", function() mc.lineAddCursor(1) end)
5858
set({"n", "x"}, "<leader><up>", function() mc.lineSkipCursor(-1) end)
5959
set({"n", "x"}, "<leader><down>", function() mc.lineSkipCursor(1) end)
60-
60+
6161
-- Add or skip adding a new cursor by matching word/selection
6262
set({"n", "x"}, "<leader>n", function() mc.matchAddCursor(1) end)
6363
set({"n", "x"}, "<leader>s", function() mc.matchSkipCursor(1) end)
6464
set({"n", "x"}, "<leader>N", function() mc.matchAddCursor(-1) end)
6565
set({"n", "x"}, "<leader>S", function() mc.matchSkipCursor(-1) end)
66-
66+
6767
-- Add and remove cursors with control + left click.
6868
set("n", "<c-leftmouse>", mc.handleMouse)
6969
set("n", "<c-leftdrag>", mc.handleMouseDrag)
7070
set("n", "<c-leftrelease>", mc.handleMouseRelease)
71-
71+
7272
-- Disable and enable cursors.
7373
set({"n", "x"}, "<c-q>", mc.toggleCursor)
74-
74+
7575
-- Mappings defined in a keymap layer only apply when there are
7676
-- multiple cursors. This lets you have overlapping mappings.
7777
mc.addKeymapLayer(function(layerSet)
78-
78+
7979
-- Select a different cursor as the main one.
8080
layerSet({"n", "x"}, "<left>", mc.prevCursor)
8181
layerSet({"n", "x"}, "<right>", mc.nextCursor)
82-
82+
8383
-- Delete the main cursor.
8484
layerSet({"n", "x"}, "<leader>x", mc.deleteCursor)
85-
85+
8686
-- Enable and clear cursors using escape.
8787
layerSet("n", "<esc>", function()
8888
if not mc.cursorsEnabled() then
@@ -92,7 +92,7 @@ Continue reading for a guide of how to use the default config.
9292
end
9393
end)
9494
end)
95-
95+
9696
-- Customize how cursors look.
9797
local hl = vim.api.nvim_set_hl
9898
hl(0, "MultiCursorCursor", { reverse = true })
@@ -229,7 +229,7 @@ mc.toggleCursor() *multicursor-toggleCursor*
229229

230230

231231
mc.lineAddCursor(direction) *multicursor-lineAddCursor*
232-
Add a cursor above or below the the main cursor, skipping empty lines,
232+
Add a cursor above or below the main cursor, skipping empty lines,
233233
specified by `direction`.
234234

235235
Usage example: >lua
@@ -390,7 +390,7 @@ mc.hasCursors() *multicursor-hasCursors*
390390

391391

392392
mc.deleteCursor() *multicursor-deleteCursor*
393-
Delete the main cursor.
393+
Delete the main cursor, the main cursor is now the previous cursor.
394394

395395
Usage example: >lua
396396
vim.keymap.set({"n", "v"}, "<leader>x", mc.deleteCursor)
@@ -439,18 +439,23 @@ mc.alignCursors() *multicursor-alignCursors*
439439
<
440440

441441
mc.splitCursors() *multicursor-splitCursors*
442-
Split visual selections with a regex separator. For example, visually
443-
selecting "a,b,c,d" and splitting with "," will create four cursors, one
444-
on each letter.
442+
Interactively ask for a regex separator, split every visual selections
443+
with the regex separator. To be used in visual/select mode only.
444+
445+
For example, visually selecting "ab,cd,ef,gh" and splitting with "," will
446+
create four cursors, each selecting a group of letters.
445447

446448
Usage example: >lua
447449
vim.keymap.set("v", "S", mc.splitCursors)
448450
<
449451

450452
mc.matchCursors() *multicursor-matchCursors*
451-
Match a pattern over a visual selection, creating a new cursor for each
452-
match. For example, visually selecting "foo bar foo" and matching with
453-
"foo" will create two cursors, one on each "foo".
453+
Interactively ask for a pattern, add a cursor for each match of this
454+
pattern over every visual selections.
455+
To be used in visual/select mode only.
456+
457+
For example, visually selecting "foo bar foo" and matching with "foo" will
458+
create two cursors, one on each "foo".
454459

455460
Usage example: >lua
456461
vim.keymap.set("v", "M", mc.matchCursors)
@@ -529,20 +534,23 @@ mc.sequenceDecrement() *multicursor-sequenceDecrement*
529534
<
530535

531536
mc.addCursorOperator() *multicursor-addCursorOperator*
532-
Takes a motion and adds a cursor for each line. For example, if it is
533-
mapped to `ga`, then typing `gaip` will add a cursor for every line in
534-
the current paragraph.
537+
Takes a motion and adds a cursor for every lines.
538+
539+
For example, if it is mapped to `ga`, then typing `gaip` will add a cursor
540+
for every line in the current paragraph.
535541

536542
Usage example: >lua
537543
vim.keymap.set("n", "ga", mc.addCursorOperator)
538544
<
539545

540546
mc.operator(opts?) *multicursor-operator*
541547
Adds a cursor for every match found in a region. The text to match is
542-
given by a motion, and the region is given by a second motion. For
543-
example, if mapped to `<leader>m` then `<leader>miwap` will find every
544-
match within the paragraph of the text contained within `iw`. If called
545-
from visual mode, the selection becomes the first motion's target text.
548+
given by a motion, and the region is given by a second motion.
549+
550+
For example, if mapped to `<leader>m` then `<leader>miwap` will find every
551+
match within the paragraph of the text contained within `iw`.
552+
If called from visual mode, the selection becomes the first motion's
553+
target text.
546554

547555
Usage example: >lua
548556
vim.keymap.set({ "x", "n" }, "<leader>m", mc.operator)
@@ -636,7 +644,7 @@ mc.disableCursors() *multicursor-disableCursors*
636644

637645

638646
mc.enableCursors() *multicursor-enableCursors*
639-
Unlocks the cursors from moving.
647+
Unlocks disabled cursors, currently active cursors will be discarded.
640648

641649
Usage example: >lua
642650
vim.keymap.set({"n", "v"}, "<c-q>", mc.enableCursors)
@@ -673,9 +681,10 @@ mc.feedkeys({keys}, {opts?}) *multicursor-feedkeys*
673681

674682

675683
mc.addKeymapLayer(callback) *multicursor-addKeymapLayer*
676-
Calls the `callback` when there are multiple cursors. Any mappings set
677-
with the provided function are automatically removed once multicursor
678-
ends.
684+
Registers a keymap layer callback.
685+
It will be called when a buffer has cursors, any mappings set with the
686+
provided function (similar to `vim.keymap.set`) will be automatically
687+
removed once multicursor ends.
679688

680689
Usage example: >lua
681690
mc.addKeymapLayer(function(layerSet)
@@ -1290,6 +1299,17 @@ Cursor:setRedoChangePos(pos) *multicursor-cursor-setRedoChangePos*
12901299
|multicursor-types-Pos|
12911300

12921301

1302+
Cursor:setUndoChangePos(pos) *multicursor-cursor-setUndoChangePos*
1303+
Sets the position of the undo position marker.
1304+
1305+
Parameters: ~
1306+
{pos} (`Pos`) new position cursor should have when undoing.
1307+
1308+
See also: ~
1309+
|multicursor-types-SimplePos|
1310+
|multicursor-types-Pos|
1311+
1312+
12931313
Cursor:registerUndo() *multicursor-cursor-registerUndo*
12941314
Registers this cursor so that its original position is restored upon undo.
12951315

@@ -1389,9 +1409,10 @@ Cursor:enable() *multicursor-cursor-enable*
13891409

13901410

13911411
Cursor:feedkeys(keys, opts?) *multicursor-cursor-feedkeys*
1392-
Makes the cursor perform a command/commands. For example,
1393-
`cursor:feedkeys('dw')` will delete a word. By default, keys are not
1394-
remapped and keycodes are not parsed.
1412+
Makes the cursor perform a command/commands.
1413+
1414+
For example, `cursor:feedkeys('dw')` will delete a word. By default, keys
1415+
are not remapped and keycodes are not parsed.
13951416

13961417
Parameters: ~
13971418
{keys} (`string`) string representing a command

lua/multicursor-nvim/core.lua

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,44 @@ local snippetManager = require("multicursor-nvim.snippet-manager")
55
local TERM_CODES = require("multicursor-nvim.term-codes")
66

77
local core = {
8+
--- @type boolean Whether there is an action in progress (to prevent nested action calls)
89
performingAction = false,
910
}
1011

11-
--- Calls callback for each cursor with old mode, new mode
12-
--- whenever mode is changed
13-
--- @param callback function(cursor: Cursor, oldMode: string, newMode: string)
12+
--- Registers a cursor callback.
13+
--- It will be called for each cursor whenever mode is changed with old mode, new mode.
14+
--- @param callback fun(cursor: mc.Cursor, oldMode: string, newMode: string)
1415
function core.onModeChanged(callback)
15-
return inputManager:onModeChanged(callback)
16+
inputManager:onModeChanged(callback)
1617
end
1718

19+
--- Registers a safe state callback.
20+
--- It will be called whenever the input manager is in a safe state.
21+
--- @param callback fun(info: mc.SafeStateInfo)
1822
function core.onSafeState(callback)
19-
return inputManager:onSafeState(callback)
23+
inputManager:onSafeState(callback)
2024
end
2125

22-
--- @param callback fun(set: KeymapSetCallback)
26+
--- Registers a keymap layer callback.
27+
--- It will be called when a buffer has cursors, any mappings set with the provided function
28+
--- (similar to `vim.keymap.set`) will be automatically removed once multicursor ends.
29+
--- @param callback fun(set: mc.KeymapSetterFunc)
2330
function core.addKeymapLayer(callback)
24-
return inputManager:addKeymapLayer(callback)
31+
inputManager:addKeymapLayer(callback)
2532
end
2633

27-
--- @param callback fun(set: KeymapSetCallback)
34+
--- Removes a previously registered keymap layer callback.
35+
--- @param callback fun(set: mc.KeymapSetterFunc)
2836
function core.removeKeymapLayer(callback)
29-
return inputManager:removeKeymapLayer(callback)
37+
inputManager:removeKeymapLayer(callback)
3038
end
3139

32-
--- @class MultiCursorOpts
40+
--- @class mc.MultiCursorOpts
3341
--- @field signs? string[] | nil,
34-
--- @field shallowUndo? boolean
35-
--- @field hlsearch? boolean
42+
--- @field shallowUndo? boolean (default: false)
43+
--- @field hlsearch? boolean Whether to keep hlsearch for main cursor after a cursor action (default: false)
3644

37-
--- @param opts? MultiCursorOpts
45+
--- @param opts? mc.MultiCursorOpts
3846
function core.setup(opts)
3947
opts = opts or {}
4048

@@ -67,6 +75,8 @@ function core.setup(opts)
6775
})
6876
end
6977

78+
--- @param direction -1|1
79+
--- @param key string
7080
local function jump(direction, key)
7181
inputManager:performAction(function()
7282
if cursorManager:hasCursors() then
@@ -77,15 +87,22 @@ local function jump(direction, key)
7787
end)
7888
end
7989

90+
--- Go to the newer cursor position in jump list.
91+
--- Behaves identically to `<C-i>` except it syncs up the cursors.
92+
--- This action is automatically mapped to `<C-i>` unless a mapping already exists.
8093
function core.jumpForward()
8194
jump(1, TERM_CODES.CTRL_I)
8295
end
8396

97+
--- Go to the older cursor position in jump list.
98+
--- Behaves identically to `<C-o>` except it syncs up the all the cursors.
99+
--- This action is automatically mapped to `<C-o>` unless a mapping already exists.
84100
function core.jumpBackward()
85101
jump(-1, TERM_CODES.CTRL_O)
86102
end
87103

88-
--- @param callback fun(ctx: CursorContext)
104+
--- Perform a complex action using the |multicursor-api|.
105+
--- @param callback fun(ctx: mc.CursorContext) Function to execute with multicursor context
89106
function core.action(callback)
90107
if core.performingAction then
91108
error("An action is already being performed")
@@ -94,7 +111,7 @@ function core.action(callback)
94111
inputManager:performAction(function()
95112
local mode = vim.fn.mode()
96113
if mode == "i" or mode == "R" then
97-
callback() --- @diagnostic disable-line
114+
callback() --- @diagnostic disable-line: missing-parameter
98115
else
99116
cursorManager:action(callback, { excludeMainCursor = false })
100117
inputManager:clear()
@@ -103,7 +120,10 @@ function core.action(callback)
103120
core.performingAction = false
104121
end
105122

106-
--- @param keys string
123+
--- Use instead of `vim.fn.feedkeys()` or `vim.api.nvim_feedkeys()` in
124+
--- multicursor mappings to avoid bugs.
125+
---
126+
--- @param keys string String representing a command
107127
--- @param opts? { remap?: boolean, keycodes?: boolean }
108128
function core.feedkeys(keys, opts)
109129
opts = opts or {}
@@ -121,23 +141,33 @@ function core.hasCursors()
121141
return cursorManager:hasCursors()
122142
end
123143

144+
--- Returns whether the cursors are locked from moving.
124145
--- @return boolean
125146
function core.cursorsEnabled()
126147
return cursorManager:cursorsEnabled()
127148
end
128149

150+
--- Returns the total number of cursors.
151+
--- There is always at least one cursor (the main cursor).
152+
--- @return integer
129153
function core.numCursors()
130154
return cursorManager:numCursors()
131155
end
132156

157+
--- Returns number of enabled cursors.
158+
--- There is always at least one enabled cursor (the main cursor).
159+
--- @return integer
133160
function core.numEnabledCursors()
134161
return cursorManager:numEnabledCursors()
135162
end
136163

164+
--- Returns the number of disabled cursors.
165+
--- @return integer
137166
function core.numDisabledCursors()
138167
return cursorManager:numDisabledCursors()
139168
end
140169

170+
--- Clear all cursors except the main cursor.
141171
function core.clearCursors()
142172
cursorManager:clear()
143173
end

0 commit comments

Comments
 (0)