Browse Source

语法错误提示

mxd 3 years ago
parent
commit
e564541748
1 changed files with 28 additions and 0 deletions
  1. 28 0
      src/components/common/magic-monaco-editor.vue

+ 28 - 0
src/components/common/magic-monaco-editor.vue

@@ -9,6 +9,9 @@ import { CommandsRegistry } from 'monaco-editor/esm/vs/platform/commands/common/
 import { KeybindingsRegistry } from 'monaco-editor/esm/vs/platform/keybinding/common/keybindingsRegistry.js'
 import { ContextKeyExpr } from 'monaco-editor/esm/vs/platform/contextkey/common/contextkey.js'
 import { nextTick, watch } from 'vue'
+import { Parser } from '../../scripts/parsing/parser.js'
+import { TokenStream } from '../../scripts/parsing/index.js'
+import tokenizer from '../../scripts/parsing/tokenizer.js'
 export default {
 	props: {
 		language: { type: String, required: true },
@@ -51,6 +54,7 @@ export default {
 		this.instance.onDidChangeModelContent((e) => {
 			this.$emit('update:value', this.instance.getValue())
 			this.$emit('change', e)
+			this.doValidate()
 
 		})
 		this.instance.addAction({
@@ -89,6 +93,30 @@ export default {
 
 	},
 	methods: {
+		doValidate(){
+			if(this.instance){
+				monaco.editor.setModelMarkers(this.instance.getModel(), 'validate', [{}])
+			}
+			if(this.language === 'magicscript'){
+				try{
+					new Parser(new TokenStream(tokenizer(this.instance.getValue()))).parse()
+				} catch(e){
+					if(e.span){
+						let line = e.span.getLine()
+						monaco.editor.setModelMarkers(this.instance.getModel(), 'validate', [
+							{
+							startLineNumber: line.lineNumber,
+							endLineNumber: line.endLineNumber,
+							startColumn: line.startCol,
+							endColumn: line.endCol,
+							message: e.message,
+							severity: monaco.MarkerSeverity.Error
+							}
+						])
+					}
+				}
+			}
+		},
 		getEditorDom(){
 			return this.$refs.editor
 		},