Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann committed Mar 3, 2024
1 parent c667f3b commit 0d93fc9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
19 changes: 4 additions & 15 deletions plugin/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __str__(self) -> str:
return "invalid URI scheme: {}".format(self.uri)


def get_line(window: sublime.Window, file_name: str, row: int) -> str:
def get_line(window: sublime.Window, file_name: str, row: int, strip: bool = True) -> str:
'''
Get the line from the buffer if the view is open, else get line from linecache.
row - is 0 based. If you want to get the first line, you should pass 0.
Expand All @@ -95,23 +95,12 @@ def get_line(window: sublime.Window, file_name: str, row: int) -> str:
if view:
# get from buffer
point = view.text_point(row, 0)
return view.substr(view.line(point)).strip()
line = view.substr(view.line(point))
else:
# get from linecache
# linecache row is not 0 based, so we increment it by 1 to get the correct line.
return linecache.getline(file_name, row + 1).strip()


def get_line2(window: sublime.Window, file_name: str, row: int) -> str:
'''
Same as get_line, but don't strip away leading and trailing whitespace.
'''
view = window.find_open_file(file_name)
if view:
point = view.text_point(row, 0)
return view.substr(view.line(point))
else:
return linecache.getline(file_name, row + 1)
line = linecache.getline(file_name, row + 1)
return line.strip() if strip else line


def get_storage_path() -> str:
Expand Down
5 changes: 3 additions & 2 deletions plugin/rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .core.typing import cast
from .core.url import parse_uri
from .core.views import first_selection_region
from .core.views import get_line2
from .core.views import get_line
from .core.views import range_to_region
from .core.views import text_document_position_params
from functools import partial
Expand Down Expand Up @@ -243,7 +243,8 @@ def _render_rename_panel(
reference_document.append(filename_line)
for edit in changes:
start_row, start_col_utf16 = parse_range(edit['range']['start'])
line_content = get_line2(wm.window, file, start_row) if scheme == 'file' else '<no preview available>'
line_content = get_line(wm.window, file, start_row, strip=False) if scheme == 'file' else \
'<no preview available>'
original_line = ROWCOL_PREFIX.format(start_row + 1, start_col_utf16 + 1, line_content.strip() + "\n")
reference_document.append(original_line)
if scheme == "file" and line_content:
Expand Down

0 comments on commit 0d93fc9

Please sign in to comment.