Maison > Java > javaDidacticiel > Comment Monaco Editor implémente les invites de code SQL et Java

Comment Monaco Editor implémente les invites de code SQL et Java

PHPz
Libérer: 2023-05-11 17:31:10
avant
2066 Les gens l'ont consulté

l'éditeur monaco crée

//创建和设置值
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)
    }
  });
}
Copier après la connexion
// 设置编辑器语言
  this.completionItemProvider = monaco.languages.registerCompletionItemProvider(
    language,
    {
      triggerCharacters: [' ', '.', ...this.triggerCharacters],
      provideCompletionItems: (model, position) =>
        this.sqlSnippets.provideCompletionItems(model, position)
    }
  )
Copier après la connexion

invite SQL (association de champs de table de bibliothèque)

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()]
    }
  }
}
Copier après la connexion

association personnalisée 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;,
            },
          ]};
      }
    }
  }
)
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:yisu.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal