From 0d93fc9c4ae34fd2b72f96afa9960cfd43b6d746 Mon Sep 17 00:00:00 2001 From: Janos Wortmann Date: Sun, 3 Mar 2024 13:11:46 +0100 Subject: [PATCH] Simplify --- plugin/core/views.py | 19 ++++--------------- plugin/rename.py | 5 +++-- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/plugin/core/views.py b/plugin/core/views.py index 41a7b5a31..327e1bb26 100644 --- a/plugin/core/views.py +++ b/plugin/core/views.py @@ -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. @@ -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: diff --git a/plugin/rename.py b/plugin/rename.py index 78867d208..d50f9d262 100644 --- a/plugin/rename.py +++ b/plugin/rename.py @@ -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 @@ -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 '' + line_content = get_line(wm.window, file, start_row, strip=False) if scheme == 'file' else \ + '' 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: