public GoFileMetadata(GoFile file) { packageName = file.getPackage().getPackageName(); main = file.getMainFunction() != null; imports = new ArrayList<String>(); GoImportDeclarations[] importDeclarations = file.getImportDeclarations(); for (GoImportDeclarations importDeclaration : importDeclarations) { GoImportDeclaration[] importSpecs = importDeclaration.getDeclarations(); for (GoImportDeclaration importSpec : importSpecs) { imports.add(importSpec.getImportPath()); } } }
private static void doAddImport(GoFile file, Editor editor, String pathToImport) { GoImportDeclarations[] ids = file.getImportDeclarations(); Document document = editor.getDocument(); if (ids.length == 0) { addImportUnderPackage(file, document, pathToImport); return; } GoImportDeclarations importDeclarations = ids[ids.length - 1]; GoImportDeclaration[] imports = importDeclarations.getDeclarations(); if (imports.length == 0) { addImportUnderPackage(file, document, pathToImport); return; } GoImportDeclaration lastImport = imports[imports.length - 1]; PsiElement lastChild = getPrevSiblingIfItsWhiteSpaceOrComment(importDeclarations.getLastChild()); if (lastChild == null) { addImportUnderPackage(file, document, pathToImport); return; } if (")".equals(lastChild.getText())) { document.insertString(lastChild.getTextOffset(), "\"" + pathToImport + "\"\n"); int line = document.getLineNumber(lastChild.getTextOffset()); reformatLines(file, editor, line, line); } else { String oldImport = lastImport.getText(); int start = lastImport.getTextOffset(); int end = start + lastImport.getTextLength(); String declarations = String.format("(\n%s\n\"%s\"\n)", oldImport, pathToImport); document.replaceString(start, end, declarations); reformatPositions(file, start, start + declarations.length()); } }