Merge pull request #603 from microsoft/lramos15/tabsAPISample

Adds a tabs sample
This commit is contained in:
Logan Ramos
2022-06-07 09:04:12 -04:00
committed by GitHub
10 changed files with 2729 additions and 0 deletions

View File

@ -0,0 +1,2 @@
vscode.d.ts
vscode.proposed.d.ts

View File

@ -0,0 +1,20 @@
/**@type {import('eslint').Linter.Config} */
// eslint-disable-next-line no-undef
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {
'semi': [2, "always"],
'@typescript-eslint/no-unused-vars': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/no-non-null-assertion': 0,
}
};

4
tabs-api-sample/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
out
node_modules
.vscode-test/
*.vsix

21
tabs-api-sample/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,21 @@
// A launch configuration that compiles the extension and then opens it inside a new window
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [{
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "npm: watch"
}
]
}

20
tabs-api-sample/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,20 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

19
tabs-api-sample/README.md Normal file
View File

@ -0,0 +1,19 @@
# Tabs API Sample
This sample demonstrates usage of [the tabs api](https://code.visualstudio.com/api/references/vscode-api#Tab) . This sample utilizes the tabs API to create a simple tab cleaner. Tabs will be closed after a configurable amount of inactivity. This threshold can be set via the `tabs.maxInactiveTime` setting.
## VS Code API
### `vscode` module
- [`workspace.getConfiguration`](https://code.visualstudio.com/api/references/vscode-api#workspace.getConfiguration)
### Contribution Points
- [`contributes.configuration`](https://code.visualstudio.com/api/references/contribution-points#contributes.configurations)
## Running the Sample
- Run `npm install` in terminal to install dependencies
- Run the `Run Extension` target in the Debug View. This will:
- Start a task `npm: watch` to compile the code
- Run the extension in a new VS Code window

2530
tabs-api-sample/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,48 @@
{
"name": "tabs-api-sample",
"displayName": "tabs-api-sample",
"description": "Sample showing how to use the tabs api",
"version": "0.0.1",
"publisher": "vscode-samples",
"private": true,
"license": "MIT",
"repository": "https://github.com/Microsoft/vscode-extension-samples",
"engines": {
"vscode": "^1.67.0"
},
"categories": [
"Other"
],
"activationEvents": [
"onStartupFinished"
],
"main": "./out/extension.js",
"browser": "./out/extension.js",
"contributes": {
"configuration": {
"title": "Tabs API Sample",
"id": "tabsSample",
"properties": {
"tabs.maxInactiveTime": {
"default": 30,
"description": "The number of seconds before closing inactive tabs",
"type": "number"
}
}
}
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"lint": "eslint . --ext .ts,.tsx",
"watch": "tsc -watch -p ./"
},
"devDependencies": {
"@types/node": "^16.11.7",
"@types/vscode": "^1.67.0",
"@typescript-eslint/eslint-plugin": "^5.17.0",
"@typescript-eslint/parser": "^5.17.0",
"eslint": "^8.12.0",
"typescript": "^4.6.3"
}
}

View File

@ -0,0 +1,53 @@
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
console.log('Congratulations, your extension "tabs-api-sample" is now active!');
/**
* You can use proposed API here. `vscode.` should start auto complete
* Proposed API as defined in vscode.proposed.<proposalName>.d.ts.
*/
const activityMap: Map<vscode.Tab, number> = new Map();
context.subscriptions.push(vscode.window.tabGroups.onDidChangeTabGroups(() => {
const tabs = vscode.window.tabGroups.all.map(group => group.tabs).flat(1);
activityMap.clear();
// Update activity map saying everything is active
tabs.forEach(t => activityMap.set(t, Date.now()));
}));
context.subscriptions.push(vscode.window.tabGroups.onDidChangeTabs(tabChangeEvent => {
// If tabs are closed no longer track their activity
for (const removedTab of tabChangeEvent.closed) {
activityMap.delete(removedTab);
}
// Update activity map last active time on changed tabs
const changedTabs = [...tabChangeEvent.opened, ...tabChangeEvent.changed];
for (const tab of changedTabs) {
// Reset the timer for the tabs last activity
activityMap.set(tab, Date.now());
}
}));
// Check every second for inactive tabs
const interval = setInterval(() => {
const activeTab = vscode.window.tabGroups.activeTabGroup.activeTab;
if (activeTab) {
// Update the activity map for the active tab, since it's still being used.
activityMap.set(activeTab, Date.now());
}
const inactiveTime = (vscode.workspace.getConfiguration().get<number>('tabs.maxInactiveTime') ?? 30) * 1000;
// Check if any tabs have been inactive for more than 30 seconds
const inactiveTabs = Array.from(activityMap.entries()).filter(([tab, lastActive]) => Date.now() - lastActive > inactiveTime && !tab.isActive && !tab.isDirty);
inactiveTabs.forEach(async ([tab]) => {
// Close the tab
await vscode.window.tabGroups.close(tab);
activityMap.delete(tab);
});
}, 1000);
// Create a small disposable to clean up the interval when extension is deactivated
context.subscriptions.push({
dispose: () => {
clearInterval(interval);
}
});
}

View File

@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2020",
"lib": ["es2020"],
"outDir": "out",
"sourceMap": true,
"strict": true,
"rootDir": "src"
},
"exclude": ["node_modules", ".vscode-test"]
}