Skip to content

Commit

Permalink
feat(2d): improve Code node (#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarthificial committed Mar 11, 2024
1 parent f7e53ec commit 6e7aaf0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
10 changes: 8 additions & 2 deletions packages/2d/src/lib/code/CodeCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ export class CodeCursor {
const mirrored = Math.abs(progress - 0.5) * 2;
alpha = clampRemap(1, 1 - timingOffset, 1, 0, mirrored);

const scale = progress < 0.5 ? 4 : -4;
const isBigger =
fragment.after.newRows > fragment.before.newRows ? 1 : -1;
const isBefore = progress < 0.5 ? 1 : -1;
const scale = isBigger * isBefore * 4;
offsetY = map(
Math.abs(fragment.after.newRows - fragment.before.newRows) / scale,
0,
Expand Down Expand Up @@ -307,7 +310,10 @@ export class CodeCursor {
}

skipAhead++;
} while (skipAhead < highlight.skipAhead);
} while (
skipAhead < highlight.skipAhead &&
code.content.charAt(i + skipAhead) !== '\n'
);

if (skipAhead > 1) {
char = code.content.slice(i, i + skipAhead);
Expand Down
2 changes: 1 addition & 1 deletion packages/2d/src/lib/code/CodeSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class CodeSignalContext<TOwner>
private readonly progress = createSignal(0);

public constructor(
initial: PossibleCodeScope,
initial: SignalValue<PossibleCodeScope>,
owner: TOwner,
private readonly highlighter?: SignalValue<CodeHighlighter>,
private readonly dialect?: SignalValue<string>,
Expand Down
15 changes: 4 additions & 11 deletions packages/2d/src/lib/code/LezerHighlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {defaultTokenize} from './CodeTokenizer';
interface LezerCache {
tree: Tree;
code: string;
colorLookup: Map<number, string>;
colorLookup: Map<string, string>;
}

export class LezerHighlighter implements CodeHighlighter<LezerCache | null> {
Expand Down Expand Up @@ -46,7 +46,7 @@ export class LezerHighlighter implements CodeHighlighter<LezerCache | null> {
return null;
}

const colorLookup = new Map<number, string>();
const colorLookup = new Map<string, string>();
const tree = parser.parse(code);
highlightTree(tree, this.style, (from, to, classes) => {
const color = this.classLookup.get(classes);
Expand Down Expand Up @@ -126,14 +126,7 @@ export class LezerHighlighter implements CodeHighlighter<LezerCache | null> {
return tokens;
}

private getNodeId(node: SyntaxNode): number {
if (!node.parent) {
return -1;
}

// NOTE: They don't want us to know about this property.
// We need a way to persistently identify nodes and this seems to work.
// Perhaps it could break if the tree is edited? But we don't do that. Yet.
return (node as any).index;
private getNodeId(node: SyntaxNode): string {
return `${node.from}:${node.to}`;
}
}
6 changes: 4 additions & 2 deletions packages/2d/src/lib/components/Code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class Code extends Shape {
* @param dialect - Custom dialect to use.
*/
public static createSignal(
initial: PossibleCodeScope,
initial: SignalValue<PossibleCodeScope>,
highlighter?: SignalValue<CodeHighlighter>,
dialect?: SignalValue<string>,
): CodeSignal<void> {
Expand Down Expand Up @@ -338,7 +338,9 @@ export class Code extends Shape {
*
* @param initial - The initial code.
*/
public createSignal(initial: PossibleCodeScope): CodeSignal<this> {
public createSignal(
initial: SignalValue<PossibleCodeScope>,
): CodeSignal<this> {
return new CodeSignalContext<this>(
initial,
this,
Expand Down

0 comments on commit 6e7aaf0

Please sign in to comment.