Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions src/grammar/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createMatchers, Matcher } from '../matcher';
import { disposeOnigString, IOnigLib, OnigScanner, OnigString } from '../onigLib';
import { IRawGrammar, IRawRepository, IRawRule } from '../rawGrammar';
import { ruleIdFromNumber, IRuleFactoryHelper, IRuleRegistry, Rule, RuleFactory, RuleId, ruleIdToNumber } from '../rule';
import { COMPUTE_FONTS } from '../tests/themes.test';
import { FontStyle, ScopeName, ScopePath, ScopeStack, StyleAttributes } from '../theme';
import { clone, containsRTL } from '../utils';
import { BasicScopeAttributes, BasicScopeAttributesProvider } from './basicScopesAttributeProvider';
Expand Down Expand Up @@ -1135,6 +1136,14 @@ export class FontInfo implements IFontInfo {
}
}

export let TOTAL_PROPERTY_COUNT = 0;
export let TOTAL_GET_ATTR_CALLS = 0;

export function resetVariables() {
TOTAL_PROPERTY_COUNT = 0;
TOTAL_GET_ATTR_CALLS = 0;
}

export class LineFonts {

private readonly _fonts: FontInfo[] = [];
Expand All @@ -1151,14 +1160,12 @@ export class LineFonts {
scopesList: AttributedScopeStack | null,
endIndex: number
): void {
const styleAttributes = scopesList?.styleAttributes;
if (!styleAttributes) {
this._lastIndex = endIndex;
if (!COMPUTE_FONTS) {
return;
}
const fontFamily = styleAttributes.fontFamily;
const fontSizeMultiplier = styleAttributes.fontSize;
const lineHeightMultiplier = styleAttributes.lineHeight;
const fontFamily = this.getFontFamily(scopesList);
const fontSizeMultiplier = this.getFontSize(scopesList);
const lineHeightMultiplier = this.getLineHeight(scopesList);
if (!fontFamily && !fontSizeMultiplier && !lineHeightMultiplier) {
this._lastIndex = endIndex;
return;
Expand All @@ -1182,4 +1189,35 @@ export class LineFonts {
public getResult(): IFontInfo[] {
return this._fonts;
}

private getFontFamily(scopesList: AttributedScopeStack | null): string | null {
TOTAL_PROPERTY_COUNT++;
return this.getAttribute(scopesList, (styleAttributes) => { return styleAttributes.fontFamily; });
}

private getFontSize(scopesList: AttributedScopeStack | null): number | null {
TOTAL_PROPERTY_COUNT++;
return this.getAttribute(scopesList, (styleAttributes) => { return styleAttributes.fontSize; });
}

private getLineHeight(scopesList: AttributedScopeStack | null): number | null {
TOTAL_PROPERTY_COUNT++;
return this.getAttribute(scopesList, (styleAttributes) => { return styleAttributes.lineHeight; });
}

private getAttribute(scopesList: AttributedScopeStack | null, getAttr: (styleAttributes: StyleAttributes) => any | null): any | null {
TOTAL_GET_ATTR_CALLS++;
if (!scopesList) {
return null;
}
const styleAttributes = scopesList.styleAttributes;
if (!styleAttributes) {
return null;
}
const attribute = getAttr(styleAttributes);
if (attribute) {
return attribute;
}
return this.getAttribute(scopesList.parent, getAttr);
}
}
116 changes: 69 additions & 47 deletions src/tests/themes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
fontStyleToString,
StyleAttributes
} from '../theme';
import { ThemeTest } from './themeTest';
import { getOniguruma } from './onigLibs';
import { Resolver, IGrammarRegistration, ILanguageRegistration } from './resolver';
import { strArrCmp, strcmp } from '../utils';
import { parsePLIST } from '../plist';
import { resetVariables, TOTAL_GET_ATTR_CALLS, TOTAL_PROPERTY_COUNT } from '../grammar';

const THEMES_TEST_PATH = path.join(__dirname, '../../test-cases/themes');

Expand Down Expand Up @@ -70,57 +70,79 @@ class ThemeInfo {
}
}

(function () {
let THEMES = [
new ThemeInfo('abyss', 'Abyss.tmTheme'),
new ThemeInfo('dark_vs', 'dark_vs.json'),
new ThemeInfo('light_vs', 'light_vs.json'),
new ThemeInfo('hc_black', 'hc_black.json'),
new ThemeInfo('dark_plus', 'dark_plus.json', 'dark_vs.json'),
new ThemeInfo('light_plus', 'light_plus.json', 'light_vs.json'),
new ThemeInfo('kimbie_dark', 'Kimbie_dark.tmTheme'),
new ThemeInfo('monokai', 'Monokai.tmTheme'),
new ThemeInfo('monokai_dimmed', 'dimmed-monokai.tmTheme'),
new ThemeInfo('quietlight', 'QuietLight.tmTheme'),
new ThemeInfo('red', 'red.tmTheme'),
new ThemeInfo('solarized_dark', 'Solarized-dark.tmTheme'),
new ThemeInfo('solarized_light', 'Solarized-light.tmTheme'),
new ThemeInfo('tomorrow_night_blue', 'Tomorrow-Night-Blue.tmTheme'),
];
export let COMPUTE_FONTS: boolean = false;

test.only('Tokenize test.ts with TypeScript grammar and dark_vs theme', async () => {

resetVariables();

// Load dark_vs theme
const themeFile = path.join(THEMES_TEST_PATH, 'dark_vs.json');
const themeContent = fs.readFileSync(themeFile).toString();
const theme: IRawTheme = JSON.parse(themeContent);

// Load TypeScript grammar
const grammarsData: IGrammarRegistration[] = JSON.parse(fs.readFileSync(path.join(THEMES_TEST_PATH, 'grammars.json')).toString('utf8'));
const languagesData: ILanguageRegistration[] = JSON.parse(fs.readFileSync(path.join(THEMES_TEST_PATH, 'languages.json')).toString('utf8'));

// Load all language/grammar metadata
let _grammars: IGrammarRegistration[] = JSON.parse(fs.readFileSync(path.join(THEMES_TEST_PATH, 'grammars.json')).toString('utf8'));
for (let grammar of _grammars) {
// Update paths for grammars
for (let grammar of grammarsData) {
grammar.path = path.join(THEMES_TEST_PATH, grammar.path);
}

let _languages: ILanguageRegistration[] = JSON.parse(fs.readFileSync(path.join(THEMES_TEST_PATH, 'languages.json')).toString('utf8'));

let _resolver = new Resolver(_grammars, _languages, getOniguruma());
let _themeData = THEMES.map(theme => theme.create(_resolver));

// Discover all tests
let testFiles = fs.readdirSync(path.join(THEMES_TEST_PATH, 'tests'));
testFiles = testFiles.filter(testFile => !/\.result$/.test(testFile));
testFiles = testFiles.filter(testFile => !/\.result.patch$/.test(testFile));
testFiles = testFiles.filter(testFile => !/\.actual$/.test(testFile));
testFiles = testFiles.filter(testFile => !/\.diff.html$/.test(testFile));

for (let testFile of testFiles) {
let tst = new ThemeTest(THEMES_TEST_PATH, testFile, _themeData, _resolver);
test(tst.testName, async function () {
this.timeout(20000);
try {
await tst.evaluate();
assert.deepStrictEqual(tst.actual, tst.expected);
} catch(err) {
tst.writeExpected();
throw err;
}
});
}
const resolver = new Resolver(grammarsData, languagesData, getOniguruma());
const registry = new Registry(resolver);
registry.setTheme(theme);

// Load TypeScript grammar
const tsGrammar = await registry.loadGrammar('source.ts');
assert.ok(tsGrammar, 'TypeScript grammar should be loaded');

// Read test.ts file
const testFilePath = path.join(THEMES_TEST_PATH, 'fixtures/test.ts');
const testFileContent = fs.readFileSync(testFilePath).toString('utf8');
const lines = testFileContent.split(/\r\n|\r|\n/);

// Tokenize all lines
const tokenizeLine = () => {
let ruleStack = null;
const tokenizedLines: any[] = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const result = tsGrammar.tokenizeLine2(line, ruleStack);
ruleStack = result.ruleStack;
tokenizedLines.push({
line: i + 1,
tokens: result.tokens,
fonts: result.fonts
});
}
return tokenizedLines;
};

// Verify we tokenized all lines
COMPUTE_FONTS = false;
const startComputeFontsFalse = Date.now();
tokenizeLine();
const endComputeFontsFalse = Date.now();

console.log('Tokenization time without fonts (ms):', (endComputeFontsFalse - startComputeFontsFalse));

COMPUTE_FONTS = true;
const startComputeFontsTrue = Date.now();
tokenizeLine();
const endComputeFontsTrue = Date.now();

console.log('Tokenization time with fonts (ms):', (endComputeFontsTrue - startComputeFontsTrue));

const getAttrCallsPerPropertyCount = TOTAL_GET_ATTR_CALLS / TOTAL_PROPERTY_COUNT;

console.log('TOTAL_PROPERTY_COUNT : ', TOTAL_PROPERTY_COUNT);
console.log('TOTAL_GET_ATTR_CALLS : ', TOTAL_GET_ATTR_CALLS);

console.log('TOTAL_PROPERTY_COUNT / TOTAL_GET_ATTR_CALLS :', getAttrCallsPerPropertyCount);
});

})();

test('Theme matching gives higher priority to deeper matches', () => {
const theme = Theme.createFromRawTheme({
Expand Down
61 changes: 44 additions & 17 deletions test-cases/themes/dark_vs.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,28 @@
"foreground": "#000080"
}
},

{
"scope": "comment",
"settings": {
"foreground": "#608b4e"
"foreground": "#608b4e",
"fontSize": 12,
"lineHeight": 18
}
},
{
"scope": "constant.language",
"settings": {
"foreground": "#569cd6"
"foreground": "#569cd6",
"fontSize": 14,
"lineHeight": 20
}
},
{
"scope": "constant.numeric",
"settings": {
"foreground": "#b5cea8"
"foreground": "#b5cea8",
"fontSize": 13,
"lineHeight": 19
}
},
{
Expand All @@ -63,7 +68,9 @@
{
"scope": "entity.name.tag",
"settings": {
"foreground": "#569cd6"
"foreground": "#569cd6",
"fontSize": 14,
"lineHeight": 21
}
},
{
Expand All @@ -75,7 +82,9 @@
{
"scope": "entity.other.attribute-name",
"settings": {
"foreground": "#9cdcfe"
"foreground": "#9cdcfe",
"fontSize": 13,
"lineHeight": 19
}
},
{
Expand All @@ -86,9 +95,7 @@
"entity.other.attribute-name.parent-selector.css",
"entity.other.attribute-name.pseudo-class.css",
"entity.other.attribute-name.pseudo-element.css",

"source.css.less entity.other.attribute-name.id",

"entity.other.attribute-name.attribute.scss",
"entity.other.attribute-name.scss"
],
Expand Down Expand Up @@ -119,7 +126,9 @@
"scope": "markup.heading",
"settings": {
"fontStyle": "bold",
"foreground": "#569cd6"
"foreground": "#569cd6",
"fontSize": 16,
"lineHeight": 24
}
},
{
Expand Down Expand Up @@ -180,7 +189,9 @@
{
"scope": "meta.preprocessor",
"settings": {
"foreground": "#569cd6"
"foreground": "#569cd6",
"fontSize": 13,
"lineHeight": 18
}
},
{
Expand Down Expand Up @@ -210,7 +221,9 @@
{
"scope": "storage",
"settings": {
"foreground": "#569cd6"
"foreground": "#569cd6",
"fontSize": 14,
"lineHeight": 20
}
},
{
Expand All @@ -228,7 +241,9 @@
{
"scope": "string",
"settings": {
"foreground": "#ce9178"
"foreground": "#ce9178",
"fontSize": 13,
"lineHeight": 19
}
},
{
Expand Down Expand Up @@ -262,13 +277,17 @@
{
"scope": "support.type.property-name",
"settings": {
"foreground": "#9cdcfe"
"foreground": "#9cdcfe",
"fontSize": 13,
"lineHeight": 19
}
},
{
"scope": "keyword",
"settings": {
"foreground": "#569cd6"
"foreground": "#569cd6",
"fontSize": 14,
"lineHeight": 21
}
},
{
Expand All @@ -284,7 +303,10 @@
}
},
{
"scope": ["keyword.operator.new", "keyword.operator.expression"],
"scope": [
"keyword.operator.new",
"keyword.operator.expression"
],
"settings": {
"foreground": "#569cd6"
}
Expand Down Expand Up @@ -318,7 +340,10 @@
},
{
"name": "coloring of the Java import and package identifiers",
"scope": ["storage.modifier.import.java", "storage.modifier.package.java"],
"scope": [
"storage.modifier.import.java",
"storage.modifier.package.java"
],
"settings": {
"foreground": "#d4d4d4"
}
Expand All @@ -327,7 +352,9 @@
"name": "this.self",
"scope": "variable.language",
"settings": {
"foreground": "#569cd6"
"foreground": "#569cd6",
"fontSize": 14,
"lineHeight": 20
}
}
]
Expand Down
Loading