大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍了Monaco Editor怎么实现SQL和Java代码提示的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Monaco Editor怎么实现SQL和Java代码提示文章都会有所收获,下面我们一起来看看吧。
成都创新互联公司作为成都网站建设公司,专注成都网站建设、网站设计,有关成都企业网站建设方案、改版、费用等问题,行业涉及iso认证等多个领域,已为上千家企业服务,得到了客户的尊重与认可。
//创建和设置值 if (!this.monacoEditor) { this.monacoEditor = monaco.editor.create(this._node, { value: value || code, language: language, ...options }); this.monacoEditor.onDidChangeModelContent(e => { const value = this.monacoEditor.getValue(); //使value和其值保持一致 if (value !== this.value) { this.value = value; this.props.getValue && this.props.getValue(value) } }); }
// 设置编辑器语言 this.completionItemProvider = monaco.languages.registerCompletionItemProvider( language, { triggerCharacters: [' ', '.', ...this.triggerCharacters], provideCompletionItems: (model, position) => this.sqlSnippets.provideCompletionItems(model, position) } )
async provideCompletionItems(model, position) { const { lineNumber, column } = position // 光标前文本 const textBeforePointer = model.getValueInRange({ startLineNumber: lineNumber, startColumn: 0, endLineNumber: lineNumber, endColumn: column }) const textBeforePointerMulti = model.getValueInRange({ startLineNumber: 1, startColumn: 0, endLineNumber: lineNumber, endColumn: column }) // 光标后文本 // const textAfterPointer = model.getValueInRange({ // startLineNumber: lineNumber, // startColumn: column, // endLineNumber: lineNumber, // endColumn: model.getLineMaxColumn(model.getLineCount()) // }) const textAfterPointerMulti = model.getValueInRange({ startLineNumber: lineNumber, startColumn: column, endLineNumber: model.getLineCount(), endColumn: model.getLineMaxColumn(model.getLineCount()) }) // const nextTokens = textAfterPointer.trim().split(/\s+/) // const nextToken = nextTokens[0].toLowerCase() const tokens = textBeforePointer.trim().split(/\s+/) const lastToken = tokens[tokens.length - 1].toLowerCase() // 数据库名联想 if (lastToken === 'database') { return { suggestions: this.getDataBaseSuggest() } // <库名>.<表名> || <别名>.<字段> } else if (lastToken.endsWith('.')) { // 去掉点后的字符串 const tokenNoDot = lastToken.slice(0, lastToken.length - 1) if (this.dbSchema.find(db => db.dbName === tokenNoDot.replace(/^.*,/g, ''))) { // <库名>.<表名>联想 return { suggestions: [...this.getTableSuggestByDbName(tokenNoDot.replace(/^.*,/g, ''))] } } else if (this.getTableNameAndTableAlia(textBeforePointerMulti.split(';')[textBeforePointerMulti.split(';').length - 1] + textAfterPointerMulti.split(';')[0])) { const tableInfoList = this.getTableNameAndTableAlia(textBeforePointerMulti.split(';')[textBeforePointerMulti.split(';').length - 1] + textAfterPointerMulti.split(';')[0]) const currentTable = tableInfoList.find(item => item.tableAlia === tokenNoDot.replace(/^.*,/g, '')) // <别名>.<字段>联想 if (currentTable && currentTable.tableName) { return { suggestions: await this.getTableColumnSuggestByTableAlia(currentTable.tableName) } } else { return { suggestions: [] } } } else { return { suggestions: [] } } // 库名联想 } else if (lastToken === 'from' || lastToken === 'join' || /(from|join)\s+.*?\s?,\s*$/.test(textBeforePointer.replace(/.*?(/gm, '').toLowerCase())) { // const tables = this.getTableSuggest() const databases = this.getDataBaseSuggest() return { suggestions: databases } // 字段联想 } else if (['select', 'where', 'order by', 'group by', 'by', 'and', 'or', 'having', 'distinct', 'on'].includes(lastToken.replace(/.*?(/g, '')) || (lastToken.endsWith('.') && !this.dbSchema.find(db => `${db.dbName}.` === lastToken)) || /(select|where|order by|group by|by|and|or|having|distinct|on)\s+.*?\s?,\s*$/.test(textBeforePointer.toLowerCase())) { return { suggestions: await this.getTableColumnSuggest() } // 自定义字段联想 } else if (this.customKeywords.toString().includes(lastToken)) { return { suggestions: this.getCustomSuggest(lastToken.startsWith('$')) } // 默认联想 } else { return { suggestions: [...this.getDataBaseSuggest(), ...this.getTableSuggest(), ...this.getKeywordSuggest()] } } }
monaco.languages.registerCompletionItemProvider( language, { triggerCharacters: ['ds.','.'], provideCompletionItems: (model, position) =>{ const { lineNumber, column } = position // 光标前文本 const textBeforePointer = model.getValueInRange({ startLineNumber: lineNumber, startColumn: 0, endLineNumber: lineNumber, endColumn: column }) if(['ds.'].includes(textBeforePointer)){ return {suggestions: [ { label: 'connection("")', //显示的提示名称 insertText: 'connection("")' //选择后粘贴到编辑器中的文字 }, { label: 'query("","")', insertText: 'query("","")' }, ]}; } if(['ds.connection("").'].includes(textBeforePointer)){ return {suggestions: [ { label: 'query("")', insertText: 'query("")', }, ]}; } } } )
关于“Monaco Editor怎么实现SQL和Java代码提示”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Monaco Editor怎么实现SQL和Java代码提示”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联行业资讯频道。