if (!(startElement instanceof GoImportDeclaration) || !(file instanceof GoFile)) {
return;
}
GoImportDeclaration declaration = (GoImportDeclaration) startElement;
final GoImportDeclarations declarations = (GoImportDeclarations) declaration.getParent();
String removeImport = declaration.getText();
if (removeImport == null) {
return;
}
GoImportDeclaration[] da = declarations.getDeclarations();
// if there are more than 2 imports, just remove current one.
if (da.length > 2) {
removeWholeElement(declaration);
return;
}
// if there are exactly 2 imports, replace the whole import to import "theOther". i.e. remove parenthesis.
if (da.length == 2) {
final PsiElement newImport = getNewImport(da, startElement.getText(), (GoFile) file);
if (newImport != null) {
WriteCommandAction writeCommandAction = new WriteCommandAction(file.getProject()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
declarations.replace(newImport);
}
};
writeCommandAction.execute();
return;
}