Monaco 中有个展开选择的功能,默认如果我们选择 function,扩展选择就会选择到行尾,再扩展就会选中整个函数,效果如下:
Monaco 可以自定义选择范围,通过 registerSelectionRangeProvider 注册 selectionRangeProvider 处理函数
实现 provideSelectionRanges 并返回选择范围
返回位置信息 SelectRange
代码实现如下
export function selectionRangeProvider(
editor: monacoEditor.editor.IStandaloneCodeEditor,
monaco: typeof monacoEditor
) {
return monaco.languages.registerSelectionRangeProvider("mySpecialLanguage", {
provideSelectionRanges: function(model, positions, token) {
const ranges = positions.map(position => {
const word = model.getWordAtPosition(position);
if (word) {
const range =
new monaco.Range(
position.lineNumber,
word.startColumn,
position.lineNumber,
word.endColumn
)
return [{range: range}];
}
return [{ range: new monaco.Range(position.lineNumber, position.column, position.lineNumber, position.column) },
{ range: new monaco.Range(position.lineNumber, position.column, position.lineNumber, position.column) }]
;
});
return ranges;
}
});
}