From 969c5affa55111f8af95a0696316a0775c2af2c7 Mon Sep 17 00:00:00 2001 From: Tyler Miller Date: Fri, 11 Aug 2023 19:52:37 -0700 Subject: [PATCH] perf(core): simplify and improve `find_line_suffix()` (#1675) Call `string.match()` once instead of calling `string.sub()` and `string.find()` multiple times in a loop. --- lua/cmp/core.lua | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index bdfe7b4..b34c360 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -189,16 +189,14 @@ core.on_moved = function(self) self:filter() end --- Find the suffix for the specified line +---Returns the suffix of the specified `line`. +--- +---Contains `%s`: returns everything after the last `%s` in `line` +---Else: returns `line` unmodified +---@param line string +---@return string suffix local function find_line_suffix(line) - local i = #line - while i > 0 do - if line:sub(i, i):find('%s') then - return line:sub(i + 1) - end - i = i - 1 - end - return line + return line:match('%S*$') --[[@as string]] end ---Check autoindent