Pretter for each ts file

This commit is contained in:
Pine Wu
2018-10-10 09:37:51 -07:00
parent c945fe9288
commit 23674b2cfe
55 changed files with 1755 additions and 1479 deletions

View File

@ -1,7 +1,6 @@
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
// Example 1: Reading Window scoped configuration
const configuredView = vscode.workspace.getConfiguration().get('conf.view.showOnWindowOpen');
switch (configuredView) {
@ -24,22 +23,22 @@ export function activate(context: vscode.ExtensionContext) {
// Example 2: Updating Window scoped configuration
vscode.commands.registerCommand('config.commands.configureViewOnWindowOpen', async () => {
// 1) Getting the value
const value = await vscode.window.showQuickPick(['explorer', 'search', 'scm', 'debug', 'extensions'], { placeHolder: 'Select the view to show when opening a window.' });
const value = await vscode.window.showQuickPick(['explorer', 'search', 'scm', 'debug', 'extensions'], {
placeHolder: 'Select the view to show when opening a window.'
});
if (vscode.workspace.workspaceFolders) {
// 2) Getting the Configuration target
const target = await vscode.window.showQuickPick(
[
{ label: 'User', description: 'User Settings', target: vscode.ConfigurationTarget.Global },
{ label: 'Workspace', description: 'Workspace Settings', target: vscode.ConfigurationTarget.Workspace }
],
{ placeHolder: 'Select the view to show when opening a window.' });
{ placeHolder: 'Select the view to show when opening a window.' }
);
if (value && target) {
// 3) Update the configuration value in the target
await vscode.workspace.getConfiguration().update('conf.view.showOnWindowOpen', value, target.target);
@ -49,33 +48,31 @@ export function activate(context: vscode.ExtensionContext) {
*/
}
} else {
// 2) Update the configuration value in User Setting in case of no workspace folders
await vscode.workspace.getConfiguration().update('conf.view.showOnWindowOpen', value, vscode.ConfigurationTarget.Global);
await vscode.workspace
.getConfiguration()
.update('conf.view.showOnWindowOpen', value, vscode.ConfigurationTarget.Global);
}
});
// Example 3: Reading Resource scoped configuration for a file
context.subscriptions.push(vscode.workspace.onDidOpenTextDocument(e => {
context.subscriptions.push(
vscode.workspace.onDidOpenTextDocument(e => {
// 1) Get the configured glob pattern value for the current file
const value = vscode.workspace.getConfiguration('', e.uri).get('conf.resource.insertEmptyLastLine');
// 1) Get the configured glob pattern value for the current file
const value = vscode.workspace.getConfiguration('', e.uri).get('conf.resource.insertEmptyLastLine');
// 2) Check if the current resource matches the glob pattern
const matches = value[e.fileName];
// 2) Check if the current resource matches the glob pattern
const matches = value[e.fileName];
// 3) If matches, insert empty last line
if (matches) {
vscode.window.showInformationMessage('An empty line will be added to the document ' + e.fileName);
}
}));
// 3) If matches, insert empty last line
if (matches) {
vscode.window.showInformationMessage('An empty line will be added to the document ' + e.fileName);
}
})
);
// Example 4: Updating Resource scoped Configuration for current file
vscode.commands.registerCommand('config.commands.configureEmptyLastLineCurrentFile', async () => {
if (vscode.window.activeTextEditor) {
const currentDocument = vscode.window.activeTextEditor.document;
@ -86,41 +83,46 @@ export function activate(context: vscode.ExtensionContext) {
const currentValue = configuration.get('conf.resource.insertEmptyLastLine', {});
// 3) Choose target to Global when there are no workspace folders
const target = vscode.workspace.workspaceFolders ? vscode.ConfigurationTarget.WorkspaceFolder : vscode.ConfigurationTarget.Global;
const target = vscode.workspace.workspaceFolders
? vscode.ConfigurationTarget.WorkspaceFolder
: vscode.ConfigurationTarget.Global;
const value = { ...currentValue, ...{ [currentDocument.fileName]: true } };
// 4) Update the configuration
await configuration.update('conf.resource.insertEmptyLastLine', value, target)
await configuration.update('conf.resource.insertEmptyLastLine', value, target);
}
});
// Example 5: Updating Resource scoped Configuration
vscode.commands.registerCommand('config.commands.configureEmptyLastLineFiles', async () => {
// 1) Getting the value
const value = await vscode.window.showInputBox({ prompt: 'Provide glob pattern of files to have empty last line.' });
const value = await vscode.window.showInputBox({
prompt: 'Provide glob pattern of files to have empty last line.'
});
if (vscode.workspace.workspaceFolders) {
// 2) Getting the target
const target = await vscode.window.showQuickPick(
[
{ label: 'Application', description: 'User Settings', target: vscode.ConfigurationTarget.Global },
{ label: 'Workspace', description: 'Workspace Settings', target: vscode.ConfigurationTarget.Workspace },
{ label: 'Workspace Folder', description: 'Workspace Folder Settings', target: vscode.ConfigurationTarget.WorkspaceFolder }
{
label: 'Workspace Folder',
description: 'Workspace Folder Settings',
target: vscode.ConfigurationTarget.WorkspaceFolder
}
],
{ placeHolder: 'Select the target to which this setting should be applied' });
{ placeHolder: 'Select the target to which this setting should be applied' }
);
if (value && target) {
if (target.target === vscode.ConfigurationTarget.WorkspaceFolder) {
// 3) Getting the workspace folder
let workspaceFolder = await vscode.window.showWorkspaceFolderPick({ placeHolder: 'Pick Workspace Folder to which this setting should be applied' })
let workspaceFolder = await vscode.window.showWorkspaceFolderPick({
placeHolder: 'Pick Workspace Folder to which this setting should be applied'
});
if (workspaceFolder) {
// 4) Get the configuration for the workspace folder
const configuration = vscode.workspace.getConfiguration('', workspaceFolder.uri);
@ -133,7 +135,6 @@ export function activate(context: vscode.ExtensionContext) {
await configuration.update('conf.resource.insertEmptyLastLine', newValue, target.target);
}
} else {
// 3) Get the configuration
const configuration = vscode.workspace.getConfiguration();
@ -143,11 +144,12 @@ export function activate(context: vscode.ExtensionContext) {
const newValue = { ...currentValue, ...{ [value]: true } };
// 3) Update the value in the target
await vscode.workspace.getConfiguration().update('conf.resource.insertEmptyLastLine', newValue, target.target);
await vscode.workspace
.getConfiguration()
.update('conf.resource.insertEmptyLastLine', newValue, target.target);
}
}
} else {
// 2) Get the configuration
const configuration = vscode.workspace.getConfiguration();
@ -157,31 +159,35 @@ export function activate(context: vscode.ExtensionContext) {
const newValue = { ...currentValue, ...{ [value]: true } };
// 4) Update the value in the User Settings
await vscode.workspace.getConfiguration().update('conf.resource.insertEmptyLastLine', newValue, vscode.ConfigurationTarget.Global);
await vscode.workspace
.getConfiguration()
.update('conf.resource.insertEmptyLastLine', newValue, vscode.ConfigurationTarget.Global);
}
});
// Example 6: Listening to configuration changes
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(e => {
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('conf.resource.insertEmptyLastLine')) {
if (vscode.window.activeTextEditor) {
const currentDocument = vscode.window.activeTextEditor.document;
if (e.affectsConfiguration('conf.resource.insertEmptyLastLine')) {
if (vscode.window.activeTextEditor) {
// 1) Get the configured glob pattern value for the current file
const value = vscode.workspace
.getConfiguration('', currentDocument.uri)
.get('conf.resource.insertEmptyLastLine');
const currentDocument = vscode.window.activeTextEditor.document;
// 2) Check if the current resource matches the glob pattern
const matches = value[currentDocument.fileName];
// 1) Get the configured glob pattern value for the current file
const value = vscode.workspace.getConfiguration('', currentDocument.uri).get('conf.resource.insertEmptyLastLine');
// 2) Check if the current resource matches the glob pattern
const matches = value[currentDocument.fileName];
// 3) If matches, insert empty last line
if (matches) {
vscode.window.showInformationMessage('An empty line will be added to the document ' + currentDocument.fileName);
// 3) If matches, insert empty last line
if (matches) {
vscode.window.showInformationMessage(
'An empty line will be added to the document ' + currentDocument.fileName
);
}
}
}
}
}));
}
})
);
}