Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend autocompletions #4

Closed
brendand opened this issue Jan 21, 2023 · 1 comment
Closed

Extend autocompletions #4

brendand opened this issue Jan 21, 2023 · 1 comment

Comments

@brendand
Copy link

I'd like to be able to add in my own autocompletions to the list of available ones.

Do you know how to do that? The documentation on the CodeMirror 6 website is not very clear on how to put it all together.

I've been trying to put some of the examples into your editor.js file, but I end up getting compile errors.

Do you know how to extend the list of autocompletions?

@brendand
Copy link
Author

I figured out how to do this. In editor.js add the following code:

const completions = [
    {label: "fetchRecords", type: "keyword"},
    {label: "record", type: "variable", info: "The current selected record"},
    {label: "form", type: "variable", info: "The current selected form"},
    {label: "search", type: "variable", info: "The current selected search"},
    {label: "document", type: "variable", info: "The current opened document"},
    {label: "Utils", type: "keyword"},
];

function customCompletions(context) {
    let word = context.matchBefore(/\w*/)
    if (word.from == word.to && !context.explicit)
        return null
        return {
            from: word.from,
        options: completions
        }
};

const myCustomCompletions = javascriptLanguage.data.of({
autocomplete: customCompletions
});

and then in the extensions part of creating the CodeMirror.EditorView, add myCustomCompletions above autocompletion()

const editorView = new CodeMirror.EditorView({
  doc: "",
  extensions: [
      lineNumbers(),
      highlightActiveLineGutter(),
      highlightSpecialChars(),
      history(),
      foldGutter(),
      drawSelection(),
      dropCursor(),
      indentOnInput(),
      syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
      bracketMatching(),
      closeBrackets(),
      myCustomCompletions,
      autocompletion(),
      rectangularSelection(),
      crosshairCursor(),
      highlightActiveLine(),
      highlightSelectionMatches(),
      keymap.of([
          ...closeBracketsKeymap,
             ...defaultKeymap,
             ...searchKeymap,
             ...historyKeymap,
             ...foldKeymap,
             ...completionKeymap,
             indentWithTab,
      ]),
      readOnly.of([]),
      lineWrapping.of([]),
      baseTheme,
      theme.of(oneDark),
      language.of(javascript()),
      listener.of([]),
  ],
  parent: document.body,
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant