Skip to content

Commit

Permalink
fix: scrollTo navigation to right code block (#994)
Browse files Browse the repository at this point in the history
Co-authored-by: Khuda Dad Nomani <[email protected]>%0ACo-authored-by: = <=>%0ACo-authored-by: Khuda Dad Nomani <[email protected]>%0ACo-authored-by: samz <[email protected]>
  • Loading branch information
Saumya40-codes and Amzani authored Mar 20, 2024
1 parent 22009e4 commit 5c7b4c1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .changeset/lovely-chefs-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"@asyncapi/studio": patch
---
scrollTo navigation to right code block in yaml.
7 changes: 5 additions & 2 deletions apps/studio/src/services/navigation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 26 additions & 3 deletions apps/studio/src/services/parser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,14 +72,36 @@ export class ParserService extends AbstractService {
getRangeForJsonPath(uri: string, jsonPath: string | Array<string | number>) {
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<string | number>) {
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);
}
}

Expand Down

0 comments on commit 5c7b4c1

Please sign in to comment.