Skip to content

Commit 13aebe5

Browse files
authored
Merge pull request #2354 from 9999years/dont-ask-third-party
Make `workspace.checkThirdParty` a string enum
2 parents 4819a7a + 3bbc8dd commit 13aebe5

File tree

6 files changed

+87
-39
lines changed

6 files changed

+87
-39
lines changed

doc/en-us/config.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -2177,13 +2177,20 @@ Automatic detection and adaptation of third-party libraries, currently supported
21772177
## type
21782178

21792179
```ts
2180-
boolean
2180+
string
21812181
```
21822182

2183+
## enum
2184+
2185+
* ``"Ask"``
2186+
* ``"Apply"``
2187+
* ``"ApplyInMemory"``
2188+
* ``"Disable"``
2189+
21832190
## default
21842191

21852192
```jsonc
2186-
true
2193+
"Ask"
21872194
```
21882195

21892196
# workspace.ignoreDir

doc/pt-br/config.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -2177,13 +2177,20 @@ Automatic detection and adaptation of third-party libraries, currently supported
21772177
## type
21782178

21792179
```ts
2180-
boolean
2180+
string
21812181
```
21822182

2183+
## enum
2184+
2185+
* ``"Ask"``
2186+
* ``"Apply"``
2187+
* ``"ApplyInMemory"``
2188+
* ``"Disable"``
2189+
21832190
## default
21842191

21852192
```jsonc
2186-
true
2193+
"Ask"
21872194
```
21882195

21892196
# workspace.ignoreDir

doc/zh-cn/config.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -2176,13 +2176,20 @@ true
21762176
## type
21772177

21782178
```ts
2179-
boolean
2179+
string
21802180
```
21812181

2182+
## enum
2183+
2184+
* ``"Ask"``
2185+
* ``"Apply"``
2186+
* ``"ApplyInMemory"``
2187+
* ``"Disable"``
2188+
21822189
## default
21832190

21842191
```jsonc
2185-
true
2192+
"Ask"
21862193
```
21872194

21882195
# workspace.ignoreDir

doc/zh-tw/config.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -2176,13 +2176,20 @@ true
21762176
## type
21772177

21782178
```ts
2179-
boolean
2179+
string
21802180
```
21812181

2182+
## enum
2183+
2184+
* ``"Ask"``
2185+
* ``"Apply"``
2186+
* ``"ApplyInMemory"``
2187+
* ``"Disable"``
2188+
21822189
## default
21832190

21842191
```jsonc
2185-
true
2192+
"Ask"
21862193
```
21872194

21882195
# workspace.ignoreDir

script/config/template.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,12 @@ local template = {
317317
['Lua.workspace.maxPreload'] = Type.Integer >> 5000,
318318
['Lua.workspace.preloadFileSize'] = Type.Integer >> 500,
319319
['Lua.workspace.library'] = Type.Array(Type.String),
320-
['Lua.workspace.checkThirdParty'] = Type.Boolean >> true,
320+
['Lua.workspace.checkThirdParty'] = Type.String >> 'Ask' << {
321+
'Ask',
322+
'Apply',
323+
'ApplyInMemory',
324+
'Disable',
325+
},
321326
['Lua.workspace.userThirdParty'] = Type.Array(Type.String),
322327
['Lua.completion.enable'] = Type.Boolean >> true,
323328
['Lua.completion.callSnippet'] = Type.String >> 'Disable' << {

script/library.lua

+45-30
Original file line numberDiff line numberDiff line change
@@ -469,34 +469,45 @@ end
469469

470470
local hasAsked = {}
471471
---@async
472-
local function askFor3rd(uri, cfg)
472+
local function askFor3rd(uri, cfg, checkThirdParty)
473473
if hasAsked[cfg.name] then
474474
return nil
475475
end
476-
hasAsked[cfg.name] = true
477-
local yes1 = lang.script.WINDOW_APPLY_WHIT_SETTING
478-
local yes2 = lang.script.WINDOW_APPLY_WHITOUT_SETTING
479-
local no = lang.script.WINDOW_DONT_SHOW_AGAIN
480-
local result = client.awaitRequestMessage('Info'
481-
, lang.script('WINDOW_ASK_APPLY_LIBRARY', cfg.name)
482-
, {yes1, yes2, no}
483-
)
484-
if not result then
485-
return nil
486-
end
487-
if result == yes1 then
476+
477+
if checkThirdParty == 'Apply' then
488478
apply3rd(uri, cfg, false)
489-
elseif result == yes2 then
479+
elseif checkThirdParty == 'ApplyInMemory' then
490480
apply3rd(uri, cfg, true)
491-
else
492-
client.setConfig({
493-
{
494-
key = 'Lua.workspace.checkThirdParty',
495-
action = 'set',
496-
value = false,
497-
uri = uri,
498-
},
499-
}, false)
481+
elseif checkThirdParty == 'Disable' then
482+
return nil
483+
elseif checkThirdParty == 'Ask' then
484+
hasAsked[cfg.name] = true
485+
local applyAndSetConfig = lang.script.WINDOW_APPLY_WHIT_SETTING
486+
local applyInMemory = lang.script.WINDOW_APPLY_WHITOUT_SETTING
487+
local dontShowAgain = lang.script.WINDOW_DONT_SHOW_AGAIN
488+
local result = client.awaitRequestMessage('Info'
489+
, lang.script('WINDOW_ASK_APPLY_LIBRARY', cfg.name)
490+
, {applyAndSetConfig, applyInMemory, dontShowAgain}
491+
)
492+
if not result then
493+
-- "If none got selected"
494+
-- See: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#window_showMessageRequest
495+
return nil
496+
end
497+
if result == applyAndSetConfig then
498+
apply3rd(uri, cfg, false)
499+
elseif result == applyInMemory then
500+
apply3rd(uri, cfg, true)
501+
else
502+
client.setConfig({
503+
{
504+
key = 'Lua.workspace.checkThirdParty',
505+
action = 'set',
506+
value = 'Disable',
507+
uri = uri,
508+
},
509+
}, false)
510+
end
500511
end
501512
end
502513

@@ -517,7 +528,7 @@ local function wholeMatch(a, b)
517528
return true
518529
end
519530

520-
local function check3rdByWords(uri, configs)
531+
local function check3rdByWords(uri, configs, checkThirdParty)
521532
if not files.isLua(uri) then
522533
return
523534
end
@@ -546,7 +557,7 @@ local function check3rdByWords(uri, configs)
546557
log.info('Found 3rd library by word: ', word, uri, library, inspect(config.get(uri, 'Lua.workspace.library')))
547558
---@async
548559
await.call(function ()
549-
askFor3rd(uri, cfg)
560+
askFor3rd(uri, cfg, checkThirdParty)
550561
end)
551562
return
552563
end
@@ -556,7 +567,7 @@ local function check3rdByWords(uri, configs)
556567
end, id)
557568
end
558569

559-
local function check3rdByFileName(uri, configs)
570+
local function check3rdByFileName(uri, configs, checkThirdParty)
560571
local path = ws.getRelativePath(uri)
561572
if not path then
562573
return
@@ -582,7 +593,7 @@ local function check3rdByFileName(uri, configs)
582593
log.info('Found 3rd library by filename: ', filename, uri, library, inspect(config.get(uri, 'Lua.workspace.library')))
583594
---@async
584595
await.call(function ()
585-
askFor3rd(uri, cfg)
596+
askFor3rd(uri, cfg, checkThirdParty)
586597
end)
587598
return
588599
end
@@ -597,8 +608,12 @@ local function check3rd(uri)
597608
if ws.isIgnored(uri) then
598609
return
599610
end
600-
if not config.get(uri, 'Lua.workspace.checkThirdParty') then
611+
local checkThirdParty = config.get(uri, 'Lua.workspace.checkThirdParty')
612+
-- Backwards compatability: `checkThirdParty` used to be a boolean.
613+
if not checkThirdParty or checkThirdParty == 'Disable' then
601614
return
615+
elseif checkThirdParty == true then
616+
checkThirdParty = 'Ask'
602617
end
603618
local scp = scope.getScope(uri)
604619
if not scp:get 'canCheckThirdParty' then
@@ -608,8 +623,8 @@ local function check3rd(uri)
608623
if not thirdConfigs then
609624
return
610625
end
611-
check3rdByWords(uri, thirdConfigs)
612-
check3rdByFileName(uri, thirdConfigs)
626+
check3rdByWords(uri, thirdConfigs, checkThirdParty)
627+
check3rdByFileName(uri, thirdConfigs, checkThirdParty)
613628
end
614629

615630
local function check3rdOfWorkspace(suri)

0 commit comments

Comments
 (0)