Skip to content

Commit

Permalink
more tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdevries committed Apr 4, 2022
1 parent d0e35dc commit 99d02fc
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 51 deletions.
74 changes: 48 additions & 26 deletions lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -729,35 +729,57 @@ function Picker:move_selection(change)

-- row = self.scroller(self.num_visible, self.manager:num_results(), row)

local new_row = self:get_selection_row() + change
if new_row >= self.num_visible then
self.offset = math.min(self.offset + new_row - self.num_visible + 1, self.num_visible - 1)
self:set_selection(self.num_visible - 1)
elseif new_row < 0 then
if self.offset == 0 and self.scroll_strategy == "cycle" then
-- self.offset = math.min(self.offset + new_row - self.num_visible + 1, self.num_visible - 1)
-- self.offset = self.manager:num_results() - self.num_visible
-- self:set_selection()
-- error(vim.inspect {
-- num_results = self.manager:num_results(),
-- num_visible = self.num_visible,
-- })

-- print(
-- self.manager:num_results(),
-- self.num_visible,
-- math.min(self.manager:num_results() - 1, self.num_visible - 1)
-- )
self.offset = math.max(0, self.manager:num_results() - self.num_visible + 1)
self:_update_results {}
-- todo: can we do this based on index??

if self.sorting_strategy == "ascending" then
local new_row = self:get_selection_row() + change
if new_row >= self.num_visible then
if self.offset == 0 and self.scroll_strategy == "cycle" then
error "ya"
end

self:set_selection(math.min(self.num_visible - 1, self.manager:num_results() - 1))
self.offset = math.min(self.offset + new_row - self.num_visible + 1, self.num_visible - 1)
self:set_selection(self.num_visible - 1)
elseif new_row < 0 then
if self.offset == 0 and self.scroll_strategy == "cycle" then
-- self.offset = math.min(self.offset + new_row - self.num_visible + 1, self.num_visible - 1)
-- self.offset = self.manager:num_results() - self.num_visible
-- self:set_selection()
-- error(vim.inspect {
-- num_results = self.manager:num_results(),
-- num_visible = self.num_visible,
-- })

-- print(
-- self.manager:num_results(),
-- self.num_visible,
-- math.min(self.manager:num_results() - 1, self.num_visible - 1)
-- )
self.offset = math.max(0, self.manager:num_results() - self.num_visible)
self:_update_results {}

self:set_selection(math.min(self.num_visible - 1, self.manager:num_results() - 1))
else
self.offset = math.max(self.offset - 1, 0)
self:set_selection(0)
end
else
self.offset = math.max(self.offset - 1, 0)
self:set_selection(0)
self:set_selection(new_row)
end
else
self:set_selection(new_row)
-- print(self.offset, self.num_visible, self.manager:num_results())

local new_row = self:get_selection_row() + change
if new_row >= self.num_visible then
self.offset = math.max(0, self.manager:num_results() - self.num_visible)
self:_update_results {}

self:set_selection(math.max(0, self.num_visible - self.manager:num_results()))
elseif new_row < 0 then
error "oh no"
else
self:set_selection(new_row)
end
end

self:_redraw { force = true }
Expand Down Expand Up @@ -924,7 +946,7 @@ function Picker:set_selection(row)

local old_row = row
row = self.scroller(self.num_visible, self.manager:num_results(), row)
print("Setting row to be:", old_row, "->", row)
-- print("Setting row to be:", old_row, "->", row)

if not self:can_select_row(row) then
-- If the current selected row exceeds number of currently displayed
Expand Down
96 changes: 71 additions & 25 deletions lua/tests/automated/pickers/scroll_cycle_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,33 @@ describe("scroll_cycle", function()
end)

for _, sorting in ipairs { "ascending", "descending" } do
it("should be able to cycle selections: cycle <c-n> " .. sorting, function()
tester.run_string([[
runner.picker("find_files", "fixtures/file<c-n><c-n>", {
post_typed = {
{ "lua/tests/fixtures/file_a.txt", helper.get_selection_value },
},
}, {
sorting_strategy = "]] .. sorting .. [[",
scroll_strategy = "cycle",
})
]])
end)

it("should be able to cycle selections: cycle <c-p>" .. sorting, function()
tester.run_string([[
runner.picker("find_files", "fixtures/file<c-p><c-p>", {
post_typed = {
{ "lua/tests/fixtures/file_a.txt", helper.get_selection_value },
},
}, {
sorting_strategy = "]] .. sorting .. [[",
scroll_strategy = "cycle",
})
]])
end)
for _, key in ipairs { "<c-n>", "<c-p>" } do
it(string.format("Cycle: %sx2 %s", key, sorting), function()
tester.run_string(([[
runner.picker("find_files", "fixtures/file%s%s", {
post_typed = {
{ "lua/tests/fixtures/file_a.txt", helper.get_selection_value },
},
}, {
sorting_strategy = "%s",
scroll_strategy = "cycle",
}
) ]]):format(key, key, sorting))
end)

it(string.format("Cycle: %sx3 %s", key, sorting), function()
tester.run_string(([[
runner.picker("find_files", "fixtures/file%s%s%s", {
post_typed = {
{ "lua/tests/fixtures/file_abc.txt", helper.get_selection_value },
},
}, {
sorting_strategy = "%s",
scroll_strategy = "cycle",
}
) ]]):format(key, key, key, sorting))
end)
end
end

it("should be able to cycle selections: limit", function()
Expand All @@ -60,4 +62,48 @@ describe("scroll_cycle", function()
})
]]
end)

it("long: cycle to top", function()
tester.run_string [[
runner.picker("find_files", "fixtures/long<c-p>", {
post_close = {
{ "lua/tests/fixtures/long_11111111111.md", helper.get_selection_value },
},
}, {
sorting_strategy = "ascending",
scroll_strategy = "cycle",
height = 10,
})
]]
end)

it("long: cycle to top", function()
tester.run_string [[
runner.picker("find_files", "fixtures/long<c-n>", {
post_close = {
{ "lua/tests/fixtures/long_11111111111.md", helper.get_selection_value },
},
}, {
sorting_strategy = "descending",
scroll_strategy = "cycle",
height = 10,
})
]]
end)

it("long: smash <c-p>", function()
tester.run_string [[
runner.picker("find_files", "fixtures/long<c-p><c-p><c-p><c-p><c-p>", {
post_typed = {
{ "lua/tests/fixtures/long_111111.md", helper.get_selection_value },
},
}, {
sorting_strategy = "descending",
scroll_strategy = "cycle",
layout_config = {
height = 8,
},
})
]]
end)
end)
Empty file added lua/tests/fixtures/long_1.md
Empty file.
Empty file added lua/tests/fixtures/long_11.md
Empty file.
Empty file added lua/tests/fixtures/long_111.md
Empty file.
Empty file added lua/tests/fixtures/long_1111.md
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
89 changes: 89 additions & 0 deletions scratch/files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from pathlib import Path
import subprocess

something = subprocess.run(["rg", "--files"], capture_output=True)
files = something.stdout.decode('utf-8').split('\n')
files.sort()

user_input = "lua finders/async"

REJECTED = -1.0
GOOD = 2.0

def is_subset(item, prompt) -> float:
prompt_chars = set()
for c in prompt:
prompt_chars.add(c)

item_chars = set()
for c in item:
item_chars.add(c)

return prompt_chars.issubset(item_chars)

def proportion_of_contained_letters(prompt, item) -> float:
prompt_chars = set()
for c in prompt:
prompt_chars.add(c)

item_chars = set()
for c in item:
item_chars.add(c)

contained = 0
for prompt_char in prompt_chars:
if prompt_char in item_chars:
contained += 1

return contained / len(prompt_chars)

def jerry_match(prompt: str, item) -> float:
p = Path(item)

split = prompt.split(" ", maxsplit=2)
language = split[0]
filter = split[1]

if p.suffix != "." + language:
return REJECTED

if filter in item:
return GOOD

proprotion = proportion_of_contained_letters(filter, item)
if proprotion < 0.75:
return REJECTED

return proprotion

def score_results(prompt, files):
results = []
for f in files:
score = jerry_match(prompt, f)
if score == REJECTED:
continue

results.append({'score': score, 'item': f})


results.sort(key=lambda x: x["score"], reverse=True)
return results

while True:
i = input("Filter Phrase > ")
if not i:
break

results = score_results(i, files)
for result in results[:10]:
print(result["item"])

# x = [1, 2, 3, 4, 4, 2, 1, 3]
# print(x)
# print(set(x))

# x = {1, 2, 3}
# y = {1}
#
# print("x.issubset(y)", x.issubset(y))
# print("y.issubset(x)", y.issubset(x))

0 comments on commit 99d02fc

Please sign in to comment.