由于我的自定义语言使用单引号(')而不是字符串的双引号(“)字符,我想在用户输入中将双引号更改为两个单引号字符。这是否可以在monaco编辑器中使用,什么是最好的方法吗?
因此,例如,如果用户输入“它将自动更改为此”。唯一的情况不是这种情况将在两个单引号字符('“')内。
有什么建议吗?
答案 0 :(得分:0)
Altough我不确定registerOnTypeFormattingEditProvider是否是执行此操作的最佳方法是我设法完成此任务的代码:
// changes the double quote charachter to two single quote characters
monaco.languages.registerOnTypeFormattingEditProvider(clarionLanguage, {
autoFormatTriggerCharacters: ['"'],
provideOnTypeFormattingEdits: (model, position, character, options, token) => {
// if inside string quotes then allow double quote character
let quotes: monaco.editor.FindMatch[] = model.findMatches(`'([^'])*'`, true, true, true, null, true);
if (quotes.length > 0) {
for (let quote of quotes) {
if (quote && (position.column >= quote.range.startColumn && position.column <= quote.range.endColumn)) {
return;
}
}
}
return [
{
range: {
startLineNumber: position.lineNumber,
startColumn: position.column - 1,
endLineNumber: position.lineNumber,
endColumn: position.column
},
text: `''`
}
];
}
});
这是一个更好的解决方案(由Rcjsuen和alexandrudima在GitHub上建议):
// change the double quotes to two single quotes
editor.onDidType(text => {
if (text === '"') {
let selection = editor.getSelection();
let range = new monaco.Range(selection.startLineNumber, selection.startColumn - 1, selection.endLineNumber, selection.endColumn);
let id = { major: 1, minor: 1 };
let op = { identifier: id, range, text: "''", forceMoveMarkers: true };
editor.getModel().pushEditOperations([], [op], undefined);
editor.setSelection(selection);
}
});