Get R snippet to accept whitespace in inserted text
Asked Answered
G

1

0

I am trying to write a snippet to allow me to quickly insert comment text using my standard format:

#######################################><###################
##  [date and time goes here] ------------------------------
##  [comment goes here, can span multiple
##  lines]
#######################################><###################

This is what I've got so far:

snippet comm
    `r paste0(
        "#######################################><###################\n##  ", 
        date(), 
        " -------------------------------\n##  ", 
        eval(
            paste0(
                gsub(
                    ".{1,51}\\s?\\K\\b", 
                    "\n##  ", 
                    gsub("\\.", " ", paste0(text)), 
                    perl = T
                )
            )
        ), 
        "###################################><###################\n"
    )`

This snippet works, but requires that the comment text not have any spaces in it. As a work around, I've written the snippet to interpret . as a space.

commlong.comment.text.1111111.aaaaaaa.2222222.bbbbbbb.3333333.ccccccc.4444444.ddddddd.5555555.eeeeeee.6666666.fffffff.7777777.ggggggg.8888888.9999999.0000000

#######################################><###################
##  Tue Jul 24 12:40:55 2018 -------------------------------
##  1111111 aaaaaaa 2222222 bbbbbbb 3333333 ccccccc 
##  4444444 ddddddd 5555555 eeeeeee 6666666 fffffff 
##  7777777 ggggggg 8888888 9999999 0000000
##  ###################################><###################

Since typing comments with periods instead of spaces is a pain, I'd like to modify my snippet to accept comment text with spaces. Any ideas how to do that? Thanks.

Gyneco answered 24/7, 2018 at 17:56 Comment(0)
G
1

Turns out this was a lot easier than anticipated. What I figured out is that anything in the code of a snippet that is not inside ticks ` is interpreted as if it were typed directly into the source pane of RStudio. Anything inside the `r ... ` is evaluated as if it were typed directly into the console pane, but the output is inserted into the current position in the source pane (relative to the existing text or the surrounding snippet). So I wrote this snippet, which does essentially what I want:

snippet commentblock
    ######################################## # # # # # # # # # #  ##  ##  ##  ##  ##
    # `r date()`
    # ${1:title} -------------------------------------------------------
    # ${2:text}
    ######################################## # # # # # # # # # #  ##  ##  ##  ##  ##

I added the --- after the title because I want this line to be interpreted as a section title by the document outline. It may be possible to write a snippet that would count the number of characters in title and insert the exact number of -'s needed to make the --- go all the way to the margin, but figuring that out doesn't seem to be a good use of my time.

Gyneco answered 1/8, 2018 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.