Loading ...
Sorry, an error occurred while loading the content.

folding ruby comments

Expand Messages
  • josh
    in ruby a line that start with # (can have spaces before) is a comment. I want to be able to toggle between fold all comments and unfold them. It will also be
    Message 1 of 5 , Dec 31, 2011
    • 0 Attachment
      in ruby a line that start with # (can have spaces before) is a
      comment.
      I want to be able to toggle between fold all comments and unfold them.
      It will also be nice to apply this rule only on 3 consecutive lines
      (or more).
      Here is what I came up so far:

      set foldmethod=expr
      set foldexpr=getline(v:lnum)=~'\s*#'

      the problem is it's folding 2 lines whenever there is only 1 comment.

      def self.perform(picture_id)
      #do something that takes time
      puts "doing some job. picture id #{picture_id}"
      sleep(5)
      puts "done!!!"
      end

      any help would be great. thanks!

      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php
    • Taylor Hedberg
      ... If I understand you correctly, Vim is folding the first 2 lines inside the body of the function you gave as an example. This is exactly what your foldexpr
      Message 2 of 5 , Dec 31, 2011
      • 0 Attachment
        josh, Sat 2011-12-31 @ 04:31:03-0800:
        > the problem is it's folding 2 lines whenever there is only 1 comment.
        >
        > def self.perform(picture_id)
        > #do something that takes time
        > puts "doing some job. picture id #{picture_id}"
        > sleep(5)
        > puts "done!!!"
        > end

        If I understand you correctly, Vim is folding the first 2 lines inside
        the body of the function you gave as an example.

        This is exactly what your foldexpr is telling it to do. It says, "Fold
        any line that contains a hash character, optionally preceded by
        whitespace". Both the comment line and the `puts` line meet this
        criterion. It's not folding an extra line after the comment line, it's
        folding 2 separate lines that each individually match the foldexpr you
        gave.

        In the case of the `puts`, the hash does not indicate the start of a
        comment. But Vim doesn't know that, it's just doing what you told it to
        do. You need to make your regex "smarter", so that it will match hashes
        that begin a comment, but not hashes that begin a variable interpolation
        within a string.

        Perhaps something like this would work:

        let &foldexpr = getline(v:lnum) =~ '\s*#[^{]'

        (I used `:let-&` instead of `:set` just because it allows more liberal
        use of unescaped spaces.)

        But the above would still match a string that contained a literal hash
        character that was not part of a variable interpolation, such as

        "My phone # is 555-2345."

        Perhaps you should count the quotation marks to determine whether the
        hash character occurs inside of a string literal to deal with this case.
        I'm not a Ruby programmer, so the precise details of the syntax are a
        little fuzzy to me; I'll leave this and other corner cases as an
        exercise for you to figure out. :)
      • josh
        Here is another attempt: set foldexpr=getline(v:lnum)=~ ^ s*# I want to fold lines that stat with # or lines that start with spaces and #. The problem is it s
        Message 3 of 5 , Jan 1, 2012
        • 0 Attachment
          Here is another attempt:
          set foldexpr=getline(v:lnum)=~'^\s*#'

          I want to fold lines that stat with # or lines that start with spaces
          and #.
          The problem is it's not working on the second case.
          Here is an example. it will only fold line 1-2 and not lines 4-5:

          # this is a comment
          # in ruby. # is the first char

          # this is also a comment.
          # there are spaces before the #

          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php
        • Gary Johnson
          ... I just ran into this myself. Add another backslash in front of the s: set foldexpr=getline(v:lnum)=~ ^ s*# I don t know why. I suspect that the set
          Message 4 of 5 , Jan 1, 2012
          • 0 Attachment
            On 2012-01-01, josh wrote:
            > Here is another attempt:
            > set foldexpr=getline(v:lnum)=~'^\s*#'
            >
            > I want to fold lines that stat with # or lines that start with spaces
            > and #.
            > The problem is it's not working on the second case.
            > Here is an example. it will only fold line 1-2 and not lines 4-5:
            >
            > # this is a comment
            > # in ruby. # is the first char
            >
            > # this is also a comment.
            > # there are spaces before the #

            I just ran into this myself. Add another backslash in front of the
            s:

            set foldexpr=getline(v:lnum)=~'^\\s*#'

            I don't know why. I suspect that the set command removes one level
            of backslash-escaping, even between single-quotes.

            Regards,
            Gary

            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php
          • Taylor Hedberg
            ... It does. Another good reason to use `:let &option = ‥` instead of `:set option=‥` for any non-trivial option value.
            Message 5 of 5 , Jan 2, 2012
            • 0 Attachment
              Gary Johnson, Sun 2012-01-01 @ 18:57:12-0800:
              > I don't know why. I suspect that the set command removes one level of
              > backslash-escaping, even between single-quotes.

              It does. Another good reason to use `:let &option = ‥` instead of
              `:set option=‥` for any non-trivial option value.
            Your message has been successfully submitted and would be delivered to recipients shortly.