mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
Initial tab api sample
This commit is contained in:
2
tabs-api-sample/.eslintignore
Normal file
2
tabs-api-sample/.eslintignore
Normal file
@ -0,0 +1,2 @@
|
||||
vscode.d.ts
|
||||
vscode.proposed.d.ts
|
||||
20
tabs-api-sample/.eslintrc.js
Normal file
20
tabs-api-sample/.eslintrc.js
Normal 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
4
tabs-api-sample/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
out
|
||||
node_modules
|
||||
.vscode-test/
|
||||
*.vsix
|
||||
21
tabs-api-sample/.vscode/launch.json
vendored
Normal file
21
tabs-api-sample/.vscode/launch.json
vendored
Normal 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
20
tabs-api-sample/.vscode/tasks.json
vendored
Normal 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
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
29
tabs-api-sample/README.md
Normal file
29
tabs-api-sample/README.md
Normal file
@ -0,0 +1,29 @@
|
||||
# Proposed API Sample
|
||||
|
||||
This sample demonstrates usage of [Proposed API](https://code.visualstudio.com/api/advanced-topics/using-proposed-api).
|
||||
|
||||
The `postinstall` script uses `vscode-dts dev && vscode-dts main` to download latest version of [`vscode.d.ts`](https://github.com/microsoft/vscode/blob/main/src/vs/vscode.d.ts) and [`vscode.proposed.<proposalName>.d.ts`](https://github.com/microsoft/vscode/blob/main/src/vscode-dts) from the main branch of [microsoft/vscode](https://github.com/microsoft/vscode).
|
||||
|
||||
You can read more about `vscode-dts` at: https://github.com/microsoft/vscode-dts.
|
||||
|
||||
- ⚠️ This sample can only be used for extension development in [Insider release](https://code.visualstudio.com/insiders/). You cannot publish extensions using Proposed API.
|
||||
- You need `"enabledApiProposals": ["<proposalName>"]` in `package.json`.
|
||||
|
||||
## VS Code API
|
||||
|
||||
### `vscode` module
|
||||
|
||||
- [`commands.registerCommand`](https://code.visualstudio.com/api/references/vscode-api#commands.registerCommand)
|
||||
- [`window.showInformationMessage`](https://code.visualstudio.com/api/references/vscode-api#window.showInformationMessage)
|
||||
|
||||
### Contribution Points
|
||||
|
||||
- [`contributes.commands`](https://code.visualstudio.com/api/references/contribution-points#contributes.commands)
|
||||
|
||||
## Running the Sample
|
||||
|
||||
- Run `npm install` in terminal to install dependencies
|
||||
- A `postinstall` script would download latest version of `vscode.proposed.<proposalName>.d.ts`
|
||||
- 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
|
||||
2606
tabs-api-sample/package-lock.json
generated
Normal file
2606
tabs-api-sample/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
40
tabs-api-sample/package.json
Normal file
40
tabs-api-sample/package.json
Normal file
@ -0,0 +1,40 @@
|
||||
{
|
||||
"enabledApiProposals": ["tabs"],
|
||||
"name": "tabs-api-sample",
|
||||
"displayName": "tabs-api-sample",
|
||||
"description": "Sample showing how to use the tabs api",
|
||||
"version": "0.0.1",
|
||||
"publisher": "lramos15",
|
||||
"private": true,
|
||||
"license": "MIT",
|
||||
"repository": "https://github.com/Microsoft/vscode-extension-samples",
|
||||
"engines": {
|
||||
"vscode": "^1.67.0"
|
||||
},
|
||||
"categories": [
|
||||
"Other"
|
||||
],
|
||||
"activationEvents": [
|
||||
"*"
|
||||
],
|
||||
"main": "./out/extension.js",
|
||||
"contributes": {
|
||||
},
|
||||
"scripts": {
|
||||
"vscode:prepublish": "npm run compile",
|
||||
"compile": "tsc -p ./",
|
||||
"lint": "eslint . --ext .ts,.tsx",
|
||||
"watch": "tsc -watch -p ./",
|
||||
"download-api": "vscode-dts dev",
|
||||
"postdownload-api": "vscode-dts main",
|
||||
"postinstall": "npm run download-api"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^16.11.7",
|
||||
"@typescript-eslint/eslint-plugin": "^5.17.0",
|
||||
"@typescript-eslint/parser": "^5.17.0",
|
||||
"eslint": "^8.12.0",
|
||||
"typescript": "^4.6.3",
|
||||
"vscode-dts": "^0.3.3"
|
||||
}
|
||||
}
|
||||
30
tabs-api-sample/src/extension.ts
Normal file
30
tabs-api-sample/src/extension.ts
Normal file
@ -0,0 +1,30 @@
|
||||
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.
|
||||
*/
|
||||
|
||||
// Whenever a tab is opened close it
|
||||
let timeout: NodeJS.Timeout | undefined = undefined;
|
||||
const disposable = vscode.window.tabGroups.onDidChangeTabs(tabs => {
|
||||
if (timeout) {
|
||||
timeout.refresh();
|
||||
} else {
|
||||
timeout = setTimeout(async () => {
|
||||
try {
|
||||
await vscode.window.tabGroups.close(vscode.window.tabGroups.groups.map(group => group.tabs).flat(1));
|
||||
vscode.window.showErrorMessage('Productivity is not allowed on fridays.', {modal: true});
|
||||
} catch {
|
||||
// No op
|
||||
}
|
||||
timeout = undefined;
|
||||
}, 700);
|
||||
}
|
||||
});
|
||||
|
||||
context.subscriptions.push(disposable);
|
||||
}
|
||||
12
tabs-api-sample/tsconfig.json
Normal file
12
tabs-api-sample/tsconfig.json
Normal 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"]
|
||||
}
|
||||
15349
tabs-api-sample/vscode.d.ts
vendored
Normal file
15349
tabs-api-sample/vscode.d.ts
vendored
Normal file
File diff suppressed because it is too large
Load Diff
188
tabs-api-sample/vscode.proposed.tabs.d.ts
vendored
Normal file
188
tabs-api-sample/vscode.proposed.tabs.d.ts
vendored
Normal file
@ -0,0 +1,188 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
declare module 'vscode' {
|
||||
|
||||
// https://github.com/Microsoft/vscode/issues/15178
|
||||
|
||||
export class TabKindText {
|
||||
readonly uri: Uri;
|
||||
constructor(uri: Uri);
|
||||
}
|
||||
|
||||
export class TabKindTextDiff {
|
||||
readonly original: Uri;
|
||||
readonly modified: Uri;
|
||||
constructor(original: Uri, modified: Uri);
|
||||
}
|
||||
|
||||
export class TabKindCustom {
|
||||
readonly uri: Uri;
|
||||
readonly viewType: string;
|
||||
constructor(uri: Uri, viewType: string);
|
||||
}
|
||||
|
||||
export class TabKindWebview {
|
||||
/**
|
||||
* The type of webview. Maps to {@linkcode WebviewPanel.viewType WebviewPanel's viewType}
|
||||
*/
|
||||
readonly viewType: string;
|
||||
constructor(viewType: string);
|
||||
}
|
||||
|
||||
export class TabKindNotebook {
|
||||
readonly uri: Uri;
|
||||
readonly notebookType: string;
|
||||
constructor(uri: Uri, notebookType: string);
|
||||
}
|
||||
|
||||
export class TabKindNotebookDiff {
|
||||
readonly original: Uri;
|
||||
readonly modified: Uri;
|
||||
readonly notebookType: string;
|
||||
constructor(original: Uri, modified: Uri, notebookType: string);
|
||||
}
|
||||
|
||||
export class TabKindTerminal {
|
||||
constructor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a tab within the window
|
||||
*/
|
||||
export interface Tab {
|
||||
|
||||
/**
|
||||
* The text displayed on the tab
|
||||
*/
|
||||
readonly label: string;
|
||||
|
||||
/**
|
||||
* The group which the tab belongs to
|
||||
*/
|
||||
readonly group: TabGroup;
|
||||
|
||||
/**
|
||||
* Defines the structure of the tab i.e. text, notebook, custom, etc.
|
||||
* Resource and other useful properties are defined on the tab kind.
|
||||
*/
|
||||
readonly kind: TabKindText | TabKindTextDiff | TabKindCustom | TabKindWebview | TabKindNotebook | TabKindNotebookDiff | TabKindTerminal | unknown;
|
||||
|
||||
/**
|
||||
* Whether or not the tab is currently active.
|
||||
* This is dictated by being the selected tab in the group
|
||||
*/
|
||||
readonly isActive: boolean;
|
||||
|
||||
/**
|
||||
* Whether or not the dirty indicator is present on the tab
|
||||
*/
|
||||
readonly isDirty: boolean;
|
||||
|
||||
/**
|
||||
* Whether or not the tab is pinned (pin icon is present)
|
||||
*/
|
||||
readonly isPinned: boolean;
|
||||
|
||||
/**
|
||||
* Whether or not the tab is in preview mode.
|
||||
*/
|
||||
readonly isPreview: boolean;
|
||||
}
|
||||
|
||||
export namespace window {
|
||||
/**
|
||||
* Represents the grid widget within the main editor area
|
||||
*/
|
||||
export const tabGroups: TabGroups;
|
||||
}
|
||||
|
||||
export interface TabGroup {
|
||||
/**
|
||||
* Whether or not the group is currently active
|
||||
*/
|
||||
readonly isActive: boolean;
|
||||
|
||||
/**
|
||||
* The view column of the group
|
||||
*/
|
||||
readonly viewColumn: ViewColumn;
|
||||
|
||||
/**
|
||||
* The active tab in the group (this is the tab currently being rendered).
|
||||
* There can be one active tab per group. There can only be one active group.
|
||||
*/
|
||||
readonly activeTab: Tab | undefined;
|
||||
|
||||
/**
|
||||
* The list of tabs contained within the group.
|
||||
* This can be empty if the group has no tabs open.
|
||||
*/
|
||||
readonly tabs: readonly Tab[];
|
||||
}
|
||||
|
||||
export interface TabGroups {
|
||||
/**
|
||||
* All the groups within the group container
|
||||
*/
|
||||
readonly groups: readonly TabGroup[];
|
||||
|
||||
/**
|
||||
* The currently active group
|
||||
*/
|
||||
// TOD@API name: maybe `activeGroup` to align with `groups` (which isn't tabGroups)
|
||||
readonly activeTabGroup: TabGroup;
|
||||
|
||||
/**
|
||||
* An {@link Event event} which fires when {@link TabGroup tab groups} has changed.
|
||||
*/
|
||||
readonly onDidChangeTabGroups: Event<TabGroup[]>;
|
||||
|
||||
/**
|
||||
* An {@link Event event} which fires when a {@link Tab tabs} have changed.
|
||||
*/
|
||||
readonly onDidChangeTabs: Event<Tab[]>;
|
||||
|
||||
/**
|
||||
* An {@link Event} which fires when an active tab changes.
|
||||
* Similar to {@link TabGroup.onDidChangeTabs} but only on tabs
|
||||
* with isActive equal to true.
|
||||
*/
|
||||
readonly onDidChangeActiveTab: Event<Tab>;
|
||||
|
||||
/**
|
||||
* An {@link Event} which fires when the active group changes.
|
||||
* This does not fire when the properties within the group change.
|
||||
*/
|
||||
readonly onDidChangeActiveTabGroup: Event<TabGroup>;
|
||||
|
||||
/**
|
||||
* Closes the tab. This makes the tab object invalid and the tab
|
||||
* should no longer be used for further actions.
|
||||
* Note: In the case of a dirty tab, a confirmation dialog will be shown which may be cancelled. If cancelled the tab is still valid
|
||||
* @param tab The tab to close, must be reference equal to a tab given by the API
|
||||
* @param preserveFocus When `true` focus will remain in its current position. If `false` it will jump to the next tab.
|
||||
* @returns A promise that resolves true when then tab is closed. Otherwise it will return false.
|
||||
* If false is returned the tab is still valid.
|
||||
*/
|
||||
close(tab: Tab | Tab[], preserveFocus?: boolean): Thenable<boolean>;
|
||||
// TODO@API support to close "all"
|
||||
// close(tab: TabGroup | TabGroup[], preserveFocus?: boolean): Thenable<boolean>;
|
||||
|
||||
/**
|
||||
* Moves a tab to the given index within the column.
|
||||
* If the index is out of range, the tab will be moved to the end of the column.
|
||||
* If the column is out of range, a new one will be created after the last existing column.
|
||||
*
|
||||
* @package tab The tab to move.
|
||||
* @param viewColumn The column to move the tab into
|
||||
* @param index The index to move the tab to
|
||||
*/
|
||||
// TODO@API support TabGroup in addition to ViewColumn
|
||||
// TODO@API support just index for moving inside current group
|
||||
// TODO@API move a tag group
|
||||
move(tab: Tab, viewColumn: ViewColumn, index: number, preserveFocus?: boolean): Thenable<void>;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user