Skip to content

Commit 10db8c6

Browse files
committed
refactor(monaco-editor): use action for 'jump to def' feature
This new monaco action is now part of apidom language plugin. New language service is not instantiated anymore, istead running apidom worker is used to get the data.
1 parent 76ab2b1 commit 10db8c6

File tree

8 files changed

+83
-86
lines changed

8 files changed

+83
-86
lines changed

package-lock.json

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"js-file-download": "^0.4.12",
7979
"js-yaml": "^4.1.0",
8080
"lodash": "^4.17.21",
81-
"monaco-editor": "^0.34.1",
81+
"monaco-editor": "^0.36.1",
8282
"prop-types": "^15.8.1",
8383
"react": "^17.0.2",
8484
"react-collapse-pane": "^3.0.1",
@@ -92,7 +92,7 @@
9292
"short-unique-id": "^4.4.4",
9393
"styled-components": "^5.3.9",
9494
"swagger-ui-react": "^5.0.0-alpha.4",
95-
"vscode": "npm:@codingame/monaco-vscode-api@~1.69.13",
95+
"vscode": "npm:@codingame/monaco-vscode-api@~1.76.2",
9696
"vscode-languageclient": "^8.1.0",
9797
"vscode-languageserver-textdocument": "^1.0.8"
9898
},

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ export class ApiDOMWorker {
9999
return this._languageService.doDeref(document, dereferenceContext);
100100
}
101101

102+
async getJsonPointerPosition(uri, jsonPointer) {
103+
const document = this._getTextDocument(uri);
104+
if (!document) {
105+
return [];
106+
}
107+
108+
return this._languageService.getJsonPointerPosition(document, jsonPointer);
109+
}
110+
102111
_getTextDocument(uri) {
103112
const model = this._ctx.getMirrorModels().find((mm) => mm.uri.toString() === uri);
104113

src/plugins/editor-monaco-language-apidom/language/actions/dereference.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { getWorker } from '../apidom-mode.js';
22

33
const dereferenceActionDescriptor = {
4-
id: 'apidom-dereference',
4+
id: 'swagger.editor.apidomDereference',
55
label: 'Resolve document',
66
async run(editor) {
77
const model = editor.getModel();
8-
const worker = await getWorker()();
8+
const worker = await getWorker()(model.uri);
99
const dereferenced = await worker.doDeref(model.uri.toString(), {
1010
baseURI: globalThis.document.baseURI || globalThis.location.href,
1111
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getWorker } from '../apidom-mode.js';
2+
3+
const jsonPointerPositionDescriptor = {
4+
id: 'swagger.editor.jsonPointerPosition',
5+
label: 'Translate JSON Pointer to editor position',
6+
async run(editor, { jsonPointer, onSuccess, onFailure }) {
7+
try {
8+
const model = editor.getModel();
9+
const worker = await getWorker()(model.uri);
10+
const position = await worker.getJsonPointerPosition(model.uri.toString(), jsonPointer);
11+
12+
if (position) {
13+
onSuccess({ startLineNumber: position.line, startColumn: position.character - 1 });
14+
} else {
15+
onFailure(new Error(`Failed to compute position from JSON Pointer "${jsonPointer}"`));
16+
}
17+
} catch (error) {
18+
onFailure(error);
19+
}
20+
},
21+
};
22+
23+
export default jsonPointerPositionDescriptor;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ModesRegistry } from 'monaco-editor/esm/vs/editor/common/languages/mode
55
import * as apidom from './apidom.js';
66
import { setupMode } from './apidom-mode.js';
77
import dereferenceActionDescriptor from './actions/dereference.js';
8+
import jsonPointerPositionActionDescriptor from './actions/json-pointer-position.js';
89

910
export { getWorker } from './apidom-mode.js';
1011

@@ -97,9 +98,10 @@ const lazyMonacoContribution = ({ createData }) => {
9798
monaco.editor.onDidCreateEditor((editor) => {
9899
disposables.push(
99100
monaco.editor.onDidCreateModel(() => {
100-
if (editor.getAction(dereferenceActionDescriptor.id)) return;
101-
102-
disposables.push(editor.addAction(dereferenceActionDescriptor));
101+
if (!editor.getAction(dereferenceActionDescriptor.id)) {
102+
disposables.push(editor.addAction(dereferenceActionDescriptor));
103+
}
104+
disposables.push(editor.addAction(jsonPointerPositionActionDescriptor));
103105
})
104106
);
105107
})

src/plugins/editor-monaco/components/MonacoEditor/MonacoEditor.jsx

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import noop from 'lodash/noop.js';
55

66
import seVsDarkTheme from '../../themes/se-vs-dark.js';
77
import seVsLightTheme from '../../themes/se-vs-light.js';
8-
import { requestGetJsonPointerPosition } from '../../utils/monaco-jump-from-path-to-line.js';
98
import { useMount, useUpdate, useSmoothResize } from './hooks.js';
109

1110
/**
@@ -150,26 +149,26 @@ const MonacoEditor = ({
150149
// given a jsonPointer, request jumping to its marker position
151150
useUpdate(
152151
() => {
153-
async function findMarkerPosition() {
154-
// via apidom-ls
155-
const foundMarkerPosition = await requestGetJsonPointerPosition(
156-
editorRef.current,
157-
requestJumpToMarker.jsonPointer
158-
);
159-
if (foundMarkerPosition?.data) {
160-
// set jumpToMarker in state, which will then call the useUpdate above
161-
onSetRequestJumpToMarker(foundMarkerPosition.data);
162-
// then clear the request itself
163-
onClearRequestJumpToMarker();
164-
} else {
165-
// just clear the request anyway
166-
onClearRequestJumpToMarker();
167-
}
168-
}
152+
if (!requestJumpToMarker?.jsonPointer) return;
169153

170-
if (requestJumpToMarker?.jsonPointer && editorRef?.current?.getModel) {
171-
// call the async/await function
172-
findMarkerPosition();
154+
/**
155+
* In monaco-editor >= 0.37, triggering the action using `editor.trigger()`
156+
* will pass the payload to the action. Currently, there is a pending bug in
157+
* monaco-editor, and we have to call the action in this hacky way.
158+
*
159+
* More info in: https://stackoverflow.com/questions/75685974/open-find-widget-with-content-programatically-in-monaco
160+
*/
161+
const action = editorRef.current.getAction('swagger.editor.jsonPointerPosition');
162+
if (action) {
163+
// eslint-disable-next-line no-underscore-dangle
164+
action._run(editorRef.current, {
165+
jsonPointer: requestJumpToMarker.jsonPointer,
166+
onSuccess: (position) => {
167+
onSetRequestJumpToMarker(position);
168+
onClearRequestJumpToMarker();
169+
},
170+
onFailure: () => onClearRequestJumpToMarker(),
171+
});
173172
}
174173
},
175174
[requestJumpToMarker, onSetRequestJumpToMarker, onClearRequestJumpToMarker],

src/plugins/editor-monaco/utils/monaco-jump-from-path-to-line.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)