- get current theme styles
// https://stackoverflow.com/questions/47117621/how-to-get-the-vscode-theme-color-in-vscode-extensions
const color = new vscode.ThemeColor('badge.background');
- commands icons can use vscode builtin
"commands": [
// ... ,
{
"command": "...",
"title": "...",
"icon": "$(add)"
},
]
- u can hide command from command palet using when
"commandPalette": [
{
"command": "...",
"when": "false"
},
]
- to get keystrokes
// keyDown
context.subscriptions.push(
vscode.commands.registerCommand('type', (e) => {
console.log(e.text)
vscode.commands.executeCommand('default:type', { text: e.text })
})
)
// use debounce
// keyUp
vscode.workspace.onDidChangeTextDocument((e) => ...)
- auto update tree provider, add any event listener to the class constructor
private _onDidChangeTreeData = new vscode.EventEmitter();
readonly onDidChangeTreeData = this._onDidChangeTreeData.event;
constructor() {
vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration('config.name')) {
this._onDidChangeTreeData.fire();
}
})
}
-
to get any data for the command "when" param u can use
workbench.action.inspectContextKeys
and check the console -
to type something, use
commands.executeCommand('type', { text: '\n' });
// or
commands.executeCommand('default:type', { text: '\n' });
- cursor movment
let selection = editor.selection.active
let newPosition = selection.with(selection.line, offset) // offset == char new offset
// move only
editor.selection = new vscode.Selection(newPosition, newPosition)
// with select
// anchor == the start of the selection
// active == the current possition of the cursor "the end of selection"
editor.selection = new vscode.Selection(editor.selection.anchor, newPosition)
- to fire custom event
const someEvent = new vscode.EventEmitter()
someEvent.fire() // anywhere in code
someEvent.event((e) => {
// do stuff
})
// at the end
function deactivate() {
someEvent.dispose()
}
- to set custom
when
for commands
vscode.commands.executeCommand('setContext', 'the_when_value', true)
// later
vscode.commands.executeCommand('setContext', 'the_when_value', false)
- for notifications with timeout
- to show without progress, use
progress.report({ increment: 0})
- to show without progress, use
return vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: 'some msg',
cancellable: true
}, async (progress, token) => {
for (let i = 1; i <= 11; i++) {
await new Promise((resolve) => {
setTimeout(() => {
progress.report({ increment: 10 })
resolve()
}, 1000)
})
}
return new Promise((resolve) => resolve())
})
- to effectivly track the doc close, save a list of opened ones
let visibleTextEditors = []
// on start
for (const editor of vscode.window.visibleTextEditors) {
visibleTextEditors.push(editor.document.fileName)
}
// on new document
vscode.window.onDidChangeVisibleTextEditors((editors) => {
for (const editor of editors) {
visibleTextEditors.push(editor.document.fileName)
}
})
// on close
vscode.workspace.onDidCloseTextDocument((doc) => {
if (doc && doc.isClosed && visibleTextEditors.includes(doc.fileName)) {
visibleTextEditors.splice(visibleTextEditors.indexOf(doc.fileName), 1)
}
})
- get selected text
let editor = vscode.window.activeTextEditor
let { document, selection } = editor
let text = document.getText(selection)
- to open settings page frome extension
vscode.commands.executeCommand('workbench.action.openSettings', $QUERY)
https://github.com/moalamri/vscode-inline-fold/issues/135