Repository: neovim/neovim
File: runtime/lua/vim/lsp/util.lua (Lua)

Code before:
  --- @return string
  local function match_end(line, match)
    local pattern = matchers[match.type]
    return line:match(string.format('^%%s*%s%%s*$', pattern[3]))
  end

  -- Clean up
  contents = vim.split(table.concat(contents, '\n'), '\n', { trimempty = true })

  local stripped = {} --- @type string[]
  local highlights = {} --- @type {ft:string,start:integer,finish:integer}[]

  local i = 1
  while i <= #contents do
    local line = contents[i]
    local match = match_begin(line)
    if match then
      local start = #stripped
      i = i + 1
      while i <= #contents do
        line = contents[i]
        if match_end(line, match) then
          i = i + 1
          break
        end
        stripped[#stripped + 1] = line
        i = i + 1
      end
      table.insert(highlights, {
        ft = match.ft,
        start = start + 1,
        finish = #stripped,
      })
      -- add a separator, but not on the last line
      if opts.separator and i < #contents then
        stripped[#stripped] = '---'
      end
    else
      -- strip any empty lines or separators prior to this separator in actual markdown
      if line:match('^---+$') then
        while
          stripped[#stripped]
          and (stripped[#stripped]:match('^%s*$') or stripped[#stripped]:match('^---+$'))
        do
          stripped[#stripped] = nil
        end
      end
      -- add the line if its not an empty line following a separator
      if
        not (
          line:match('^%s*$')

Review comment (performance):
For my own learning: Why change this? Is it just personal preference or is `table.insert` slower?

Code after:
  --- @return string
  local function match_end(line, match)
    local pattern = matchers[match.type]
    return line:match(string.format('^%%s*%s%%s*$', pattern[3]))
  end

  -- Clean up
  contents = vim.split(table.concat(contents, '\n'), '\n', { trimempty = true })

  local stripped = {} --- @type string[]
  local highlights = {} --- @type {ft:string,start:integer,finish:integer}[]

  local i = 1
  while i <= #contents do
    local line = contents[i]
    local match = match_begin(line)
    if match then
      local start = #stripped
      i = i + 1
      while i <= #contents do
        line = contents[i]
        if match_end(line, match) then
          i = i + 1
          break
        end
        stripped[#stripped + 1] = line
        i = i + 1
      end
      table.insert(highlights, {
        ft = match.ft,
        start = start + 1,
        finish = #stripped,
      })
      -- add a separator, but not on the last line
      if opts.separator and i < #contents then
        stripped[#stripped + 1] = '---'
      end
    else
      -- strip any empty lines or separators prior to this separator in actual markdown
      if line:match('^---+$') then
        while
          stripped[#stripped]
          and (stripped[#stripped]:match('^%s*$') or stripped[#stripped]:match('^---+$'))
        do
          stripped[#stripped] = nil
        end
      end
      -- add the line if its not an empty line following a separator
      if
        not (line:match('^%s*$') and stripped[#stripped] and stripped[#stripped]:match('^---+$'))
      then