codemirror - 如何正确定义CodeMirror语言?

标签 codemirror react-codemirror

我正在尝试为这样的事情提供基本的自动完成功能:

db.collection("Items").where("name", "==", "temp").limit(1).get();

这是我到目前为止使用 CodeMirror 6StreamLanguage 的代码:

import {
  IndentContext,
  LanguageSupport,
  StreamLanguage,
  StringStream
} from "@codemirror/language";
import { tags as t } from "@lezer/highlight";

export const FireStoreLanguage = StreamLanguage.define({
  name: "firestore",
  startState: (indentUnit: number) => {
    return {};
  },
  token: (stream: StringStream, state: any = {}): string | null => {
    console.log(stream);
    if (stream.match("db")) {
      state.db = true;
      return "keyword";
    }
    if (stream.match(".")) {
      if (state.db) {
        state.db = false;
        state.collection = true;
        return "keyword";
      } else if (state.collection) {
        state.collection = false;
        state.where = true;
        return "keyword";
      } else if (state.where) {
        state.where = false;
        state.limit = true;
        return "keyword";
      } else if (state.limit) {
        state.limit = false;
        return "keyword";
      }
    }
    if (stream.match("collection")) {
      if (state.db) {
        state.collection = true;
        return "keyword";
      }
    }
    if (stream.match("where")) {
      if (state.collection) {
        state.where = true;
        return "keyword";
      }
    }
    if (stream.match("limit")) {
      if (state.where) {
        state.limit = true;
        return "keyword";
      }
    }
    if (stream.match("get")) {
      if (state.limit) {
        state.limit = false;
        return "keyword";
      }
    }
    if (stream.match(/"(?:[^\\"]|\\.)*"/)) {
      if (state.collection) {
        return "string";
      }
      if (state.where) {
        state.where = false;
        state.whereValue = true;
        return "string";
      }
      if (state.whereValue) {
        state.whereValue = false;
        return "string";
      }
      if (stream.match("==")) {
        if (state.whereValue) {
          state.whereValue = false;
          state.whereOperator = true;
          return "operator";
        }
      }
      if (stream.match(/[0-9]+/)) {
        if (state.limit) {
          return "number";
        }
      }
    }
    stream.next();
    return null;
  },
  blankLine: (state: {}, indentUnit: number): void => {},
  copyState: (state: {}) => {},
  indent: (
    state: {},
    textAfter: string,
    context: IndentContext
  ): number | null => {
    return 1;
  },
  languageData: {
    commentTokens: { line: ";" },
  },
  tokenTable: {
    db: t.keyword,
    dot: t.punctuation,
    collection: t.keyword,
    get: t.keyword,
    lParen: t.punctuation,
    rParen: t.punctuation,
    string: t.string,
  },
});

export function firestore() {
  return new LanguageSupport(FireStoreLanguage);
}

在 React 中,我是这样使用它的(构建之后):

import CodeMirror from "@uiw/react-codemirror";
import React from "react";
import { firestore } from "./firestore";

function App() {
  const onChange = React.useCallback((value, viewUpdate) => {
    console.log("value:", value);
  }, []);
  return (
    <CodeMirror
      value={``}
      height="100vh"
      extensions={[firestore()]}
      onChange={onChange}
    />
  );
}

export default App;

编辑器加载正常,但在我输入时没有提供自动完成功能!

上面的代码中我做错了什么或遗漏了什么?

最佳答案

我缺少这些部分:

export const FireStoreCompletion = FireStoreLanguage.data.of({
  autocomplete: completeFromList([
    { label: "db", type: "namespace" },
    { label: "collection", type: "function" },
    { label: "where", type: "function" },
    { label: "limit", type: "function" },
    { label: "get", type: "function" },
  ]),
});

export function firestore() {
  return new LanguageSupport(FireStoreLanguage, [FireStoreCompletion]);
}

关于codemirror - 如何正确定义CodeMirror语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75191956/

相关文章:

css - 如何使用 React Codemirror 自动建议 css 关键字?

javascript - 在React组件中导出方法

javascript - 我正在尝试使用 CodeMirror 编辑器构建 D3.js,但我在其中输入的任何内容都无法由 D3.js 正确可视化。我缺少什么?

java - 使用 native JS 的 GWT JSNI 方法 : "Invalid Label"

javascript - 带有 Zen Coding 的 CodeMirror 不工作

javascript - 使用 CSS 调整 CodeMirror 编辑器大小不起作用

javascript - 代码镜像 html 在 chrome 中不起作用,但在其他浏览器中起作用

reactjs - Codemirror 在一行上显示所有 JSON