Skip to content

Commit b50caab

Browse files
committed
feat: add please.look_up_target (closes #65)
1 parent 52841cb commit b50caab

File tree

6 files changed

+83
-1
lines changed

6 files changed

+83
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ please.nvim is a plugin which allows you interact with your Please repository fr
77
* Display history of previous commands and run any of them again with `please.history()`.
88
* Set the profile to use with `please.set_profile()`.
99
* Jump from a source file to its build target definition with `please.jump_to_target()`.
10+
* Look up a build target by its label with `please.look_up_target()`.
1011
* Yank a target's label with `please.yank()`.
1112
* `please` configured as the `filetype` for `BUILD`, `BUILD.plz`, `*.build`, and `*.build_defs`
1213
files.

doc/please.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ command to get you started.
117117
vim.keymap.set('n', '<leader>pp', please.set_profile)
118118
vim.keymap.set('n', '<leader>pm', please.maximise_popup)
119119
vim.keymap.set('n', '<leader>pj', please.jump_to_target)
120+
vim.keymap.set('n', '<leader>pl', please.look_up_target)
120121
vim.keymap.set('n', '<leader>py', please.yank)
121122
<
122123
==============================================================================
@@ -216,6 +217,13 @@ jump_to_target() *please.jump_to_target()*
216217
found which should be the case for all targets except for those with names
217218
which are generated when the `BUILD` file is executed.
218219

220+
look_up_target() *please.look_up_target()*
221+
Looks up a build target by its label and jumps to its location.
222+
223+
The cursor will be moved to where the build target is created if it can be
224+
found which should be the case for all targets except for those with names
225+
which are generated when the `BUILD` file is executed.
226+
219227
yank() *please.yank()*
220228
If the current file is a `BUILD` file, yank the label of the target which
221229
is under the cursor. Otherwise, yank the label of the target which takes

lua/please.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,34 @@ function M.jump_to_target()
542542
end)
543543
end
544544

545+
---Looks up a build target by its label and jumps to its location.
546+
---
547+
---The cursor will be moved to where the build target is created if it can be
548+
---found which should be the case for all targets except for those with names
549+
---which are generated when the `BUILD` file is executed.
550+
function M.look_up_target()
551+
logging.log_call('please.look_up_target')
552+
553+
logging.log_errors('Failed to look up target', function()
554+
local filepath = assert(get_filepath())
555+
local root = assert(get_repo_root(filepath))
556+
vim.ui.input({ prompt = 'Enter target to look up' }, function(label)
557+
if not label then
558+
return
559+
end
560+
label = vim.trim(label)
561+
local target, errmsg = parsing.locate_build_target(root, label)
562+
if not target then
563+
logging.error('Failed to look up target: %s', errmsg)
564+
return
565+
end
566+
logging.debug('opening %s at %s', target.file, vim.inspect(target.position))
567+
vim.cmd('edit ' .. target.file)
568+
vim.api.nvim_win_set_cursor(0, target.position)
569+
end)
570+
end)
571+
end
572+
545573
---If the current file is a `BUILD` file, yank the label of the target which is
546574
---under the cursor. Otherwise, yank the label of the target which takes the
547575
---current file as an input.

lua/please/parsing.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ function M.locate_build_target(root, label)
9595
check_parser_installed('please')
9696

9797
local pkg, name = label:match('^//([^:]*):([^/]+)$')
98+
if not pkg or not name then
99+
return nil, string.format('"%s" is not a valid label', label)
100+
end
98101
local pkg_path = vim.fs.joinpath(root, pkg)
99102
for _, build_file_name in ipairs(build_file_names) do
100103
local build_path = vim.fs.joinpath(pkg_path, build_file_name)

tests/please/parsing_spec.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ describe('locate_build_target', function()
7777
label = '//does/not/exist:target',
7878
expected_err = 'no build file exists for package "does/not/exist"',
7979
},
80+
{
81+
name = 'should return error if label is not a valid',
82+
tree = { '.plzconfig' },
83+
label = 'foo',
84+
expected_err = '"foo" is not a valid label',
85+
},
8086
{
8187
name = 'should return position for target at the start of a BUILD file',
8288
tree = {

tests/please_spec.lua

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local please = require('please')
33
local runner = require('please.runner')
44
local temptree = require('tests.temptree')
55

6-
-- require('please.logging').toggle_debug()
6+
require('please.logging').toggle_debug()
77

88
-- When this test file is run multiple times in parallel (in a non-sandboxed environment), at least one of the runs
99
-- usually fails because some functionality being tested relies on use of the clipboard which is being shared between
@@ -1007,6 +1007,42 @@ describe('jump_to_target', function()
10071007
end)
10081008
end)
10091009

1010+
describe('look_up_target', function()
1011+
it('should jump to build target which uses it as an input', function()
1012+
local root = temptree.create({
1013+
'.plzconfig',
1014+
['pkg/'] = {
1015+
BUILD = [[
1016+
export_file(
1017+
name = "foo1",
1018+
src = "foo1.txt",
1019+
)
1020+
1021+
export_file(
1022+
name = "foo2",
1023+
src = "foo2.txt",
1024+
)
1025+
]],
1026+
},
1027+
})
1028+
local input_fake = InputFake:new()
1029+
1030+
-- GIVEN we're editing a file
1031+
vim.cmd('edit ' .. root .. '/foo1.txt')
1032+
-- WHEN we call look_up_target
1033+
please.look_up_target()
1034+
-- THEN we're prompted to enter the build target to look up
1035+
input_fake:assert_prompt('Enter target to look up')
1036+
-- WHEN we enter a build target
1037+
input_fake:enter_input('//pkg:foo2')
1038+
vim.wait(500)
1039+
-- THEN the BUILD file containing the build target is opened
1040+
assert.equal(root .. '/pkg/BUILD', vim.api.nvim_buf_get_name(0), 'incorrect BUILD file')
1041+
-- AND the cursor is moved to the build target
1042+
assert.same({ 6, 0 }, vim.api.nvim_win_get_cursor(0), 'incorrect cursor position')
1043+
end)
1044+
end)
1045+
10101046
describe('yank', function()
10111047
local function create_temp_tree()
10121048
return temptree.create({

0 commit comments

Comments
 (0)