Skip to content

Commit

Permalink
key binding finished
Browse files Browse the repository at this point in the history
  • Loading branch information
qiray committed Dec 14, 2017
1 parent 91a64bf commit 42f5cf4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 59 deletions.
11 changes: 5 additions & 6 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,28 @@

{
"keys": ["right"],
"command": "kate_documents_unfold",
"command": "kate_documents_act", "args": {"act": "unfold"},
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.kate_documents" },
]
},
{
"keys": ["left"],
"command": "kate_documents_fold",
"command": "kate_documents_act", "args": {"act": "fold"},
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.kate_documents" },
]
},

{
"keys": ["backspace"],
"command": "kate_documents_fold",
"command": "kate_documents_act", "args": {"act": "fold"},
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.kate_documents" },
]
},
{
"keys": ["\\"],
"command": "kate_documents",
"keys": ["o"],
"command": "kate_documents_open_external",
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.kate_documents" },
]
Expand Down
68 changes: 20 additions & 48 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,76 +81,48 @@ def run(self, edit):
tree = generate_tree(view_list)
draw_tree(window, edit, tree)

#TODO: make 1 class/command for all actions - fold/unfold and open
#TODO: make correct cursor moving
#TODO: open file browser or any default app on "/" pressed

class KateDocumentsActCommand(sublime_plugin.TextCommand):
def run(self, edit):
def run(self, edit, act = 'default'):
selection = self.view.sel()[0]
self.open_file(edit, selection)
self.open_file(edit, selection, act)

def open_file(self, edit, selection):
def open_file(self, edit, selection, act):
global tree
window = self.view.window()
(row, col) = self.view.rowcol(selection.begin())

action = tree.get_action(row)
if action is None:
return
if action['action'] == 'file':
view = first(window.views(), lambda v: v.id() == action['view_id'])
window.focus_view(view)
elif action['action'] == 'fold':
node = tree.nodes[action['id']]
goto_linenumber = row + 1
if action['action'] == 'file' and act == 'default':
view = first(window.views(), lambda v: v.id() == action['view_id'])
window.focus_view(view)
elif action['action'] == 'fold' and act != 'unfold':
tree.nodes[action['id']].status = 'unfold'
draw_tree(window, edit, tree)
elif action['action'] == 'unfold':
elif action['action'] == 'unfold' and act != 'fold':
tree.nodes[action['id']].status = 'fold'
draw_tree(window, edit, tree)
self.view.run_command("goto_line", {"line": row + 1})

class KateDocumentsFoldCommand(sublime_plugin.TextCommand):
def run(self, edit):
global tree
selection = self.view.sel()[0]
window = self.view.window()
(row, col) = self.view.rowcol(selection.begin())
action = tree.get_action(row)
if action is None:
return
if action['action'] == 'file':
node = tree.nodes[action['id']]
if node.parent is not None:
tree.nodes[node.parent].status = 'unfold'
draw_tree(window, edit, tree)
elif action['action'] == 'fold':
tree.nodes[action['id']].status = 'unfold'
elif act == 'fold' and node.parent is not None and node.parent != '':
goto_linenumber = tree.nodes[node.parent].stringnum
tree.nodes[node.parent].status = 'unfold'
draw_tree(window, edit, tree)
if row != 0:
self.view.run_command("goto_line", {"line": row})
elif act == 'unfold' and len(node.children) > 0:
goto_linenumber = tree.nodes[sorted(node.children)[0]].stringnum
print(len(node.children))
self.view.run_command("goto_line", {"line": goto_linenumber})

class KateDocumentsUnfoldCommand(sublime_plugin.TextCommand):
class KateDocumentsOpenExternalCommand(sublime_plugin.TextCommand):
def run(self, edit):
global tree
selection = self.view.sel()[0]
window = self.view.window()
(row, col) = self.view.rowcol(selection.begin())
action = tree.get_action(row)
if action is None:
return
if action['action'] == 'file':
node = tree.nodes[action['id']]
if node.parent is not None:
tree.nodes[node.parent].status = 'fold'
draw_tree(window, edit, tree)
elif action['action'] == 'unfold':
tree.nodes[action['id']].status = 'fold'
draw_tree(window, edit, tree)
self.view.run_command("goto_line", {"line": row + 2})

class OpenedDocumentsNoopCommand(sublime_plugin.TextCommand):
def run(self, edit):
pass
node = tree.nodes[action['id']]
self.view.window().run_command("open_dir", {"dir": node.node_id})

# MOUSE ACTIONS ##############################################

Expand Down
15 changes: 10 additions & 5 deletions nodetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ def get_name(self):
return '▾ ' + arr[-1]
return '≡ '+ arr[-1]

def print_children(self, array, actions_map, length):
def print_children(self, array, actions_map, length, stringnum):
result = separator*length + self.get_name() + '\n'
self.stringnum = stringnum
stringnum += 1
actions_map.add_action(self)
if self.status == 'unfold':
return result
return result, stringnum
children = sorted(self.children)
for child in children:
result += array[child].print_children(array, actions_map, length + 1)
return result
temp, stringnum = array[child].print_children(array, actions_map, length + 1, stringnum)
result += temp
return result, stringnum

class Tree(object):

Expand Down Expand Up @@ -87,8 +90,10 @@ def __str__(self):
if flag:
break
printed_parents = templist
stringnum = 1
for name in printed_parents:
result += self.nodes[name].print_children(self.nodes, self.actions_map, 0)
temp, stringnum = self.nodes[name].print_children(self.nodes, self.actions_map, 0, stringnum)
result += temp
return result

def get_action(self, number):
Expand Down

0 comments on commit 42f5cf4

Please sign in to comment.