mirror of
https://github.com/bendtherules/completion-viz.git
synced 2026-08-18 13:42:51 +00:00
refactor(misc): Move source and completion mappings to parent using context
Also change most consumers to use the context value
This commit is contained in:
@@ -10,12 +10,14 @@ import {
|
|||||||
|
|
||||||
import { sourceMapping } from "../../logic/sourceMapping";
|
import { sourceMapping } from "../../logic/sourceMapping";
|
||||||
import {
|
import {
|
||||||
completionMapping,
|
useCompletionMapping,
|
||||||
ICompletionDetail,
|
ICompletionDetail,
|
||||||
|
ICompletionMapping,
|
||||||
} from "../../logic/completionMapping";
|
} from "../../logic/completionMapping";
|
||||||
|
|
||||||
function getCompletionFromSourceToken(
|
function getCompletionFromSourceToken(
|
||||||
matcheeToken: Token
|
matcheeToken: Token,
|
||||||
|
completionMapping: ICompletionMapping
|
||||||
): ICompletionDetail | null {
|
): ICompletionDetail | null {
|
||||||
const matchingSourceDetails = sourceMapping.mappingArray.find(
|
const matchingSourceDetails = sourceMapping.mappingArray.find(
|
||||||
({ token: tmpToken }) => matcheeToken === tmpToken
|
({ token: tmpToken }) => matcheeToken === tmpToken
|
||||||
@@ -120,13 +122,18 @@ function TokenRenderer({
|
|||||||
isLastTokenInLine,
|
isLastTokenInLine,
|
||||||
getTokenProps,
|
getTokenProps,
|
||||||
}: ITokenRendererProps) {
|
}: ITokenRendererProps) {
|
||||||
|
const completionMapping = useCompletionMapping();
|
||||||
|
|
||||||
const refFn = sourceMapping.registerToken(token);
|
const refFn = sourceMapping.registerToken(token);
|
||||||
if (isLastTokenInLine) {
|
if (isLastTokenInLine) {
|
||||||
sourceMapping.registerLineEnd();
|
sourceMapping.registerLineEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
const onClickToken = (tmpToken: Token) => {
|
const onClickToken = (tmpToken: Token) => {
|
||||||
const matchingCompletion = getCompletionFromSourceToken(tmpToken);
|
const matchingCompletion = getCompletionFromSourceToken(
|
||||||
|
tmpToken,
|
||||||
|
completionMapping
|
||||||
|
);
|
||||||
if (matchingCompletion !== null) {
|
if (matchingCompletion !== null) {
|
||||||
console.log(
|
console.log(
|
||||||
`Clicked: ${tmpToken.content} => Completion (${matchingCompletion.completionType},${matchingCompletion.completionValueString})`
|
`Clicked: ${tmpToken.content} => Completion (${matchingCompletion.completionType},${matchingCompletion.completionValueString})`
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { createContext, useContext } from "react";
|
||||||
import { Node } from "acorn";
|
import { Node } from "acorn";
|
||||||
import { inspect } from "../engine262/dist/engine262";
|
import { inspect } from "../engine262/dist/engine262";
|
||||||
|
|
||||||
@@ -12,11 +13,13 @@ export interface ICompletionDetail {
|
|||||||
completionValue: any;
|
completionValue: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const completionMapping: {
|
export interface ICompletionMapping {
|
||||||
completionDetails: ICompletionDetail[];
|
completionDetails: ICompletionDetail[];
|
||||||
addCompletion(_: { node: Node; result: CompletionRecord }): void;
|
addCompletion(_: { node: Node; result: CompletionRecord }): void;
|
||||||
reset(): void;
|
reset(): void;
|
||||||
} = {
|
}
|
||||||
|
|
||||||
|
export const completionMapping: ICompletionMapping = {
|
||||||
completionDetails: [],
|
completionDetails: [],
|
||||||
addCompletion({ node, result }) {
|
addCompletion({ node, result }) {
|
||||||
const { start, end } = node;
|
const { start, end } = node;
|
||||||
@@ -48,3 +51,12 @@ export const completionMapping: {
|
|||||||
this.completionDetails = [];
|
this.completionDetails = [];
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const CompletionMappingContext = createContext(completionMapping);
|
||||||
|
|
||||||
|
// Context consumer
|
||||||
|
export const useCompletionMapping = () => {
|
||||||
|
const value = useContext(CompletionMappingContext);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { createContext, useContext } from "react";
|
||||||
import { Token } from "../types/prism-types";
|
import { Token } from "../types/prism-types";
|
||||||
|
|
||||||
export interface TMappingELement {
|
export interface TMappingELement {
|
||||||
@@ -47,3 +48,12 @@ export const sourceMapping: TSourceMapping = {
|
|||||||
return [];
|
return [];
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const SourceMappingContext = createContext(sourceMapping);
|
||||||
|
|
||||||
|
// Context consumer
|
||||||
|
export const useSourceMapping = () => {
|
||||||
|
const value = useContext(SourceMappingContext);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user