Skip to content

Commit 2dd193e

Browse files
committed
another refactoring of getting next todo, now it uses itertools chaining and doesn't use recursive search for indices, at this point implementation completely replaces old implementation
1 parent 638e80a commit 2dd193e

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

ftplugin/orgmode/plugins/Todo.py

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from orgmode._vim import echom, ORGMODE, apply_count, repeat, realign_tags
77
from orgmode import settings
8-
from orgmode.liborgmode.base import Direction, flatten_list
8+
from orgmode.liborgmode.base import Direction
99
from orgmode.menu import Submenu, ActionEntry
1010
from orgmode.keybinding import Keybinding, Plug
1111

@@ -104,53 +104,54 @@ def _get_next_state(
104104

105105
# TODO this would not work if there are 2 keys with same name... this
106106
# also causes problem for find in below method
107-
def find_current_todo_state(current, all_states, stop=0):
108-
u""" Find current todo state
109-
110-
Args:
111-
current: Current todo state
112-
all_states: List of todo states
113-
stop: Internal parameter for parsing only two levels of lists
114-
115-
Returns:
116-
list: First position of todo state in list in the form
117-
(IDX_TOPLEVEL, IDX_SECOND_LEVEL (0|1), IDX_OF_ITEM)
118-
"""
119-
for i, element in enumerate(all_states):
120-
if type(element) in (tuple, list) and stop < 2:
121-
res = find_current_todo_state(current, element, stop=stop + 1)
122-
if res:
123-
res.insert(0, i)
124-
return res
125-
# ensure that only on the second level of sublists todo states
126-
# are found
127-
if type(element) == unicode and stop == 2:
128-
if current == split_access_key(element)[0]:
129-
return [i]
130-
131-
ci = find_current_todo_state(current_state, all_states)
107+
# def find_current_todo_state(current, all_states, stop=0):
108+
# u""" Find current todo state
109+
110+
# Args:
111+
# current: Current todo state
112+
# all_states: List of todo states
113+
# stop: Internal parameter for parsing only two levels of lists
114+
115+
# Returns:
116+
# list: First position of todo state in list in the form
117+
# (IDX_TOPLEVEL, IDX_SECOND_LEVEL (0|1), IDX_OF_ITEM)
118+
# """
119+
# for i, element in enumerate(all_states):
120+
# if type(element) in (tuple, list) and stop < 2:
121+
# res = find_current_todo_state(current, element, stop=stop + 1)
122+
# if res:
123+
# res.insert(0, i)
124+
# return res
125+
# # ensure that only on the second level of sublists todo states
126+
# # are found
127+
# if type(element) == unicode and stop == 2:
128+
# if current == split_access_key(element)[0]:
129+
# return [i]
130+
131+
# ci = find_current_todo_state(current_state, all_states)
132+
133+
cleaned_todos = [[split_access_key(todo)[0] for todo in
134+
it.chain.from_iterable(x)] for x in all_states]
135+
todo_position = [1 if current_state in set else 0 for set in cleaned_todos]
136+
# TODO This is the case when there are 2 todo states with the same
137+
# name. Wat do?
138+
if sum(todo_position) > 1: pass
132139

133140
# backward direction should really be -1 not 2
134-
dir = 1
135-
if direction == Direction.BACKWARD:
136-
dir = -1
141+
dir = -1 if direction == Direction.BACKWARD else 1
137142
# work only with top level index
138143
if next_set:
139-
top_set = ci[0] if ci is not None else 0
140-
ind = (top_set + dir) % len(all_states)
144+
top_set = todo_position.index(1) if todo_position else 0
145+
ind = (top_set + dir) % len(cleaned_todos)
141146
echom("Using set: %s" % str(all_states[ind]))
142-
# NOTE: List must be flatten because todo states can be empty, this
143-
# is also valid for above use of flat_list
144-
return split_access_key(flatten_list(all_states[ind])[0])[0]
147+
return cleaned_todos[ind][0]
145148
# No next set, cycle around everything
146149
else:
147-
tmp = [split_access_key(x)[0] for x in flatten_list(all_states)] + [None]
148-
# TODO same problem as above if there are 2 todo states with same
149-
# name
150+
tmp = list(it.chain.from_iterable(cleaned_todos)) + [None]
150151
try:
151152
ind = (tmp.index(current_state) + dir) % len(tmp)
152153
except ValueError:
153-
# TODO should this return None like or first todo item?
154+
# TODO should this return None or first todo item?
154155
ind = 0
155156
return tmp[ind]
156157

0 commit comments

Comments
 (0)