Fix completions for fish

This commit is contained in:
Kovid Goyal 2021-07-17 08:00:23 +05:30
parent 20688661aa
commit a10c19456a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 50 additions and 10 deletions

View File

@ -161,6 +161,14 @@ end
complete -f -c kitty -a "(__kitty_completions)" complete -f -c kitty -a "(__kitty_completions)"
''', ''',
'fish2': '''
if functions -q _ksi_completions
complete -f -c kitty -a "(_ksi_completions)"
else
complete -f -c kitty -a "(commandline -cop | kitty +complete fish)"
end
''',
} }
ParseResult = Tuple[List[str], bool] ParseResult = Tuple[List[str], bool]
@ -199,6 +207,11 @@ def fish_input_parser(data: str) -> ParseResult:
return data.rstrip().splitlines(), True return data.rstrip().splitlines(), True
@input_parser
def fish2_input_parser(data: str) -> ParseResult:
return bash_input_parser(data)
@output_serializer @output_serializer
def zsh_output_serializer(ans: Completions) -> str: def zsh_output_serializer(ans: Completions) -> str:
lines = [] lines = []
@ -288,6 +301,17 @@ def fish_output_serializer(ans: Completions) -> str:
lines.append(word.replace('\n', ' ')) lines.append(word.replace('\n', ' '))
# debug('\n'.join(lines)) # debug('\n'.join(lines))
return '\n'.join(lines) return '\n'.join(lines)
@output_serializer
def fish2_output_serializer(ans: Completions) -> str:
lines = []
for description, matches in ans.match_groups.items():
for word in matches:
lines.append(word.replace('\n', ' '))
# debug('\n'.join(lines))
return '\n'.join(lines)
# }}} # }}}

View File

@ -6,6 +6,7 @@
import os import os
import shutil import shutil
import time import time
from contextlib import suppress
from tempfile import mkstemp from tempfile import mkstemp
from typing import Optional, Union from typing import Optional, Union
@ -27,7 +28,8 @@ def atomic_write(path: str, data: Union[str, bytes]) -> None:
os.makedirs(base, exist_ok=True) os.makedirs(base, exist_ok=True)
fd, tpath = mkstemp(dir=base, text=isinstance(data, str)) fd, tpath = mkstemp(dir=base, text=isinstance(data, str))
with open(fd, mode) as f: with open(fd, mode) as f:
shutil.copystat(path, tpath) with suppress(FileNotFoundError):
shutil.copystat(path, tpath)
f.write(data) f.write(data)
try: try:
os.rename(tpath, path) os.rename(tpath, path)
@ -36,16 +38,17 @@ def atomic_write(path: str, data: Union[str, bytes]) -> None:
raise raise
def safe_read(path: str) -> str:
with suppress(FileNotFoundError):
with open(path) as f:
return f.read()
return ''
def setup_integration(shell_name: str, rc_path: str, template: str = posix_template) -> None: def setup_integration(shell_name: str, rc_path: str, template: str = posix_template) -> None:
import re import re
rc_path = os.path.realpath(rc_path) rc_path = os.path.realpath(rc_path)
try: rc = safe_read(rc_path)
with open(rc_path) as f:
rc = f.read()
except FileNotFoundError:
rc = ''
except Exception:
raise
home = os.path.expanduser('~') + '/' home = os.path.expanduser('~') + '/'
path = os.path.join(shell_integration_dir, f'kitty.{shell_name}') path = os.path.join(shell_integration_dir, f'kitty.{shell_name}')
if path.startswith(home): if path.startswith(home):
@ -83,9 +86,14 @@ def atomic_symlink(destination: str, in_directory: str) -> str:
def setup_fish_integration() -> None: def setup_fish_integration() -> None:
base = os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config')) base = os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
base = os.path.join(base, 'fish', 'conf.d') base = os.path.join(base, 'fish')
path = os.path.join(shell_integration_dir, 'kitty.fish') path = os.path.join(shell_integration_dir, 'kitty.fish')
atomic_symlink(path, base) atomic_symlink(path, os.path.join(base, 'conf.d'))
from .complete import completion_scripts
path = os.path.join(base, 'completions', 'kitty.fish')
rc = safe_read(path)
if rc != completion_scripts['fish2']:
atomic_write(path, completion_scripts['fish2'])
SUPPORTED_SHELLS = { SUPPORTED_SHELLS = {

View File

@ -9,6 +9,14 @@ function _ksi_main
printf "\e]%s\a" "$argv[1]" printf "\e]%s\a" "$argv[1]"
end end
if not contains "no-complete" $_ksi
function _ksi_completions
set --local ct (commandline --current-token)
set --local tokens (commandline --tokenize --cut-at-cursor --current-process)
printf "%s\n" $tokens $ct | kitty +complete fish2
end
end
if not contains "no-cursor" $_ksi if not contains "no-cursor" $_ksi
function _ksi_bar_cursor --on-event fish_prompt function _ksi_bar_cursor --on-event fish_prompt
printf "\e[5 q" printf "\e[5 q"