Get R snippet to accept whitespace in inserted text

◇◆丶佛笑我妖孽 提交于 2019-12-11 23:44:08

问题


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.


回答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.



来源:https://stackoverflow.com/questions/51504879/get-r-snippet-to-accept-whitespace-in-inserted-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!