首頁 > Java > java教程 > 主體

Monaco Editor怎麼實作SQL和Java程式碼提示

PHPz
發布: 2023-05-11 17:31:10
轉載
2054 人瀏覽過

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(&#39;.&#39;)) {
    // 去掉点后的字符串
    const tokenNoDot = lastToken.slice(0, lastToken.length - 1)
    if (this.dbSchema.find(db => db.dbName === tokenNoDot.replace(/^.*,/g, &#39;&#39;))) {
      // <库名>.<表名>联想
      return {
        suggestions: [...this.getTableSuggestByDbName(tokenNoDot.replace(/^.*,/g, &#39;&#39;))]
      }
    } else if (this.getTableNameAndTableAlia(textBeforePointerMulti.split(&#39;;&#39;)[textBeforePointerMulti.split(&#39;;&#39;).length - 1] + textAfterPointerMulti.split(&#39;;&#39;)[0])) {
      const tableInfoList = this.getTableNameAndTableAlia(textBeforePointerMulti.split(&#39;;&#39;)[textBeforePointerMulti.split(&#39;;&#39;).length - 1] + textAfterPointerMulti.split(&#39;;&#39;)[0])
      const currentTable = tableInfoList.find(item => item.tableAlia === tokenNoDot.replace(/^.*,/g, &#39;&#39;))
      // <别名>.<字段>联想
      if (currentTable && currentTable.tableName) {
        return {
          suggestions: await this.getTableColumnSuggestByTableAlia(currentTable.tableName)
        }
      } else {
        return {
          suggestions: []
        }
      }
    } else {
      return {
        suggestions: []
      }
    }
    // 库名联想
  } else if (lastToken === &#39;from&#39; || lastToken === &#39;join&#39; || /(from|join)\s+.*?\s?,\s*$/.test(textBeforePointer.replace(/.*?(/gm, &#39;&#39;).toLowerCase())) {
    // const tables = this.getTableSuggest()
    const databases = this.getDataBaseSuggest()
    return {
      suggestions: databases
    }
    // 字段联想
  } else if ([&#39;select&#39;, &#39;where&#39;, &#39;order by&#39;, &#39;group by&#39;, &#39;by&#39;, &#39;and&#39;, &#39;or&#39;, &#39;having&#39;, &#39;distinct&#39;, &#39;on&#39;].includes(lastToken.replace(/.*?(/g, &#39;&#39;)) || (lastToken.endsWith(&#39;.&#39;) && !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(&#39;$&#39;))
    }
    // 默认联想
  } else {
    return {
      suggestions: [...this.getDataBaseSuggest(), ...this.getTableSuggest(), ...this.getKeywordSuggest()]
    }
  }
}
登入後複製

java自訂聯想

 monaco.languages.registerCompletionItemProvider(
  language,
  {
    triggerCharacters: [&#39;ds.&#39;,&#39;.&#39;],
    provideCompletionItems: (model, position) =>{
      const { lineNumber, column } = position
      // 光标前文本
      const textBeforePointer = model.getValueInRange({
        startLineNumber: lineNumber,
        startColumn: 0,
        endLineNumber: lineNumber,
        endColumn: column
      })
      if([&#39;ds.&#39;].includes(textBeforePointer)){
        return {suggestions: [
            {
              label: &#39;connection("")&#39;, //显示的提示名称
              insertText: &#39;connection("")&#39; //选择后粘贴到编辑器中的文字
            },
            {
              label: &#39;query("","")&#39;,
              insertText: &#39;query("","")&#39;
            },
          ]};
      }
      if([&#39;ds.connection("").&#39;].includes(textBeforePointer)){
        return {suggestions: [
            {
              label: &#39;query("")&#39;,
              insertText: &#39;query("")&#39;,
            },
          ]};
      }
    }
  }
)
登入後複製

以上是Monaco Editor怎麼實作SQL和Java程式碼提示的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板