Skip to content

Commit

Permalink
feat(vscode): add selection lock
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 21, 2022
1 parent c1ed290 commit c1f34ff
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
22 changes: 22 additions & 0 deletions packages/typescript-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@
"title": "Toggle Base Class Information",
"command": "typescriptExplorer.typeTree.view.show.baseClass.toggle",
"category": "%typescriptExplorer.typeTree.title%"
},
{
"title": "Lock Type Selection",
"command": "typescriptExplorer.selection.lock",
"category": "%typescriptExplorer.title%",
"icon": "$(unlock)"
},
{
"title": "Unlock Type Selection",
"command": "typescriptExplorer.selection.unlock",
"category": "%typescriptExplorer.title%",
"icon": "$(lock)"
}
],
"viewsContainers": {
Expand Down Expand Up @@ -110,6 +122,16 @@
{
"command": "typescriptExplorer.typeTree.view.refresh",
"when": "view == type-tree",
"group": "navigation@3"
},
{
"command": "typescriptExplorer.selection.lock",
"when": "view == type-tree && typescriptExplorer.selection.locked == false",
"group": "navigation@2"
},
{
"command": "typescriptExplorer.selection.unlock",
"when": "view == type-tree && typescriptExplorer.selection.locked == true",
"group": "navigation@2"
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-explorer/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type CommandHandler = (...args: any[]) => void|Promise<void>
type CommandInfo = [id: string, handler: CommandHandler]

const normalCommands: CommandInfo[] = [
["typescriptExplorer.expandedHover.enable.toggle", TSExplorer.Config.toggleExpandedHover],
// ["typescriptExplorer.expandedHover.enable.toggle", TSExplorer.Config.toggleExpandedHover],
]

const typeTreeViewCommands = (typeTreeProvider: TypeTreeProvider): CommandInfo[] => ([
Expand Down
54 changes: 47 additions & 7 deletions packages/typescript-explorer/src/state/stateManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TypeInfo } from '@ts-expand-type/api'
import { toEditorSettings } from 'typescript'
import * as vscode from 'vscode'
import { getQuickInfoAtPosition } from '../util'
import { TypeTreeItem, TypeTreeProvider } from '../view/typeTreeView'
Expand All @@ -10,19 +11,17 @@ export class StateManager {
private typeTree: TypeInfo|undefined
private typeTreeProvider?: TypeTreeProvider

private selectionLocked: boolean = false

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

vscode.window.onDidChangeTextEditorSelection((e) => {
if(e.selections.length === 0 || e.kind === vscode.TextEditorSelectionChangeKind.Command || !e.kind) {
if(e.kind === vscode.TextEditorSelectionChangeKind.Command || !e.kind) {
return
}

getQuickInfoAtPosition(e.textEditor.document.fileName, e.selections[0].start)
.then((body) => {
const { __displayTree } = body ?? {}
this.setTypeTree(__displayTree)
})
this.selectTypeAtPosition(e.textEditor, ...e.selections)
})

context.subscriptions.push(
Expand All @@ -43,7 +42,22 @@ export class StateManager {
vscode.commands.registerCommand(
"typescriptExplorer.typeTree.view.declared.goTo",
(item: TypeTreeItem) => item.goToDefinition()

)
)

this.setSelectionLock(this.selectionLocked)

context.subscriptions.push(
vscode.commands.registerCommand(
"typescriptExplorer.selection.lock",
() => this.setSelectionLock(true)
)
)

context.subscriptions.push(
vscode.commands.registerCommand(
"typescriptExplorer.selection.unlock",
() => this.setSelectionLock(false)
)
)
}
Expand All @@ -56,4 +70,30 @@ export class StateManager {
getTypeTree() {
return this.typeTree
}

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

getSelectionLock() {
return this.selectionLocked
}

selectTypeAtPosition(textEditor: vscode.TextEditor, ...selections: vscode.Selection[]) {
if(this.getSelectionLock()) {
return
}

if(selections.length === 0) {
this.setTypeTree(undefined)
return
}

getQuickInfoAtPosition(textEditor.document.fileName, selections[0].start)
.then((body) => {
const { __displayTree } = body ?? {}
this.setTypeTree(__displayTree)
})
}
}

0 comments on commit c1f34ff

Please sign in to comment.