Skip to content

Commit

Permalink
[refactor] Add comp_action_e type
Browse files Browse the repository at this point in the history
Add test in spec/ysh-completion
  • Loading branch information
Andy C committed Sep 12, 2023
1 parent 4be911b commit ee552f1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
38 changes: 16 additions & 22 deletions core/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
from _devbuild.gen.id_kind_asdl import Id
from _devbuild.gen.syntax_asdl import (CompoundWord, word_part_e, word_t,
redir_param_e, Token)
from _devbuild.gen.runtime_asdl import value, value_e, scope_e, Proc
from _devbuild.gen.runtime_asdl import (value, value_e, scope_e, Proc,
comp_action_e, comp_action_t)
from _devbuild.gen.types_asdl import redir_arg_type_e
from core import error
from core import pyos
Expand Down Expand Up @@ -359,15 +360,9 @@ def Matches(self, comp):
# type: (Api) -> Iterator[str]
pass

# mycpp: for rewrites of isinstance()
def IsFileSystemAction(self):
# type: () -> bool
return False

# mycpp: for rewrites of isinstance()
def IsShellFuncAction(self):
# type: () -> bool
return False
def ActionKind(self):
# type: () -> comp_action_t
return comp_action_e.Other

def Print(self, f):
# type: (mylib.BufWriter) -> None
Expand Down Expand Up @@ -466,10 +461,9 @@ def __init__(self, dirs_only, exec_only, add_slash):
# filenames.
self.add_slash = add_slash # for directories

# mycpp: rewrite of isinstance()
def IsFileSystemAction(self):
# type: () -> bool
return True
def ActionKind(self):
# type: () -> comp_action_t
return comp_action_e.FileSystem

def Print(self, f):
# type: (mylib.BufWriter) -> None
Expand Down Expand Up @@ -529,6 +523,7 @@ def Matches(self, comp):


class CommandAction(CompletionAction):
""" TODO: Implement complete -C """

def __init__(self, cmd_ev, command_name):
# type: (CommandEvaluator, str) -> None
Expand Down Expand Up @@ -560,10 +555,9 @@ def Print(self, f):

f.write('[ShellFuncAction %s] ' % self.func.name)

# mycpp: rewrite of isinstance()
def IsShellFuncAction(self):
# type: () -> bool
return True
def ActionKind(self):
# type: () -> comp_action_t
return comp_action_e.BashFunc

def debug(self, msg):
# type: (str) -> None
Expand Down Expand Up @@ -664,9 +658,8 @@ def Print(self, f):
f.write('VariablesAction ')



class ExternalCommandAction(CompletionAction):
"""complete commands in $PATH.
"""Complete commands in $PATH.
This is PART of compgen -A command.
"""
Expand Down Expand Up @@ -867,15 +860,16 @@ def Matches(self, comp):
num_matches = 0

for a in self.actions:
is_fs_action = a.IsFileSystemAction()
action_kind = a.ActionKind()
is_fs_action = action_kind == comp_action_e.FileSystem
for match in a.Matches(comp):
# Special case hack to match bash for compgen -F. It doesn't filter by
# to_complete!
show = (
self.predicate.Evaluate(match) and
# ShellFuncAction results are NOT filtered by prefix!
(match.startswith(comp.to_complete) or
a.IsShellFuncAction()))
action_kind == comp_action_e.BashFunc))

# There are two kinds of filters: changing the string, and filtering
# the set of strings. So maybe have modifiers AND filters? A triple.
Expand Down
2 changes: 2 additions & 0 deletions core/runtime.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ module runtime

# Hay "first word" namespace
HayNode = (Dict[str, HayNode] children)

comp_action = Other | FileSystem | BashFunc
}

# vim: sw=2
2 changes: 1 addition & 1 deletion osh/builtin_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import yajl


_ = log

from typing import Dict, List, Iterator, cast, TYPE_CHECKING
Expand All @@ -35,6 +34,7 @@
from osh.split import SplitContext
from osh.word_eval import NormalWordEvaluator


class _FixedWordsAction(completion.CompletionAction):
def __init__(self, d):
# type: (List[str]) -> None
Expand Down
15 changes: 14 additions & 1 deletion spec/ysh-completion.test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## oils_failures_allowed: 7
## oils_failures_allowed: 8

#### compexport

Expand Down Expand Up @@ -52,6 +52,19 @@ compexport -c 'sq-argv '
## STDOUT:
## END

#### complete -W quoting

. $REPO_ROOT/testdata/completion/quoting.bash

compexport -c 'q2-argv c'
echo

## STDOUT:
"q2-argv can\\'t "
"q2-argv checkout "
"q2-argv cherry "
## END

#### filenames are completed

touch foo bar baz
Expand Down

0 comments on commit ee552f1

Please sign in to comment.