fix: adaptive highlighting

- transpiler now uses m function with globalOffset
- patternifyAST now accepts global offset
- patternifyAST now calls .withLoc with global leaf location
- .withLoc replaces .withLocation + .withMiniLocation
- simple locs (offsets) are now used everywhere
- some tests fail, seems some haps have reordered...
- wip: Repl still uses hardcoded updateMiniLocations
- todo: find way to call updateMiniLocations dynamically
This commit is contained in:
Felix Roos 2023-07-03 05:15:32 +02:00
parent 08abec8fd5
commit 0b5d905120
9 changed files with 108 additions and 110 deletions

View file

@ -11,13 +11,13 @@ const simple = { wrapAsync: false, addReturn: false, simpleLocs: true };
describe('transpiler', () => {
it('wraps double quote string with mini and adds location', () => {
expect(transpiler('"c3"', simple).output).toEqual("mini('c3').withMiniLocation(0, 4);");
expect(transpiler('"c3"', simple).output).toEqual("m('c3', 0);");
expect(transpiler('stack("c3","bd sd")', simple).output).toEqual(
"stack(mini('c3').withMiniLocation(6, 10), mini('bd sd').withMiniLocation(11, 18));",
"stack(m('c3', 6), m('bd sd', 11));",
);
});
it('wraps backtick string with mini and adds location', () => {
expect(transpiler('`c3`', simple).output).toEqual("mini('c3').withMiniLocation(0, 4);");
expect(transpiler('`c3`', simple).output).toEqual("m('c3', 0);");
});
it('replaces note variables with note strings', () => {
expect(transpiler('seq(c3, d3)', simple).output).toEqual("seq('c3', 'd3');");

View file

@ -5,7 +5,7 @@ import { isNoteWithOctave } from '@strudel.cycles/core';
import { getLeafLocations } from '@strudel.cycles/mini';
export function transpiler(input, options = {}) {
const { wrapAsync = false, addReturn = true, simpleLocs = false, emitMiniLocations = true } = options;
const { wrapAsync = false, addReturn = true, emitMiniLocations = true } = options;
let ast = parse(input, {
ecmaVersion: 2022,
@ -15,9 +15,9 @@ export function transpiler(input, options = {}) {
let miniLocations = [];
const collectMiniLocations = (value, node) => {
const leafLocs = getLeafLocations(`"${value}"`);
const withOffset = leafLocs.map((offsets) => offsets.map((o) => o + node.start));
miniLocations = miniLocations.concat(withOffset);
const leafLocs = getLeafLocations(`"${value}"`, node.start); // stimmt!
//const withOffset = leafLocs.map((offsets) => offsets.map((o) => o + node.start));
miniLocations = miniLocations.concat(leafLocs);
};
walk(ast, {
@ -27,13 +27,13 @@ export function transpiler(input, options = {}) {
const { raw } = quasis[0].value;
this.skip();
emitMiniLocations && collectMiniLocations(raw, node);
return this.replace(miniWithLocation(raw, node, simpleLocs, miniLocations));
return this.replace(miniWithLocation(raw, node));
}
if (isStringWithDoubleQuotes(node)) {
const { value } = node;
this.skip();
emitMiniLocations && collectMiniLocations(value, node);
return this.replace(miniWithLocation(value, node, simpleLocs, miniLocations));
return this.replace(miniWithLocation(value, node));
}
// TODO: remove pseudo note variables?
if (node.type === 'Identifier' && isNoteWithOctave(node.name)) {
@ -79,64 +79,18 @@ function isBackTickString(node, parent) {
return node.type === 'TemplateLiteral' && parent.type !== 'TaggedTemplateExpression';
}
function miniWithLocation(value, node, simpleLocs) {
let locs;
const { start: fromOffset, end: toOffset } = node;
if (simpleLocs) {
locs = [
{
type: 'Literal',
value: fromOffset,
},
{
type: 'Literal',
value: toOffset,
},
];
} else {
const {
loc: {
start: { line: fromLine, column: fromColumn },
end: { line: toLine, column: toColumn },
},
} = node;
locs = [
{
type: 'ArrayExpression',
elements: [fromLine, fromColumn, fromOffset].map((value) => ({
type: 'Literal',
value,
})),
},
{
type: 'ArrayExpression',
elements: [toLine, toColumn, toOffset].map((value) => ({
type: 'Literal',
value,
})),
},
];
}
// with location
function miniWithLocation(value, node) {
const { start: fromOffset } = node;
return {
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'mini',
},
arguments: [{ type: 'Literal', value }],
optional: false,
},
property: {
type: 'Identifier',
name: 'withMiniLocation',
},
type: 'Identifier',
name: 'm',
},
arguments: locs,
arguments: [
{ type: 'Literal', value },
{ type: 'Literal', value: fromOffset },
],
optional: false,
};
}