Skip to content

Commit

Permalink
Added paragraph counting
Browse files Browse the repository at this point in the history
Counts paragraphs based on the definition that a paragraph is a sequence of characters separated by 1 more more blank lines.
  • Loading branch information
adrian-alpha committed May 17, 2020
1 parent bad728d commit 04d780c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export function activate(ctx: ExtensionContext) {
${mode}: |
Words: ${wordCounter._getWordCount(doc, sel)} |
Characters (without spaces): ${wordCounter._getCharCount(doc, sel)} |
Sentences: ${wordCounter._getSentenceCount(doc, sel)}
Sentences: ${wordCounter._getSentenceCount(doc, sel)} |
Paragraphs: ${wordCounter._getParagraphCount(doc, sel)}
`);
});

Expand Down Expand Up @@ -118,6 +119,22 @@ export class WordCounter {
return sentenceCount;
}

//this function counts the number of paragraphs across the document by defining a paragraph as characters split by
// two or more new lines
public _getParagraphCount(doc: TextDocument, sel?: Selection): number {
let docContent = sel === undefined || sel.isEmpty ? doc.getText() : doc.getText(sel);
docContent += "\r\n\r\n" /* Added so trailing newlines at the end of the document
can be factored out regardless if the user inputted it or not. Otherwise,
if the user leaves 2 trailing newlines, the program will split it and add both
sides to the counter. */
let paragraphCount = 0;
if (docContent != "") {
paragraphCount = docContent.split(/[^\r\n][\r\n]{4}[\s\n\r]*/).length - 1;
}

return paragraphCount;
}

public dispose() {
this._statusBarItem.dispose();
}
Expand Down

0 comments on commit 04d780c

Please sign in to comment.