mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-06-13 07:10:26 +08:00
add example for language specific settings
This commit is contained in:
@ -2,10 +2,12 @@
|
||||
|
||||
This sample shows
|
||||
|
||||
- How to define a `window` and `resource` scoped configurations.
|
||||
- How to define a `window`, `resource` and `language-overridable` scoped configurations.
|
||||
- How to read and update a `window` scoped configuration
|
||||
- How to read a `resource` scoped configuration of a resource
|
||||
- How to update a value for `resource` scoped configuration of a resource
|
||||
- How to read a `language-overridable` scoped configuration
|
||||
- How to override a `language-overridable` scoped configuration under a language
|
||||
- How to listen to configuration changes
|
||||
- How to test it
|
||||
|
||||
@ -34,6 +36,17 @@ Explains how to test this extension in an Empty workspace
|
||||
- Run the command `Configure empty last line for files` and provide abosoulte path of another file
|
||||
- Value should be updated in User Settings
|
||||
|
||||
### Testing Language Specific configuration
|
||||
|
||||
- Set `"conf.language.showSize": true` in user settings
|
||||
- Open a file and you should see the size of the file in the status
|
||||
- Unset `conf.language.showSize`
|
||||
- Run the command `Configuration Sample: Configure show size for language`
|
||||
- Enter the language for which you want to configure this feature and press Enter.
|
||||
- Open a file with above configured language and size of the file is shown in status
|
||||
- Open a file with a different language and no status is shown.
|
||||
|
||||
|
||||
## Folder Workspace
|
||||
|
||||
Explains how to test this extension in a Folder workspace
|
||||
@ -57,6 +70,17 @@ Explains how to test this extension in a Folder workspace
|
||||
- Pick the target `User Settings` or `Workspace Settings` into which the value should be updated
|
||||
- Value should be updated in selected target
|
||||
|
||||
### Testing Language Specific configuration
|
||||
|
||||
- Set `"conf.language.showSize": true` in any settings (user, workspace)
|
||||
- Open a file and you should see the size of the file in the status
|
||||
- Unset `conf.language.showSize`
|
||||
- Run the command `Configuration Sample: Configure show size for language`
|
||||
- Enter the language for which you want to configure this feature and press Enter.
|
||||
- Open a file with above configured language and size of the file is shown in status
|
||||
- Open a file with a different language and no status is shown.
|
||||
|
||||
|
||||
### Multiroot Workspace
|
||||
|
||||
Explains how to test this extension in a Multiroot workspace
|
||||
@ -82,3 +106,13 @@ Explains how to test this extension in a Multiroot workspace
|
||||
- Selecting User or Workspace Settings should update the value in respective targets.
|
||||
- Selecting Workspace Folder Settings will show a Workspace Folder Picker
|
||||
- Picking a workspace folder should update the value in the respective folder settings file.
|
||||
|
||||
### Testing Language Specific configuration
|
||||
|
||||
- Set `"conf.language.showSize": true` in any settings (user, workspace or workspace folder)
|
||||
- Open a file and you should see the size of the file in the status
|
||||
- Unset `conf.language.showSize`
|
||||
- Run the command `Configuration Sample: Configure show size for language`
|
||||
- Enter the language for which you want to configure this feature and press Enter.
|
||||
- Open a file with above configured language and size of the file is shown in status
|
||||
- Open a file with a different language and no status is shown.
|
||||
|
||||
6
configuration-sample/package-lock.json
generated
6
configuration-sample/package-lock.json
generated
@ -308,9 +308,9 @@
|
||||
}
|
||||
},
|
||||
"typescript": {
|
||||
"version": "3.4.5",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz",
|
||||
"integrity": "sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==",
|
||||
"version": "3.7.5",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.5.tgz",
|
||||
"integrity": "sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw==",
|
||||
"dev": true
|
||||
},
|
||||
"wrappy": {
|
||||
|
||||
@ -42,6 +42,12 @@
|
||||
"default": {},
|
||||
"description": "Resource configuration: Configure files using glob patterns to have an empty last line always",
|
||||
"scope": "resource"
|
||||
},
|
||||
"conf.language.showSize": {
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
"description": "Shows the size of the document",
|
||||
"scope": "language-overridable"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -61,6 +67,11 @@
|
||||
"category": "Configuration Sample",
|
||||
"command": "config.commands.configureEmptyLastLineFiles",
|
||||
"title": "Configure empty last line for files"
|
||||
},
|
||||
{
|
||||
"category": "Configuration Sample",
|
||||
"command": "config.commands.overrideLanguageValue",
|
||||
"title": "Configure show size for language"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@ -2,7 +2,7 @@ import * as vscode from 'vscode';
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
|
||||
// Example 1: Reading Window scoped configuration
|
||||
// Example: Reading Window scoped configuration
|
||||
const configuredView = vscode.workspace.getConfiguration().get('conf.view.showOnWindowOpen');
|
||||
switch (configuredView) {
|
||||
case 'explorer':
|
||||
@ -22,7 +22,7 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Example 2: Updating Window scoped configuration
|
||||
// Example: Updating Window scoped configuration
|
||||
vscode.commands.registerCommand('config.commands.configureViewOnWindowOpen', async () => {
|
||||
|
||||
// 1) Getting the value
|
||||
@ -57,7 +57,7 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
|
||||
});
|
||||
|
||||
// Example 3: Reading Resource scoped configuration for a file
|
||||
// Example: Reading Resource scoped configuration for a file
|
||||
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument(e => {
|
||||
|
||||
// 1) Get the configured glob pattern value for the current file
|
||||
@ -73,7 +73,7 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
|
||||
}));
|
||||
|
||||
// Example 4: Updating Resource scoped Configuration for current file
|
||||
// Example: Updating Resource scoped Configuration for current file
|
||||
vscode.commands.registerCommand('config.commands.configureEmptyLastLineCurrentFile', async () => {
|
||||
|
||||
if (vscode.window.activeTextEditor) {
|
||||
@ -95,7 +95,7 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
}
|
||||
});
|
||||
|
||||
// Example 5: Updating Resource scoped Configuration
|
||||
// Example: Updating Resource scoped Configuration
|
||||
vscode.commands.registerCommand('config.commands.configureEmptyLastLineFiles', async () => {
|
||||
|
||||
// 1) Getting the value
|
||||
@ -121,7 +121,7 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
if (workspaceFolder) {
|
||||
|
||||
// 4) Get the configuration for the workspace folder
|
||||
const configuration = vscode.workspace.getConfiguration('', workspaceFolder.uri);
|
||||
const configuration = vscode.workspace.getConfiguration('', workspaceFolder);
|
||||
|
||||
// 5) Get the current value
|
||||
const currentValue = configuration.get('conf.resource.insertEmptyLastLine');
|
||||
@ -160,7 +160,36 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
}
|
||||
});
|
||||
|
||||
// Example 6: Listening to configuration changes
|
||||
let statusSizeDisposable: vscode.Disposable;
|
||||
// Example: Reading language overridable configuration for a document
|
||||
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument(e => {
|
||||
|
||||
if (statusSizeDisposable) {
|
||||
statusSizeDisposable.dispose();
|
||||
}
|
||||
|
||||
// 1) Check if showing size is configured for current file
|
||||
const showSize: any = vscode.workspace.getConfiguration('', e).get('conf.language.showSize');
|
||||
|
||||
// 3) If matches, insert empty last line
|
||||
if (showSize) {
|
||||
statusSizeDisposable = vscode.window.setStatusBarMessage(`${e.getText().length}`);
|
||||
}
|
||||
|
||||
}));
|
||||
|
||||
// Example: Overriding configuration value for a language
|
||||
context.subscriptions.push(vscode.commands.registerCommand('config.commands.overrideLanguageValue', async () => {
|
||||
|
||||
// 1) Getting the languge id
|
||||
const languageId = await vscode.window.showInputBox({ placeHolder: 'Enter the language id' });
|
||||
|
||||
// 2) Update
|
||||
vscode.workspace.getConfiguration('', { languageId }).update('conf.language.showSize', true, false, true);
|
||||
|
||||
}));
|
||||
|
||||
// Example: Listening to configuration changes
|
||||
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(e => {
|
||||
|
||||
if (e.affectsConfiguration('conf.resource.insertEmptyLastLine')) {
|
||||
@ -181,6 +210,16 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
}
|
||||
}
|
||||
|
||||
// Check if a language configuration is changed for a text document
|
||||
if (e.affectsConfiguration('conf.language.showSize', vscode.window.activeTextEditor)) {
|
||||
|
||||
}
|
||||
|
||||
// Check if a language configuration is changed for a language
|
||||
if (e.affectsConfiguration('conf.language.showSize', { languageId: 'typescript' })) {
|
||||
|
||||
}
|
||||
|
||||
}));
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user