Skip to content

Commit

Permalink
Percent encoding (#456)
Browse files Browse the repository at this point in the history
The Language Server Protocol refers to https://www.rfc-editor.org/rfc/rfc3986.

When converting a file-path into an URI, transported via UTF-8 (as by
the encoding for messages to the server), it used a percentage encoding
but with fewer bytes represented.

The following: `file:///Ümläutö`
was encoded in: `file:///%dcml%e4ut%f6`
now it encodes to: `file://%c3%9cml%c3%a4ut%c3%b6`

Decoding was adapted accordingly.

---
Credit to: Mr. Tim Pope - https://github.com/tpope/vim-unimpaired/blob/master/plugin/unimpaired.vim

Co-authored-by: Thomas Hage <tomes@DELLRakete>
  • Loading branch information
thchha and Thomas Hage committed Mar 25, 2023
1 parent 2e2e22c commit d0b53d4
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions autoload/lsc/uri.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ function! lsc#uri#documentPath(uri) abort
endfunction

function! s:EncodePath(value) abort
return substitute(a:value, '\([^a-zA-Z0-9-_.~/]\)',
\ '\=s:EncodeChar(submatch(1))', 'g')
" shamelessly taken from Mr. T. Pope and adapted:
" (https://github.com/tpope/vim-unimpaired/blob/master/plugin/unimpaired.vim#L461)
" This follows the VIM License over at https://github.com/vim/vim/blob/master/LICENSE
return substitute(iconv(a:value, "latin-1", 'utf-8'),
\ '[^a-zA-Z0-9-_.~/]',
\ '\=s:EncodeChar(submatch(0))', 'g')
endfunction

function! s:EncodeChar(char) abort
Expand All @@ -22,13 +26,16 @@ function! s:EncodeChar(char) abort
endfunction

function! s:DecodePath(value) abort
return substitute(a:value, '%\([a-fA-F0-9]\{2}\)',
\ '\=s:DecodeChar(submatch(1))', 'g')
endfunction

function! s:DecodeChar(hexcode) abort
let l:charcode = str2nr(a:hexcode, 16)
return nr2char(l:charcode)
" shamelessly taken from Mr. T. Pope and adapted:
" (https://github.com/tpope/vim-unimpaired/blob/master/plugin/unimpaired.vim#L466)
" This follows the VIM License over at https://github.com/vim/vim/blob/master/LICENSE
return iconv(
\ substitute(
\ a:value,
\ '%\(\x\x\)',
\ '\=nr2char("0x".submatch(1))','g'),
\ 'utf-8',
\ 'latin1')
endfunction

function! s:filePrefix(...) abort
Expand Down

0 comments on commit d0b53d4

Please sign in to comment.