JulianEberius / SublimeRope

ST2 only, use SublimePythonIDE with ST3: Adds Python completions and some IDE-like functions to Sublime Text 2, through the use of the Rope library
GNU General Public License v2.0
250 stars 26 forks source link

Autoimport adds import below class definition if it has an import right above it #61

Closed jasonmyers closed 11 years ago

jasonmyers commented 11 years ago

For example, after auto-import

import ...

import logging
class Foo(object):
      bar = datetime.datetime.utcnow()

becomes

import ...

import logging
class Foo(object):
import datetime
      bar = datetime.datetime.utcnow()

Due to the offset + 1 in autoimport.py. Removed the +1 and changed it to a '\n' before the import statement, so that import datetime moves above class Foo(object)

Also fixed a regex bug that wasn't finding the first class/def since re.MULTILINE was not specified (it was always processing the whole file)

JulianEberius commented 11 years ago

Hi again,

I looked closer into the line numbers Rope returns, and they are in fact 1-based. So instead of changing two places in the library we use (this pull request, and the one I just merged), its much better to add a simple "-1" in our own codebase, to compensate for the different indexing or Rope and ST2.

About the other issues: You're right about the regular expression and re.M, I will introduce this, and it should be reported the the Rope maintainers, since it's a real bug. Finally: why did you add the leading "\n" to the insert_import_str?

jasonmyers commented 11 years ago

I believe I added the \n to maintain the '1 space between imports' that the +1 was causing.

So added imports come out as

import old

import new

rather than

import old
import new

I think it can be removed if you don't care about the spacing between imports