Skip to content

Commit e1306d4

Browse files
authored
refactor(language-apidom): enhance dereference monaco action (#4000)
- infere format (JSON/YAML) from editor content - do nothing on unknown format - use js-yaml library to pretty-format the dereferenced YAML
1 parent fd4d66e commit e1306d4

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/plugins/editor-monaco-language-apidom/after-load.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const makeAfterLoad =
88
(system) => {
99
if (isLanguageRegistered()) return;
1010

11-
lazyMonacoContribution({ createData });
11+
lazyMonacoContribution({ createData, system });
1212
system.editorActions.setLanguage(apidomDefaults.getLanguageId());
1313
};
1414

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1+
import YAML from 'js-yaml';
2+
13
import { getWorker } from '../apidom-mode.js';
24

3-
const dereferenceActionDescriptor = {
5+
const createDereferenceActionDescriptor = ({ getSystem }) => ({
46
id: 'swagger.editor.apidomDereference',
57
label: 'Resolve document',
68
async run(editor) {
9+
const system = getSystem();
10+
const isContentJSON = system.editorSelectors.selectIsContentFormatJSON();
11+
const isContentYAML = system.editorSelectors.selectIsContentFormatYAML();
12+
13+
if (!isContentJSON && !isContentYAML) return; // nothing to do here
14+
715
const model = editor.getModel();
816
const worker = await getWorker()(model.uri);
917
const dereferenced = await worker.doDeref(model.uri.toString(), {
1018
baseURI: globalThis.document.baseURI || globalThis.location.href,
19+
format: isContentJSON ? 0 : isContentYAML ? 1 : 'unknown', // eslint-disable-line no-nested-ternary
1120
});
1221

13-
editor.setValue(dereferenced);
22+
if (isContentYAML) {
23+
const nicelyFormattedYAML = YAML.dump(YAML.load(dereferenced));
24+
editor.setValue(nicelyFormattedYAML);
25+
} else if (isContentJSON) {
26+
editor.setValue(dereferenced);
27+
}
1428
},
15-
};
29+
});
1630

17-
export default dereferenceActionDescriptor;
31+
export default createDereferenceActionDescriptor;

src/plugins/editor-monaco-language-apidom/language/monaco.contribution.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ModesRegistry } from 'monaco-editor/esm/vs/editor/common/languages/mode
44

55
import * as apidom from './apidom.js';
66
import { setupMode } from './apidom-mode.js';
7-
import dereferenceActionDescriptor from './actions/dereference.js';
7+
import createDereferenceActionDescriptor from './actions/dereference.js';
88
import jsonPointerPositionActionDescriptor from './actions/json-pointer-position.js';
99

1010
export { getWorker } from './apidom-mode.js';
@@ -65,7 +65,7 @@ export const isLanguageRegistered = () => {
6565
return languages.includes(apidom.languageId);
6666
};
6767

68-
const lazyMonacoContribution = ({ createData }) => {
68+
const lazyMonacoContribution = ({ createData, system }) => {
6969
const disposables = [];
7070

7171
// register apidom language
@@ -98,9 +98,11 @@ const lazyMonacoContribution = ({ createData }) => {
9898
monaco.editor.onDidCreateEditor((editor) => {
9999
disposables.push(
100100
monaco.editor.onDidCreateModel(() => {
101+
const dereferenceActionDescriptor = createDereferenceActionDescriptor(system);
101102
if (!editor.getAction(dereferenceActionDescriptor.id)) {
102103
disposables.push(editor.addAction(dereferenceActionDescriptor));
103104
}
105+
104106
disposables.push(editor.addAction(jsonPointerPositionActionDescriptor));
105107
})
106108
);

src/plugins/editor-preview-asyncapi/util/raml-1-0-parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import yaml from 'js-yaml';
1+
import YAML from 'js-yaml';
22

33
/* eslint-disable no-param-reassign */
44

55
export async function parse({ message, defaultSchemaFormat }) {
66
try {
77
let { payload } = message;
88
if (typeof payload === 'object') {
9-
payload = yaml.dump(payload);
9+
payload = YAML.dump(payload);
1010
}
1111

1212
message['x-parser-original-schema-format'] = message.schemaFormat || defaultSchemaFormat;

0 commit comments

Comments
 (0)