robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Notes on incremental sync: 2-- Per the protocol, the text range should be: 3-- 4-- A position inside a document (see Position definition below) is expressed as 5-- a zero-based line and character offset. The offsets are based on a UTF-16 6-- string representation. So a string of the form a𐐀b the character offset 7-- of the character a is 0, the character offset of 𐐀 is 1 and the character 8-- offset of b is 3 since 𐐀 is represented using two code units in UTF-16. 9-- 10-- To ensure that both client and server split the string into the same line 11-- representation the protocol specifies the following end-of-line sequences: ‘\n’, ‘\r\n’ and ‘\r’. 12-- 13-- Positions are line end character agnostic. So you can not specify a position that 14-- denotes \r|\n or \n| where | represents the character offset. This means *no* defining 15-- a range than ends on the same line after a terminating character 16-- 17-- Generic warnings about byte level changes in neovim. Many apparently "single" 18-- operations in on_lines callbacks are actually multiple operations. 19-- 20-- Join operation (2 operations): 21-- * extends line 1 with the contents of line 2 22-- * deletes line 2 23-- 24-- test 1 test 1 test 2 test 1 test 2 25-- test 2 -> test 2 -> test 3 26-- test 3 test 3 27-- 28-- Deleting (and undoing) two middle lines (1 operation): 29-- 30-- test 1 test 1 31-- test 2 -> test 4 32-- test 3 33-- test 4 34-- 35-- Deleting partial lines (5 operations) deleting between asterisks below: 36-- 37-- test *1 test * test * test * test *4 test *4* 38-- test 2 -> test 2 -> test *4 -> *4 -> *4 -> 39-- test 3 test 3 40-- test *4 test 4 41 42local M ={} 43 44-- local string.byte, unclear if this is necessary for JIT compilation 45local str_byte =string . byte 46local min =math . min 47local str_utfindex =vim . str_utfindex 48local str_utf_start =vim . str_utf_start 49local str_utf_end =vim . str_utf_end 50 51-- Given a line, byte idx, alignment, and position_encoding convert to the aligned 52-- utf-8 index and either the utf-16, or utf-32 index. 53---@param line string the line to index into 54---@param byte integer the byte idx 55---@param position_encoding 'utf-8'|'utf-16'|'utf-32'? (default: utf-8) 56---@return integer byte_idx of first change position 57---@return integer char_idx of first change position 58local function align_end_position ( line , byte , position_encoding ) 59position_encoding =position_encoding or 'utf-8' 60local char --- @type integer 61-- If on the first byte, or an empty string: the trivial case 62if byte == 1 or # line == 0 then 63char =byte 64-- Called in the case of extending an empty line "" -> "a" 65elseif byte == # line + 1 then 66char =str_utfindex ( line , position_encoding ) + 1 67else 68-- Modifying line, find the nearest utf codepoint 69local offset =str_utf_start ( line , byte ) 70-- If the byte does not fall on the start of the character, then 71-- align to the start of the next character. 72if offset < 0 then 73byte =byte + str_utf_end ( line , byte ) + 1 74end 75if byte <= # line then 76--- Convert to 0 based for input, and from 0 based for output 77char =str_utfindex ( line , position_encoding , byte - 1 ) + 1 78else 79char =str_utfindex ( line , position_encoding ) + 1 80end 81-- Extending line, find the nearest utf codepoint for the last valid character 82end 83return byte , char 84end 85 86---@class vim.lsp.sync.Range 87---@field line_idx integer 88---@field byte_idx integer 89---@field char_idx integer 90 91--- Finds the first line, byte, and char index of the difference between the previous and current lines buffer normalized to the previous codepoint. 92---@param prev_lines string[] list of lines from previous buffer 93---@param curr_lines string[] list of lines from current buffer 94---@param firstline integer firstline from on_lines, adjusted to 1-index 95---@param lastline integer lastline from on_lines, adjusted to 1-index 96---@param new_lastline integer new_lastline from on_lines, adjusted to 1-index 97---@param position_encoding 'utf-8'|'utf-16'|'utf-32'? (default: utf-8) 98---@return vim.lsp.sync.Range result table include line_idx, byte_idx, and char_idx of first change position 99local function compute_start_range ( 100prev_lines , 101curr_lines , 102firstline , 103lastline , 104new_lastline , 105position_encoding 106) 107position_encoding =position_encoding or 'utf-8' 108 109local char_idx --- @type integer 110local byte_idx --- @type integer 111-- If firstline == lastline, no existing text is changed. All edit operations 112-- occur on a new line pointed to by lastline. This occurs during insertion of 113-- new lines(O), the new newline is inserted at the line indicated by 114-- new_lastline. 115if firstline == lastline then 116local line_idx --- @type integer 117local line =prev_lines [ firstline - 1 ] 118if line then 119line_idx =firstline - 1 120byte_idx =# line + 1 121char_idx =str_utfindex ( line , position_encoding ) + 1 122else 123line_idx =firstline 124byte_idx =1 125char_idx =1 126end 127return { line_idx =line_idx , byte_idx =byte_idx , char_idx =char_idx } 128end 129 130-- If firstline == new_lastline, the first change occurred on a line that was deleted. 131-- In this case, the first byte change is also at the first byte of firstline 132if firstline == new_lastline then 133return { line_idx =firstline , byte_idx =1 , char_idx =1 } 134end 135 136local prev_line =assert ( prev_lines [ firstline ]) 137local curr_line =assert ( curr_lines [ firstline ]) 138 139-- Iterate across previous and current line containing first change 140-- to find the first different byte. 141-- Note: *about -> a*about will register the second a as the first 142-- difference, regardless of edit since we do not receive the first 143-- column of the edit from on_lines. 144local start_byte_idx =1 145for idx =1 , # prev_line + 1 do 146start_byte_idx =idx 147if str_byte ( prev_line , idx ) ~= str_byte ( curr_line , idx ) then 148break 149end 150end 151 152-- Convert byte to codepoint if applicable 153if start_byte_idx == 1 or ( # prev_line == 0 and start_byte_idx == 1 ) then 154byte_idx =start_byte_idx 155char_idx =1 156elseif start_byte_idx == # prev_line + 1 then 157byte_idx =start_byte_idx 158char_idx =str_utfindex ( prev_line , position_encoding ) + 1 159else 160byte_idx =start_byte_idx + str_utf_start ( prev_line , start_byte_idx ) 161--- Convert to 0 based for input, and from 0 based for output 162char_idx =str_utfindex ( prev_line , position_encoding , byte_idx - 1 ) + 1 163end 164 165-- Return the start difference (shared for new and prev lines) 166return { line_idx =firstline , byte_idx =byte_idx , char_idx =char_idx } 167end 168 169--- Finds the last line and byte index of the differences between prev and current buffer. 170--- Normalized to the next codepoint. 171--- prev_end_range is the text range sent to the server representing the changed region. 172--- curr_end_range is the text that should be collected and sent to the server. 173--- 174---@param prev_lines string[] list of lines 175---@param curr_lines string[] list of lines 176---@param start_range vim.lsp.sync.Range 177---@param firstline integer 178---@param lastline integer 179---@param new_lastline integer 180---@param position_encoding 'utf-8'|'utf-16'|'utf-32' 181---@return vim.lsp.sync.Range prev_end_range 182---@return vim.lsp.sync.Range curr_end_range 183local function compute_end_range ( 184prev_lines , 185curr_lines , 186start_range , 187firstline , 188lastline , 189new_lastline , 190position_encoding 191) 192-- A special case for the following `firstline == new_lastline` case where lines are deleted. 193-- Even if the buffer has become empty, nvim behaves as if it has an empty line with eol. 194if # curr_lines == 1 and curr_lines [ 1 ] == '' then 195local prev_line =assert ( prev_lines [ lastline - 1 ]) 196return { 197line_idx =lastline - 1 , 198byte_idx =# prev_line + 1 , 199char_idx =str_utfindex ( prev_line , position_encoding ) + 1 , 200} , { line_idx =1 , byte_idx =1 , char_idx =1 } 201end 202-- If firstline == new_lastline, the first change occurred on a line that was deleted. 203-- In this case, the last_byte... 204if firstline == new_lastline then 205return { line_idx =( lastline - new_lastline + firstline ), byte_idx =1 , char_idx =1 } , { 206line_idx =firstline , 207byte_idx =1 , 208char_idx =1 , 209} 210end 211if firstline == lastline then 212return { line_idx =firstline , byte_idx =1 , char_idx =1 } , { 213line_idx =new_lastline - lastline + firstline , 214byte_idx =1 , 215char_idx =1 , 216} 217end 218-- Compare on last line, at minimum will be the start range 219local start_line_idx =start_range . line_idx 220 221-- lastline and new_lastline were last lines that were *not* replaced, compare previous lines 222local prev_line_idx =lastline - 1 223local curr_line_idx =new_lastline - 1 224 225local prev_line =assert ( prev_lines [ lastline - 1 ]) 226local curr_line =assert ( curr_lines [ new_lastline - 1 ]) 227 228local prev_line_length =# prev_line 229local curr_line_length =# curr_line 230 231local byte_offset =0 232 233-- Editing the same line 234-- If the byte offset is zero, that means there is a difference on the last byte (not newline) 235if prev_line_idx == curr_line_idx then 236local max_length --- @type integer 237if start_line_idx == prev_line_idx then 238-- Search until beginning of difference 239max_length =min ( 240prev_line_length - start_range . byte_idx , 241curr_line_length - start_range . byte_idx 242) + 1 243else 244max_length =min ( prev_line_length , curr_line_length ) + 1 245end 246for idx =0 , max_length do 247byte_offset =idx 248if 249str_byte ( prev_line , prev_line_length - byte_offset ) 250~= str_byte ( curr_line , curr_line_length - byte_offset ) 251then 252break 253end 254end 255end 256 257-- Iterate from end to beginning of shortest line 258local prev_end_byte_idx =prev_line_length - byte_offset + 1 259 260-- Handle case where lines match 261if prev_end_byte_idx == 0 then 262prev_end_byte_idx =1 263end 264local prev_byte_idx , prev_char_idx = 265align_end_position ( prev_line , prev_end_byte_idx , position_encoding ) 266local prev_end_range = 267{ line_idx =prev_line_idx , byte_idx =prev_byte_idx , char_idx =prev_char_idx } 268 269local curr_end_range ---@type vim.lsp.sync.Range 270-- Deletion event, new_range cannot be before start 271if curr_line_idx < start_line_idx then 272curr_end_range ={ line_idx =start_line_idx , byte_idx =1 , char_idx =1 } 273else 274local curr_end_byte_idx =curr_line_length - byte_offset + 1 275-- Handle case where lines match 276if curr_end_byte_idx == 0 then 277curr_end_byte_idx =1 278end 279local curr_byte_idx , curr_char_idx = 280align_end_position ( curr_line , curr_end_byte_idx , position_encoding ) 281curr_end_range = 282{ line_idx =curr_line_idx , byte_idx =curr_byte_idx , char_idx =curr_char_idx } 283end 284 285return prev_end_range , curr_end_range 286end 287 288--- Get the text of the range defined by start and end line/column 289---@param lines table list of lines 290---@param start_range vim.lsp.sync.Range table returned by first_difference 291---@param end_range vim.lsp.sync.Range new_end_range returned by last_difference 292---@param line_ending string 293---@return string text extracted from defined region 294local function extract_text ( lines , start_range , end_range , line_ending ) 295if not lines [ start_range . line_idx ] then 296return '' 297end 298-- Trivial case: start and end range are the same line, directly grab changed text 299if start_range . line_idx == end_range . line_idx then 300-- string.sub is inclusive, end_range is not 301return string . sub ( lines [ start_range . line_idx ], start_range . byte_idx , end_range . byte_idx - 1 ) 302else 303-- Handle deletion case 304-- Collect the changed portion of the first changed line 305local result ={ string . sub ( lines [ start_range . line_idx ], start_range . byte_idx ) } 306 307-- Collect the full line for intermediate lines 308for idx =start_range . line_idx + 1 , end_range . line_idx - 1 do 309table . insert ( result , lines [ idx ]) 310end 311 312if lines [ end_range . line_idx ] then 313-- Collect the changed portion of the last changed line. 314table . insert ( result , string . sub ( lines [ end_range . line_idx ], 1 , end_range . byte_idx - 1 )) 315else 316table . insert ( result , '' ) 317end 318 319-- Add line ending between all lines 320return table . concat ( result , line_ending ) 321end 322end 323 324-- rangelength depends on the position encoding 325-- bytes for utf-8 (clangd with extension) 326-- codepoints for utf-16 327-- codeunits for utf-32 328-- Line endings count here as 2 chars for \r\n (dos), 1 char for \n (unix), and 1 char for \r (mac) 329-- These correspond to Windows, Linux/macOS (OSX and newer), and macOS (version 9 and prior) 330---@param lines string[] 331---@param start_range vim.lsp.sync.Range 332---@param end_range vim.lsp.sync.Range 333---@param position_encoding 'utf-8'|'utf-16'|'utf-32' 334---@param line_ending string 335---@return integer 336local function compute_range_length ( lines , start_range , end_range , position_encoding , line_ending ) 337local line_ending_length =# line_ending 338-- Single line case 339if start_range . line_idx == end_range . line_idx then 340return end_range . char_idx - start_range . char_idx 341end 342 343local start_line =lines [ start_range . line_idx ] 344local range_length --- @type integer 345if start_line and # start_line > 0 then 346range_length =str_utfindex ( start_line , position_encoding ) 347- start_range . char_idx 348+ 1 349+ line_ending_length 350else 351-- Length of newline character 352range_length =line_ending_length 353end 354 355-- The first and last range of the line idx may be partial lines 356for idx =start_range . line_idx + 1 , end_range . line_idx - 1 do 357-- Length full line plus newline character 358if # lines [ idx ] > 0 then 359range_length =range_length 360+ str_utfindex ( assert ( lines [ idx ]), position_encoding ) 361+ # line_ending 362else 363range_length =range_length + line_ending_length 364end 365end 366 367local end_line =lines [ end_range . line_idx ] 368if end_line and # end_line > 0 then 369range_length =range_length + end_range . char_idx - 1 370end 371 372return range_length 373end 374 375--- Returns the range table for the difference between prev and curr lines 376---@param prev_lines table list of lines 377---@param curr_lines table list of lines 378---@param firstline integer line to begin search for first difference 379---@param lastline integer line to begin search in old_lines for last difference 380---@param new_lastline integer line to begin search in new_lines for last difference 381---@param position_encoding 'utf-8'|'utf-16'|'utf-32' encoding requested by language server 382---@param line_ending string 383---@return lsp.TextDocumentContentChangeEvent : see https://microsoft.github.io/language-server-protocol/specification/#textDocumentContentChangeEvent 384function M . compute_diff ( 385prev_lines , 386curr_lines , 387firstline , 388lastline , 389new_lastline , 390position_encoding , 391line_ending 392) 393-- Find the start of changes between the previous and current buffer. Common between both. 394-- Sent to the server as the start of the changed range. 395-- Used to grab the changed text from the latest buffer. 396local start_range =compute_start_range ( 397prev_lines , 398curr_lines , 399firstline + 1 , 400lastline + 1 , 401new_lastline + 1 , 402position_encoding 403) 404-- Find the last position changed in the previous and current buffer. 405-- prev_end_range is sent to the server as the end of the changed range. 406-- curr_end_range is used to grab the changed text from the latest buffer. 407local prev_end_range , curr_end_range =compute_end_range ( 408prev_lines , 409curr_lines , 410start_range , 411firstline + 1 , 412lastline + 1 , 413new_lastline + 1 , 414position_encoding 415) 416 417-- Grab the changed text of from start_range to curr_end_range in the current buffer. 418-- The text range is "" if entire range is deleted. 419local text =extract_text ( curr_lines , start_range , curr_end_range , line_ending ) 420 421-- Compute the range of the replaced text. Deprecated but still required for certain language servers 422local range_length = 423compute_range_length ( prev_lines , start_range , prev_end_range , position_encoding , line_ending ) 424 425-- convert to 0 based indexing 426local result ={ 427range ={ 428[ 'start' ] ={ line =start_range . line_idx - 1 , character =start_range . char_idx - 1 } , 429[ 'end' ] ={ line =prev_end_range . line_idx - 1 , character =prev_end_range . char_idx - 1 } , 430} , 431text =text , 432rangeLength =range_length , 433} 434 435return result 436end 437 438return M