public override void EnterImportSpec(GoParser.ImportSpecContext context) { // Base class parses current import package path base.EnterImportSpec(context); string alternateName = SanitizedIdentifier(context.IDENTIFIER()?.GetText()); bool useStatic = alternateName is null && context.ChildCount > 1 && context.GetChild(0).GetText().Equals("."); int lastSlash = CurrentImportPath.LastIndexOf('/'); string packageName = SanitizedIdentifier(lastSlash > -1 ? CurrentImportPath.Substring(lastSlash + 1) : CurrentImportPath); string targetUsing = $"{RootNamespace}.{string.Join(".", CurrentImportPath.Split('/').Select(SanitizedIdentifier))}{ClassSuffix}"; string alias; if (useStatic) { alias = $"static {targetUsing}"; } else if (alternateName?.Equals("_") ?? false) { alias = $"_{packageName}_"; } else { alias = alternateName ?? packageName; } m_importAliases[alias] = (CurrentImportPath, targetUsing); }
public override void EnterImportSpec(GoParser.ImportSpecContext context) { // Base class parses current import package path base.EnterImportSpec(context); if (!m_firstImportSpec) { if (!string.IsNullOrEmpty(m_lastImportSpecComment)) { m_targetFile.Append(m_lastImportSpecComment); if (!EndsWithLineFeed(m_lastImportSpecComment)) { m_targetFile.AppendLine(); } } else if (!WroteLineFeed) { m_targetFile.AppendLine(); } } KeyValuePair <string, (string targetImport, string targetUsing)> importAlias = ImportAliases.FirstOrDefault(import => import.Value.targetImport.Equals(CurrentImportPath)); if (!string.IsNullOrEmpty(importAlias.Key)) { string alias = importAlias.Key; string targetUsing = importAlias.Value.targetUsing; string targetImport = importAlias.Value.targetImport; string usingStatement; if (alias.StartsWith("static ", StringComparison.Ordinal)) { usingStatement = $"using {alias};"; } else { usingStatement = $"using {alias} = {targetUsing};"; } m_targetFile.Append(usingStatement); m_usingStatements.Add(usingStatement); FolderMetadata metadata = LoadImportMetadata(Options, targetImport, out string warning); if ((object)metadata == null) { AddWarning(context, warning); } else { ImportMetadata[targetImport] = metadata; } } else { m_targetFile.Append($"//using {RootNamespace}.{string.Join(".", CurrentImportPath.Split('/').Select(SanitizedIdentifier))}{ClassSuffix}; // ?? metadata not found"); AddWarning(context, $"Could not find import metadata for \"{CurrentImportPath}\""); } m_lastImportSpecComment = CheckForCommentsRight(context); m_lastEolImportSpecComment = CheckForEndOfLineComment(context); // Check for comments on lines in-between imports if (!m_lastImportSpecComment.Equals(m_lastEolImportSpecComment)) { if (m_lastImportSpecComment.StartsWith(m_lastEolImportSpecComment)) { m_lastImportSpecComment = m_lastImportSpecComment.Substring(m_lastEolImportSpecComment.Length); } } if (!string.IsNullOrEmpty(m_lastEolImportSpecComment)) { m_targetFile.Append(m_lastEolImportSpecComment); } }