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 5, 2019
1 parent a40790d commit c7ebd43
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ function buildRegex(language: string) {
return RegExp("(\\b)(" + tokens.join('|') + ")(\\b)", "gm");
}

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

function updateDecorations() {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
Expand All @@ -78,6 +94,11 @@ 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, text);
while (match = regExs[activeEditor.document.languageId].exec(text)) {
const startIndex = match.index + match[1].length;
const startPos = activeEditor.document.positionAt(startIndex);
Expand Down
28 changes: 28 additions & 0 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,30 @@ export const languages: {
},
elixir: {
caseSensitive: true,
ignoreInDelimiters: [{
open: "#",
close: "\n"
},
{
open: '"""',
close: '"""'
}, {
open: '"',
close: '"'
},
{
open: "'",
close: "'"
},
],
inlineOpenTokens: [],
openTokens: [
"fn",
"defmodule",
"defmacro",
"defmacrop",
"def",
"defp",
"if",
"while",
"for",
Expand All @@ -67,12 +90,17 @@ export const languages: {
"try",
"quote",
"with",
"defprotocol",
"defimpl",
],
closeTokens: [
"end",
", do"
],
neutralTokens: [
"do",
" -> ",
" <- ",
"else",
"elseif",
"rescue",
Expand Down

0 comments on commit c7ebd43

Please sign in to comment.