Skip to content

Commit

Permalink
Pretter for each ts file
Browse files Browse the repository at this point in the history
  • Loading branch information
octref committed Oct 10, 2018
1 parent c945fe9 commit 23674b2
Show file tree
Hide file tree
Showing 55 changed files with 1,889 additions and 1,613 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"useTabs": true,
"printWidth": 120,
"singleQuote": true
}
120 changes: 65 additions & 55 deletions basic-multi-root-sample/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,81 @@

'use strict';

import { ExtensionContext, StatusBarAlignment, window, StatusBarItem, Selection, workspace, TextEditor, commands } from 'vscode';
import {
ExtensionContext,
StatusBarAlignment,
window,
StatusBarItem,
Selection,
workspace,
TextEditor,
commands
} from 'vscode';
import { basename } from 'path';

export function activate(context: ExtensionContext) {
// Create a status bar item
const status = window.createStatusBarItem(StatusBarAlignment.Left, 1000000);
context.subscriptions.push(status);

// Create a status bar item
const status = window.createStatusBarItem(StatusBarAlignment.Left, 1000000);
context.subscriptions.push(status);
// Update status bar item based on events for multi root folder changes
context.subscriptions.push(workspace.onDidChangeWorkspaceFolders(e => updateStatus(status)));

// Update status bar item based on events for multi root folder changes
context.subscriptions.push(workspace.onDidChangeWorkspaceFolders(e => updateStatus(status)));
// Update status bar item based on events for configuration
context.subscriptions.push(workspace.onDidChangeConfiguration(e => this.updateStatus(status)));

// Update status bar item based on events for configuration
context.subscriptions.push(workspace.onDidChangeConfiguration(e => this.updateStatus(status)));
// Update status bar item based on events around the active editor
context.subscriptions.push(window.onDidChangeActiveTextEditor(e => updateStatus(status)));
context.subscriptions.push(window.onDidChangeTextEditorViewColumn(e => updateStatus(status)));
context.subscriptions.push(workspace.onDidOpenTextDocument(e => updateStatus(status)));
context.subscriptions.push(workspace.onDidCloseTextDocument(e => updateStatus(status)));

// Update status bar item based on events around the active editor
context.subscriptions.push(window.onDidChangeActiveTextEditor(e => updateStatus(status)));
context.subscriptions.push(window.onDidChangeTextEditorViewColumn(e => updateStatus(status)));
context.subscriptions.push(workspace.onDidOpenTextDocument(e => updateStatus(status)));
context.subscriptions.push(workspace.onDidCloseTextDocument(e => updateStatus(status)));

updateStatus(status);
updateStatus(status);
}

function updateStatus(status: StatusBarItem): void {
const info = getEditorInfo();
status.text = info ? info.text : void 0;
status.tooltip = info ? info.tooltip : void 0;
status.color = info ? info.color : void 0;

if (info) {
status.show();
} else {
status.hide();
}
const info = getEditorInfo();
status.text = info ? info.text : void 0;
status.tooltip = info ? info.tooltip : void 0;
status.color = info ? info.color : void 0;

if (info) {
status.show();
} else {
status.hide();
}
}

function getEditorInfo(): { text: string; tooltip: string; color: string; } {
const editor = window.activeTextEditor;

// If no workspace is opened or just a single folder, we return without any status label
// because our extension only works when more than one folder is opened in a workspace.
if (!editor || !workspace.workspaceFolders || workspace.workspaceFolders.length < 2) {
return null;
}

let text: string;
let tooltip: string;
let color: string;

// If we have a file:// resource we resolve the WorkspaceFolder this file is from and update
// the status accordingly.
const resource = editor.document.uri;
if (resource.scheme === 'file') {
const folder = workspace.getWorkspaceFolder(resource);
if (!folder) {
text = `$(alert) <outside workspace> → ${basename(resource.fsPath)}`;
} else {
text = `$(file-submodule) ${basename(folder.uri.fsPath)} (${folder.index + 1} of ${workspace.workspaceFolders.length}) → $(file-code) ${basename(resource.fsPath)}`;
tooltip = resource.fsPath;

const multiRootConfigForResource = workspace.getConfiguration('multiRootSample', resource);
color = multiRootConfigForResource.get('statusColor');
}
}

return { text, tooltip, color };
function getEditorInfo(): { text: string; tooltip: string; color: string } {
const editor = window.activeTextEditor;

// If no workspace is opened or just a single folder, we return without any status label
// because our extension only works when more than one folder is opened in a workspace.
if (!editor || !workspace.workspaceFolders || workspace.workspaceFolders.length < 2) {
return null;
}

let text: string;
let tooltip: string;
let color: string;

// If we have a file:// resource we resolve the WorkspaceFolder this file is from and update
// the status accordingly.
const resource = editor.document.uri;
if (resource.scheme === 'file') {
const folder = workspace.getWorkspaceFolder(resource);
if (!folder) {
text = `$(alert) <outside workspace> → ${basename(resource.fsPath)}`;
} else {
text = `$(file-submodule) ${basename(folder.uri.fsPath)} (${folder.index + 1} of ${
workspace.workspaceFolders.length
}) → $(file-code) ${basename(resource.fsPath)}`;
tooltip = resource.fsPath;

const multiRootConfigForResource = workspace.getConfiguration('multiRootSample', resource);
color = multiRootConfigForResource.get('statusColor');
}
}

return { text, tooltip, color };
}
24 changes: 13 additions & 11 deletions completions-sample/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {

// The most simple completion item provider which
// The most simple completion item provider which
// * registers for text files (`'plaintext'`), and
// * return the 'Hello World' and
// * return the 'Hello World' and
// a snippet-based completion item.
vscode.languages.registerCompletionItemProvider('plaintext', {
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext) {
return [
new vscode.CompletionItem('Hello World!'),
createSnippetItem()
];
provideCompletionItems(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken,
context: vscode.CompletionContext
) {
return [new vscode.CompletionItem('Hello World!'), createSnippetItem()];
}
});

function createSnippetItem(): vscode.CompletionItem {

// Read more here:
// https://code.visualstudio.com/docs/extensionAPI/vscode-api#CompletionItem
// https://code.visualstudio.com/docs/extensionAPI/vscode-api#SnippetString
Expand All @@ -31,8 +31,10 @@ export function activate(context: vscode.ExtensionContext) {
// https://code.visualstudio.com/docs/editor/userdefinedsnippets#_creating-your-own-snippets

let item = new vscode.CompletionItem('Good part of the day', vscode.CompletionItemKind.Snippet);
item.insertText = new vscode.SnippetString("Good ${1|morning,afternoon,evening|}. It is ${1}, right?");
item.documentation = new vscode.MarkdownString("Inserts a snippet that lets you select the _appropriate_ part of the day for your greeting.");
item.insertText = new vscode.SnippetString('Good ${1|morning,afternoon,evening|}. It is ${1}, right?');
item.documentation = new vscode.MarkdownString(
'Inserts a snippet that lets you select the _appropriate_ part of the day for your greeting.'
);

return item;
}
Expand Down
Loading

0 comments on commit 23674b2

Please sign in to comment.