Skip to content

Commit 5c7b4c1

Browse files
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 <khudadad414@gmail.com>%0ACo-authored-by: samz <samir.amzani@gmail.com>
1 parent 22009e4 commit 5c7b4c1

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

.changeset/lovely-chefs-yawn.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@asyncapi/studio": patch
3+
---
4+
scrollTo navigation to right code block in yaml.

apps/studio/src/services/navigation.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ export class NavigationService extends AbstractService {
2828
hash: string,
2929
) {
3030
try {
31-
const range = this.svcs.parserSvc.getRangeForJsonPath('asyncapi', jsonPointer);
31+
const doc = this.svcs.editorSvc;
32+
const methodType = doc.value.startsWith('asyncapi') ? 'getRangeForYamlPath' : 'getRangeForJsonPath';
33+
const range = this.svcs.parserSvc[methodType]('asyncapi', jsonPointer);
34+
3235
if (range) {
33-
await this.scrollToEditorLine(range.start.line + 1);
36+
await this.scrollToEditorLine(range.start.line+1);
3437
}
3538

3639
await this.scrollToHash(hash);

apps/studio/src/services/parser.service.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { filesState, documentsState, settingsState } from '../state';
1212
import type { Diagnostic, ParseOptions } from '@asyncapi/parser/cjs';
1313
import type { DocumentDiagnostics } from '../state/documents.state';
1414
import { SchemaParser } from '@asyncapi/parser';
15+
import { getLocationForJsonPath, parseWithPointers } from '@stoplight/yaml';
1516

1617
export class ParserService extends AbstractService {
1718
private parser!: Parser;
@@ -71,14 +72,36 @@ export class ParserService extends AbstractService {
7172
getRangeForJsonPath(uri: string, jsonPath: string | Array<string | number>) {
7273
try {
7374
const { documents } = documentsState.getState();
75+
76+
const extras = documents[String(uri)]?.extras;
77+
78+
if (extras) {
79+
jsonPath = Array.isArray(jsonPath) ? jsonPath : jsonPath.split('/').map(untilde);
80+
if (jsonPath[0] === '') jsonPath.shift();
81+
82+
return extras.document.getRangeForJsonPath(jsonPath);
83+
}
84+
} catch (err) {
85+
console.error(err);
86+
}
87+
}
88+
89+
getRangeForYamlPath(uri: string, jsonPath: string | Array<string | number>) {
90+
try {
91+
const { documents } = documentsState.getState();
92+
7493
const extras = documents[String(uri)]?.extras;
94+
7595
if (extras) {
7696
jsonPath = Array.isArray(jsonPath) ? jsonPath : jsonPath.split('/').map(untilde);
7797
if (jsonPath[0] === '') jsonPath.shift();
78-
return extras.document.getRangeForJsonPath(jsonPath, true);
98+
const yamlDoc = parseWithPointers(this.svcs.editorSvc.value);
99+
100+
const location = getLocationForJsonPath(yamlDoc, jsonPath, true);
101+
return location?.range || { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } };
79102
}
80-
} catch (err: any) {
81-
return;
103+
} catch (err) {
104+
console.error(err);
82105
}
83106
}
84107

0 commit comments

Comments
 (0)