Skip to content
This repository has been archived by the owner on Jun 24, 2023. It is now read-only.

Replace visual block selection #24

Open
rajaravivarma-r opened this issue Aug 4, 2017 · 1 comment
Open

Replace visual block selection #24

rajaravivarma-r opened this issue Aug 4, 2017 · 1 comment

Comments

@rajaravivarma-r
Copy link

Replacing contents in visual block selection mode is not working as expected when returning contents from a function decorated with @snake.visual_key_map.

Example,
.vimrc.py

import snake

def _try_int(text):
    try:
        val = int(text)
        return val
    except ValueError:
        return 0

def format_numbers(numbers):
    max_number = max(numbers)
    no_of_digits_in_max_no = len(str(max_number))
    format_string = '%{}d'.format(no_of_digits_in_max_no)
    return [format_string % no for no in numbers]

@snake.visual_key_map("<leader>fmt")
def align_numbers(selected_snippet):
    selected_numbers = selected_snippet.split('\n')
    numbers = [_try_int(no) for no in selected_numbers]
    return '\n'.join(format_numbers(numbers))

Given file:

Score: 1
Score: 2
Score: 3
Score: 4
Score: 5
Score: 6
Score: 7
Score: 8
Score: 9
Score: 10
Score: 100

After selecting the number block and pressing <leader>fmt
Result:

Score:  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
100 
Score: 
Score: 
Score: 
Score: 
Score: 
Score: 
Score: 
Score: 
Score: 
Score: 

Expected:

Score:   1
Score:   2
Score:   3
Score:   4
Score:   5
Score:   6
Score:   7
Score:   8
Score:   9
Score:  10
Score: 100

Gif for demonstration:
Result:
visual_block_selection_result

Expected:
visual_block_selection_expected

@amoffat
Copy link
Owner

amoffat commented Aug 6, 2017

Thank you for reporting @rajaravivarma-r. I'll take a look at this closer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants