Golang injection not working right

I’m developing a plugin to support injecting golang into a yml file.
The yml file looks like this:

mapper:
     partitioner:
         package:  some.golang.package.path.here
         func:  a.func.from.the.above.golang.pakcage

And My MultiHostInjector Look like

public class PakcageInjector implements MultiHostInjector {
    @Override
    public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
        String pos = InjectPos(context);
        if (pos == null) {
            return;
        }
        String prefix, suffix;
        registrar.startInjecting(GoLanguage.INSTANCE);
        if (pos.equals("package")) {
            prefix = "package main\nimport p \"";
            suffix = "\"\n";
            registrar.addPlace(prefix, suffix, (PsiLanguageInjectionHost) context,
                    new TextRange(0, context.getTextLength()));
        } else if (pos.equals("func")) {
            prefix = "\nvar _ string = p.";
            suffix = "()\n";
            registrar.addPlace(prefix, suffix, (PsiLanguageInjectionHost) context,
                    new TextRange(0, context.getTextLength()));
        }
        registrar.doneInjecting();
    }

    public String InjectPos(PsiElement context) {
        if (!(context instanceof YAMLScalarListImpl) && !(context instanceof YAMLPlainTextImpl)) {
            return null;
        }
        PsiElement parent = context.getParent();
        if (!(parent instanceof YAMLKeyValue kv)) {
            return null;
        }
        if (kv.getKeyText().equals("package")) {
            return "package";
        }
        if (kv.getKeyText().equals("func")) {
            return "func";
        }
        return null;
    }

    @Override
    public @NotNull List<? extends Class<? extends PsiElement>> elementsToInjectIn() {
        return List.of(YAMLScalarListImpl.class, YAMLPlainTextImpl.class);
    }
}

It does inject GO at value of package . But it never working right

“context” is a valid standard package, but it always sad

Cannot resolve symbol '"contex'

I don’t know why the “t” at the end is ignored.

And there’re no auto completion pop-up as I type the “context” word.

If I remove those prefix & suffix, and write full goland code in the “package” value, it works like I expect. So I guess the problem is with prefix & suffix ? Can anyone tell me what’s wrong with my injection?

Please use the simpler API LanguageInjectionContributor. MultiHostInjector is known to be quite advanced and hard to do right, you probably have errors in ranges computations.

Most of injections should use LanguageInjectionContributor instead where they only need to check their place and choose the target language.

well I changed to LanguageInjectionContributor and got exactly the same error.

you probably have errors in ranges computations

The ranges code use context.getTextLength(). I think it should right. And I’ve tried context.getTextLength() + 1, but it proceduced an out of range by one error.

registrar.addPlace(prefix, suffix, (PsiLanguageInjectionHost) context,
                    new TextRange(0, context.getTextLength()));