From 34ac775c55d0d14244c46ea584bc3d2b400a3c35 Mon Sep 17 00:00:00 2001 From: GideonKoenig Date: Fri, 8 Jul 2022 14:28:04 +0200 Subject: [PATCH 1/2] feat: meth lab is done, walter sends his regard --- .../selectionView/DocumentationText.tsx | 59 +++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/api-editor/gui/src/features/packageData/selectionView/DocumentationText.tsx b/api-editor/gui/src/features/packageData/selectionView/DocumentationText.tsx index f851697ab..74815fa5f 100644 --- a/api-editor/gui/src/features/packageData/selectionView/DocumentationText.tsx +++ b/api-editor/gui/src/features/packageData/selectionView/DocumentationText.tsx @@ -28,6 +28,11 @@ import { selectExpandDocumentationByDefault } from '../../ui/uiSlice'; import { Link as RouterLink } from 'react-router-dom'; import { PythonDeclaration } from '../model/PythonDeclaration'; import { PythonPackage } from '../model/PythonPackage'; +import {Optional} from "../../../common/util/types"; +import {PythonFunction} from "../model/PythonFunction"; +import {PythonClass} from "../model/PythonClass"; +import {PythonParameter} from "../model/PythonParameter"; +import {PythonModule} from "../model/PythonModule"; interface DocumentationTextProps { declaration: PythonDeclaration; @@ -87,9 +92,10 @@ export const DocumentationText: React.FC = function ({ d // replace block math elements .replaceAll(/\.\. math::\s*(\S.*)\n\n/gu, '$$\n$1\n$$\n\n') // replace relative links to classes - .replaceAll(/:class:`(\w*)`/gu, (_match, name) => resolveRelativeLink(declaration, name)) + .replaceAll(/:class:`(\w*)`/gu, (_match, name) => resolveRelativeLink(declaration, name, DeclarationLevel.CLASS)) // replace relative links to functions - .replaceAll(/:func:`(\w*)`/gu, (_match, name) => resolveRelativeLink(declaration, name)) + .replaceAll(/:func:`(\w*)`/gu, (_match, name) => resolveRelativeLink(declaration, name, DeclarationLevel.FUNCTION)) + .replaceAll(/:meth:`(\w*)`/gu, (_match, name) => resolveRelativeLink(declaration, name, DeclarationLevel.FUNCTION)) // replace absolute links to modules .replaceAll(/:mod:`([\w.]*)`/gu, (_match, qualifiedName) => resolveAbsoluteLink(declaration, qualifiedName, 1)) // replace absolute links to classes @@ -147,12 +153,31 @@ export const DocumentationText: React.FC = function ({ d ); }; -const resolveRelativeLink = function (currentDeclaration: PythonDeclaration, linkedDeclarationName: string): string { - const parent = currentDeclaration.parent(); +const resolveRelativeLink = function ( + currentDeclaration: PythonDeclaration, + linkedDeclarationName: string, + targetLevel: DeclarationLevel +): string { + + let parent: Optional = currentDeclaration; + do { + parent = parent.parent(); + } while (parent && Object.keys(DeclarationLevel)[getDeclarationLevel(parent)] >= Object.keys(DeclarationLevel)[targetLevel]) + if (!parent) { return linkedDeclarationName; } + if (targetLevel === DeclarationLevel.FUNCTION) { + const grandparent = parent.parent(); + if (grandparent) { + const sibling = grandparent.children().find((it) => it.name === linkedDeclarationName); + if (sibling) { + return `[${sibling.preferredQualifiedName()}](${sibling.id})`; + } + } + } + const sibling = parent.children().find((it) => it.name === linkedDeclarationName); if (!sibling) { return linkedDeclarationName; @@ -192,3 +217,29 @@ const resolveAbsoluteLink = function ( return `[${current.preferredQualifiedName()}](${current.id})`; }; + +const getDeclarationLevel = function (element: PythonDeclaration): DeclarationLevel { + if (element instanceof PythonPackage) { + return DeclarationLevel.PACKAGE + } else if (element instanceof PythonModule) { + return DeclarationLevel.MODULE + } else if (element instanceof PythonClass) { + return DeclarationLevel.CLASS + } else if (element instanceof PythonFunction) { + return DeclarationLevel.FUNCTION + } else if (element instanceof PythonParameter) { + return DeclarationLevel.PARAMETER + } else { + return DeclarationLevel.DEFAULT + } +} + + +enum DeclarationLevel { + DEFAULT, + PACKAGE, + MODULE, + CLASS, + FUNCTION, + PARAMETER +} From 0cc095f3eaf8997d9c882b4b3a84bf111290a91c Mon Sep 17 00:00:00 2001 From: GideonKoenig Date: Fri, 8 Jul 2022 12:35:19 +0000 Subject: [PATCH 2/2] style: apply automatic fixes of linters --- .../selectionView/DocumentationText.tsx | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/api-editor/gui/src/features/packageData/selectionView/DocumentationText.tsx b/api-editor/gui/src/features/packageData/selectionView/DocumentationText.tsx index 374a02709..f3e0ecfe9 100644 --- a/api-editor/gui/src/features/packageData/selectionView/DocumentationText.tsx +++ b/api-editor/gui/src/features/packageData/selectionView/DocumentationText.tsx @@ -28,11 +28,11 @@ import { selectExpandDocumentationByDefault } from '../../ui/uiSlice'; import { Link as RouterLink } from 'react-router-dom'; import { PythonDeclaration } from '../model/PythonDeclaration'; import { PythonPackage } from '../model/PythonPackage'; -import {Optional} from "../../../common/util/types"; -import {PythonFunction} from "../model/PythonFunction"; -import {PythonClass} from "../model/PythonClass"; -import {PythonParameter} from "../model/PythonParameter"; -import {PythonModule} from "../model/PythonModule"; +import { Optional } from '../../../common/util/types'; +import { PythonFunction } from '../model/PythonFunction'; +import { PythonClass } from '../model/PythonClass'; +import { PythonParameter } from '../model/PythonParameter'; +import { PythonModule } from '../model/PythonModule'; interface DocumentationTextProps { declaration: PythonDeclaration; @@ -92,10 +92,16 @@ export const DocumentationText: React.FC = function ({ d // replace double colons with single colon .replaceAll(/::/gu, ':') // replace relative links to classes - .replaceAll(/:class:`(\w*)`/gu, (_match, name) => resolveRelativeLink(declaration, name, DeclarationLevel.CLASS)) + .replaceAll(/:class:`(\w*)`/gu, (_match, name) => + resolveRelativeLink(declaration, name, DeclarationLevel.CLASS), + ) // replace relative links to functions - .replaceAll(/:func:`(\w*)`/gu, (_match, name) => resolveRelativeLink(declaration, name, DeclarationLevel.FUNCTION)) - .replaceAll(/:meth:`(\w*)`/gu, (_match, name) => resolveRelativeLink(declaration, name, DeclarationLevel.FUNCTION)) + .replaceAll(/:func:`(\w*)`/gu, (_match, name) => + resolveRelativeLink(declaration, name, DeclarationLevel.FUNCTION), + ) + .replaceAll(/:meth:`(\w*)`/gu, (_match, name) => + resolveRelativeLink(declaration, name, DeclarationLevel.FUNCTION), + ) // replace absolute links to modules .replaceAll(/:mod:`([\w.]*)`/gu, (_match, qualifiedName) => resolveAbsoluteLink(declaration, qualifiedName, 1)) // replace absolute links to classes @@ -156,13 +162,15 @@ export const DocumentationText: React.FC = function ({ d const resolveRelativeLink = function ( currentDeclaration: PythonDeclaration, linkedDeclarationName: string, - targetLevel: DeclarationLevel + targetLevel: DeclarationLevel, ): string { - - let parent: Optional = currentDeclaration; + let parent: Optional = currentDeclaration; do { parent = parent.parent(); - } while (parent && Object.keys(DeclarationLevel)[getDeclarationLevel(parent)] >= Object.keys(DeclarationLevel)[targetLevel]) + } while ( + parent && + Object.keys(DeclarationLevel)[getDeclarationLevel(parent)] >= Object.keys(DeclarationLevel)[targetLevel] + ); if (!parent) { return linkedDeclarationName; @@ -220,20 +228,19 @@ const resolveAbsoluteLink = function ( const getDeclarationLevel = function (element: PythonDeclaration): DeclarationLevel { if (element instanceof PythonPackage) { - return DeclarationLevel.PACKAGE + return DeclarationLevel.PACKAGE; } else if (element instanceof PythonModule) { - return DeclarationLevel.MODULE + return DeclarationLevel.MODULE; } else if (element instanceof PythonClass) { - return DeclarationLevel.CLASS + return DeclarationLevel.CLASS; } else if (element instanceof PythonFunction) { - return DeclarationLevel.FUNCTION + return DeclarationLevel.FUNCTION; } else if (element instanceof PythonParameter) { - return DeclarationLevel.PARAMETER + return DeclarationLevel.PARAMETER; } else { - return DeclarationLevel.DEFAULT + return DeclarationLevel.DEFAULT; } -} - +}; enum DeclarationLevel { DEFAULT, @@ -241,5 +248,5 @@ enum DeclarationLevel { MODULE, CLASS, FUNCTION, - PARAMETER + PARAMETER, }