terryma / vim-expand-region

Vim plugin that allows you to visually select increasingly larger regions of text using the same key combination.
MIT License
1.1k stars 46 forks source link

Allow per-filetype configuration #1

Closed kljohann closed 11 years ago

kljohann commented 11 years ago

Hey, thanks for porting this over to vim. I have been using expand-region.el with evil and have been waiting for someone to do a vim port. I particularly like how you reused text objects! Sometimes it would be nice to have per-filetype configuration (f.ex. indentation based text objects in python, ap instead of ip in clojure/lisp). I see two approaches: • Allow the configuraion to be shadowed by a buffer-local variable (that may be set in an autocmd FileType …) • Implement some white-/blacklisting/dictionary magic I think I prefer the first approach. If you tell me your opinion I will work on a patch.

terryma commented 11 years ago

Thanks! Per filetype configuration is definitely a feature that's needed. I prefer the user configuration to be as lean as possible. I think the following makes sense:

  1. Give user the ability to specify a global default dictionary that applies to every filetype (This is already there)
  2. Give user the ability to extend the global default (This is already there)
  3. Give user the ability to extend the runtime textobject dict on a per filetype basis

I think for 3, we could just add another global method expand_region#custom_text_objects(filetype, dict). This way, the user could easily extend it for ruby for example by calling the following in their vimrc

call expand_region#custom_text_objects('ruby', { ... })

kljohann commented 11 years ago

I think in many cases it would be preferable for per-filetype configuration to overwrite global default configuration instead of extending it (same example as above: use ab instead of ib for lisp). See the attached pull request for what I had in mind. It now tries to read the configuration from g:expand_region_text_objects_ruby etc. and falls back to use g:expand_region_text_objects as a default if no per-filetype configuration was found.

A possible definition of a custom_text_objects like you described above could then be:

function! expand_region#custom_text_objects(...)
  if a:0 == 1
    call extend(g:expand_region_text_objects, a:1)
  elseif a:0 == 2
    if !exists("g:expand_region_text_objects_".a:1)
      let g:expand_region_text_objects_{a:1} = {}
      call extend(g:expand_region_text_objects_{a:1}, g:expand_region_text_objects)
    endif
    call extend(g:expand_region_text_objects_{a:1}, a:2)
  endif
endfunction
terryma commented 11 years ago

Thanks for adding this! I like your idea of modifying the custom_text_objects method as well. The combination of the two give users enough flexibility to define per filetype config however they want. Could you make the other change as well and perhaps update the documentation?

terryma commented 11 years ago

I made the change and updated the doc, thanks for the suggestion!