Skip to content

Instantly share code, notes, and snippets.

@andresdominguez
Last active December 3, 2016 18:29
Show Gist options
  • Save andresdominguez/8811f467eab0bdab7e1c51479a0a17a4 to your computer and use it in GitHub Desktop.
Save andresdominguez/8811f467eab0bdab7e1c51479a0a17a4 to your computer and use it in GitHub Desktop.
public class InjectTestAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent e) {
PsiFile file = e.getData(PlatformDataKeys.PSI_FILE);
Caret caret = e.getData(PlatformDataKeys.CARET);
Editor editor = e.getData(PlatformDataKeys.EDITOR);
JSParameterList injectParameterList = findInjectParameterList(file);
// Subtract 1 because it ends with ).
int paramListOffset = injectParameterList.getTextRange().getEndOffset() - 1;
// Surround injectable with underscores.
String injectableWithUnderscores = "_" + caret.getSelectedText() + "_";
Document document = editor.getDocument();
// Assign the variable <selection> = _<selection>_;
document.insertString(caret.getSelectionEnd(), " = " + injectableWithUnderscores + ";");
// Add the dependency parameter list.
document.insertString(paramListOffset, injectableWithUnderscores);
}
JSParameterList findInjectParameterList(...) { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment