8000 Merge pull request #7 from mcepl/PR7-theospears-add-location · python-rope/ropemode@ed5b72e · GitHub
[go: up one dir, main page]

Skip to content

Commit ed5b72e

Browse files
committed
Merge pull request #7 from mcepl/PR7-theospears-add-location
Add Location.line_content property to provide context for locations
2 parents 6ba1153 + bc4fc73 commit ed5b72e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

ropemode/interface.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,18 @@ def lineno(self):
552552
return self.location.lineno
553553
return self.location.resource.read().count('\n', 0, self.offset) + 1
554554

555+
@property
556+
def line_content(self):
557+
resource_contents = self.location.resource.read()
558+
# rfind returns -1 for start of string, so by adding 1 we get the start
559+
# of the string. When a match is found we want to exclude the matching
560+
# character so again we add 1.
561+
line_start = resource_contents.rfind("\n", 0, self.offset) + 1
562+
line_end = resource_contents.find("\n", self.offset)
563+
if line_end < 0:
564+
line_end = len(resource_contents)
565+
return resource_contents[line_start:line_end]
566+
555567

556568
class _CodeAssist(object):
557569

0 commit comments

Comments
 (0)
0