protected RunnerAndConfigurationSettings findExistingByElement(
      Location location, @NotNull List<RunnerAndConfigurationSettings> existingConfigurations) {
    for (RunnerAndConfigurationSettings existingConfiguration : existingConfigurations) {
      final RunConfiguration configuration = existingConfiguration.getConfiguration();

      if (!(configuration instanceof GoApplicationConfiguration)) {
        continue;
      }

      GoApplicationConfiguration goApplicationConfiguration =
          (GoApplicationConfiguration) configuration;

      GoFile goFile = locationToFile(location);
      if (goFile != null) {
        VirtualFile virtualFile = goFile.getVirtualFile();
        if (virtualFile != null
            && virtualFile
                .getPath()
                .endsWith(
                    FileUtil.toSystemIndependentName(goApplicationConfiguration.scriptName))) {
          return existingConfiguration;
        }
      }
    }

    return null;
  }
  @Override
  public boolean processDeclarations(
      @NotNull PsiScopeProcessor processor,
      @NotNull ResolveState state,
      PsiElement lastParent,
      @NotNull PsiElement place) {
    // import _ "a"; ( no declarations are visible from this import )
    if (getPackageReference() != null && getPackageReference().isBlank()) {
      return true;
    }

    GoNamesCache namesCache =
        ContainerUtil.findInstance(
            getProject().getExtensions(PsiShortNamesCache.EP_NAME), GoNamesCache.class);

    if (namesCache == null) {
      return true;
    }

    // get the file included in the imported package name
    Collection<GoFile> files = namesCache.getFilesByPackageName(getPackageName());

    for (GoFile file : files) {
      if (!file.processDeclarations(
          processor,
          GoResolveStates.imported(getPackageName(), getVisiblePackageName()),
          null,
          place)) {
        return false;
      }
    }

    return true;
  }
  protected RunnerAndConfigurationSettings createConfigurationByElement(
      Location location, ConfigurationContext context) {

    GoFile goFile = locationToFile(location);

    if (goFile == null) return null;

    if (goFile.getMainFunction() == null) {
      return null;
    }

    return createConfiguration(goFile, context.getModule());
  }
  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());
      }
    }
  }
  //    @Override
  protected void buildDialog(
      Project project, PsiDirectory directory, CreateFileFromTemplateDialog.Builder builder) {

    PsiFile childs[] = directory.getFiles();

    Set<String> packages = new HashSet<String>();

    for (PsiFile child : childs) {
      if (child instanceof GoFile) {
        GoFile goFile = (GoFile) child;

        if (!goFile.getPackage().isMainPackage()) {
          packages.add(goFile.getPackage().getPackageName());
        }
      }
    }

    builder.addKind("New file", GoIcons.GO_ICON_16x16, "single");

    for (String packageName : packages) {
      builder.addKind(
          "New file in library: " + packageName, GoIcons.GO_ICON_16x16, "lib." + packageName);
    }
  }
  private RunnerAndConfigurationSettings createConfiguration(GoFile goFile, Module module) {
    final Project project = goFile.getProject();

    element = goFile;

    RunnerAndConfigurationSettings settings =
        RunManagerEx.getInstanceEx(project).createRunConfiguration("", getConfigurationFactory());
    GoApplicationConfiguration applicationConfiguration =
        (GoApplicationConfiguration) settings.getConfiguration();

    final PsiDirectory dir = goFile.getContainingDirectory();
    if (dir == null) return null;

    applicationConfiguration.setName(goFile.getName());
    VirtualFile scriptFile = goFile.getOriginalFile().getVirtualFile();
    if (scriptFile == null) {
      return null;
    }

    applicationConfiguration.scriptName = scriptFile.getCanonicalPath();
    applicationConfiguration.setModule(module);

    return settings;
  }
  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());
    }
  }
 private static void addImportUnderPackage(GoFile file, Document document, String pathToImport) {
   int insertPoint = file.getPackage().getTextRange().getEndOffset();
   document.insertString(insertPoint, String.format("\n\nimport \"%s\"", pathToImport));
 }