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

editing Python files -- how to keep track of class membership?

Expand Messages
  • Reckoner
    When editing Python files, I have a hard time keeping track of the class in which I am changes. This is because the class definition is somewhere way up there
    Message 1 of 6 , Jul 24, 2009
    • 0 Attachment
      When editing Python files, I have a hard time keeping track of the
      class in which I am changes. This is because the class definition is
      somewhere way up there in the file that I have scrolled past.

      Anybody have any advice for dealing with this? I am currently just
      splitting the current file into two windows so I can keep track of the
      class definition in one window. It's ugly, but it works.

      I'm sure somebody has done something better, however.

      Any advice appreciated.

      Thanks in advance.
      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Pete Johns
      ... Personally, I find having lots of classes in one source file ugly. But I sympathise, we can t always edit the code we re reading. ... I m not sure about
      Message 2 of 6 , Jul 24, 2009
      • 0 Attachment
        On Jul 25, 7:23 am, Reckoner <recko...@...> wrote:
        > When editing Python files, I have a hard time keeping track of the
        > class in which I am changes. This is because the class definition is
        > somewhere way up there in the file that I have scrolled past.
        >
        > Anybody have any advice for dealing with this? I am currently just
        > splitting the current file into two windows so I can keep track of the
        > class definition in one window. It's ugly, but it works.
        >
        Personally, I find having lots of classes in one source file ugly. But
        I sympathise, we can't always edit the code we're reading.


        > I'm sure somebody has done something better, however.
        >
        I'm not sure about 'better', but the following mapping should help
        you:

        :nmap <Leader>c :?^\s*class? mark c\| echo getline("'c")<cr>

        This will search back to the last class definition, mark it with 'c,
        and echo that line at the bottom of the window without changing your
        current cursor position.


        > Any advice appreciated.
        >
        See the following help for a fuller explanation:

        :he :nmap
        :he <Leader>
        :he :?
        :he mark
        :he echo
        :he getline


        > Thanks in advance.
        >
        Hope this helps;


        --paj
        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Reckoner
        Thanks! Big help! Is there a way to put this on the statusline? thanks again. ... --~--~---------~--~----~------------~-------~--~----~ You received this
        Message 3 of 6 , Jul 27, 2009
        • 0 Attachment
          Thanks! Big help!

          Is there a way to put this on the statusline?

          thanks again.


          On Jul 24, 6:13 pm, Pete Johns <johnsy...@...> wrote:
          > On Jul 25, 7:23 am, Reckoner <recko...@...> wrote:> When editing Python files, I have a hard time keeping track of the
          > > class in which I am changes. This is because the class definition is
          > > somewhere way up there in the file that I have scrolled past.
          >
          > > Anybody have any advice for dealing with this? I am currently just
          > > splitting the current file into two windows so I can keep track of the
          > > class definition in one window. It's ugly, but it works.
          >
          > Personally, I find having lots of classes in one source file ugly. But
          > I sympathise, we can't always edit the code we're reading.
          >
          > > I'm sure somebody has done something better, however.
          >
          > I'm not sure about 'better', but the following mapping should help
          > you:
          >
          > :nmap <Leader>c :?^\s*class? mark c\| echo getline("'c")<cr>
          >
          > This will search back to the last class definition, mark it with 'c,
          > and echo that line at the bottom of the window without changing your
          > current cursor position.
          >
          > > Any advice appreciated.
          >
          > See the following help for a fuller explanation:
          >
          > :he :nmap
          > :he <Leader>
          > :he :?
          > :he mark
          > :he echo
          > :he getline
          >
          > > Thanks in advance.
          >
          > Hope this helps;
          >
          > --paj
          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • John Beckett
          Please bottom post on this list. Quote a small (relevant) part of the message you are replying to, and put your text underneath. See the list guidelines:
          Message 4 of 6 , Jul 27, 2009
          • 0 Attachment
            Please bottom post on this list. Quote a small (relevant) part
            of the message you are replying to, and put your text underneath.

            See the list guidelines:
            http://groups.google.com/group/vim_use/web/vim-information


            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Pete Johns
            ... Excellent. ... Undoubtedly! This is Vim, after all! Of course, this is a bit more complicated as you want to ensure that it only displays a match if you
            Message 5 of 6 , Jul 29, 2009
            • 0 Attachment
              On Jul 27, 11:18 pm, Reckoner <recko...@...> wrote:
              > Thanks! Big help!
              >
              Excellent.


              > Is there a way to put this on the statusline?
              >
              Undoubtedly! This is Vim, after all!

              Of course, this is a bit more complicated as you want to ensure that
              it only displays a match if you are actually within a class. My
              previous method involved some user intelligence. For instance, if you
              are at the top of a Python file, you don't want to search backwards
              and find the last class definition in the file, the user calling the
              mapping would know this. Also if you are in a global function beneath
              a class definition, you wouldn't want that class definition being
              displayed as this would be misleading.

              This sounded like an interesting challenge to me and I'd never played
              with the statusline before so I delved into the help-files and off I
              went. This is what I came up with in a few minutes:

              function! GetLastClass()
              let l:retval = "[No class]"

              let l:last_line_declaring_a_class = search('^\s*class', 'bnW')
              let l:last_line_starting_with_a_word_other_than_class = search('^\
              (\<\)\@=\(class\)\@!', 'bnW')

              if l:last_line_starting_with_a_word_other_than_class <
              l:last_line_declaring_a_class
              let l:retval = getline(l:last_line_declaring_a_class)
              endif

              return l:retval
              endfunction

              setlocal statusline=[%n]\ %{GetLastClass()}\ %<%f%h%m%r%=%b\ 0x%B\ \
              %l,%c%V\ %P

              This is in my .vim/ftplugin/python.vim.

              There are some obvious shortcomings around nested classes but I shall
              leave those as an exercise for the reader. I reckon any Python
              developer can write Vimscript.

              Here are the relevant help pages, which should explain how this little
              lot works.

              :he 'statusline'
              :he /multi " \@= and \@! atoms were not obvious to me
              at first glance
              :he /ordinary-atom "For \< . In fact the whole of pattern.txt
              is essential reading for any Vim user.
              :he :function
              :he :endfunction
              :he :getline
              :he :let
              :he :return
              :he :search
              :he :setlocal
              :he ftplugin
              :he local-variable

              > thanks again.
              >
              You're welcome. I hope this satisfies your requirements. If you solve
              the problems with nested classes, I would very much like to see the
              solution.

              Cheers;

              --paj
              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • Yura Machnovskii
              I think that next plugin can solve almost your problem: http://vim.sourceforge.net/scripts/script.php?script_id=435 It has some known problem but as I see it
              Message 6 of 6 , Aug 2, 2009
              • 0 Attachment
                I think that next plugin can solve almost your problem:

                http://vim.sourceforge.net/scripts/script.php?script_id=435

                It has some known problem but as I see it was updated recently. I didn't try new version.

                Best regards.

                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---

              Your message has been successfully submitted and would be delivered to recipients shortly.