2023-12-28 14:04:44 +03:00
|
|
|
import { syntaxTree } from '@codemirror/language';
|
2023-09-25 23:50:41 +03:00
|
|
|
import { Extension } from '@codemirror/state';
|
|
|
|
import { hoverTooltip } from '@codemirror/view';
|
|
|
|
|
2024-06-19 12:13:05 +03:00
|
|
|
import { IEntityReference, ISyntacticReference } from '@/models/language';
|
2024-04-07 15:38:24 +03:00
|
|
|
import { IRSForm } from '@/models/rsform';
|
2024-04-04 16:34:07 +03:00
|
|
|
import { IColorTheme } from '@/styling/color';
|
2023-12-28 14:04:44 +03:00
|
|
|
import {
|
|
|
|
domTooltipEntityReference,
|
|
|
|
domTooltipSyntacticReference,
|
|
|
|
findContainedNodes,
|
2024-06-19 12:13:05 +03:00
|
|
|
findReferenceAt
|
2023-12-28 14:04:44 +03:00
|
|
|
} from '@/utils/codemirror';
|
2023-12-15 17:34:50 +03:00
|
|
|
|
2024-06-19 12:13:05 +03:00
|
|
|
import { RefEntity } from './parse/parser.terms';
|
2023-09-25 23:50:41 +03:00
|
|
|
|
2024-06-19 12:13:05 +03:00
|
|
|
export const tooltipProducer = (schema: IRSForm, colors: IColorTheme, canClick?: boolean) => {
|
2023-12-28 14:04:44 +03:00
|
|
|
return hoverTooltip((view, pos) => {
|
2024-06-19 12:13:05 +03:00
|
|
|
const parse = findReferenceAt(pos, view.state);
|
|
|
|
if (!parse) {
|
2023-09-25 23:50:41 +03:00
|
|
|
return null;
|
|
|
|
}
|
2024-06-19 12:13:05 +03:00
|
|
|
|
|
|
|
if ('entity' in parse.ref) {
|
|
|
|
const cst = schema.cstByAlias.get(parse.ref.entity);
|
2023-09-27 23:36:51 +03:00
|
|
|
return {
|
2024-06-19 12:13:05 +03:00
|
|
|
pos: parse.start,
|
|
|
|
end: parse.end,
|
2023-09-27 23:36:51 +03:00
|
|
|
above: false,
|
2024-06-19 12:13:05 +03:00
|
|
|
create: () => domTooltipEntityReference(parse.ref as IEntityReference, cst, colors, canClick)
|
2023-12-28 14:04:44 +03:00
|
|
|
};
|
2024-06-19 12:13:05 +03:00
|
|
|
} else {
|
2023-09-28 13:05:43 +03:00
|
|
|
let masterText: string | undefined = undefined;
|
2024-06-19 12:13:05 +03:00
|
|
|
if (parse.ref.offset > 0) {
|
|
|
|
const entities = findContainedNodes(parse.end, view.state.doc.length, syntaxTree(view.state), [RefEntity]);
|
|
|
|
if (parse.ref.offset <= entities.length) {
|
|
|
|
const master = entities[parse.ref.offset - 1];
|
2023-09-28 13:05:43 +03:00
|
|
|
masterText = view.state.doc.sliceString(master.from, master.to);
|
|
|
|
}
|
|
|
|
} else {
|
2024-06-19 12:13:05 +03:00
|
|
|
const entities = findContainedNodes(0, parse.start, syntaxTree(view.state), [RefEntity]);
|
|
|
|
if (-parse.ref.offset <= entities.length) {
|
|
|
|
const master = entities[-parse.ref.offset - 1];
|
2023-09-28 13:05:43 +03:00
|
|
|
masterText = view.state.doc.sliceString(master.from, master.to);
|
|
|
|
}
|
|
|
|
}
|
2023-09-27 23:36:51 +03:00
|
|
|
return {
|
2024-06-19 12:13:05 +03:00
|
|
|
pos: parse.start,
|
|
|
|
end: parse.end,
|
2023-09-27 23:36:51 +03:00
|
|
|
above: false,
|
2024-06-19 12:13:05 +03:00
|
|
|
create: () => domTooltipSyntacticReference(parse.ref as ISyntacticReference, masterText, canClick)
|
2023-12-28 14:04:44 +03:00
|
|
|
};
|
2023-09-25 23:50:41 +03:00
|
|
|
}
|
|
|
|
});
|
2023-12-28 14:04:44 +03:00
|
|
|
};
|
2023-09-25 23:50:41 +03:00
|
|
|
|
2024-06-19 12:13:05 +03:00
|
|
|
export function refsHoverTooltip(schema: IRSForm, colors: IColorTheme, canClick?: boolean): Extension {
|
|
|
|
return [tooltipProducer(schema, colors, canClick)];
|
2023-12-28 14:04:44 +03:00
|
|
|
}
|