Monaco?Editor實(shí)現(xiàn)sql和java代碼提示實(shí)現(xiàn)示例
更新時(shí)間:2022年08月05日 17:11:38 作者:柳杉
這篇文章主要為大家介紹了Monaco?Editor代碼提示sql和java實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
monaco editor創(chuàng)建
//創(chuàng)建和設(shè)置值
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)
}
});
}
// 設(shè)置編輯器語言
this.completionItemProvider = monaco.languages.registerCompletionItemProvider(
language,
{
triggerCharacters: [' ', '.', ...this.triggerCharacters],
provideCompletionItems: (model, position) =>
this.sqlSnippets.provideCompletionItems(model, position)
}
)
sql提示(庫表字段關(guān)聯(lián))
async provideCompletionItems(model, position) {
const { lineNumber, column } = position
// 光標(biāo)前文本
const textBeforePointer = model.getValueInRange({
startLineNumber: lineNumber,
startColumn: 0,
endLineNumber: lineNumber,
endColumn: column
})
const textBeforePointerMulti = model.getValueInRange({
startLineNumber: 1,
startColumn: 0,
endLineNumber: lineNumber,
endColumn: column
})
// 光標(biāo)后文本
// 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()
// 數(shù)據(jù)庫名聯(lián)想
if (lastToken === 'database') {
return {
suggestions: this.getDataBaseSuggest()
}
// <庫名>.<表名> || <別名>.<字段>
} else if (lastToken.endsWith('.')) {
// 去掉點(diǎn)后的字符串
const tokenNoDot = lastToken.slice(0, lastToken.length - 1)
if (this.dbSchema.find(db => db.dbName === tokenNoDot.replace(/^.*,/g, ''))) {
// <庫名>.<表名>聯(lián)想
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, ''))
// <別名>.<字段>聯(lián)想
if (currentTable && currentTable.tableName) {
return {
suggestions: await this.getTableColumnSuggestByTableAlia(currentTable.tableName)
}
} else {
return {
suggestions: []
}
}
} else {
return {
suggestions: []
}
}
// 庫名聯(lián)想
} 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
}
// 字段聯(lián)想
} 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()
}
// 自定義字段聯(lián)想
} else if (this.customKeywords.toString().includes(lastToken)) {
return {
suggestions: this.getCustomSuggest(lastToken.startsWith('$'))
}
// 默認(rèn)聯(lián)想
} else {
return {
suggestions: [...this.getDataBaseSuggest(), ...this.getTableSuggest(), ...this.getKeywordSuggest()]
}
}
}
java自定義聯(lián)想
monaco.languages.registerCompletionItemProvider(
language,
{
triggerCharacters: ['ds.','.'],
provideCompletionItems: (model, position) =>{
const { lineNumber, column } = position
// 光標(biāo)前文本
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實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于Monaco Editor代碼提示的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java如何利用Socket進(jìn)行數(shù)據(jù)讀寫
這篇文章主要介紹了Java如何利用Socket進(jìn)行數(shù)據(jù)讀寫,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java終止循環(huán)體的具體實(shí)現(xiàn)
這篇文章主要介紹了Java終止循環(huán)體的具體實(shí)現(xiàn),需要的朋友可以參考下2014-02-02
Spring實(shí)戰(zhàn)之Bean銷毀之前的行為操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之Bean銷毀之前的行為操作,結(jié)合實(shí)例形式分析了spring在bean銷毀之前的行為相關(guān)設(shè)置與使用技巧,需要的朋友可以參考下2019-11-11
Idea中如何調(diào)出Run dashboard 或services窗口
這篇文章主要介紹了Idea中如何調(diào)出Run dashboard 或services窗口問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
基于Java匯總Spock框架Mock靜態(tài)資源經(jīng)驗(yàn)
這篇文章主要介紹了基于Java匯總Spock框架Mock靜態(tài)資源經(jīng)驗(yàn),前面講了?Spock框架Mock對象、方法經(jīng)驗(yàn)總結(jié),今天分享一下Spock框架中Mock靜態(tài)資源的實(shí)踐經(jīng)驗(yàn)匯總。分成靜態(tài)資源和混合場景,需要的朋友可以參考一下2022-02-02
解決httpServletRequest.getParameter獲取不到參數(shù)的問題
這篇文章主要介紹了解決httpServletRequest.getParameter獲取不到參數(shù)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07

