Skip to content

Commit

Permalink
feat: implement ignore blocks to allow for proper commented code and …
Browse files Browse the repository at this point in the history
…string text ignoring and improve elixir syntax matching
  • Loading branch information
polvalente committed Aug 16, 2019
1 parent a40790d commit aed0fbe
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "Apache-2.0",
"displayName": "Rainbow End",
"description": "This extension allows to identify keyword / end with colours.",
"version": "0.6.0",
"version": "0.7.0",
"icon": "images/logo.png",
"engines": {
"vscode": "^1.29.0"
Expand Down
40 changes: 32 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
'use strict';

import * as vscode from 'vscode';
import {languages} from './languages';
import { languages } from './languages';


const deepDecorations = [
vscode.window.createTextEditorDecorationType({
color: {id: "rainbowend.deep1"}
color: { id: "rainbowend.deep1" }
}),
vscode.window.createTextEditorDecorationType({
color: {id: "rainbowend.deep2"}
color: { id: "rainbowend.deep2" }
}),
vscode.window.createTextEditorDecorationType({
color: {id: "rainbowend.deep3"}
color: { id: "rainbowend.deep3" }
})
];

let timeout : NodeJS.Timer | null = null;
let regExs: { [index:string] : RegExp} = {};
let timeout: NodeJS.Timer | null = null;
let regExs: { [index: string]: RegExp } = {};

export function activate(context: vscode.ExtensionContext) {
Object.keys(languages).forEach(language => {
Expand Down Expand Up @@ -51,12 +51,31 @@ function triggerUpdateDecorations(activeEditor: vscode.TextEditor) {
}

function buildRegex(language: string) {

const languageConfiguration = languages[language];
let tokens: Array<string> = languageConfiguration["openTokens"];
tokens = tokens.concat(languageConfiguration["inlineOpenTokens"]);
tokens = tokens.concat(languageConfiguration["closeTokens"]);
tokens = tokens.concat(languageConfiguration["neutralTokens"]);
return RegExp("(\\b)(" + tokens.join('|') + ")(\\b)", "gm");
return RegExp("([^\\w]|^)(" + tokens.join('|') + ")([^\\w]|$)", "gm");
}

function ignoreInDelimiters(token_pairs: Array<{
open: string,
close: string
}> | undefined, text: string) {
if (token_pairs) {
token_pairs.forEach(({
open: open_delim,
close: close_delim
}) => {
let regexp = RegExp(`${open_delim}[^${close_delim}]*${close_delim}`, "gm");
text = text.replace(regexp, (match) => {
return " ".repeat(match.length);
});
})
}
return text;
}

function updateDecorations() {
Expand All @@ -78,11 +97,16 @@ function updateDecorations() {
if (!languageConfiguration.caseSensitive) {
text = text.toLowerCase();
}
// substitute all ignore intervals with spaces
// this ensures commented code or
// keywords inside strings are ignored properly

text = ignoreInDelimiters(languageConfiguration.ignoreInDelimiters, text);
while (match = regExs[activeEditor.document.languageId].exec(text)) {
const startIndex = match.index + match[1].length;
const startPos = activeEditor.document.positionAt(startIndex);
const endPos = activeEditor.document.positionAt(startIndex + match[2].length);
const decoration: vscode.DecorationOptions = { range: new vscode.Range(startPos, endPos) };
const decoration: vscode.DecorationOptions = { range: new vscode.Range(startPos, endPos) };

if (languageConfiguration.closeTokens.indexOf(match[2]) > -1) {
if (deep > 0) {
Expand Down
37 changes: 35 additions & 2 deletions src/languages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export const languages: {
[index: string]: {
caseSensitive: boolean,
ignoreInDelimiters?: Array<{
open: string,
close: string
}>,
inlineOpenTokens: Array<string>,
openTokens: Array<string>,
closeTokens: Array<string>,
Expand Down Expand Up @@ -53,11 +57,31 @@ export const languages: {
},
elixir: {
caseSensitive: true,
ignoreInDelimiters: [{
open: "#",
close: "\n"
},
{
open: '"""',
close: '"""'
}, {
open: '"',
close: '"'
},
{
open: "'",
close: "'"
},
],
inlineOpenTokens: [],
openTokens: [
"fn",
"defmodule",
"defmacro",
"def",
"defmacro(?=.+do)",
"defmacrop(?=.+do)",
"def(?=.+do)",
"defp(?=.+do)",
"(?<!\w+)do",
"if",
"while",
"for",
Expand All @@ -67,12 +91,21 @@ export const languages: {
"try",
"quote",
"with",
"defprotocol",
"defimpl",
"schema",
"embedded_schema",
"resources(?=.+do)",
"scope(?=.+do)"
],
closeTokens: [
"end",
"do:"
],
neutralTokens: [
"do",
"->",
"<-",
"else",
"elseif",
"rescue",
Expand Down

0 comments on commit aed0fbe

Please sign in to comment.