lua-language-server
lua-language-server copied to clipboard
Feature Request: Option for hexadecimals to not be displayed as decimals on mouseover
Currently if you have defined a number like this for example:
WS_BORDER = 0x00800000
Hovering over WS_BORDER displays:
(global) WS_BORDER : integer = 8388608
Would it be possible to add an option so that numbers that are not defined as decimals are not resolved to decimals when hovering over them? I. e. :
(global) WS_BORDER : integer = 0x00800000
string literals can be displayed according to their defined quotes
For string literals, currently luals is capable to distinguish between different quote styles
local s1 = 's1'
-- hover: local s1: string = 's1'
local s2 = "s2"
-- hover: local s2: string = "s2"
local s3 = [[s3]]
-- hover: local s3: string = [[s3]]
- By looking at the code, I believe this is done by storing the mark when parsing string literals: https://github.com/LuaLS/lua-language-server/blob/06943cfabb3c8957a15c28ffc0ac7fbc18dfd8f7/script/parser/compile.lua#L1145-L1152
- and then pass this mark to
util.viewString()https://github.com/LuaLS/lua-language-server/blob/06943cfabb3c8957a15c28ffc0ac7fbc18dfd8f7/script/vm/infer.lua#L499-L512 - and finally the formatting https://github.com/LuaLS/lua-language-server/blob/06943cfabb3c8957a15c28ffc0ac7fbc18dfd8f7/script/utility.lua#L468-L505
borrowing this idea for viewing integer?
By applying the same method, I think we can:
- firstly, store the integer mode when parsing integers here: https://github.com/LuaLS/lua-language-server/blob/06943cfabb3c8957a15c28ffc0ac7fbc18dfd8f7/script/parser/compile.lua#L1361-L1387
- then add a
util.viewInteger()and format as hex if it is in hexadecimal mode
function m.viewInteger(int, mode)
if mode == 'x' then
-- view as hex integer
return ('0x%x'):format(int)
end
return tostring(int)
end
- finally add an elseif case for
n.type == 'integer'inmt:viewLiterals()of vm/infer.lua
the POC patch
Here is a POC patch file you might want to try, you may work from here and PR it after testing 😄
click to expand
From c0359b7cfb6b147c5c60e1c97c211cc3bf335ed3 Mon Sep 17 00:00:00 2001
From: Tom Lau <[email protected]>
Date: Thu, 6 Feb 2025 12:10:24 +0800
Subject: [PATCH] poc: hex integer hover
---
script/parser/compile.lua | 4 +++-
script/utility.lua | 8 ++++++++
script/vm/infer.lua | 2 ++
3 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/script/parser/compile.lua b/script/parser/compile.lua
index 41b0da17..333bc4e0 100644
--- a/script/parser/compile.lua
+++ b/script/parser/compile.lua
@@ -1353,7 +1353,7 @@ local function parseNumber()
neg = true
offset = offset + 1
end
- local number, integer
+ local number, integer, mode
local firstChar = ssub(Lua, offset, offset)
if firstChar == '.' then
number, offset = parseNumber10(offset)
@@ -1362,6 +1362,7 @@ local function parseNumber()
local nextChar = ssub(Lua, offset + 1, offset + 1)
if CharMapN16[nextChar] then
number, offset, integer = parseNumber16(offset + 2)
+ mode = 'x'
elseif CharMapN2[nextChar] then
number, offset = parseNumber2(offset + 2)
integer = true
@@ -1384,6 +1385,7 @@ local function parseNumber()
start = startPos,
finish = getPosition(offset - 1, 'right'),
[1] = number,
+ [2] = integer and mode or nil,
}
offset = dropNumberTail(offset, integer)
fastForwardToken(offset)
diff --git a/script/utility.lua b/script/utility.lua
index 7a1fd0ff..ba33b4a5 100644
--- a/script/utility.lua
+++ b/script/utility.lua
@@ -504,6 +504,14 @@ function m.viewString(str, quo)
end
end
+function m.viewInteger(int, mode)
+ if mode == 'x' then
+ -- view as hex integer
+ return ('0x%x'):format(int)
+ end
+ return tostring(int)
+end
+
function m.viewLiteral(v)
local tp = type(v)
if tp == 'nil' then
diff --git a/script/vm/infer.lua b/script/vm/infer.lua
index 6f21a76a..200d5059 100644
--- a/script/vm/infer.lua
+++ b/script/vm/infer.lua
@@ -510,6 +510,8 @@ function mt:viewLiterals()
local literal
if n.type == 'string' then
literal = util.viewString(n[1], n[2])
+ elseif n.type == 'integer' then
+ literal = util.viewInteger(n[1], n[2])
else
literal = util.viewLiteral(n[1])
end
--
2.45.2
result:
(global) WS_BORDER: integer = 0x800000