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

feat: support other languages in ignore blocks
  • Loading branch information
polvalente committed Aug 20, 2019
1 parent a40790d commit 7154672
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 217 deletions.
38 changes: 24 additions & 14 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
## 0.7.0

By https://github.com/polvalente

- Improve Elixir support
- Add code block ignore functionality
- Add said ignore blocks for some of the supported languages
- Allow matching for the first character in the document

## 0.6.0

By https://github.com/spgennard:
* Support for case insensitive language
* Cobol support

- Support for case insensitive language
- Cobol support

## 0.5.4

* Fixes for Verilog
- Fixes for Verilog

## 0.5.3

* Fixes for VHDL
- Fixes for VHDL

## 0.5.2

* Fix do-end for crystal
- Fix do-end for crystal

## 0.5.1

* Add loop keyword for ruby
* Add with and quote keywords for Elixir (#4)
- Add loop keyword for ruby
- Add with and quote keywords for Elixir (#4)

## 0.5.0

* Verilog support
- Verilog support

## 0.4.0

* Upgrade dependencies
* Add a dedicated changelog
* Add support for the ruby keyword unless
* Ruby inline if & unless support (Fix #2)
- Upgrade dependencies
- Add a dedicated changelog
- Add support for the ruby keyword unless
- Ruby inline if & unless support (Fix #2)

### 0.3.0

* Add support for Crystal language
* Add support for Shell language
- Add support for Crystal language
- Add support for Shell language

### 0.2.0

Expand Down
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
43 changes: 35 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,6 +51,7 @@ function triggerUpdateDecorations(activeEditor: vscode.TextEditor) {
}

function buildRegex(language: string) {

const languageConfiguration = languages[language];
let tokens: Array<string> = languageConfiguration["openTokens"];
tokens = tokens.concat(languageConfiguration["inlineOpenTokens"]);
Expand All @@ -59,6 +60,24 @@ function buildRegex(language: string) {
return RegExp("(\\b)(" + tokens.join('|') + ")(\\b)", "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() {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
Expand All @@ -78,11 +97,19 @@ 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

// also, prepend a whitespace to allow matching the first character in document
// if needed

text = ' ' + ignoreInDelimiters(languageConfiguration.ignoreInDelimiters, text);
while (match = regExs[activeEditor.document.languageId].exec(text)) {
const startIndex = match.index + match[1].length;
const startIndex = match.index + match[1].length - 1; // Decrement to compensate for added character
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
Loading

0 comments on commit 7154672

Please sign in to comment.