Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export class WordCounter {
private _statusBarItem: StatusBarItem;

public updateWordCount() {

// Create as needed
if (!this._statusBarItem) {
this._statusBarItem = window.createStatusBarItem(StatusBarAlignment.Left);
}
}

// Get the current text editor
let editor = window.activeTextEditor;
Expand All @@ -39,21 +39,23 @@ export class WordCounter {
}

let doc = editor.document;
let sel = editor.selection;

// Only update status if an MD file
if (doc.languageId === "markdown") {
let wordCount = this._getWordCount(doc);
let selectedWordCount = this._getWordCount(doc, sel);

// Update the status bar
this._statusBarItem.text = wordCount !== 1 ? `$(pencil) ${wordCount} Words` : '$(pencil) 1 Word';
this._statusBarItem.text = wordCount !== 1 ? `$(pencil) ${wordCount} Words | ${selectedWordCount} selected` : '$(pencil) 1 Word';
Comment thread
jfgilmore marked this conversation as resolved.
this._statusBarItem.show();
} else {
this._statusBarItem.hide();
}
}

public _getWordCount(doc: TextDocument): number {
let docContent = doc.getText();
public _getWordCount(doc: TextDocument, sel?: Selection): number {
let docContent = sel === undefined ? doc.getText() : doc.getText(sel);

// Parse out unwanted whitespace so the split is accurate
docContent = docContent.replace(/(< ([^>]+)<)/g, '').replace(/\s+/g, ' ');
Expand Down