From a3ae15ed63ce60c8ea6398ceff027d37c7054e68 Mon Sep 17 00:00:00 2001 From: Sergey Fukanchik Date: Thu, 13 Mar 2025 06:29:09 +0300 Subject: [PATCH] Do not call str.splitlines() twice in the same function. fixes #627 --- pylsp/workspace.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pylsp/workspace.py b/pylsp/workspace.py index 846527f6..005c177d 100644 --- a/pylsp/workspace.py +++ b/pylsp/workspace.py @@ -460,7 +460,8 @@ def apply_change(self, change): end_col = change_range["end"]["character"] # Check for an edit occuring at the very end of the file - if start_line == len(self.lines): + lines = self.lines + if start_line == len(lines): self._source = self.source + text return @@ -469,7 +470,7 @@ def apply_change(self, change): # Iterate over the existing document until we hit the edit range, # at which point we write the new text, then loop until we hit # the end of the range and continue writing. - for i, line in enumerate(self.lines): + for i, line in enumerate(lines): if i < start_line: new.write(line) continue @@ -493,10 +494,11 @@ def offset_at_position(self, position): def word_at_position(self, position): """Get the word under the cursor returning the start and end positions.""" - if position["line"] >= len(self.lines): + lines = self.lines + if position["line"] >= len(lines): return "" - line = self.lines[position["line"]] + line = lines[position["line"]] i = position["character"] # Split word in two start = line[:i]