Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selected word count #25

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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';
jfgilmore marked this conversation as resolved.
Show resolved Hide 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