如何覆盖编辑器服务
How to override editor-services
我正在尝试使用一种自定义方法来实现转到定义,这需要覆盖编辑器服务,尤其是 openEditor()
和 findModel()
方法,据我所知看到了。
我试着关注这条评论:
https://github.com/microsoft/monaco-editor/issues/291#issuecomment-450706479
但是不能 运行 它在摩纳哥的操场上,因为没有覆盖 findModel
。
我试图将它添加到 playground,所以它看起来像这样:
monaco.languages.register({ id: 'mySpecialLanguage' });
monaco.languages.registerDefinitionProvider('mySpecialLanguage', {
provideDefinition: function(model, position, cancellationToken) {
return {
uri: monaco.Uri.parse('http://a/different/file.txt'),
range: new monaco.Range(3, 1, 3, 1)
};
}
});
var editorService = {
openEditor: function() {
alert(`open editor called!` + JSON.stringify(arguments));
},
resolveEditor: function() {
alert(`resolve editor called!` + JSON.stringify(arguments));
},
findModel:function(editor, data) {
alert(`resolve editor called!` + JSON.stringify(arguments));
}
};
monaco.editor.create(document.getElementById("container"), {
value: '\n\Go to definition on this text',
language: 'mySpecialLanguage'
}, { editorService: editorService });
但这也不起作用,因为它没有 运行 此处的 findModel 实现并在控制台中记录模型不存在的错误。
所以我想看看 monaco.editor.create()
中的第三个参数看起来如何以及它应该如何表现。第三个参数是 (?Override: IEditorOverrideServices).
The monaco docs aren't helpful 其 TypeScript 定义过于空泛:
export interface IEditorOverrideServices {
[index: string]: any;
}
那么到底应该如何使用呢?
最后我的解决方案是:
const editor = monaco.editor.create(document.getElementById("container")!, {
model: monaco.editor.createModel(value, 'python', monaco.Uri.parse('file:///users/gilad/monaco-languageclient/example/lib/python3/main.py')),
glyphMargin: true,
lightbulb: {
enabled: true
},
});
const editorService = (editor as any)._codeEditorService;
const openEditorBase = editorService.openCodeEditor.bind(editorService);
if(openEditorBase){
}
editorService.openCodeEditor = async (input: any, source: any, sideBySide: any) => {
debugger
const result = await openEditorBase(input, source);
if (result === null) {
const fullPath = input.resource.path
const lineNumber = input.options.selection.startLineNumber
alert("file is at " + fullPath + ":" + lineNumber )
}
return result; // always return the base result
};
我正在尝试使用一种自定义方法来实现转到定义,这需要覆盖编辑器服务,尤其是 openEditor()
和 findModel()
方法,据我所知看到了。
我试着关注这条评论:
https://github.com/microsoft/monaco-editor/issues/291#issuecomment-450706479
但是不能 运行 它在摩纳哥的操场上,因为没有覆盖 findModel
。
我试图将它添加到 playground,所以它看起来像这样:
monaco.languages.register({ id: 'mySpecialLanguage' });
monaco.languages.registerDefinitionProvider('mySpecialLanguage', {
provideDefinition: function(model, position, cancellationToken) {
return {
uri: monaco.Uri.parse('http://a/different/file.txt'),
range: new monaco.Range(3, 1, 3, 1)
};
}
});
var editorService = {
openEditor: function() {
alert(`open editor called!` + JSON.stringify(arguments));
},
resolveEditor: function() {
alert(`resolve editor called!` + JSON.stringify(arguments));
},
findModel:function(editor, data) {
alert(`resolve editor called!` + JSON.stringify(arguments));
}
};
monaco.editor.create(document.getElementById("container"), {
value: '\n\Go to definition on this text',
language: 'mySpecialLanguage'
}, { editorService: editorService });
但这也不起作用,因为它没有 运行 此处的 findModel 实现并在控制台中记录模型不存在的错误。
所以我想看看 monaco.editor.create()
中的第三个参数看起来如何以及它应该如何表现。第三个参数是 (?Override: IEditorOverrideServices).
The monaco docs aren't helpful 其 TypeScript 定义过于空泛:
export interface IEditorOverrideServices {
[index: string]: any;
}
那么到底应该如何使用呢?
最后我的解决方案是:
const editor = monaco.editor.create(document.getElementById("container")!, {
model: monaco.editor.createModel(value, 'python', monaco.Uri.parse('file:///users/gilad/monaco-languageclient/example/lib/python3/main.py')),
glyphMargin: true,
lightbulb: {
enabled: true
},
});
const editorService = (editor as any)._codeEditorService;
const openEditorBase = editorService.openCodeEditor.bind(editorService);
if(openEditorBase){
}
editorService.openCodeEditor = async (input: any, source: any, sideBySide: any) => {
debugger
const result = await openEditorBase(input, source);
if (result === null) {
const fullPath = input.resource.path
const lineNumber = input.options.selection.startLineNumber
alert("file is at " + fullPath + ":" + lineNumber )
}
return result; // always return the base result
};