Skip to content

Commit 4ba871c

Browse files
authored
Fix incorrect syntax highlighting for variables starting with "function" (#992)
* closes #982 * fixed pathing bug in testing
1 parent 26c0887 commit 4ba871c

2 files changed

Lines changed: 74 additions & 1 deletion

File tree

src/test/suite/syntax.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
import { readFileSync } from 'fs';
4+
import { join } from 'path';
5+
import * as assert from 'assert';
6+
7+
interface syntaxFile {
8+
'repository': {
9+
'function-declarations': {
10+
'patterns': [
11+
{
12+
'match':string
13+
}
14+
]
15+
}
16+
}
17+
}
18+
19+
20+
/**
21+
* Converts a POSIX regular expression string into a format
22+
* that JavaScript can use. This is because vscode parses
23+
* syntax files using POSIX but JavaScript can't support it.
24+
* @param {string} s - A string to be used as a regular expression
25+
*/
26+
const regex_from_posix = function (s: string): string {
27+
const mappings = [
28+
['[:alnum:]', 'a-zA-Z0-9'],
29+
['[:alpha:]', 'a-zA-Z']
30+
];
31+
let s2 = s;
32+
mappings.forEach((el) => {
33+
s2 = s2.replace(el[0], el[1]);
34+
});
35+
return s2;
36+
};
37+
38+
39+
const extension_root: string = join(__dirname, '..', '..', '..');
40+
41+
const r_syntax_file: string = join(extension_root, 'syntax', 'r.json');
42+
console.log(r_syntax_file);
43+
const rsyntax_raw = readFileSync(r_syntax_file) as unknown;
44+
const rsyntax: syntaxFile = JSON.parse(rsyntax_raw as string) as syntaxFile;
45+
46+
const function_pattern: string = rsyntax.repository['function-declarations'].patterns[0].match;
47+
const function_pattern_fixed: string = regex_from_posix(function_pattern);
48+
49+
50+
suite('Syntax Highlighting', () => {
51+
52+
test('function-declarations - basic match', () => {
53+
const re = new RegExp(function_pattern_fixed);
54+
const line = 'x <- function(x) {';
55+
const match = re.exec(line);
56+
assert.strictEqual(match[3], 'function');
57+
});
58+
59+
test('function-declarations - extra spacing', () => {
60+
const re = new RegExp(function_pattern_fixed);
61+
const line = 'x <- function (x) {';
62+
const match = re.exec(line);
63+
assert.strictEqual(match[3], 'function');
64+
});
65+
66+
test('function-declarations - false function', () => {
67+
const re = new RegExp(function_pattern_fixed);
68+
const line = 'x <- functions';
69+
const match = re.exec(line);
70+
assert.strictEqual(match, null);
71+
});
72+
73+
});

syntax/r.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@
440440
"function-declarations": {
441441
"patterns": [
442442
{
443-
"match": "((?:`[^`\\\\]*(?:\\\\.[^`\\\\]*)*`)|(?:[[:alpha:].][[:alnum:]._]*))\\s*(<?<-|=(?!=))\\s*(function)",
443+
"match": "((?:`[^`\\\\]*(?:\\\\.[^`\\\\]*)*`)|(?:[[:alpha:].][[:alnum:]._]*))\\s*(<?<-|=(?!=))\\s*(function)(?!\\w)",
444444
"captures": {
445445
"1": {
446446
"name": "entity.name.function.r"

0 commit comments

Comments
 (0)