Skip to content

Commit f9687e2

Browse files
committed
👍 Update type definition for cursor-function
1 parent 0515eb5 commit f9687e2

File tree

2 files changed

+112
-31
lines changed

2 files changed

+112
-31
lines changed

denops_std/function/cursor.ts

Lines changed: 98 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@ import type { Position, ScreenPos } from "./types.ts";
1919
* out of range then col() returns zero.
2020
* To get the line number use |line()|. To get both use
2121
* |getpos()|.
22-
* For the screen column position use |virtcol()|.
22+
* For the screen column position use |virtcol()|. For the
23+
* character position use |charcol()|.
2324
* Note that only marks in the current file can be used.
2425
* Examples:
2526
* col(".") column of cursor
2627
* col("$") length of cursor line plus one
2728
* col("'t") column of mark t
28-
* col("'" . markname) column of mark markname
29-
* The first column is 1. 0 is returned for an error.
29+
* col("'" .. markname) column of mark markname
30+
* The first column is 1. Returns 0 if {expr} is invalid.
3031
* For an uppercase mark the column may actually be in another
3132
* buffer.
3233
* For the cursor position, when 'virtualedit' is active, the
3334
* column is one higher if the cursor is after the end of the
3435
* line. This can be used to obtain the column in Insert mode:
35-
* :imap <F2> <C-O>:let save_ve = &ve<CR
36-
* \<C-O>:set ve=all<CR
37-
* \<C-O>:echo col(".") . "\n" <Bar
38-
* \let &ve = save_ve<CR
36+
* :imap <F2> <C-O>:let save_ve = &ve<CR>
37+
* \<C-O>:set ve=all<CR>
38+
* \<C-O>:echo col(".") .. "\n" <Bar>
39+
* \let &ve = save_ve<CR>
3940
* GetPos()->col()
4041
*/
4142
export async function col(
@@ -57,12 +58,13 @@ export async function col(
5758
* set to 8, it returns 8. |conceal| is ignored.
5859
* For the byte position use |col()|.
5960
* For the use of {expr} see |col()|.
60-
* When 'virtualedit' is used {expr} can be [lnum, col, off], where
61-
* "off" is the offset in screen columns from the start of the
62-
* character. E.g., a position within a <Tab> or after the last
63-
* character. When "off" is omitted zero is used.
64-
* When Virtual editing is active in the current mode, a position
65-
* beyond the end of the line can be returned. |'virtualedit'|
61+
* When 'virtualedit' is used {expr} can be [lnum, col, off],
62+
* where "off" is the offset in screen columns from the start of
63+
* the character. E.g., a position within a <Tab> or after the
64+
* last character. When "off" is omitted zero is used. When
65+
* Virtual editing is active in the current mode, a position
66+
* beyond the end of the line can be returned. Also see
67+
* |'virtualedit'|
6668
* The accepted positions are:
6769
* . the cursor position
6870
* $ the end of the cursor line (the result is the
@@ -101,7 +103,8 @@ export async function virtcol(
101103

102104
/**
103105
* The result is a Number, which is the line number of the file
104-
* position given with {expr}. The accepted positions are:
106+
* position given with {expr}. The {expr} argument is a string.
107+
* The accepted positions are:
105108
* . the cursor position
106109
* $ the last line in the current buffer
107110
* 'x position of mark x (if the mark is not set, 0 is
@@ -118,17 +121,27 @@ export async function virtcol(
118121
* then applies to another buffer.
119122
* To get the column number use |col()|. To get both use
120123
* |getpos()|.
124+
* With the optional {winid} argument the values are obtained for
125+
* that window instead of the current window.
126+
* Returns 0 for invalid values of {expr} and {winid}.
121127
* Examples:
122128
* line(".") line number of the cursor
129+
* line(".", winid) idem, in window "winid"
123130
* line("'t") line number of mark t
124-
* line("'" . marker) line number of mark marker
131+
* line("'" .. marker) line number of mark marker
132+
*
125133
* To jump to the last known position when opening a file see
126134
* |last-position-jump|.
135+
*
127136
* Can also be used as a |method|:
128137
* GetValue()->line()
129138
*/
130-
export async function line(denops: Denops, expr: string): Promise<number> {
131-
return await denops.call("line", expr) as number;
139+
export async function line(
140+
denops: Denops,
141+
expr: string,
142+
winid?: number | string,
143+
): Promise<number> {
144+
return await denops.call("line", expr, winid) as number;
132145
}
133146

134147
/**
@@ -154,14 +167,20 @@ export async function winline(denops: Denops): Promise<number> {
154167
/**
155168
* Positions the cursor at the column (byte count) {col} in the
156169
* line {lnum}. The first column is one.
170+
*
157171
* When there is one argument {list} this is used as a |List|
158172
* with two, three or four item:
159173
* [{lnum}, {col}]
160174
* [{lnum}, {col}, {off}]
161175
* [{lnum}, {col}, {off}, {curswant}]
162176
* This is like the return value of |getpos()| or |getcurpos()|,
163177
* but without the first item.
178+
*
179+
* To position the cursor using the character count, use
180+
* |setcursorcharpos()|.
181+
*
164182
* Does not change the jumplist.
183+
* {lnum} is used like with |getline()|.
165184
* If {lnum} is greater than the number of lines in the buffer,
166185
* the cursor will be positioned at the last line in the buffer.
167186
* If {lnum} is zero, the cursor will stay in the current line.
@@ -171,10 +190,12 @@ export async function winline(denops: Denops): Promise<number> {
171190
* If {col} is zero, the cursor will stay in the current column.
172191
* If {curswant} is given it is used to set the preferred column
173192
* for vertical movement. Otherwise {col} is used.
193+
*
174194
* When 'virtualedit' is used {off} specifies the offset in
175195
* screen columns from the start of the character. E.g., a
176196
* position within a <Tab> or after the last character.
177197
* Returns 0 when the position could be set, -1 otherwise.
198+
*
178199
* Can also be used as a |method|:
179200
* GetCursorPos()->cursor()
180201
*/
@@ -218,6 +239,14 @@ export async function cursor(
218239
* The "curscol" value is where the cursor would be placed. For
219240
* a Tab it would be the same as "endcol", while for a double
220241
* width character it would be the same as "col".
242+
* The |conceal| feature is ignored here, the column numbers are
243+
* as if 'conceallevel' is zero. You can set the cursor to the
244+
* right position and use |screencol()| to get the value with
245+
* |conceal| taken into account.
246+
* If the position is in a closed fold the screen position of the
247+
* first character is returned, {col} is not used.
248+
* Returns an empty Dict if {winid} is invalid.
249+
*
221250
* Can also be used as a |method|:
222251
* GetWinid()->screenpos(lnum, col)
223252
*/
@@ -232,26 +261,42 @@ export async function screenpos(
232261

233262
/**
234263
* Get the position of the cursor. This is like getpos('.'), but
235-
* includes an extra item in the list:
236-
* [bufnum, lnum, col, off, curswant] ~
264+
* includes an extra "curswant" item in the list:
265+
* [0, lnum, col, off, curswant] ~
237266
* The "curswant" number is the preferred column when moving the
238-
* cursor vertically. Also see |getpos()|.
267+
* cursor vertically. After |$| command it will be a very large
268+
* number equal to |v:maxcol|. Also see |getcursorcharpos()| and
269+
* |getpos()|.
270+
* The first "bufnum" item is always zero. The byte position of
271+
* the cursor is returned in 'col'. To get the character
272+
* position, use |getcursorcharpos()|.
273+
*
274+
* The optional {winid} argument can specify the window. It can
275+
* be the window number or the |window-ID|. The last known
276+
* cursor position is returned, this may be invalid for the
277+
* current value of the buffer if it is not the current window.
278+
* If {winid} is invalid a list with zeroes is returned.
279+
*
239280
* This can be used to save and restore the cursor position:
240281
* let save_cursor = getcurpos()
241282
* MoveTheCursorAround
242283
* call setpos('.', save_cursor)
243284
* Note that this only works within the window. See
244285
* |winrestview()| for restoring more state.
286+
*
287+
* Can also be used as a |method|:
288+
* GetWinid()->getcurpos()
245289
*/
246290
export async function getcurpos(
247291
denops: Denops,
292+
winid?: number | string,
248293
): Promise<Position> {
249-
return await denops.call("getcurpos") as Position;
294+
return await denops.call("getcurpos", winid) as Position;
250295
}
251296

252297
/**
253-
* Get the position for {expr}. For possible values of {expr}
254-
* see |line()|. For getting the cursor position see
298+
* Get the position for String {expr}. For possible values of
299+
* {expr} see |line()|. For getting the cursor position see
255300
* |getcurpos()|.
256301
* The result is a |List| with four numbers:
257302
* [bufnum, lnum, col, off]
@@ -265,12 +310,19 @@ export async function getcurpos(
265310
* character.
266311
* Note that for '< and '> Visual mode matters: when it is "V"
267312
* (visual line mode) the column of '< is zero and the column of
268-
* '> is a large number.
313+
* '> is a large number equal to |v:maxcol|.
314+
* The column number in the returned List is the byte position
315+
* within the line. To get the character position in the line,
316+
* use |getcharpos()|.
317+
* A very large column number equal to |v:maxcol| can be returned,
318+
* in which case it means "after the end of the line".
319+
* If {expr} is invalid, returns a list with all zeros.
269320
* This can be used to save and restore the position of a mark:
270321
* let save_a_mark = getpos("'a")
271322
* ...
272323
* call setpos("'a", save_a_mark)
273-
* Also see |getcurpos()| and |setpos()|.
324+
* Also see |getcharpos()|, |getcurpos()| and |setpos()|.
325+
*
274326
* Can also be used as a |method|:
275327
* GetMark()->getpos()
276328
*/
@@ -279,12 +331,13 @@ export async function getpos(denops: Denops, expr: string): Promise<Position> {
279331
}
280332

281333
/**
282-
* Set the position for {expr}. Possible values:
334+
* Set the position for String {expr}. Possible values:
283335
* . the cursor
284336
* 'x mark x
285337
* {list} must be a |List| with four or five numbers:
286338
* [bufnum, lnum, col, off]
287339
* [bufnum, lnum, col, off, curswant]
340+
*
288341
* "bufnum" is the buffer number. Zero can be used for the
289342
* current buffer. When setting an uppercase mark "bufnum" is
290343
* used for the mark position. For other marks it specifies the
@@ -293,29 +346,38 @@ export async function getpos(denops: Denops, expr: string): Promise<Position> {
293346
* For setting the cursor and the ' mark "bufnum" is ignored,
294347
* since these are associated with a window, not a buffer.
295348
* Does not change the jumplist.
349+
*
296350
* "lnum" and "col" are the position in the buffer. The first
297351
* column is 1. Use a zero "lnum" to delete a mark. If "col" is
298-
* smaller than 1 then 1 is used.
352+
* smaller than 1 then 1 is used. To use the character count
353+
* instead of the byte count, use |setcharpos()|.
354+
*
299355
* The "off" number is only used when 'virtualedit' is set. Then
300356
* it is the offset in screen columns from the start of the
301357
* character. E.g., a position within a <Tab> or after the last
302358
* character.
359+
*
303360
* The "curswant" number is only used when setting the cursor
304361
* position. It sets the preferred column for when moving the
305362
* cursor vertically. When the "curswant" number is missing the
306363
* preferred column is not set. When it is present and setting a
307364
* mark position it is not used.
365+
*
308366
* Note that for '< and '> changing the line number may result in
309367
* the marks to be effectively be swapped, so that '< is always
310368
* before '>.
369+
*
311370
* Returns 0 when the position could be set, -1 otherwise.
312371
* An error message is given if {expr} is invalid.
313-
* Also see |getpos()| and |getcurpos()|.
372+
*
373+
* Also see |setcharpos()|, |getpos()| and |getcurpos()|.
374+
*
314375
* This does not restore the preferred column for moving
315376
* vertically; if you set the cursor position with this, |j| and
316377
* |k| motions will jump to previous columns! Use |cursor()| to
317378
* also set the preferred column. Also see the "curswant" key in
318379
* |winrestview()|.
380+
*
319381
* Can also be used as a |method|:
320382
* GetPosition()->setpos('.')
321383
*/
@@ -334,6 +396,9 @@ export async function setpos(
334396
* for the current buffer. The first character has byte count
335397
* one.
336398
* Also see |line2byte()|, |go| and |:goto|.
399+
*
400+
* Returns -1 if the {byte} value is invalid.
401+
*
337402
* Can also be used as a |method|:
338403
* GetOffset()->byte2line()
339404
* {not available when compiled without the |+byte_offset|
@@ -352,10 +417,11 @@ export async function byte2line(denops: Denops, byte: number): Promise<number> {
352417
* below the last line:
353418
* line2byte(line("$") + 1)
354419
* This is the buffer size plus one. If 'fileencoding' is empty
355-
* it is the file size plus one.
356-
* When {lnum} is invalid, or the |+byte_offset| feature has been
357-
* disabled at compile time, -1 is returned.
420+
* it is the file size plus one. {lnum} is used like with
421+
* |getline()|. When {lnum} is invalid, or the |+byte_offset|
422+
* feature has been disabled at compile time, -1 is returned.
358423
* Also see |byte2line()|, |go| and |:goto|.
424+
*
359425
* Can also be used as a |method|:
360426
* GetLnum()->line2byte()
361427
*/
@@ -371,6 +437,7 @@ export async function line2byte(denops: Denops, lnum: number): Promise<number> {
371437
* {lnum} is used like with |getline()|. Thus "." is the current
372438
* line, "'m" mark m, etc.
373439
* Returns 0 if the current window is not in diff mode.
440+
*
374441
* Can also be used as a |method|:
375442
* GetLnum()->diff_filler()
376443
*/

denops_std/function/cursor_test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ test({
4242
name: "line()",
4343
fn: async () => {
4444
await denops.cmd("enew!");
45+
const winid = await denops.call("bufwinid", "%") as number;
4546
await denops.call("setline", 1, ["a", "b", "c"]);
47+
4648
assertNumber(await cursor.line(denops, "."));
4749
assertEquals(await cursor.line(denops, "."), 1);
4850
assertEquals(await cursor.line(denops, "$"), 3);
51+
assertNumber(await cursor.line(denops, ".", winid));
52+
assertEquals(await cursor.line(denops, ".", winid), 1);
4953
},
5054
});
5155

@@ -80,6 +84,9 @@ test({
8084
name: "screenpos()",
8185
fn: async () => {
8286
await denops.cmd("enew!");
87+
const winid = await denops.call("bufwinid", "%") as number;
88+
89+
assertScreenPos(await cursor.screenpos(denops, winid, 1, 1));
8390
assertScreenPos(await cursor.screenpos(denops, "%", 1, 1));
8491
// screenpos() returns `{}` if failed
8592
assertEquals(
@@ -93,7 +100,14 @@ test({
93100
name: "getcurpos",
94101
fn: async () => {
95102
await denops.cmd("enew!");
103+
const winid = await denops.call("bufwinid", "%") as number;
104+
96105
assertPosition(await cursor.getcurpos(denops));
106+
assertEquals(await cursor.getcurpos(denops), [0, 1, 1, 0, 1]);
107+
assertPosition(await cursor.getcurpos(denops, winid));
108+
assertEquals(await cursor.getcurpos(denops, winid), [0, 1, 1, 0, 1]);
109+
assertEquals(await cursor.getcurpos(denops, "%"), [0, 1, 1, 0, 1]);
110+
assertEquals(await cursor.getcurpos(denops, -1), [0, 0, 0, 0, 0]);
97111
},
98112
});
99113

0 commit comments

Comments
 (0)