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

Replace visual block selection #24

@rajaravivarma-r

Description

@rajaravivarma-r

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions