Skip to content

Commit

Permalink
feat(vscode): update config smartly
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 21, 2022
1 parent bf830d2 commit c1ed290
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
13 changes: 2 additions & 11 deletions packages/typescript-explorer/src/config.ts
@@ -1,10 +1,10 @@
import * as vscode from 'vscode'
import { smartlySetConfigValue } from './util'

export namespace TSExplorer {
export namespace Config {
const getBoolean = (id: string, defaultValue?: boolean) => () => !!config().get(id, defaultValue)
// TODO: smartly set config value based on user config status
const toggleBoolean = (id: string, defaultValue?: boolean) => async () => config().update(id, !getBoolean(id, defaultValue)())
const toggleBoolean = (id: string, defaultValue?: boolean) => () => smartlySetConfigValue(id, !getBoolean(id, defaultValue)(), config())
const configBoolean = (id: string, defaultValue?: boolean) => ({ get: getBoolean(id, defaultValue), toggle: toggleBoolean(id) })

export namespace TypeTreeView {
Expand All @@ -13,15 +13,6 @@ export namespace TSExplorer {
export const { get: showTypeParameterInfo, toggle: toggleShowTypeParameterInfo } = configBoolean("typescriptExplorer.typeTree.view.show.typeParameters")
export const { get: showBaseClassInfo, toggle: toggleShowBaseClassInfo } = configBoolean("typescriptExplorer.typeTree.view.show.baseClass")
}

export function toggleExpandedHover() {
config().update('typescriptExplorer.expandedHover.enable', !isExpandedHover(), true)
}

export function isExpandedHover() {
const { defaultValue, globalValue } = config().inspect<boolean>('typescriptExplorer.expandedHover.enable') ?? {}
return globalValue ?? defaultValue ?? false
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions packages/typescript-explorer/src/util.ts
Expand Up @@ -30,4 +30,28 @@ export function getTypescriptMd(code: string) {
export async function getQuickInfoAtPosition(fileName: string, position: vscode.Position) {
return await vscode.commands.executeCommand("typescript.tsserverRequest", "quickinfo-full", toFileLocationRequestArgs(fileName, position))
.then((r) => (r as { body: ExpandedQuickInfo|undefined }).body)
}

/**
* Smartly set configuration value based on workspace configuration. This will set
* configuration on a workspace level if a workspace value has been set, and will
* set it globally otherwise.
*
* This will also reset a config value if it is set to its default.
*
* @param id
* @param value
* @param config Config object to use, defaults to workspace configuration
*/
export function smartlySetConfigValue<T>(id: string, value: T, config?: vscode.WorkspaceConfiguration) {
config ??= vscode.workspace.getConfiguration()
const { workspaceValue, defaultValue } = config.inspect<T>(id) ?? {}

let setValue: T|undefined = value

if(defaultValue !== undefined && defaultValue === value) {
setValue = undefined
}

return config.update(id, setValue, workspaceValue === undefined)
}

0 comments on commit c1ed290

Please sign in to comment.