import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/syntactic_entity.dart';
import 'package:source_span/source_span.dart';

/// Returns [SourceSpan] with information about original code for [node] from [source]
SourceSpan nodeLocation({
  required SyntacticEntity node,
  required ResolvedUnitResult source,
  bool withCommentOrMetadata = false,
}) {
  final offset = !withCommentOrMetadata && node is AnnotatedNode
      ? node.firstTokenAfterCommentAndMetadata.offset
      : node.offset;
  final end = node.end;

  final offsetLocation = source.unit?.lineInfo?.getLocation(offset);
  final endLocation = source.unit?.lineInfo?.getLocation(end);

  return SourceSpan(
    SourceLocation(
      offset,
      sourceUrl: source.path,
      line: offsetLocation?.lineNumber,
      column: offsetLocation?.columnNumber,
    ),
    SourceLocation(
      end,
      sourceUrl: source.path,
      line: endLocation?.lineNumber,
      column: endLocation?.columnNumber,
    ),
    source.content!.substring(offset, end),
  );
}
