Skip to content

context.matchBefore for creating autocompletions ignores other matches if there is a match at the start of the line #1510

Open
codemirror/autocomplete
#27
@nsrCodes

Description

@nsrCodes

Here's a demo.ts file to help reproduce the bug:

import {EditorView, basicSetup} from "codemirror"
import {javascript} from "@codemirror/lang-javascript"
import { autocompletion, Completion, CompletionResult, CompletionSource } from "@codemirror/autocomplete";


const dummyCompletions: Completion[] = [{label: "foo"}, {label: "bar"}] 

function generateCompletionSource(
  matchPattern: RegExp,
  completions: Completion[],
  lengthOfStartingChars: number
): CompletionSource {
  return (context) => {
    const match = context.matchBefore(matchPattern);
    if (match) {
      return {
        from: match.from + lengthOfStartingChars,
        to: match.to,
        options: completions,
        filter: true,
      } as CompletionResult;
    } else return null;
  };
}

function generateAutocompletePlugin() {
  return autocompletion({
    override: [generateCompletionSource(/\{\{.*?/g, dummyCompletions, 2)]
  })
}

;(window as any).view = new EditorView({
  doc: 'console.log("Hello world")',
  extensions: [
    basicSetup,
    javascript(),
    generateAutocompletePlugin()
  ],
  parent: document.body
})

if the editor has a line like {{anything}} {{fo and the cursor is at the end, the auto completions are not shown even though it should be captured by the regex in the code (/\{\{.*?/g at least since there is an explicit global flag)

The auto completions are not shown for any matching occurrences after the first one

Screen.Recording.2025-01-22.at.10.19.53.PM.mov

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions