Skip to content

Make workspace.checkThirdParty a string enum #2354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions doc/en-us/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -2177,13 +2177,20 @@ Automatic detection and adaptation of third-party libraries, currently supported
## type

```ts
boolean
string
```

## enum

* ``"Ask"``
* ``"Apply"``
* ``"ApplyInMemory"``
* ``"Disable"``

## default

```jsonc
true
"Ask"
```

# workspace.ignoreDir
Expand Down
11 changes: 9 additions & 2 deletions doc/pt-br/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -2177,13 +2177,20 @@ Automatic detection and adaptation of third-party libraries, currently supported
## type

```ts
boolean
string
```

## enum

* ``"Ask"``
* ``"Apply"``
* ``"ApplyInMemory"``
* ``"Disable"``

## default

```jsonc
true
"Ask"
```

# workspace.ignoreDir
Expand Down
11 changes: 9 additions & 2 deletions doc/zh-cn/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -2176,13 +2176,20 @@ true
## type

```ts
boolean
string
```

## enum

* ``"Ask"``
* ``"Apply"``
* ``"ApplyInMemory"``
* ``"Disable"``

## default

```jsonc
true
"Ask"
```

# workspace.ignoreDir
Expand Down
11 changes: 9 additions & 2 deletions doc/zh-tw/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -2176,13 +2176,20 @@ true
## type

```ts
boolean
string
```

## enum

* ``"Ask"``
* ``"Apply"``
* ``"ApplyInMemory"``
* ``"Disable"``

## default

```jsonc
true
"Ask"
```

# workspace.ignoreDir
Expand Down
7 changes: 6 additions & 1 deletion script/config/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,12 @@ local template = {
['Lua.workspace.maxPreload'] = Type.Integer >> 5000,
['Lua.workspace.preloadFileSize'] = Type.Integer >> 500,
['Lua.workspace.library'] = Type.Array(Type.String),
['Lua.workspace.checkThirdParty'] = Type.Boolean >> true,
['Lua.workspace.checkThirdParty'] = Type.String >> 'Ask' << {
'Ask',
'Apply',
'ApplyInMemory',
'Disable',
},
['Lua.workspace.userThirdParty'] = Type.Array(Type.String),
['Lua.completion.enable'] = Type.Boolean >> true,
['Lua.completion.callSnippet'] = Type.String >> 'Disable' << {
Expand Down
75 changes: 45 additions & 30 deletions script/library.lua
Original file line number Diff line number Diff line change
Expand Up @@ -469,34 +469,45 @@ end

local hasAsked = {}
---@async
local function askFor3rd(uri, cfg)
local function askFor3rd(uri, cfg, checkThirdParty)
if hasAsked[cfg.name] then
return nil
end
hasAsked[cfg.name] = true
local yes1 = lang.script.WINDOW_APPLY_WHIT_SETTING
local yes2 = lang.script.WINDOW_APPLY_WHITOUT_SETTING
local no = lang.script.WINDOW_DONT_SHOW_AGAIN
local result = client.awaitRequestMessage('Info'
, lang.script('WINDOW_ASK_APPLY_LIBRARY', cfg.name)
, {yes1, yes2, no}
)
if not result then
return nil
end
if result == yes1 then

if checkThirdParty == 'Apply' then
apply3rd(uri, cfg, false)
elseif result == yes2 then
elseif checkThirdParty == 'ApplyInMemory' then
apply3rd(uri, cfg, true)
else
client.setConfig({
{
key = 'Lua.workspace.checkThirdParty',
action = 'set',
value = false,
uri = uri,
},
}, false)
elseif checkThirdParty == 'Disable' then
return nil
elseif checkThirdParty == 'Ask' then
hasAsked[cfg.name] = true
local applyAndSetConfig = lang.script.WINDOW_APPLY_WHIT_SETTING
local applyInMemory = lang.script.WINDOW_APPLY_WHITOUT_SETTING
local dontShowAgain = lang.script.WINDOW_DONT_SHOW_AGAIN
local result = client.awaitRequestMessage('Info'
, lang.script('WINDOW_ASK_APPLY_LIBRARY', cfg.name)
, {applyAndSetConfig, applyInMemory, dontShowAgain}
)
if not result then
-- "If none got selected"
-- See: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#window_showMessageRequest
return nil
end
if result == applyAndSetConfig then
apply3rd(uri, cfg, false)
elseif result == applyInMemory then
apply3rd(uri, cfg, true)
else
client.setConfig({
{
key = 'Lua.workspace.checkThirdParty',
action = 'set',
value = 'Disable',
uri = uri,
},
}, false)
end
end
end

Expand All @@ -517,7 +528,7 @@ local function wholeMatch(a, b)
return true
end

local function check3rdByWords(uri, configs)
local function check3rdByWords(uri, configs, checkThirdParty)
if not files.isLua(uri) then
return
end
Expand Down Expand Up @@ -546,7 +557,7 @@ local function check3rdByWords(uri, configs)
log.info('Found 3rd library by word: ', word, uri, library, inspect(config.get(uri, 'Lua.workspace.library')))
---@async
await.call(function ()
askFor3rd(uri, cfg)
askFor3rd(uri, cfg, checkThirdParty)
end)
return
end
Expand All @@ -556,7 +567,7 @@ local function check3rdByWords(uri, configs)
end, id)
end

local function check3rdByFileName(uri, configs)
local function check3rdByFileName(uri, configs, checkThirdParty)
local path = ws.getRelativePath(uri)
if not path then
return
Expand All @@ -582,7 +593,7 @@ local function check3rdByFileName(uri, configs)
log.info('Found 3rd library by filename: ', filename, uri, library, inspect(config.get(uri, 'Lua.workspace.library')))
---@async
await.call(function ()
askFor3rd(uri, cfg)
askFor3rd(uri, cfg, checkThirdParty)
end)
return
end
Expand All @@ -597,8 +608,12 @@ local function check3rd(uri)
if ws.isIgnored(uri) then
return
end
if not config.get(uri, 'Lua.workspace.checkThirdParty') then
local checkThirdParty = config.get(uri, 'Lua.workspace.checkThirdParty')
-- Backwards compatability: `checkThirdParty` used to be a boolean.
if not checkThirdParty or checkThirdParty == 'Disable' then
return
elseif checkThirdParty == true then
checkThirdParty = 'Ask'
end
local scp = scope.getScope(uri)
if not scp:get 'canCheckThirdParty' then
Expand All @@ -608,8 +623,8 @@ local function check3rd(uri)
if not thirdConfigs then
return
end
check3rdByWords(uri, thirdConfigs)
check3rdByFileName(uri, thirdConfigs)
check3rdByWords(uri, thirdConfigs, checkThirdParty)
check3rdByFileName(uri, thirdConfigs, checkThirdParty)
end

local function check3rdOfWorkspace(suri)
Expand Down