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?