Skip to content

Commit

Permalink
Sample complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean McBreen committed Oct 9, 2015
1 parent 4ae5f20 commit f99266a
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
out
node_modules
16 changes: 16 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,16 @@
// A launch configuration that compiles the extension and then opens it inside a new window
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch Extension",
"type": "extensionHost",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "out",
"preLaunchTask": "npm"
}
]
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
@@ -0,0 +1,6 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"out": false // set this to true to hide the "out" folder with the compiled JS files
}
}
30 changes: 30 additions & 0 deletions .vscode/tasks.json
@@ -0,0 +1,30 @@
// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process

// A task runner that calls a custom npm script that compiles the extension.
{
"version": "0.1.0",

// we want to run npm
"command": "npm",

// the command is a shell script
"isShellCommand": true,

// show the output window only if unrecognized errors occur.
"showOutput": "silent",

// we run the custom script "compile" as defined in package.json
"args": ["run", "compile"],

// The tsc compiler is started in watching mode
"isWatching": true,

// use the standard tsc in watch mode problem matcher to find compile problems in the output.
"problemMatcher": "$tsc-watch"
}
6 changes: 6 additions & 0 deletions .vscodeignore
@@ -0,0 +1,6 @@
.vscode/**
typings/**
**/*.ts
.gitignore
tsconfig.json
vsc-extension-quickstart.md
16 changes: 14 additions & 2 deletions README.md
@@ -1,2 +1,14 @@
# vscode-wordcount
Sample Word Count extension for VS Code.
# README
## This the readme for your extension "Markdown-Tools"
-------------------
To author your README use Visual Studio Code:

* Split the editor (`Cmd+\` on OSX or `Ctrl+\` on Windows and Linux)
* Toggle preview (`Shift+CMD+V` on OSX or `Shift+Ctrl+V` on Windows and Linux)
* Press `Ctrl+Space` (Windows, Linux) or `Cmd+Space` (OSX) to see a list of Markdown snippets

### For more information
* [Visual Studio Code's Markdown Support](http://code.visualstudio.com/docs/languages/markdown)
* [Markdown Syntax Reference](http://daringfireball.net)

** Enjoy!**
90 changes: 90 additions & 0 deletions extension.ts
@@ -0,0 +1,90 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import {window, workspace, commands, Disposable} from 'vscode';

// this method is called when your extension is activated. activation is
// controlled by the activation events defined in package.json
export function activate(disposables: Disposable[]) {

// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "Markdown-Tools" is now active!');

// create a new word counter
let wordCounter = new WordCounter();
let controller = new WordCounterController(wordCounter);

// add to a list of disposables which are disposed when this extension
// is deactivated again.
disposables.push(controller);
disposables.push(wordCounter);
}

class WordCounter {

private _statusBarMessage: Disposable;

dispose() {
this.hideWordCount();
}

public showWordCount() {

// Remove previous status bar message
this.hideWordCount();

// Get the current text editor
let editor = window.getActiveTextEditor();
if (!editor) {
return;
}

let doc = editor.getTextDocument();

// Only update status if an MD file
if (doc.getLanguageId() === "markdown") {
let docContent = doc.getText();

// Parse out unwanted whitespace so the split is accurate
docContent = docContent.replace(/(< ([^>]+)<)/g, '').replace(/\s+/g, ' ');
docContent = docContent.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
let wordCount = docContent.split(" ").length;

// Update the status bar
this._statusBarMessage = window.setStatusBarMessage(wordCount !== 1 ? `${wordCount} Words` : '1 Word');
}
}

public hideWordCount() {
if (this._statusBarMessage) {
this._statusBarMessage.dispose();
}
}
}

class WordCounterController {

private _wordCounter: WordCounter;
private _disposable: Disposable;

constructor(wordCounter: WordCounter) {
this._wordCounter = wordCounter;
this._wordCounter.showWordCount();

// subscribe to selection change and editor activation events
let subscriptions: Disposable[] = [];
window.onDidChangeTextEditorSelection(this._onEvent, this, subscriptions);
window.onDidChangeActiveTextEditor(this._onEvent, this, subscriptions);

// create a combined disposable from both event subscriptions
this._disposable = Disposable.of(...subscriptions);
}

dispose() {
this._disposable.dispose();
}

private _onEvent() {
this._wordCounter.showWordCount();
}
}
19 changes: 19 additions & 0 deletions package.json
@@ -0,0 +1,19 @@
{
"name": "Markdown-Tools",
"version": "0.0.1",
"publisher": "SeanMcBreen",
"activationEvents": [
"onLanguage:markdown"
],
"engines": {
"vscode": "*"
},
"main": "./out/extension",
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
},
"devDependencies": {
"vscode": "*"
}
}
11 changes: 11 additions & 0 deletions tsconfig.json
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "commonjs",
"outDir": "out",
"noLib": true,
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
1 change: 1 addition & 0 deletions typings/vscode-typings.d.ts
@@ -0,0 +1 @@
/// <reference path="../node_modules/vscode/typings/index.d.ts" />
19 changes: 19 additions & 0 deletions vsc-extension-quickstart.md
@@ -0,0 +1,19 @@
# Welcome to your first VS Code Extension

## What's in the folder
* This folder contains all of the files necessary for your extension
* package.json - this is the manifest file in which you declare your extension and command. The sample plugin registers a command and defines its title and command name. With this information VS Code can show the command in the command palette. It doesn’t yet need to load the plugin.
* extension.ts - this is the main file where you will provide the implementation of your command. The file exports one function, activate, which is called the very first time your extension is activated (in this case by executing the command). Inside the activate function we call registerCommand. We pass the function containing the implementation of the command as the second parameter to registerCommand

## Get up and running straight away
* press F5 to open a new window with your extension loaded
* run your command from the command palette by pressing F1 and typing 'Hello World'
* set breakpoints in your code inside extension.ts to debug your extension
* find output from your extension in the debug console

## Make changes
* you can relaunch the extension from the debug toolbar after changing code in extension.ts
* you can also reload (`Ctrl+R` or `Cmd+R` on Mac) the VS Code window with your extension to load your changes

## Explore the API
* you can open the full set of our API when you open the file node_modules/vscode/vscode.d.ts

0 comments on commit f99266a

Please sign in to comment.