|
@@ -1,5 +1,6 @@
|
|
import React from "react";
|
|
import React from "react";
|
|
import CodeMirror from "codemirror";
|
|
import CodeMirror from "codemirror";
|
|
|
|
+import { codeCheck } from "./request";
|
|
import 'codemirror/lib/codemirror.css' // CodeMirrory原生样式
|
|
import 'codemirror/lib/codemirror.css' // CodeMirrory原生样式
|
|
import 'codemirror/mode/sql/sql'
|
|
import 'codemirror/mode/sql/sql'
|
|
import 'codemirror/mode/python/python'
|
|
import 'codemirror/mode/python/python'
|
|
@@ -8,20 +9,155 @@ import 'codemirror/addon/display/placeholder'
|
|
import 'codemirror/addon/hint/show-hint.css' // 用来做代码提示
|
|
import 'codemirror/addon/hint/show-hint.css' // 用来做代码提示
|
|
import 'codemirror/addon/hint/show-hint.js' // 用来做代码提示
|
|
import 'codemirror/addon/hint/show-hint.js' // 用来做代码提示
|
|
import 'codemirror/addon/hint/sql-hint.js' // 用来做代码提示
|
|
import 'codemirror/addon/hint/sql-hint.js' // 用来做代码提示
|
|
|
|
+import 'codemirror/addon/lint/lint.js'
|
|
|
|
+import 'codemirror/addon/lint/lint.css'
|
|
|
|
+import { message } from "antd";
|
|
|
|
|
|
|
|
+/* async function check_python_syntax(text: any, result_cb: any) {
|
|
|
|
+ const error_list = [
|
|
|
|
+ {
|
|
|
|
+ line_no: 1,
|
|
|
|
+ column_no_start: 0,
|
|
|
|
+ column_no_stop: 3,
|
|
|
|
+ message: "invalid syntax",
|
|
|
|
+ severity: "error"
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ line_no: 2,
|
|
|
|
+ column_no_start: 1,
|
|
|
|
+ column_no_stop: 5,
|
|
|
|
+ message: "convention violation",
|
|
|
|
+ severity: "warning"
|
|
|
|
+ }
|
|
|
|
+ ]
|
|
|
|
+ console.log('text:', text);
|
|
|
|
+ result_cb(error_list)
|
|
|
|
+} */
|
|
export default class ScriptEditor extends React.Component<any, any> {
|
|
export default class ScriptEditor extends React.Component<any, any> {
|
|
constructor(props: any) {
|
|
constructor(props: any) {
|
|
super(props)
|
|
super(props)
|
|
this.state = {
|
|
this.state = {
|
|
- editor: {}
|
|
|
|
|
|
+ editor: {},
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ python_validator(content: any, updateLinting: any, options: any, cm: any) {
|
|
|
|
+ const text = cm.getValue() + "\n";
|
|
|
|
+
|
|
|
|
+ if(text.trim() == "")
|
|
|
|
+ {
|
|
|
|
+ updateLinting(cm, []);
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ console.log('options:', options);
|
|
|
|
+ updateLinting(cm, options.error_list)
|
|
|
|
+ // function result_cb(error_list: any)
|
|
|
|
+ // {
|
|
|
|
+ // const found: any = [];
|
|
|
|
+ // error_list.forEach((error: any) => {
|
|
|
|
+ // if(error != null) {
|
|
|
|
+ // const start_line = error.line_no;
|
|
|
|
+ // const start_char = error.column_no_start;
|
|
|
|
+ // const end_line = error.line_no;
|
|
|
|
+ // const end_char = error.column_no_stop;
|
|
|
|
+ // const message = error.message;
|
|
|
|
+ // const severity = error.severity;
|
|
|
|
+ // found.push({
|
|
|
|
+ // from: CodeMirror.Pos(start_line - 1, start_char),
|
|
|
|
+ // to: CodeMirror.Pos(end_line - 1, end_char),
|
|
|
|
+ // message: message,
|
|
|
|
+ // severity: severity
|
|
|
|
+ // });
|
|
|
|
+ // }
|
|
|
|
+ // })
|
|
|
|
+ // updateLinting(cm, found);
|
|
|
|
+ // }
|
|
|
|
+ // updateLinting(cm, this.error_found);
|
|
|
|
+
|
|
|
|
+ // check_python_syntax(text, result_cb)
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ async checkSyntax() {
|
|
|
|
+ message.loading({content: '正在校验代码,请稍等...', key: 'check'})
|
|
|
|
+ const editor = this.state.editor
|
|
|
|
+ const code = editor.getValue()
|
|
|
|
+ const code_type = this.props.type === 'SQL' ? 'sql' : 'python'
|
|
|
|
+ const { data } = await codeCheck({
|
|
|
|
+ code,
|
|
|
|
+ code_type
|
|
|
|
+ })
|
|
|
|
+ const error_list: Array<any> = []
|
|
|
|
+ if (data.code === 200) {
|
|
|
|
+ switch (code_type) {
|
|
|
|
+ case 'python':
|
|
|
|
+ data.data.forEach((item: any) => {
|
|
|
|
+ if(item.type === 'error' || item.type === 'warning') {
|
|
|
|
+ error_list.push({
|
|
|
|
+ from: CodeMirror.Pos(item.line - 1, 0),
|
|
|
|
+ to: CodeMirror.Pos(item.line - 1, 0),
|
|
|
|
+ message: item.message,
|
|
|
|
+ severity: item.type
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ break;
|
|
|
|
+ case 'sql':
|
|
|
|
+ data.data.forEach((item: any) => {
|
|
|
|
+ error_list.push({
|
|
|
|
+ from: CodeMirror.Pos(item.line_no - 1, 0),
|
|
|
|
+ to: CodeMirror.Pos(item.line_no - 1, 0),
|
|
|
|
+ message: item.description,
|
|
|
|
+ severity: 'error'
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ message.error('获取校验数据失败')
|
|
|
|
+ }
|
|
|
|
+ editor.setOption('lint', {
|
|
|
|
+ getAnnotations: this.python_validator,
|
|
|
|
+ async: true,
|
|
|
|
+ options: {
|
|
|
|
+ error_list,
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ message.success({content: '代码校验完成!', key: 'check', duration: 2})
|
|
}
|
|
}
|
|
|
|
|
|
componentDidMount() {
|
|
componentDidMount() {
|
|
const editor = CodeMirror.fromTextArea(document.querySelector('.editor') as any, {
|
|
const editor = CodeMirror.fromTextArea(document.querySelector('.editor') as any, {
|
|
lineNumbers: true,
|
|
lineNumbers: true,
|
|
mode: this.props.type === 'SQL' ? 'sql' : 'python',
|
|
mode: this.props.type === 'SQL' ? 'sql' : 'python',
|
|
|
|
+ gutters: ["CodeMirror-lint-markers"],
|
|
|
|
+ lint: {
|
|
|
|
+ getAnnotations: this.python_validator,
|
|
|
|
+ async: true,
|
|
|
|
+ options: {
|
|
|
|
+ error_list: [],
|
|
|
|
+ }
|
|
|
|
+ },
|
|
})
|
|
})
|
|
|
|
+ /* setTimeout(() => {
|
|
|
|
+ editor.setOption('lint', {
|
|
|
|
+ getAnnotations: this.python_validator,
|
|
|
|
+ async: true,
|
|
|
|
+ options: {
|
|
|
|
+ error_list: [
|
|
|
|
+ {
|
|
|
|
+ from: CodeMirror.Pos(1, 0),
|
|
|
|
+ to: CodeMirror.Pos(1, 0),
|
|
|
|
+ message: '456456\n123\n123',
|
|
|
|
+ severity: 'warning'
|
|
|
|
+ }
|
|
|
|
+ ]
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }, 2000) */
|
|
editor.setSize(null, '100%')
|
|
editor.setSize(null, '100%')
|
|
editor.setValue(this.props.scriptText)
|
|
editor.setValue(this.props.scriptText)
|
|
this.setState({editor})
|
|
this.setState({editor})
|