Skip to content

Commit

Permalink
feat(vscode): allow disablling selection on click
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 21, 2022
1 parent 6279ec6 commit c385e69
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
18 changes: 15 additions & 3 deletions packages/typescript-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"onCommand:typescriptExplorer.typeTree.view.show.baseClass.toggle",
"onCommand:typescriptExplorer.selection.lock",
"onCommand:typescriptExplorer.selection.unlock",
"onCommand:typescriptExplorer.selection.select"
"onCommand:typescriptExplorer.selection.select",
"onCommand:typescriptExplorer.typeTree.selection.enable.toggle"
],
"main": "./out/extension.js",
"contributes": {
Expand All @@ -47,6 +48,12 @@
"title": "Show Base Class",
"type": "boolean",
"default": true
},
"typescriptExplorer.typeTree.selection.enable": {
"title": "Enable Type Tree Selection",
"description": "Enable selecting a symbol type on click in the editor",
"type": "boolean",
"default": true
}
}
},
Expand Down Expand Up @@ -107,6 +114,11 @@
"title": "Select Symbol in Type Tree",
"command": "typescriptExplorer.selection.select",
"category": "%typescriptExplorer.title%"
},
{
"title": "Toggle Type Selection On Click",
"command": "typescriptExplorer.typeTree.selection.enable.toggle",
"category": "%typescriptExplorer.title%"
}
],
"viewsContainers": {
Expand Down Expand Up @@ -140,12 +152,12 @@
},
{
"command": "typescriptExplorer.selection.lock",
"when": "view == type-tree && typescriptExplorer.selection.locked == false",
"when": "view == type-tree && typescriptExplorer.selection.locked == false && typescriptExplorer.selection.enabled == true",
"group": "navigation@2"
},
{
"command": "typescriptExplorer.selection.unlock",
"when": "view == type-tree && typescriptExplorer.selection.locked == true",
"when": "view == type-tree && typescriptExplorer.selection.locked == true && typescriptExplorer.selection.enabled == true",
"group": "navigation@2"
},
{
Expand Down
11 changes: 8 additions & 3 deletions packages/typescript-explorer/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const typeTreeViewCommands: RefreshableCommandInfo[] = [
["typescriptExplorer.typeTree.view.icons.enabled.toggle", "typescriptExplorer.typeTree.view.icons.enable", TSExplorer.Config.TypeTreeView.toggleIconsEnabled],
["typescriptExplorer.typeTree.view.icons.colors.enabled.toggle", "typescriptExplorer.typeTree.view.icons.colors.enable", TSExplorer.Config.TypeTreeView.toggleIconColorsEnabled],
["typescriptExplorer.typeTree.view.show.typeParameters.toggle", "typescriptExplorer.typeTree.view.show.typeParameters", TSExplorer.Config.TypeTreeView.toggleShowTypeParameterInfo],
["typescriptExplorer.typeTree.view.show.baseClass.toggle", "typescriptExplorer.typeTree.view.show.baseClass", TSExplorer.Config.TypeTreeView.toggleShowBaseClassInfo]
["typescriptExplorer.typeTree.view.show.baseClass.toggle", "typescriptExplorer.typeTree.view.show.baseClass", TSExplorer.Config.TypeTreeView.toggleShowBaseClassInfo],
["typescriptExplorer.typeTree.selection.enable.toggle", "typescriptExplorer.typeTree.selection.enable", TSExplorer.Config.TypeTreeView.toggleSelectionEnabled, false ]
]

export function registerCommands(context: vscode.ExtensionContext, { typeTreeProvider }: ViewProviders) {
Expand All @@ -24,10 +25,14 @@ export function registerCommands(context: vscode.ExtensionContext, { typeTreePro
commands.forEach(c => registerCommand(c, context))
}

type RefreshableCommandInfo = [id: string, configId: string, handler: CommandHandler]
type RefreshableCommandInfo = [id: string, configId: string, handler: CommandHandler, refresh?: boolean]

function wrapRefresh(context: vscode.ExtensionContext, command: RefreshableCommandInfo, refreshable: {refresh(): void}): CommandInfo {
const [id, configId, handler] = command
const [id, configId, handler, refresh] = command

if(!refresh) {
return [id, handler]
}

vscode.workspace.onDidChangeConfiguration((event) => {
if(event.affectsConfiguration(configId)) {
Expand Down
1 change: 1 addition & 0 deletions packages/typescript-explorer/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export namespace TSExplorer {
export const { get: iconColorsEnabled, toggle: toggleIconColorsEnabled } = configBoolean("typescriptExplorer.typeTree.view.icons.colors.enable")
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 const { get: selectionEnabled, toggle: toggleSelectionEnabled } = configBoolean("typescriptExplorer.typeTree.selection.enable")
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion packages/typescript-explorer/src/state/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TypeInfo } from '@ts-expand-type/api'
import assert = require('assert')
import { toEditorSettings } from 'typescript'
import * as vscode from 'vscode'
import { TSExplorer } from '../config'
import { getQuickInfoAtPosition, showError } from '../util'
import { TypeTreeItem, TypeTreeProvider } from '../view/typeTreeView'
import { ViewProviders } from '../view/views'
Expand All @@ -13,10 +14,19 @@ export class StateManager {
private typeTreeProvider?: TypeTreeProvider

private selectionLocked: boolean = false
private selectionEnabled: boolean = true

init(context: vscode.ExtensionContext, { typeTreeProvider }: ViewProviders) {
this.typeTreeProvider = typeTreeProvider

this.updateSelectionContext()

vscode.workspace.onDidChangeConfiguration(event => {
if(event.affectsConfiguration("typescriptExplorer.typeTree.selection.enable")) {
this.updateSelectionContext()
}
}, undefined, context.subscriptions)

vscode.window.onDidChangeTextEditorSelection((e) => {
if(e.kind === vscode.TextEditorSelectionChangeKind.Command || !e.kind) {
return
Expand Down Expand Up @@ -88,13 +98,18 @@ export class StateManager {
return this.typeTree
}

private updateSelectionContext() {
this.selectionEnabled = TSExplorer.Config.TypeTreeView.selectionEnabled()
vscode.commands.executeCommand("setContext", "typescriptExplorer.selection.enabled", this.selectionEnabled)
}

setSelectionLock(locked: boolean) {
this.selectionLocked = locked
vscode.commands.executeCommand("setContext", "typescriptExplorer.selection.locked", locked)
}

getSelectionLock() {
return this.selectionLocked
return this.selectionLocked || !this.selectionEnabled
}

selectTypeAtPosition(fileName: string, selections: readonly vscode.Selection[], ignoreSelectionLock: boolean = false) {
Expand Down

0 comments on commit c385e69

Please sign in to comment.