From 5c7b4c1c47c55e4dc316203d068338f0b0d7115f Mon Sep 17 00:00:00 2001 From: Saumya Shah <115284013+Saumya40-codes@users.noreply.github.com> Date: Wed, 20 Mar 2024 08:40:39 +0530 Subject: [PATCH] fix: scrollTo navigation to right code block (#994) Co-authored-by: Khuda Dad Nomani <32505158+KhudaDad414@users.noreply.github.com>%0ACo-authored-by: = <=>%0ACo-authored-by: Khuda Dad Nomani %0ACo-authored-by: samz --- .changeset/lovely-chefs-yawn.md | 4 +++ .../studio/src/services/navigation.service.ts | 7 +++-- apps/studio/src/services/parser.service.ts | 29 +++++++++++++++++-- 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 .changeset/lovely-chefs-yawn.md diff --git a/.changeset/lovely-chefs-yawn.md b/.changeset/lovely-chefs-yawn.md new file mode 100644 index 000000000..350c790c2 --- /dev/null +++ b/.changeset/lovely-chefs-yawn.md @@ -0,0 +1,4 @@ +--- +"@asyncapi/studio": patch +--- +scrollTo navigation to right code block in yaml. diff --git a/apps/studio/src/services/navigation.service.ts b/apps/studio/src/services/navigation.service.ts index b81521f52..b0180e935 100644 --- a/apps/studio/src/services/navigation.service.ts +++ b/apps/studio/src/services/navigation.service.ts @@ -28,9 +28,12 @@ export class NavigationService extends AbstractService { hash: string, ) { try { - const range = this.svcs.parserSvc.getRangeForJsonPath('asyncapi', jsonPointer); + const doc = this.svcs.editorSvc; + const methodType = doc.value.startsWith('asyncapi') ? 'getRangeForYamlPath' : 'getRangeForJsonPath'; + const range = this.svcs.parserSvc[methodType]('asyncapi', jsonPointer); + if (range) { - await this.scrollToEditorLine(range.start.line + 1); + await this.scrollToEditorLine(range.start.line+1); } await this.scrollToHash(hash); diff --git a/apps/studio/src/services/parser.service.ts b/apps/studio/src/services/parser.service.ts index 3797d48ca..4ade21f36 100644 --- a/apps/studio/src/services/parser.service.ts +++ b/apps/studio/src/services/parser.service.ts @@ -12,6 +12,7 @@ import { filesState, documentsState, settingsState } from '../state'; import type { Diagnostic, ParseOptions } from '@asyncapi/parser/cjs'; import type { DocumentDiagnostics } from '../state/documents.state'; import { SchemaParser } from '@asyncapi/parser'; +import { getLocationForJsonPath, parseWithPointers } from '@stoplight/yaml'; export class ParserService extends AbstractService { private parser!: Parser; @@ -71,14 +72,36 @@ export class ParserService extends AbstractService { getRangeForJsonPath(uri: string, jsonPath: string | Array) { try { const { documents } = documentsState.getState(); + + const extras = documents[String(uri)]?.extras; + + if (extras) { + jsonPath = Array.isArray(jsonPath) ? jsonPath : jsonPath.split('/').map(untilde); + if (jsonPath[0] === '') jsonPath.shift(); + + return extras.document.getRangeForJsonPath(jsonPath); + } + } catch (err) { + console.error(err); + } + } + + getRangeForYamlPath(uri: string, jsonPath: string | Array) { + try { + const { documents } = documentsState.getState(); + const extras = documents[String(uri)]?.extras; + if (extras) { jsonPath = Array.isArray(jsonPath) ? jsonPath : jsonPath.split('/').map(untilde); if (jsonPath[0] === '') jsonPath.shift(); - return extras.document.getRangeForJsonPath(jsonPath, true); + const yamlDoc = parseWithPointers(this.svcs.editorSvc.value); + + const location = getLocationForJsonPath(yamlDoc, jsonPath, true); + return location?.range || { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } }; } - } catch (err: any) { - return; + } catch (err) { + console.error(err); } }