From 27ff774697e0068cf9bcd67fa31601aa54f96baf Mon Sep 17 00:00:00 2001 From: aaron-bond Date: Sun, 20 May 2018 17:26:27 +0100 Subject: [PATCH] initial commit of plaintext functionality --- package.json | 6 ++++++ src/parser.ts | 26 +++++++++++++++++++++++--- src/test/samples/plaintext.txt | 5 +++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index a908a08..6aeb1a9 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "onLanguage:pascal", "onLanguage:perl", "onLanguage:perl6", + "onLanguage:plaintext", "onLanguage:plsql", "onLanguage:php", "onLanguage:powershell", @@ -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", diff --git a/src/parser.ts b/src/parser.ts index 5beae8c..b9c7e53 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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 @@ -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 += "("; @@ -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)) { @@ -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": @@ -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; diff --git a/src/test/samples/plaintext.txt b/src/test/samples/plaintext.txt index 5753a3e..0a19c1b 100644 --- a/src/test/samples/plaintext.txt +++ b/src/test/samples/plaintext.txt @@ -2,5 +2,6 @@ Hello world this is some plain text and here is a comment of my own design -:: ! my comment -:: comment \ No newline at end of file +! my comment +? comment +This is some more text \ No newline at end of file