diff --git a/src/components/SourceViewer/SourceViewer.tsx b/src/components/SourceViewer/SourceViewer.tsx index a05d11a..5be1d70 100644 --- a/src/components/SourceViewer/SourceViewer.tsx +++ b/src/components/SourceViewer/SourceViewer.tsx @@ -1,7 +1,147 @@ import React from "react"; import Highlight, { defaultProps } from "prism-react-renderer"; +import { + TgetLineProps, + TgetTokenProps, + StyleObj, + Token, +} from "../../types/prism-types"; + import { sourceMapping } from "../../logic/sourceMapping"; +import { + completionMapping, + ICompletionDetail, +} from "../../logic/completionMapping"; + +function getCompletionFromSourceToken( + matcheeToken: Token +): ICompletionDetail | null { + const matchingSourceDetails = sourceMapping.mappingArray.find( + ({ token: tmpToken }) => matcheeToken === tmpToken + ); + if (matchingSourceDetails === undefined) { + return null; + } + + const { start: sourceStart, end: sourceEnd } = matchingSourceDetails; + + let closestCompletion: ICompletionDetail | null = null; + let closestMatchScore = -Infinity; + + completionMapping.completionDetails.forEach((tmpCompletionDetails) => { + const { start: compStart, end: compEnd } = tmpCompletionDetails; + const sourceLen = sourceEnd - sourceStart; + const compLen = compEnd - compStart; + + // If not intersecting at all, ignore + if (compEnd < sourceStart || compStart > sourceEnd) { + return; + } + + const intersectionStart = Math.max(sourceStart, compStart); + const intersectionEnd = Math.min(sourceEnd, compEnd); + + const intersectionDistance = intersectionEnd - intersectionStart; + // Exclusion = Total Len - (2 * intersection) + const exclusionDistance = sourceLen + compLen - intersectionDistance; + + const score = intersectionDistance - exclusionDistance; + if (score >= closestMatchScore) { + closestCompletion = tmpCompletionDetails; + closestMatchScore = score; + } + }); + + return closestCompletion; +} + +interface IPreRendererProps { + className: string; + style: StyleObj; + tokens: Token[][]; + getLineProps: TgetLineProps; + getTokenProps: TgetTokenProps; +} +function PreRenderer({ + className, + style, + tokens, + getLineProps, + getTokenProps, +}: IPreRendererProps) { + sourceMapping.reset(); + return ( +
+ {tokens.map((line, i) => (
+
+ ))}
+
+ );
+}
+
+interface ILineRendererProps {
+ line: Token[];
+ getLineProps: TgetLineProps;
+ getTokenProps: TgetTokenProps;
+}
+function LineRenderer({
+ line,
+ getLineProps,
+ getTokenProps,
+}: ILineRendererProps) {
+ return (
+
- {tokens.map((line, i) => (
-
- {line.map(function (token, key) {
- const refFn = sourceMapping.registerToken(token);
- return (
-
- );
- })}
- {(sourceMapping.registerLineEnd(), null)}
-
- ))}
-
- )
+