machakann / vim-sandwich

Set of operators and textobjects to search/select/edit sandwiched texts.
1.44k stars 38 forks source link

How to get more assertive code blocks on markdown files? #142

Open hbarcelos opened 2 years ago

hbarcelos commented 2 years ago

I'm trying to figure out how to bend vim-sandwich to my will. I spent a couple of hours in the docs already, but I'm having a hard time figuring this out.

Currently I have:

let g:sandwich#recipes += [
  \ "...
  \   {'buns': ['```', '```'], 'nesting': 0, 'cursor': 'headend', 'linewise': 1, 'kind': ['add', 'replace'], 'action': ['add'], 'input': ['G']},
  \   {'buns': ['```\w*', '```'], 'regex': 1, 'nesting': 0, 'linewise': 1, 'kind': ['delete', 'replace', 'textobj'], 'action': ['delete'], 'input': ['G']},
  \ "...
  \]

What is already working

Adding a code block

this should#be some code

After I press saipG, I get:

` ` `#
this should be some code
` ` `

From there, if I want to specify the syntax for the block code I can go to intert mode with a.

Deleting a code block surrounding

Also if I have:

` ` `javascript
function foo() {
  console.log('foo');#
}
` ` `

After I press sdG:

function foo() {
  console.log('foo');
}

What is not working properly

Replacing inline code with a code block

If I have this inline code:

`() => {#console.log('foo') }`

When I press sr`G, I get:

` ` `#() => {console.log('foo') }` ` `

However I'd like the surrounding replacement to include the required line breaks:

` ` `#
() => {#console.log('foo') }
` ` `

Text object for inner code block

` ` `javascript#
() => {#console.log('foo') }
` ` `

When I press disG, I get:

` ` `javascript#` ` `

However I'd like to keep the line breaks:

` ` `javascript
#
` ` `
hbarcelos commented 2 years ago

I made some progress with Text object for inner code block with this:

  \   {
  \     'buns':       ['```', '```'],
  \     'motionwise': ['line'],
  \     'cursor':     'headend',
  \     'nesting':    0,
  \     'linewise':   1,
  \     'kind':       ['add', 'replace'],
  \     'action':     ['add'],
  \     'filetype':   ['markdown'],
  \     'input':      ['G'],
  \   },
  \   {
  \     'buns':       ['```\w*\s*\n', '\n```'],
  \     'regex':      1,
  \     'nesting':    0,
  \     'linewise':   1,
  \     'kind':       ['delete', 'replace', 'textobj'],
  \     'action':     ['delete'],
  \     'filetype':   ['markdown'],
  \     'input':      ['G'],
  \   },

However because of 'motionwise': ['line'], when I press sr`G, I get:

G() => {#console.log('foo') }G
machakann commented 2 years ago

As for the text object, probably the option skip_break will work.

\ {
\   'buns': ['```\w*', '```'],
\   'regex': 1,
\   'nesting': 0,
\   'linewise': 1,
\   'kind': ['delete', 'replace', 'textobj'],
\   'action': ['delete'],
\   'skip_break': 1,
\   'input': ['G']
\ },

For replacing inline code with a code block operation, it might be a bug. I will check it later.