Skip to content

Commit 4b8d968

Browse files
committed
add language configs
`Lua.language.fixIndent` `Lua.language.completeAnnotation`
1 parent a4a7369 commit 4b8d968

File tree

5 files changed

+134
-22
lines changed

5 files changed

+134
-22
lines changed

script/config/template.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ local template = {
396396
>> {},
397397
['Lua.misc.parameters'] = Type.Array(Type.String),
398398
['Lua.misc.executablePath'] = Type.String,
399+
['Lua.language.fixIndent'] = Type.Boolean >> true,
400+
['Lua.language.completeAnnotation'] = Type.Boolean >> true,
399401
['Lua.type.castNumberToInteger'] = Type.Boolean >> true,
400402
['Lua.type.weakUnionCheck'] = Type.Boolean >> false,
401403
['Lua.type.weakNilCheck'] = Type.Boolean >> false,

script/core/fix-indent.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local proto = require 'proto.proto'
44
local lookBackward = require 'core.look-backward'
55
local util = require 'utility'
66
local client = require 'client'
7+
local config = require 'config'
78

89
---@param uri uri
910
---@param change table
@@ -172,6 +173,10 @@ return function (uri, changes)
172173
return
173174
end
174175

176+
if not config.get(uri, 'Lua.language.fixIndent') then
177+
return
178+
end
179+
175180
local firstChange = changes[1]
176181
if firstChange.range then
177182
local edits = removeSpacesAfterEnter(uri, firstChange)

script/provider/language-configuration.lua

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
local config = require 'config'
2+
local ws = require 'workspace'
3+
local util = require 'utility'
4+
5+
local function getConfig(key)
6+
local scope = ws.getFirstScope()
7+
local uri = scope.uri
8+
return config.get(uri, key)
9+
end
10+
111
-- Enumeration of commonly encountered syntax token types.
212
local SyntaxTokenType = {
313
Other = 0, -- Everything except tokens that are part of comments, string literals and regular expressions.
@@ -51,6 +61,13 @@ local languageConfiguration = {
5161
indentAction = IndentAction.IndentOutdent,
5262
}
5363
},
64+
},
65+
},
66+
}
67+
68+
local completeAnnotation = {
69+
configuration = {
70+
onEnterRules = {
5471
{
5572
beforeText = [[^\s*---@]],
5673
action = {
@@ -83,4 +100,14 @@ local languageConfiguration = {
83100
},
84101
}
85102

86-
return languageConfiguration
103+
local M = {}
104+
105+
function M.make()
106+
local result = languageConfiguration
107+
if getConfig 'Lua.language.completeAnnotation' then
108+
result = util.mergeStruct(result, completeAnnotation)
109+
end
110+
return result
111+
end
112+
113+
return M

script/provider/provider.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,8 @@ local function refreshLanguageConfiguration()
16051605
if not client.getOption('languageConfiguration') then
16061606
return
16071607
end
1608-
proto.notify('$/languageConfiguration', require 'provider.language-configuration')
1608+
local lc = require 'provider.language-configuration'
1609+
proto.notify('$/languageConfiguration', lc.make())
16091610
end
16101611

16111612
config.watch(function (_uri, key, _value)

script/utility.lua

Lines changed: 97 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -692,21 +692,6 @@ function m.sortCallbackOfIndex(arr)
692692
end
693693
end
694694

695-
---@param datas any[]
696-
---@param scores integer[]
697-
---@return SortByScoreCallback
698-
function m.sortCallbackOfScore(datas, scores)
699-
local map = {}
700-
for i = 1, #datas do
701-
local data = datas[i]
702-
local score = scores[i]
703-
map[data] = score
704-
end
705-
return function (v)
706-
return map[v]
707-
end
708-
end
709-
710695
---裁剪字符串
711696
---@param str string
712697
---@param mode? '"left"'|'"right"'
@@ -721,15 +706,20 @@ function m.trim(str, mode)
721706
return (str:match '^%s*(.-)%s*$')
722707
end
723708

724-
function m.expandPath(path)
709+
---@param path string
710+
---@param env? { [string]: string }
711+
---@return string
712+
function m.expandPath(path, env)
725713
if path:sub(1, 1) == '~' then
726714
local home = getenv('HOME')
727715
if not home then -- has to be Windows
728716
home = getenv 'USERPROFILE' or (getenv 'HOMEDRIVE' .. getenv 'HOMEPATH')
729717
end
730718
return home .. path:sub(2)
731719
elseif path:sub(1, 1) == '$' then
732-
path = path:gsub('%$([%w_]+)', getenv)
720+
path = path:gsub('%$([%w_]+)', function (name)
721+
return env and env[name] or getenv(name) or ''
722+
end)
733723
return path
734724
end
735725
return path
@@ -865,13 +855,12 @@ function m.multiTable(count, default)
865855
end })
866856
end
867857
for _ = 3, count do
868-
local tt = current
869858
current = setmetatable({}, { __index = function (t, k)
870859
if k == nil then
871860
return nil
872861
end
873-
t[k] = tt
874-
return tt
862+
t[k] = current
863+
return current
875864
end })
876865
end
877866
return current
@@ -963,4 +952,92 @@ function m.arrayMerge(a, b)
963952
return a
964953
end
965954

955+
---@generic K
956+
---@param t { [K]: any }
957+
---@return K[]
958+
function m.keysOf(t)
959+
local keys = {}
960+
for k in pairs(t) do
961+
keys[#keys+1] = k
962+
end
963+
return keys
964+
end
965+
966+
---@generic V
967+
---@param t { [any]: V }
968+
---@return V[]
969+
function m.valuesOf(t)
970+
local values = {}
971+
for _, v in pairs(t) do
972+
values[#values+1] = v
973+
end
974+
return values
975+
end
976+
977+
---@param t table
978+
---@return integer
979+
function m.countTable(t)
980+
local count = 0
981+
for _ in pairs(t) do
982+
count = count + 1
983+
end
984+
return count
985+
end
986+
987+
---@param arr any[]
988+
function m.arrayRemoveDuplicate(arr)
989+
local mark = {}
990+
local offset = 0
991+
local len = #arr
992+
for i = 1, len do
993+
local v = arr[i]
994+
if mark[v] then
995+
offset = offset + 1
996+
else
997+
arr[i - offset] = v
998+
mark[v] = true
999+
end
1000+
end
1001+
for i = len - offset + 1, len do
1002+
arr[i] = nil
1003+
end
1004+
end
1005+
1006+
---@param ... table
1007+
---@return table
1008+
function m.mergeStruct(...)
1009+
local result
1010+
local copyed = {}
1011+
1012+
local function merge(a, b)
1013+
if copyed[b] then
1014+
return copyed[b]
1015+
end
1016+
if type(b) ~= 'table' then
1017+
return b
1018+
end
1019+
if not a then
1020+
a = {}
1021+
end
1022+
copyed[b] = a
1023+
local usedKeys = {}
1024+
for i, v in ipairs(b) do
1025+
a[#a+1] = v
1026+
usedKeys[i] = true
1027+
end
1028+
for k, v in pairs(b) do
1029+
if not usedKeys[k] then
1030+
a[k] = merge(a[k], v)
1031+
end
1032+
end
1033+
return a
1034+
end
1035+
1036+
for _, t in ipairs { ... } do
1037+
result = merge(result, t)
1038+
end
1039+
1040+
return result
1041+
end
1042+
9661043
return m

0 commit comments

Comments
 (0)