Skip to content

Commit 7ebbc8f

Browse files
authored
Merge pull request #5 from fidgetingbits/api-cleanup
refactor: cleanup names and better expose cursorless lua funcs
2 parents 7ddc06f + ab956e5 commit 7ebbc8f

6 files changed

Lines changed: 20 additions & 15 deletions

File tree

cursorless.nvim/lua/cursorless/cursorless.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local M = {}
33
-- Get the first and last visible line of the current window/buffer
44
-- @see https://vi.stackexchange.com/questions/28471/get-first-and-last-visible-line-from-other-buffer-than-current
55
-- w0/w$ are indexed from 1, similarly to what is shown in neovim
6-
-- e.g. :lua print(dump_table(require('talon.cursorless').window_get_visible_lines()))"
6+
-- e.g. :lua print(vim.inspect(require('cursorless').window_get_visible_lines()))"
77
-- window_get_visible_lines
88
-- { [1] = 28, [2] = 74 }
99
function M.window_get_visible_lines()
@@ -15,7 +15,7 @@ end
1515
-- https://neovim.io/doc/user/api.html#nvim_win_get_cursor()
1616
--
1717
-- luacheck:ignore 631
18-
-- e.g. run in command mode :vmap <c-a> <Cmd>lua print(vim.inspect(require('talon.cursorless').buffer_get_selection()))<Cr>
18+
-- e.g. run in command mode :vmap <c-a> <Cmd>lua print(vim.inspect(require('cursorless').buffer_get_selection()))<Cr>
1919
-- then go in visual mode with "v" and select "hello" on the first line and continue selection with "air"
2020
-- on the second line.
2121
-- Then hit ctrl+b and it will show the selection
@@ -89,7 +89,7 @@ end
8989

9090
-- https://www.reddit.com/r/neovim/comments/p4u4zy/how_to_pass_visual_selection_range_to_lua_function/
9191
-- luacheck:ignore 631
92-
-- e.g. run in command mode :vmap <c-b> <Cmd>lua print(vim.inspect(require('talon.cursorless').buffer_get_selection_text()))<Cr>
92+
-- e.g. run in command mode :vmap <c-b> <Cmd>lua print(vim.inspect(require('cursorless').buffer_get_selection_text()))<Cr>
9393
-- then go in visual mode with "v" and select "hello" on the first line and continue selection with "air"
9494
-- on the second line.
9595
-- Then hit ctrl+b and it will show the selection

cursorless.nvim/lua/cursorless/init.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ local function setup(user_config)
8888
configure_command_server_shortcut(config.shortcut)
8989
end
9090

91+
local cursorless = require("cursorless.cursorless")
9192
M = {
9293
setup = setup,
9394
config = require("cursorless.config").get_config,
95+
window_get_visible_lines = cursorless.window_get_visible_lines,
96+
buffer_get_selection = cursorless.buffer_get_selection,
97+
buffer_get_selection_text = cursorless.buffer_get_selection_text,
98+
select_range = cursorless.select_range,
9499
}
95100

96101
return M

cursorless.nvim/lua/cursorless/utils.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ end
77

88
-- :lua print(require('cursorless.utils').get_path_separator())
99
function M.get_path_separator()
10-
if require("cursorless.utils").is_platform_windows() then
10+
if M.is_platform_windows() then
1111
return "\\"
1212
end
1313
return "/"
@@ -26,7 +26,7 @@ function M.cursorless_nvim_path()
2626
-- skip as the file name is prefixed by "@"
2727
str = str:sub(2)
2828
-- print(('source_file2=%s'):format(str))
29-
if require("cursorless.utils").is_platform_windows() then
29+
if M.is_platform_windows() then
3030
str = str:gsub("/", "\\")
3131
-- print('is_platform_windows')
3232
end
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { Clipboard } from "@cursorless/common";
2-
import { getFromClipboard, putToClipboard } from "../../neovimApi";
2+
import { getClipboard, setClipboard } from "../../neovimApi";
33
import type { NeovimClient } from "neovim";
44

55
export default class NeovimClipboard implements Clipboard {
66
constructor(private client: NeovimClient) {}
77

88
async readText(): Promise<string> {
9-
return await getFromClipboard(this.client);
9+
return await getClipboard(this.client);
1010
}
1111

1212
async writeText(value: string): Promise<void> {
13-
return await putToClipboard(value, this.client);
13+
return await setClipboard(value, this.client);
1414
}
1515
}

packages/neovim-common/src/neovimApi.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function bufferGetSelections(
1313
window: Window,
1414
client: NeovimClient,
1515
): Promise<Selection[]> {
16-
const luaCode = `return require("cursorless.cursorless").buffer_get_selection()`;
16+
const luaCode = `return require("cursorless").buffer_get_selection()`;
1717
// Note lines are indexed from 1, similarly to what is shown in neovim
1818
// and columns are also indexed from 1
1919
const [startLine, startCol, endLine, endCol, reverse] =
@@ -60,7 +60,7 @@ export async function bufferSetSelections(
6060
// cursorless has 0-based lines/columns, but neovim has 1-based lines and 0-based columns
6161
// also, experience shows we need to subtract 1 from the end character to stop on it in visual mode (instead of after it)
6262
// https://neovim.io/doc/user/api.html#nvim_win_set_cursor()
63-
const luaCode = `return require("cursorless.cursorless").select_range(${
63+
const luaCode = `return require("cursorless").select_range(${
6464
selections[0].start.line + 1
6565
}, ${selections[0].start.character}, ${selections[0].end.line + 1}, ${
6666
selections[0].end.character
@@ -85,7 +85,7 @@ export async function windowGetVisibleRanges(
8585
): Promise<Range[]> {
8686
// Get the first and last visible lines of the current window
8787
// Note they are indexed from 1, similarly to what is shown in neovim*
88-
const luaCode = `return require("cursorless.cursorless").window_get_visible_lines()`;
88+
const luaCode = `return require("cursorless").window_get_visible_lines()`;
8989
const [firstLine, lastLine] = (await client.executeLua(luaCode, [])) as [
9090
number,
9191
number,
@@ -116,15 +116,15 @@ export async function getCursorlessNvimPath(
116116
* https://stackoverflow.com/questions/11489428/how-can-i-make-vim-paste-from-and-copy-to-the-systems-clipboard?page=1&tab=scoredesc#tab-top
117117
* https://stackoverflow.com/questions/30691466/what-is-difference-between-vims-clipboard-unnamed-and-unnamedplus-settings
118118
*/
119-
export async function putToClipboard(data: string, client: NeovimClient) {
119+
export async function setClipboard(data: string, client: NeovimClient) {
120120
await client.callFunction("setreg", ["*", data]);
121121
}
122122

123123
/**
124124
* Return the string from the operating system clipboard
125125
* https://vimdoc.sourceforge.net/htmldoc/eval.html#getreg()
126126
*/
127-
export async function getFromClipboard(client: NeovimClient): Promise<string> {
127+
export async function getClipboard(client: NeovimClient): Promise<string> {
128128
return await client.callFunction("getreg", ["*"]);
129129
}
130130

packages/neovim-common/src/neovimHelpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import {
44
bufferGetSelections,
55
pasteFromClipboard,
6-
putToClipboard,
6+
setClipboard,
77
} from "@cursorless/neovim-common";
88
import { NeovimTextEditorImpl } from "./ide/neovim/NeovimTextEditorImpl";
99
import type { NeovimClient } from "neovim";
@@ -17,7 +17,7 @@ export async function neovimClipboardCopy(
1717
const window = await client.window;
1818
const selections = await bufferGetSelections(window, client);
1919
const data = editor.document.getText(selections[0]);
20-
await putToClipboard(data, client);
20+
await setClipboard(data, client);
2121
}
2222

2323
export async function neovimClipboardPaste(

0 commit comments

Comments
 (0)