这篇文章主要为大家介绍了Monaco Editor代码提示sql和java实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
monaco editor创建
//创建和设置值
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)
}
)
sql提示(库表字段关联)
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()]
}
}
}
java自定义联想
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代码提示的资料请关注编程学习网其它相关文章!
沃梦达教程
本文标题为:Monaco Editor实现sql和java代码提示实现示例
基础教程推荐
猜你喜欢
- Java实现查找文件和替换文件内容 2023-04-06
- Java实现线程插队的示例代码 2022-09-03
- Java文件管理操作的知识点整理 2023-05-19
- springboot自定义starter方法及注解实例 2023-03-31
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- Java并发编程进阶之线程控制篇 2023-03-07
- java基础知识之FileInputStream流的使用 2023-08-11
- java实现多人聊天系统 2023-05-19
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- Java数据结构之对象比较详解 2023-03-07