Skip to content

Commit

Permalink
initial commit of plaintext functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-bond committed May 20, 2018
1 parent ea150de commit 27ff774
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"onLanguage:pascal",
"onLanguage:perl",
"onLanguage:perl6",
"onLanguage:plaintext",
"onLanguage:plsql",
"onLanguage:php",
"onLanguage:powershell",
Expand Down Expand Up @@ -101,6 +102,11 @@
"description": "Whether the multiline comment highlighter should be active",
"default": true
},
"better-comments.highlightPlainText": {
"type": "boolean",
"description": "Whether the plaintext comment highlighter should be active",
"default": false
},
"better-comments.tags": {
"type": "array",
"description": "Tags which are used to color the comments. Changes require a restart of VS Code to take effect",
Expand Down
26 changes: 23 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ export class Parser {
private tags: CommentTag[] = [];
private expression: string = "";
private delimiter: string = "";

private highlightMultilineComments = false;

// * this will allow plaintext files to show comment highlighting if switched on
private isPlainText = false;

// * this is used to prevent the first line of the file (specifically python) from coloring like other comments
private ignoreFirstLine = false;

// * this is used to trigger the events when a supported language code is found
Expand All @@ -47,8 +53,13 @@ export class Parser {
characters.push(commentTag.escapedTag);
}

// start by finding the delimiter (//, --, #, ') with optional spaces or tabs
this.expression = "(" + this.delimiter.replace(/\//ig, "\\/") + ")+( |\t)*";
if (this.isPlainText) {
// start by tying the regex to the first character in a line
this.expression = "(^)+([ \\t]*[ \\t]*)";
} else {
// start by finding the delimiter (//, --, #, ') with optional spaces or tabs
this.expression = "(" + this.delimiter.replace(/\//ig, "\\/") + ")+( |\t)*";
}

// Apply all configurable comment start tags
this.expression += "(";
Expand All @@ -62,7 +73,10 @@ export class Parser {
*/
public FindSingleLineComments(activeEditor: vscode.TextEditor): void {
let text = activeEditor.document.getText();
let regEx = new RegExp(this.expression, "ig");

// if it's plain text, we have to do mutliline regex to catch the start of the line with ^
let regexFlags = (this.isPlainText) ? "igm" : "ig";
let regEx = new RegExp(this.expression, regexFlags);

let match: any;
while (match = regEx.exec(text)) {
Expand Down Expand Up @@ -161,6 +175,8 @@ export class Parser {
*/
private setDelimiter(languageCode: string): void {
this.supportedLanguage = true;
this.ignoreFirstLine = false;
this.isPlainText = false;

switch (languageCode) {
case "al":
Expand Down Expand Up @@ -240,6 +256,10 @@ export class Parser {
this.delimiter = "#";
this.highlightMultilineComments = this.contributions.multilineComments;
break;

case "plaintext":
this.isPlainText = true;
break;

default:
this.supportedLanguage = false;
Expand Down
5 changes: 3 additions & 2 deletions src/test/samples/plaintext.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ Hello world
this is some plain text
and here is a comment of my own design

:: ! my comment
:: comment
! my comment
? comment
This is some more text

0 comments on commit 27ff774

Please sign in to comment.