您好,登錄后才能下訂單哦!
這篇文章主要介紹了Monaco Editor如何實現sql和java代碼提示的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Monaco Editor如何實現sql和java代碼提示文章都會有所收獲,下面我們一起來看看吧。
//創建和設置值 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代碼提示”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。