Skip to content

Commit 863d072

Browse files
committed
Merge pull request #3446 from Wilfred/fix-invalid-utf8-in-json-output
2 parents 18ea5a5 + 42a0b92 commit 863d072

4 files changed

Lines changed: 36 additions & 0 deletions

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* `FIX` Correct `math.type` meta return annotation to use `nil` instead of the string literal `'nil'`
1313
* `FIX` Fix initial `nameStyle.config` not getting loaded in the appropriate workspace.
1414
* `FIX` Fix method-level generic return type being overwritten by receiver class generic resolution [#3438](https://github.com/LuaLS/lua-language-server/discussions/3438)
15+
* `FIX` Fix invalid LSP responses in files containing string literals with escaped bytes.
1516

1617
## 3.18.2
1718
* `CHG` `duplicate-set-field` diagnostic now supports linked suppression: when any occurrence of a duplicate field is suppressed with `---@diagnostic disable` or `---@diagnostic disable-next-line`, all warnings for that field name will be suppressed

script/utility.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,26 @@ local esc = {
469469
['\n'] = '\\\n',
470470
}
471471

472+
local function escapeInvalidUtf8(str)
473+
local result = {}
474+
local start = 1
475+
while true do
476+
local _, invalid = utf8Len(str, start)
477+
if not invalid then
478+
result[#result+1] = str:sub(start)
479+
break
480+
end
481+
result[#result+1] = str:sub(start, invalid - 1)
482+
result[#result+1] = ('\\%03d'):format(stringByte(str, invalid))
483+
start = invalid + 1
484+
end
485+
return tableConcat(result)
486+
end
487+
472488
function m.viewString(str, quo)
489+
if not utf8Len(str) then
490+
str = escapeInvalidUtf8(str)
491+
end
473492
if not quo then
474493
if str:find('[\r\n]') then
475494
quo = '[['

test/other/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
--require 'other.filewatch'
2+
require 'other.view-string'

test/other/view-string.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local util = require 'utility'
2+
3+
local function assertView(value, expected)
4+
local literal = util.viewString(value)
5+
assert(utf8.len(literal))
6+
assert(literal == expected, ('expected %q, got %q'):format(expected, literal))
7+
end
8+
9+
assertView('plain text', '"plain text"')
10+
assertView('é中文', '"é中文"')
11+
assertView('\x80', '"\\128"')
12+
assertView('\xff', '"\\255"')
13+
assertView('\xc2A', '"\\194A"')
14+
assertView('\xe2\x82', '"\\226\\130"')
15+
assertView('[^%w_\x80-\xff]', '"[^%w_\\128-\\255]"')

0 commit comments

Comments
 (0)