Skip to content

Commit f765d54

Browse files
committed
feat: support auto close tag / auto .value
1 parent 37bb88b commit f765d54

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/monaco/Monaco.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ onMounted(async () => {
5252
throw new Error("Cannot find containerRef");
5353
}
5454
55-
const ls = await setupLs(documentModelMap)
56-
5755
const editorInstance = monaco.editor.create(containerRef.value, {
5856
theme,
5957
model: currentModel.value,
@@ -69,6 +67,8 @@ onMounted(async () => {
6967
});
7068
editor.value = editorInstance
7169
70+
const ls = await setupLs(editorInstance, documentModelMap)
71+
7272
await loadGrammars(editorInstance);
7373
7474
editorInstance.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => {

src/monaco/ls.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ onBeforeUnmount(() => {
4343
});
4444
});
4545

46-
export async function setupLs(modelsMap: Ref<Map<string, monaco.editor.ITextModel>>): Promise<LanguageService> {
46+
export async function setupLs(editor: monaco.editor.IStandaloneCodeEditor, modelsMap: Ref<Map<string, monaco.editor.ITextModel>>): Promise<LanguageService> {
4747
const libEs5Url = monaco.Uri.parse('file:///lib.es5.d.ts');
4848
const libDomUrl = monaco.Uri.parse('file:///lib.dom.d.ts');
4949
const libDtsUrl = monaco.Uri.parse('file:///lib.d.ts');
@@ -215,6 +215,49 @@ export async function setupLs(modelsMap: Ref<Map<string, monaco.editor.ITextMode
215215
const documents = new WeakMap<monaco.editor.ITextModel, vscode.TextDocument>();
216216

217217
disposables.value.push(
218+
editor.onDidChangeModelContent(async e => {
219+
const model = editor.getModel();
220+
const monacoSelection = editor.getSelection();
221+
if (model && monacoSelection && e.changes.length === 1) {
222+
const document = documents.get(model);
223+
if (document) {
224+
const range = monaco2code.asRange(monacoSelection);
225+
const change = e.changes[0];
226+
// auto-close-tag
227+
let edit = await ds.doAutoInsert(document, range.start, {
228+
lastChange: {
229+
range: monaco2code.asRange(change.range),
230+
rangeOffset: change.rangeOffset,
231+
rangeLength: change.rangeLength,
232+
text: change.text,
233+
},
234+
});
235+
if (!edit) {
236+
// auto .value
237+
edit = await ls.doAutoInsert(document.uri, range.start, {
238+
lastChange: {
239+
range: monaco2code.asRange(change.range),
240+
rangeOffset: change.rangeOffset,
241+
rangeLength: change.rangeLength,
242+
text: change.text,
243+
},
244+
});
245+
}
246+
if (edit) {
247+
if (typeof edit == 'string') {
248+
model.applyEdits([{
249+
range: monacoSelection,
250+
text: edit,
251+
}]);
252+
}
253+
else {
254+
model.applyEdits([code2monaco.asTextEdit(edit)]);
255+
}
256+
}
257+
}
258+
}
259+
}),
260+
218261
// TODO: registerTokensProviderFactory
219262
// TODO: setTokensProvider
220263
// TODO: setMonarchTokensProvider

src/monaco/monaco2code.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function asPosition(position: monaco.Position): vscode.Position {
77
);
88
}
99

10-
export function asRange(range: monaco.Range): vscode.Range {
10+
export function asRange(range: monaco.IRange): vscode.Range {
1111
return vscode.Range.create(
1212
range.startLineNumber - 1,
1313
range.startColumn - 1,

0 commit comments

Comments
 (0)