Skip to content

allow for zero-indention of comments #64

Closed

Description

@lorenzwalthert

For breaking code into sections (either manually or with a tool such as strcode), it is desirable that these are not indented. This is not currently supported since all tokens (including comments) will be indented as the current level requires. One way to deal with that would be to set indention of these token to -Inf and using max(0, indention) in serialize_parse_data_nested() although this to be not particularly elegant.

Activity

  1. krlmlr commented on Jul 25, 2017

    @krlmlr
    Member

    Can you give an example where it is relevant not to indent? In DBItest I do indent the roxygen2 comments inside functions.

  2. lorenzwalthert commented on Jul 25, 2017

    @lorenzwalthert
    CollaboratorAuthor

    Mainly for code separators, I want the comments to start on the very first
    column of a line because if I indent them, it creates visual distortion.
    I think it's more natural to not indent code separators.

    Have a look here:

    #' Insert a code break of arbitrary level
    #'
    #'A helper function to insert a code break for a given level
    #' @param level The level, a numeric value bounded between 1 and 3
    #' @param insert_with_shiny A boolean value indicating whether to use
    #'   a shiny gadget to add separator and possibly title. If set to \code{FALSE},
    #'   simply a separator will be inserted and the user has to set the title
    #'   himself. To permanently set
    #'   this argument, you can alter the global option strcode$insert_with_shiny,
    #'   which is the location where \code{insert_break} looks up the value when
    #'   used as an RStudio Add-in.
    #' @keywords internal
    insert_break <- function(level,
                             insert_with_shiny = options()$strcode$insert_with_shiny){
    
    ### .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
    ### elicit title of section (and possible reset level)
    
      if (insert_with_shiny) {
        ret_value <- find_title(level)
        if (ret_value$cancel) return("")
        title <- ret_value$text1
        anchor_in_sep <- ret_value$anchor_in_sep
    
        # set options so anchor_in_sep is remembered
        op <- options()$strcode
        op$anchor_in_sep <- anchor_in_sep
        options(strcode = op)
        level <- as.numeric(unlist(strsplit(ret_value$level, ""))[nchar(ret_value$level)])
    
    
      } else {
        title <- ""
        anchor_in_sep <- FALSE
      }
    ### .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
    ### set parameter depending on level
      start <- paste0(rep("#", level), collapse = "")
      break_char = give_breakchar(level)
      sep = paste(rep(" ", 4 - level), collapse = "")
    
    
    
    #   ____________________________________________________________________________
    
    ### .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
    ### create break sequence to insert
      seq_break <- help_create_break(start = start,
                                     break_char = break_char,
                                     sep = sep, anchor_in_sep = anchor_in_sep)
      help_insert(seq_break,
                  start_row = 1,
                  start_indention = Inf,
                  start_indention_margin = 0,
                  end_row = 2,
                  end_indention = Inf)
    
    ### .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
    ### create title sequence to insert
      if (insert_with_shiny) {
        seq_title <- help_create_title(start = start,
                                       fill = title,
                                       sep = sep,
                                       end = "####")
    
    
        if (!is.null(seq_title)) {
          help_insert(seq_title,
                      start_row = 0,
                      start_indention = Inf,
                      start_indention_margin = 0,
                      end_row = 1,
                      end_indention = Inf)
        }
      }
    }

    What do you think?

  3. krlmlr commented on Jul 25, 2017

    @krlmlr
    Member

    I think we could specify different indention rules based on regexes that apply to the comment strings.

  4. lorenzwalthert commented on Sep 10, 2017

    @lorenzwalthert
    CollaboratorAuthor

    WIP @ branch unindent_regex_comments and pr106_3_regex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions