mirror of
https://github.com/bendtherules/completion-viz.git
synced 2026-08-18 13:42:51 +00:00
refactor(misc): Use hook version of completionMapping and sourceMapping everywhere
Also stopped exporting the mappings directly from respective files to enforce this
This commit is contained in:
+2
-3
@@ -3,8 +3,7 @@ import React, { useState } from "react";
|
||||
import { SourceViewer } from "./components/SourceViewer";
|
||||
import { CompletionViewer } from "./components/CompletionViewer";
|
||||
|
||||
import { completionMapping } from "./logic/completionMapping";
|
||||
import { sourceMapping } from "./logic/sourceMapping";
|
||||
import { useCompletionMapping } from "./logic/completionMapping";
|
||||
import { runEngine } from "./logic/runEngine";
|
||||
|
||||
import "./tailwind.output.css";
|
||||
@@ -15,6 +14,7 @@ World!\`; abc()`;
|
||||
|
||||
function App() {
|
||||
const [inputCode, setInputCode] = useState<string>(sampleInputCode);
|
||||
const completionMapping = useCompletionMapping();
|
||||
|
||||
// useLayoutEffect(() => {
|
||||
runEngine(inputCode);
|
||||
@@ -31,7 +31,6 @@ function App() {
|
||||
<CompletionViewer
|
||||
code={inputCode}
|
||||
completionDetails={completionMapping.completionDetails}
|
||||
sourceMapping={sourceMapping}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,18 +1,14 @@
|
||||
import React from "react";
|
||||
import { ICompletionDetail } from "../../logic/completionMapping";
|
||||
import { TSourceMapping } from "../../logic/sourceMapping";
|
||||
|
||||
import { CompletionCardFull } from "../CompletionCard";
|
||||
|
||||
export function CompletionViewer(props: {
|
||||
code: string;
|
||||
completionDetails: ICompletionDetail[];
|
||||
sourceMapping: TSourceMapping;
|
||||
}) {
|
||||
const { code, completionDetails } = props;
|
||||
|
||||
// function renderCompletionResult(inputCompletion: ICompletionDetail) {}
|
||||
|
||||
return (
|
||||
<div className="">
|
||||
{/* Title for completion section */}
|
||||
|
||||
@@ -8,16 +8,17 @@ import {
|
||||
Token,
|
||||
} from "../../types/prism-types";
|
||||
|
||||
import { sourceMapping } from "../../logic/sourceMapping";
|
||||
import { TSourceMapping, useSourceMapping } from "../../logic/sourceMapping";
|
||||
import {
|
||||
useCompletionMapping,
|
||||
ICompletionDetail,
|
||||
ICompletionMapping,
|
||||
TCompletionMapping,
|
||||
} from "../../logic/completionMapping";
|
||||
|
||||
function getCompletionFromSourceToken(
|
||||
matcheeToken: Token,
|
||||
completionMapping: ICompletionMapping
|
||||
completionMapping: TCompletionMapping,
|
||||
sourceMapping: TSourceMapping
|
||||
): ICompletionDetail | null {
|
||||
const matchingSourceDetails = sourceMapping.mappingArray.find(
|
||||
({ token: tmpToken }) => matcheeToken === tmpToken
|
||||
@@ -72,7 +73,9 @@ function PreRenderer({
|
||||
getLineProps,
|
||||
getTokenProps,
|
||||
}: IPreRendererProps) {
|
||||
const sourceMapping = useSourceMapping();
|
||||
sourceMapping.reset();
|
||||
|
||||
return (
|
||||
<pre className={`${className} px-4 py-2 overflow-auto`} style={style}>
|
||||
{tokens.map((line, i) => (
|
||||
@@ -123,6 +126,7 @@ function TokenRenderer({
|
||||
getTokenProps,
|
||||
}: ITokenRendererProps) {
|
||||
const completionMapping = useCompletionMapping();
|
||||
const sourceMapping = useSourceMapping();
|
||||
|
||||
const refFn = sourceMapping.registerToken(token);
|
||||
if (isLastTokenInLine) {
|
||||
@@ -132,7 +136,8 @@ function TokenRenderer({
|
||||
const onClickToken = (tmpToken: Token) => {
|
||||
const matchingCompletion = getCompletionFromSourceToken(
|
||||
tmpToken,
|
||||
completionMapping
|
||||
completionMapping,
|
||||
sourceMapping
|
||||
);
|
||||
if (matchingCompletion !== null) {
|
||||
console.log(
|
||||
|
||||
@@ -13,13 +13,13 @@ export interface ICompletionDetail {
|
||||
completionValue: any;
|
||||
}
|
||||
|
||||
export interface ICompletionMapping {
|
||||
export interface TCompletionMapping {
|
||||
completionDetails: ICompletionDetail[];
|
||||
addCompletion(_: { node: Node; result: CompletionRecord }): void;
|
||||
reset(): void;
|
||||
}
|
||||
|
||||
export const completionMapping: ICompletionMapping = {
|
||||
const completionMapping: TCompletionMapping = {
|
||||
completionDetails: [],
|
||||
addCompletion({ node, result }) {
|
||||
const { start, end } = node;
|
||||
|
||||
@@ -17,7 +17,7 @@ export interface TSourceMapping {
|
||||
getElementsInRange(start: number, end: number): HTMLElement[];
|
||||
}
|
||||
|
||||
export const sourceMapping: TSourceMapping = {
|
||||
const sourceMapping: TSourceMapping = {
|
||||
mappingArray: [],
|
||||
lastEnd: 0,
|
||||
registerToken(t: Token) {
|
||||
|
||||
Reference in New Issue
Block a user