kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

ruby.kak (10446B)


      1 # http://ruby-lang.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*(([.](rb))|(irbrc)|(pryrc)|(Brewfile)|(Capfile|[.]cap)|(Gemfile|[.]gemspec)|(Guardfile)|(Rakefile|[.]rake)|(Thorfile|[.]thor)|(Vagrantfile)) %{
      8     set-option buffer filetype ruby
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=ruby %{
     15     require-module ruby
     16 
     17     set-option window static_words %opt{ruby_static_words}
     18 
     19     hook window ModeChange pop:insert:.* -group ruby-trim-indent ruby-trim-indent
     20     hook window InsertChar .* -group ruby-indent ruby-indent-on-char
     21     hook window InsertChar \n -group ruby-indent ruby-indent-on-new-line
     22     hook window InsertChar \n -group ruby-insert ruby-insert-on-new-line
     23 
     24     alias window alt ruby-alternative-file
     25 
     26     hook -once -always window WinSetOption filetype=.* %{
     27         remove-hooks window ruby-.+
     28         unalias window alt ruby-alternative-file
     29     }
     30 }
     31 
     32 hook -group ruby-highlight global WinSetOption filetype=ruby %{
     33     add-highlighter window/ruby ref ruby
     34     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/ruby }
     35 }
     36 
     37 provide-module ruby %§
     38 
     39 # Highlighters
     40 # ‾‾‾‾‾‾‾‾‾‾‾‾
     41 
     42 add-highlighter shared/ruby regions
     43 add-highlighter shared/ruby/code default-region group
     44 add-highlighter shared/ruby/double_symbol region ':"' (?<!\\)(\\\\)*"                regions
     45 add-highlighter shared/ruby/single_symbol region ":'" (?<!\\)(\\\\)*'                fill variable
     46 add-highlighter shared/ruby/double_string region '"' (?<!\\)(\\\\)*"                 regions
     47 add-highlighter shared/ruby/single_string region "'" (?<!\\)(\\\\)*'                 fill string
     48 add-highlighter shared/ruby/backtick      region '(?<![$:])`' (?<!\\)(\\\\)*`        regions
     49 add-highlighter shared/ruby/regex         region '(?<![$:])/' (?<!\\)(\\\\)*/[imox]* regions
     50 add-highlighter shared/ruby/              region '#' '$'                             fill comment
     51 add-highlighter shared/ruby/              region ^=begin ^=end                       fill comment
     52 add-highlighter shared/ruby/              region -recurse \( '%[qwQW]?\(' \)         fill string
     53 add-highlighter shared/ruby/              region -recurse \{ '%[qwQW]?\{' \}         fill string
     54 add-highlighter shared/ruby/              region -recurse \[ '%[qwQW]?\[' \]         fill string
     55 add-highlighter shared/ruby/              region -recurse  < '%[qwQW]?<'   >         fill string
     56 add-highlighter shared/ruby/              region -recurse \( '%[isIS]\('  \)         fill variable
     57 add-highlighter shared/ruby/              region -recurse \{ '%[isIS]\{'  \}         fill variable
     58 add-highlighter shared/ruby/              region -recurse \[ '%[isIS]\['  \]         fill variable
     59 add-highlighter shared/ruby/              region -recurse  < '%[isIS]<'    >         fill variable
     60 add-highlighter shared/ruby/              region -recurse \( '%[rxRX]\('  \)         fill meta
     61 add-highlighter shared/ruby/              region -recurse \{ '%[rxRX]\{'  \}         fill meta
     62 add-highlighter shared/ruby/              region -recurse \[ '%[rxRX]\['  \]         fill meta
     63 add-highlighter shared/ruby/              region -recurse  < '%[rxRX]<'    >         fill meta
     64 add-highlighter shared/ruby/              region -match-capture '%[qwQW]?([^\s0-9A-Za-z\(\{\[<>\]\}\)])' ([^\s0-9A-Za-z\(\{\[<>\]\}\)]) fill string
     65 add-highlighter shared/ruby/              region -match-capture '%[isIS]([^\s0-9A-Za-z\(\{\[<>\]\}\)])' ([^\s0-9A-Za-z\(\{\[<>\]\}\)]) fill variable
     66 add-highlighter shared/ruby/              region -match-capture '%[rxRX]([^\s0-9A-Za-z\(\{\[<>\]\}\)])' ([^\s0-9A-Za-z\(\{\[<>\]\}\)]) fill meta
     67 add-highlighter shared/ruby/heredoc region -match-capture '<<[-~]?(?!self)(\w+)'      '^\h*(\w+)$' fill string
     68 add-highlighter shared/ruby/division region '[\w\)\]]\K(/|(\h+/\h+))' '\w' group # Help Kakoune to better detect /…/ literals
     69 
     70 # Regular expression flags are: i → ignore case, m → multi-lines, o → only interpolate #{} blocks once, x → extended mode (ignore white spaces)
     71 # Literals are: i → array of symbols, q → string, r → regular expression, s → symbol, w → array of words, x → capture shell result
     72 
     73 add-highlighter shared/ruby/double_string/ default-region fill string
     74 add-highlighter shared/ruby/double_string/interpolation region -recurse \{ \Q#{ \} fill meta
     75 
     76 add-highlighter shared/ruby/double_symbol/ default-region fill variable
     77 add-highlighter shared/ruby/double_symbol/interpolation region -recurse \{ \Q#{ \} fill meta
     78 
     79 add-highlighter shared/ruby/backtick/ default-region fill meta
     80 add-highlighter shared/ruby/backtick/interpolation region -recurse \{ \Q#{ \} fill meta
     81 
     82 add-highlighter shared/ruby/regex/ default-region fill meta
     83 add-highlighter shared/ruby/regex/interpolation region -recurse \{ \Q#{ \} fill meta
     84 
     85 evaluate-commands %sh{
     86     # Grammar
     87     # Keywords are collected searching for keywords at
     88     # https://github.com/ruby/ruby/blob/trunk/parse.y
     89     keywords="alias|and|begin|break|case|class|def|defined|do|else|elsif|end"
     90     keywords="${keywords}|ensure|false|for|if|in|module|next|nil|not|or|private|protected|public|redo"
     91     keywords="${keywords}|rescue|retry|return|self|super|then|true|undef|unless|until|when|while|yield"
     92     attributes="attr_reader|attr_writer|attr_accessor"
     93     values="false|true|nil"
     94     meta="require|require_relative|include|extend"
     95 
     96     # Add the language's grammar to the static completion list
     97     printf %s\\n "declare-option str-list ruby_static_words ${keywords} ${attributes} ${values} ${meta}" | tr '|' ' '
     98 
     99     # Highlight keywords
    100     printf %s "
    101         add-highlighter shared/ruby/code/ regex \b(${keywords})[^0-9A-Za-z_!?] 1:keyword
    102         add-highlighter shared/ruby/code/ regex \b(${attributes})\b 0:attribute
    103         add-highlighter shared/ruby/code/ regex \b(${values})\b 0:value
    104         add-highlighter shared/ruby/code/ regex \b(${meta})\b 0:meta
    105     "
    106 }
    107 
    108 add-highlighter shared/ruby/code/ regex \b(\w+:(?!:))|(:?(\$(-[0FIKWadilpvw]|["'`/~&+=!$*,:.\;<>?@\\])|(\$|@@?)\w+))|((?<!:):(![~=]|=~|>[=>]?|<((=>?)|<)?|[+\-]@?|\*\*?|===?|[/`%&!^|~]|(\w+[=?!]?)|(\[\]=?)))|([A-Z]\w*|^|\h)\K::(?=[A-Z]) 0:variable
    109 
    110 # Commands
    111 # ‾‾‾‾‾‾‾‾
    112 
    113 define-command ruby-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ evaluate-commands %sh{
    114     case "$kak_buffile" in
    115         *spec/*_spec.rb)
    116             altfile=$(find "$(dirname "$kak_buffile" | sed s,spec$,,g)"*/ -name "$(basename "$kak_buffile" | sed s/_spec//)" | head -n1)
    117             [ ! -f $altfile ] && echo "fail 'implementation file not found'" && exit
    118         ;;
    119         *test/*_test.rb)
    120             altfile=$(find "$(dirname "$kak_buffile" | sed s,test$,,g)"*/ -name "$(basename "$kak_buffile" | sed s/_test//)" | head -n1)
    121             [ ! -f $altfile ] && echo "fail 'implementation file not found'" && exit
    122         ;;
    123         *.rb)
    124             altfile=""
    125             altdir=""
    126             path="$kak_buffile"
    127             dirs=$(while [ $path ]; do echo $path; path=${path%/*}; done | tail -n +2)
    128             for dir in $dirs; do
    129                 altdir=$dir/spec && suffix=spec
    130                 [ ! -d $altdir ] && altdir=$dir/test && suffix=test
    131                 if [ -d $altdir ]; then
    132                     altfile=$altdir/$(realpath "$kak_buffile" --relative-to $dir | sed s+[^/]'*'/++';'s/.rb$/_${suffix}.rb/)
    133                     break
    134                 fi
    135             done
    136             [ ! -d "$altdir" ] && echo "fail 'spec/ and test/ not found'" && exit
    137         ;;
    138         *)
    139             echo "fail 'alternative file not found'" && exit
    140         ;;
    141     esac
    142     echo "edit $altfile"
    143 }}
    144 
    145 define-command -hidden ruby-trim-indent %{
    146     evaluate-commands -no-hooks -draft -itersel %{
    147         execute-keys x
    148         # remove trailing white spaces
    149         try %{ execute-keys -draft s \h + $ <ret> d }
    150     }
    151 }
    152 
    153 define-command -hidden ruby-indent-on-char %{
    154     evaluate-commands -no-hooks -draft -itersel %{
    155         # align middle and end structures to start
    156         try %{ execute-keys -draft x <a-k> ^ \h * (else)   $ <ret> <a-a> i <a-semicolon> <a-?> ^ \h * (if|case)                                               <ret> <a-S> 1<a-&> }
    157         try %{ execute-keys -draft x <a-k> ^ \h * (elsif)  $ <ret> <a-a> i <a-semicolon> <a-?> ^ \h * (if)                                                    <ret> <a-S> 1<a-&> }
    158         try %{ execute-keys -draft x <a-k> ^ \h * (when)   $ <ret> <a-a> i <a-semicolon> <a-?> ^ \h * (case)                                                  <ret> <a-S> 1<a-&> }
    159         try %{ execute-keys -draft x <a-k> ^ \h * (rescue) $ <ret> <a-a> i <a-semicolon> <a-?> ^ \h * (begin|def)                                             <ret> <a-S> 1<a-&> }
    160     }
    161 }
    162 
    163 define-command -hidden ruby-indent-on-new-line %{
    164     evaluate-commands -no-hooks -draft -itersel %{
    165         # preserve previous line indent
    166         try %{ execute-keys -draft K <a-&> }
    167         # filter previous line
    168         try %{ execute-keys -draft k : ruby-trim-indent <ret> }
    169         # indent after start structure
    170         try %{ execute-keys -draft k x <a-k> ^ \h * (begin|case|class|def|else|elsif|ensure|for|if|module|rescue|unless|until|when|while|.+\bdo$|.+\bdo\h\|.+(?=\|)) [^0-9A-Za-z_!?] <ret> j <a-gt> }
    171     }
    172 }
    173 
    174 define-command -hidden ruby-insert-on-new-line %[
    175     evaluate-commands -no-hooks -draft -itersel %[
    176         # copy _#_ comment prefix and following white spaces
    177         try %{ execute-keys -draft k x s ^\h*\K#\h* <ret> y jgi P }
    178         # wisely add end structure
    179         evaluate-commands -save-regs x %[
    180             try %{ execute-keys -draft k x s ^ \h + <ret> \" x y } catch %{ reg x '' }
    181             try %[
    182                 evaluate-commands -draft %[
    183                     # Check if previous line opens a block
    184                     execute-keys -draft kx <a-k>^<c-r>x(begin|case|class|def|for|if|module|unless|until|while|.+\bdo$|.+\bdo\h\|.+(?=\|))[^0-9A-Za-z_!?]<ret>
    185                     # Check that we do not already have an end for this indent level which is first set via `ruby-indent-on-new-line` hook
    186                     execute-keys -draft }i J x <a-K> ^<c-r>x(end|else|elsif|rescue|when)[^0-9A-Za-z_!?]<ret>
    187                 ]
    188                 execute-keys -draft o<c-r>xend<esc> # insert a new line with containing end
    189             ]
    190         ]
    191     ]
    192 ]
    193 
    194 §