Skip to content

Commit 3ad7035

Browse files
committedFeb 10, 2024
feat: register language configuration
ref Kingwl/monaco-volar#25
1 parent d3ee13d commit 3ad7035

File tree

2 files changed

+555
-20
lines changed

2 files changed

+555
-20
lines changed
 

‎src/monaco/env.ts

+26-20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { Store } from '../store'
77
import { getOrCreateModel } from './utils'
88
import type { CreateData } from './vue.worker'
99
import vueWorker from './vue.worker?worker'
10+
import * as languageConfigs from './language-configs'
1011

1112
let initted = false
1213
export function initMonaco(store: Store) {
@@ -36,26 +37,6 @@ export function initMonaco(store: Store) {
3637
}
3738
})
3839

39-
// Support for go to definition
40-
editor.registerEditorOpener({
41-
openCodeEditor(_, resource) {
42-
if (resource.toString().startsWith(jsDelivrUriBase + '/')) {
43-
return true
44-
}
45-
46-
const path = resource.path
47-
if (/^\//.test(path)) {
48-
const fileName = path.replace('/', '')
49-
if (fileName !== store.activeFile.filename) {
50-
store.setActive(fileName)
51-
return true
52-
}
53-
}
54-
55-
return false
56-
},
57-
})
58-
5940
initted = true
6041
}
6142

@@ -166,7 +147,32 @@ export function loadMonacoEnv(store: Store) {
166147
languages.register({ id: 'vue', extensions: ['.vue'] })
167148
languages.register({ id: 'javascript', extensions: ['.js'] })
168149
languages.register({ id: 'typescript', extensions: ['.ts'] })
150+
languages.register({ id: 'css', extensions: ['.css'] })
151+
languages.setLanguageConfiguration('vue', languageConfigs.vue)
152+
languages.setLanguageConfiguration('javascript', languageConfigs.js)
153+
languages.setLanguageConfiguration('typescript', languageConfigs.ts)
154+
languages.setLanguageConfiguration('css', languageConfigs.css)
169155

170156
store.reloadLanguageTools = () => reloadLanguageTools(store)
171157
languages.onLanguage('vue', () => store.reloadLanguageTools!())
158+
159+
// Support for go to definition
160+
editor.registerEditorOpener({
161+
openCodeEditor(_, resource) {
162+
if (resource.toString().startsWith(jsDelivrUriBase + '/')) {
163+
return true
164+
}
165+
166+
const path = resource.path
167+
if (/^\//.test(path)) {
168+
const fileName = path.replace('/', '')
169+
if (fileName !== store.activeFile.filename) {
170+
store.setActive(fileName)
171+
return true
172+
}
173+
}
174+
175+
return false
176+
},
177+
})
172178
}

‎src/monaco/language-configs.ts

+529
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,529 @@
1+
import { languages } from 'monaco-editor-core'
2+
3+
// export const html: languages.LanguageConfiguration = {
4+
// comments: {
5+
// blockComment: ['<!--', '-->'],
6+
// },
7+
// brackets: [
8+
// ['<!--', '-->'],
9+
// ['{', '}'],
10+
// ['(', ')'],
11+
// ],
12+
// autoClosingPairs: [
13+
// { open: '{', close: '}' },
14+
// { open: '[', close: ']' },
15+
// { open: '(', close: ')' },
16+
// { open: "'", close: "'" },
17+
// { open: '"', close: '"' },
18+
// { open: '<!--', close: '-->', notIn: ['comment', 'string'] },
19+
// ],
20+
// surroundingPairs: [
21+
// { open: "'", close: "'" },
22+
// { open: '"', close: '"' },
23+
// { open: '{', close: '}' },
24+
// { open: '[', close: ']' },
25+
// { open: '(', close: ')' },
26+
// { open: '<', close: '>' },
27+
// ],
28+
// colorizedBracketPairs: [],
29+
// folding: {
30+
// markers: {
31+
// start: /^\s*<!--\s*#region\b.*-->/,
32+
// end: /^\s*<!--\s*#endregion\b.*-->/,
33+
// },
34+
// },
35+
// wordPattern: new RegExp(
36+
// '(-?\\d*\\.\\d\\w*)|([^\\`\\~\\!\\@\\$\\^\\&\\*\\(\\)\\=\\+\\[\\{\\]\\}\\\\\\|\\;\\:\\\'\\"\\,\\.\\<\\>\\/\\s]+)',
37+
// ),
38+
// onEnterRules: [
39+
// {
40+
// beforeText: new RegExp(
41+
// '<(?!(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr))([_:\\w][_:\\w-.\\d]*)(?:(?:[^\'"/>]|"[^"]*"|\'[^\']*\')*?(?!\\/)>)[^<]*$',
42+
// 'i',
43+
// ),
44+
// afterText: new RegExp('^<\\/([_:\\w][_:\\w-.\\d]*)\\s*>', 'i'),
45+
// action: {
46+
// indentAction: languages.IndentAction.IndentOutdent,
47+
// },
48+
// },
49+
// {
50+
// beforeText: new RegExp(
51+
// '<(?!(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr))([_:\\w][_:\\w-.\\d]*)(?:(?:[^\'"/>]|"[^"]*"|\'[^\']*\')*?(?!\\/)>)[^<]*$',
52+
// 'i',
53+
// ),
54+
// action: {
55+
// indentAction: languages.IndentAction.Indent,
56+
// },
57+
// },
58+
// ],
59+
// indentationRules: {
60+
// increaseIndentPattern: new RegExp(
61+
// '<(?!\\?|(?:area|base|br|col|frame|hr|html|img|input|keygen|link|menuitem|meta|param|source|track|wbr)\\b|[^>]*\\/>)([-_\\.A-Za-z0-9]+)(?=\\s|>)\\b[^>]*>(?!.*<\\/\\1>)|<!--(?!.*-->)|\\{[^}"\']*$',
62+
// ),
63+
// decreaseIndentPattern: new RegExp(
64+
// '^\\s*(<\\/(?!html)[-_\\.A-Za-z0-9]+\\b[^>]*>|-->|\\})',
65+
// ),
66+
// },
67+
// }
68+
69+
export const css: languages.LanguageConfiguration = {
70+
comments: {
71+
blockComment: ['/*', '*/'],
72+
},
73+
brackets: [
74+
['{', '}'],
75+
['[', ']'],
76+
['(', ')'],
77+
],
78+
autoClosingPairs: [
79+
{ open: '{', close: '}', notIn: ['string', 'comment'] },
80+
{ open: '[', close: ']', notIn: ['string', 'comment'] },
81+
{ open: '(', close: ')', notIn: ['string', 'comment'] },
82+
{ open: '"', close: '"', notIn: ['string', 'comment'] },
83+
{ open: "'", close: "'", notIn: ['string', 'comment'] },
84+
],
85+
surroundingPairs: [
86+
{
87+
open: "'",
88+
close: "'",
89+
},
90+
{
91+
open: '"',
92+
close: '"',
93+
},
94+
{
95+
open: '{',
96+
close: '}',
97+
},
98+
{
99+
open: '[',
100+
close: ']',
101+
},
102+
{
103+
open: '(',
104+
close: ')',
105+
},
106+
],
107+
folding: {
108+
markers: {
109+
start: new RegExp('^\\s*\\/\\*\\s*#region\\b\\s*(.*?)\\s*\\*\\/'),
110+
end: new RegExp('^\\s*\\/\\*\\s*#endregion\\b.*\\*\\/'),
111+
},
112+
},
113+
indentationRules: {
114+
increaseIndentPattern: new RegExp('(^.*\\{[^}]*$)'),
115+
decreaseIndentPattern: new RegExp('^\\s*\\}'),
116+
},
117+
wordPattern: new RegExp(
118+
'(#?-?\\d*\\.\\d\\w*%?)|(::?[\\w-]*(?=[^,{;]*[,{]))|(([@#.!])?[\\w-?]+%?|[@#!.])',
119+
),
120+
}
121+
122+
export const vue: languages.LanguageConfiguration = {
123+
comments: {
124+
blockComment: ['<!--', '-->'],
125+
},
126+
brackets: [
127+
['<!--', '-->'],
128+
['<', '>'],
129+
['{', '}'],
130+
['(', ')'],
131+
],
132+
autoClosingPairs: [
133+
{
134+
open: '{',
135+
close: '}',
136+
},
137+
{
138+
open: '[',
139+
close: ']',
140+
},
141+
{
142+
open: '(',
143+
close: ')',
144+
},
145+
{
146+
open: "'",
147+
close: "'",
148+
},
149+
{
150+
open: '"',
151+
close: '"',
152+
},
153+
{
154+
open: '<!--',
155+
close: '-->',
156+
notIn: ['comment', 'string'],
157+
},
158+
{
159+
open: '`',
160+
close: '`',
161+
notIn: ['string', 'comment'],
162+
},
163+
{
164+
open: '/**',
165+
close: ' */',
166+
notIn: ['string'],
167+
},
168+
],
169+
autoCloseBefore: ';:.,=}])><`\'" \n\t',
170+
surroundingPairs: [
171+
{
172+
open: "'",
173+
close: "'",
174+
},
175+
{
176+
open: '"',
177+
close: '"',
178+
},
179+
{
180+
open: '{',
181+
close: '}',
182+
},
183+
{
184+
open: '[',
185+
close: ']',
186+
},
187+
{
188+
open: '(',
189+
close: ')',
190+
},
191+
{
192+
open: '<',
193+
close: '>',
194+
},
195+
{
196+
open: '`',
197+
close: '`',
198+
},
199+
],
200+
colorizedBracketPairs: [],
201+
folding: {
202+
markers: {
203+
start: /^\s*<!--\s*#region\b.*-->/,
204+
end: /^\s*<!--\s*#endregion\b.*-->/,
205+
},
206+
},
207+
wordPattern:
208+
/(-?\d*\.\d\w*)|([^\`\@\~\!\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>/\?\s]+)/,
209+
onEnterRules: [
210+
{
211+
beforeText:
212+
/<(?!(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr|script|style))([_:\w][_:\w-.\d]*)(?:(?:[^'"/>]|"[^"]*"|'[^']*')*?(?!\/)>)[^<]*$/i,
213+
afterText: /^<\/([_:\w][_:\w-.\d]*)\s*>/i,
214+
action: {
215+
indentAction: languages.IndentAction.IndentOutdent,
216+
},
217+
},
218+
{
219+
beforeText:
220+
/<(?!(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr|script|style))([_:\w][_:\w-.\d]*)(?:(?:[^'"/>]|"[^"]*"|'[^']*')*?(?!\/)>)[^<]*$/i,
221+
action: {
222+
indentAction: languages.IndentAction.Indent,
223+
},
224+
},
225+
],
226+
indentationRules: {
227+
increaseIndentPattern:
228+
/<(?!\?|(?:area|base|br|col|frame|hr|html|img|input|keygen|link|menuitem|meta|param|source|track|wbr|script|style)\b|[^>]*\/>)([-_\.A-Za-z0-9]+)(?=\s|>)\b[^>]*>(?!\s*\()(?!.*<\/\1>)|<!--(?!.*-->)|\{[^}"']*$/i,
229+
decreaseIndentPattern: /^\s*(<\/(?!html)[-_\.A-Za-z0-9]+\b[^>]*>|-->|\})/,
230+
},
231+
}
232+
233+
export const js: languages.LanguageConfiguration = {
234+
comments: {
235+
lineComment: '//',
236+
blockComment: ['/*', '*/'],
237+
},
238+
brackets: [
239+
['${', '}'],
240+
['{', '}'],
241+
['[', ']'],
242+
['(', ')'],
243+
],
244+
autoClosingPairs: [
245+
{
246+
open: '{',
247+
close: '}',
248+
},
249+
{
250+
open: '[',
251+
close: ']',
252+
},
253+
{
254+
open: '(',
255+
close: ')',
256+
},
257+
{
258+
open: "'",
259+
close: "'",
260+
notIn: ['string', 'comment'],
261+
},
262+
{
263+
open: '"',
264+
close: '"',
265+
notIn: ['string'],
266+
},
267+
{
268+
open: '`',
269+
close: '`',
270+
notIn: ['string', 'comment'],
271+
},
272+
{
273+
open: '/**',
274+
close: ' */',
275+
notIn: ['string'],
276+
},
277+
],
278+
surroundingPairs: [
279+
{
280+
open: "'",
281+
close: "'",
282+
},
283+
{
284+
open: '"',
285+
close: '"',
286+
},
287+
{
288+
open: '{',
289+
close: '}',
290+
},
291+
{
292+
open: '[',
293+
close: ']',
294+
},
295+
{
296+
open: '(',
297+
close: ')',
298+
},
299+
{
300+
open: '<',
301+
close: '>',
302+
},
303+
{
304+
open: '`',
305+
close: '`',
306+
},
307+
],
308+
autoCloseBefore: ';:.,=}])>` \n\t',
309+
folding: {
310+
markers: {
311+
start: /^\s*\/\/\s*#?region\b/,
312+
end: /^\s*\/\/\s*#?endregion\b/,
313+
},
314+
},
315+
wordPattern:
316+
/(-?\d*\.\d\w*)|([^\`\~\@\!\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>/\?\s]+)/,
317+
indentationRules: {
318+
decreaseIndentPattern: /^((?!.*?\/\*).*\*\/)?\s*[\}\]].*$/,
319+
increaseIndentPattern:
320+
/^((?!\/\/).)*(\{([^}"'`/]*|(\t|[ ])*\/\/.*)|\([^)"'`/]*|\[[^\]"'`/]*)$/,
321+
unIndentedLinePattern:
322+
/^(\t|[ ])*[ ]\*[^/]*\*\/\s*$|^(\t|[ ])*[ ]\*\/\s*$|^(\t|[ ])*[ ]\*([ ]([^\*]|\*(?!\/))*)?$/,
323+
},
324+
onEnterRules: [
325+
{
326+
beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
327+
afterText: /^\s*\*\/$/,
328+
action: {
329+
indentAction: languages.IndentAction.IndentOutdent,
330+
appendText: ' * ',
331+
},
332+
},
333+
{
334+
beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
335+
action: {
336+
indentAction: languages.IndentAction.None,
337+
appendText: ' * ',
338+
},
339+
},
340+
{
341+
beforeText: /^(\t|[ ])*[ ]\*([ ]([^\*]|\*(?!\/))*)?$/,
342+
previousLineText: /(?=^(\s*(\/\*\*|\*)).*)(?=(?!(\s*\*\/)))/,
343+
action: {
344+
indentAction: languages.IndentAction.None,
345+
appendText: '* ',
346+
},
347+
},
348+
{
349+
beforeText: /^(\t|[ ])*[ ]\*\/\s*$/,
350+
action: {
351+
indentAction: languages.IndentAction.None,
352+
removeText: 1,
353+
},
354+
},
355+
{
356+
beforeText: /^(\t|[ ])*[ ]\*[^/]*\*\/\s*$/,
357+
action: {
358+
indentAction: languages.IndentAction.None,
359+
removeText: 1,
360+
},
361+
},
362+
{
363+
beforeText: /^\s*(\bcase\s.+:|\bdefault:)$/,
364+
afterText: /^(?!\s*(\bcase\b|\bdefault\b))/,
365+
action: {
366+
indentAction: languages.IndentAction.Indent,
367+
},
368+
},
369+
{
370+
previousLineText: /^\s*(((else ?)?if|for|while)\s*\(.*\)\s*|else\s*)$/,
371+
beforeText: /^\s+([^{i\s]|i(?!f\b))/,
372+
action: {
373+
indentAction: languages.IndentAction.Outdent,
374+
},
375+
},
376+
],
377+
}
378+
379+
export const ts: languages.LanguageConfiguration = {
380+
comments: {
381+
lineComment: '//',
382+
blockComment: ['/*', '*/'],
383+
},
384+
brackets: [
385+
['${', '}'],
386+
['{', '}'],
387+
['[', ']'],
388+
['(', ')'],
389+
],
390+
autoClosingPairs: [
391+
{
392+
open: '{',
393+
close: '}',
394+
},
395+
{
396+
open: '[',
397+
close: ']',
398+
},
399+
{
400+
open: '(',
401+
close: ')',
402+
},
403+
{
404+
open: "'",
405+
close: "'",
406+
notIn: ['string', 'comment'],
407+
},
408+
{
409+
open: '"',
410+
close: '"',
411+
notIn: ['string'],
412+
},
413+
{
414+
open: '`',
415+
close: '`',
416+
notIn: ['string', 'comment'],
417+
},
418+
{
419+
open: '/**',
420+
close: ' */',
421+
notIn: ['string'],
422+
},
423+
],
424+
surroundingPairs: [
425+
{
426+
open: "'",
427+
close: "'",
428+
},
429+
{
430+
open: '"',
431+
close: '"',
432+
},
433+
{
434+
open: '{',
435+
close: '}',
436+
},
437+
{
438+
open: '[',
439+
close: ']',
440+
},
441+
{
442+
open: '(',
443+
close: ')',
444+
},
445+
{
446+
open: '<',
447+
close: '>',
448+
},
449+
{
450+
open: '`',
451+
close: '`',
452+
},
453+
],
454+
colorizedBracketPairs: [
455+
['(', ')'],
456+
['[', ']'],
457+
['{', '}'],
458+
['<', '>'],
459+
],
460+
autoCloseBefore: ';:.,=}])>` \n\t',
461+
folding: {
462+
markers: {
463+
start: /^\s*\/\/\s*#?region\b/,
464+
end: /^\s*\/\/\s*#?endregion\b/,
465+
},
466+
},
467+
wordPattern:
468+
/(-?\d*\.\d\w*)|([^\`\~\@\!\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>/\?\s]+)/,
469+
indentationRules: {
470+
decreaseIndentPattern: /^((?!.*?\/\*).*\*\/)?\s*[\}\]].*$/,
471+
increaseIndentPattern:
472+
/^((?!\/\/).)*(\{([^}"'`/]*|(\t|[ ])*\/\/.*)|\([^)"'`/]*|\[[^\]"'`/]*)$/,
473+
unIndentedLinePattern:
474+
/^(\t|[ ])*[ ]\*[^/]*\*\/\s*$|^(\t|[ ])*[ ]\*\/\s*$|^(\t|[ ])*[ ]\*([ ]([^\*]|\*(?!\/))*)?$/,
475+
},
476+
onEnterRules: [
477+
{
478+
beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
479+
afterText: /^\s*\*\/$/,
480+
action: {
481+
indentAction: languages.IndentAction.IndentOutdent,
482+
appendText: ' * ',
483+
},
484+
},
485+
{
486+
beforeText: /^\s*\/\*\*(?!\/)([^\*]|\*(?!\/))*$/,
487+
action: {
488+
indentAction: languages.IndentAction.None,
489+
appendText: ' * ',
490+
},
491+
},
492+
{
493+
beforeText: /^(\t|[ ])*[ ]\*([ ]([^\*]|\*(?!\/))*)?$/,
494+
previousLineText: /(?=^(\s*(\/\*\*|\*)).*)(?=(?!(\s*\*\/)))/,
495+
action: {
496+
indentAction: languages.IndentAction.None,
497+
appendText: '* ',
498+
},
499+
},
500+
{
501+
beforeText: /^(\t|[ ])*[ ]\*\/\s*$/,
502+
action: {
503+
indentAction: languages.IndentAction.None,
504+
removeText: 1,
505+
},
506+
},
507+
{
508+
beforeText: /^(\t|[ ])*[ ]\*[^/]*\*\/\s*$/,
509+
action: {
510+
indentAction: languages.IndentAction.None,
511+
removeText: 1,
512+
},
513+
},
514+
{
515+
beforeText: /^\s*(\bcase\s.+:|\bdefault:)$/,
516+
afterText: /^(?!\s*(\bcase\b|\bdefault\b))/,
517+
action: {
518+
indentAction: languages.IndentAction.Indent,
519+
},
520+
},
521+
{
522+
previousLineText: /^\s*(((else ?)?if|for|while)\s*\(.*\)\s*|else\s*)$/,
523+
beforeText: /^\s+([^{i\s]|i(?!f\b))/,
524+
action: {
525+
indentAction: languages.IndentAction.Outdent,
526+
},
527+
},
528+
],
529+
}

0 commit comments

Comments
 (0)
Please sign in to comment.