Skip to content

Commit

Permalink
Add detection for zsh completion files (#1933)
Browse files Browse the repository at this point in the history
* Add detection for zsh completion files

* Added tag name in shell

* Improve zsh detection with #autoload and #compdef. Add tests

---------

Co-authored-by: German Riano <germanr@amazon.com>
  • Loading branch information
griano and germanriano authored May 15, 2023
1 parent 56a5798 commit 0255a0e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/rouge/lexers/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ class Shell < RegexLexer

tag 'shell'
aliases 'bash', 'zsh', 'ksh', 'sh'
filenames '*.sh', '*.bash', '*.zsh', '*.ksh', '.bashrc', '.zshrc',
'.kshrc', '.profile', 'APKBUILD', 'PKGBUILD', '*.ebuild',
filenames '*.sh', '*.bash', '*.zsh', '*.ksh', '.bashrc',
'.kshrc', '.profile',
'.zshenv', '.zprofile', '.zshrc', '.zlogin', '.zlogout',
'zshenv', 'zprofile', 'zshrc', 'zlogin', 'zlogout',
'APKBUILD', 'PKGBUILD', '*.ebuild',
'*.eclass', '*.exheres-0', '*.exlib'

mimetypes 'application/x-sh', 'application/x-shellscript', 'text/x-sh',
'text/x-shellscript'

def self.detect?(text)
return true if text.shebang?(/(ba|z|k)?sh/)
return true if text.start_with?('#compdef', '#autoload')
end

KEYWORDS = %w(
Expand Down
13 changes: 13 additions & 0 deletions spec/lexers/shell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
assert_guess :filename => 'foo.eclass'
assert_guess :filename => 'foo.exheres-0'
assert_guess :filename => 'foo.exlib'
assert_guess :filename => '.zshenv'
assert_guess :filename => '.zprofile'
assert_guess :filename => '.zshrc'
assert_guess :filename => '.zlogin'
assert_guess :filename => '.zlogout'
assert_guess :filename => 'zshenv'
assert_guess :filename => 'zprofile'
assert_guess :filename => 'zshrc'
assert_guess :filename => 'zlogin'
assert_guess :filename => 'zlogout'
deny_guess :filename => 'foo'
end

Expand All @@ -62,6 +72,9 @@
assert_guess :source => ' #! /bin/bash'
assert_guess :source => '#!/usr/bin/env bash'
assert_guess :source => '#!/usr/bin/env bash -i'
assert_guess :source => '#compdef myfunction1 mufunction2'
assert_guess :source => '#autoload'
assert_guess :source => '#autoload +X'
deny_guess :source => '#!/bin/smash'
# not sure why you would do this, but hey, whatevs
deny_guess :source => '#!/bin/bash/python'
Expand Down

0 comments on commit 0255a0e

Please sign in to comment.