getSuggestions method

  1. @override
Future<List<CompletionSuggestion>> getSuggestions(
  1. String query,
  2. SuggestionContext context
)
override

Implementation

@override
Future<List<CompletionSuggestion>> getSuggestions(
  String query,
  SuggestionContext context,
) async {
  final search = query.substring(1).toLowerCase(); // Remove '#'
  final suggestions = <CompletionSuggestion>[];

  try {
    final result = await Process.run('git', [
      'branch',
      '-a',
      '--format=%(refname:short)',
    ], workingDirectory: context.currentDirectory);
    if (result.exitCode == 0) {
      final branches = (result.stdout as String)
          .split('\n')
          .where((b) => b.trim().isNotEmpty)
          .toList();

      for (final branch in branches) {
        final name = branch.trim();
        if (search.isEmpty || _fuzzyMatch(name, search)) {
          final isRemote = name.startsWith('origin/');
          suggestions.add(
            CompletionSuggestion(
              value: name,
              displayText: name,
              description: isRemote ? 'Remote branch' : 'Local branch',
              type: SuggestionType.gitBranch,
              icon: isRemote ? 'cloud' : 'branch',
              score: _fuzzyScore(name, search),
            ),
          );
        }
      }
    }

    // Also add recent tags.
    final tagResult = await Process.run('git', [
      'tag',
      '-l',
      '--sort=-version:refname',
    ], workingDirectory: context.currentDirectory);
    if (tagResult.exitCode == 0) {
      final tags = (tagResult.stdout as String)
          .split('\n')
          .where((t) => t.trim().isNotEmpty)
          .take(10)
          .toList();

      for (final tag in tags) {
        final name = tag.trim();
        if (search.isEmpty || _fuzzyMatch(name, search)) {
          suggestions.add(
            CompletionSuggestion(
              value: name,
              displayText: name,
              description: 'Tag',
              type: SuggestionType.gitRef,
              icon: 'tag',
              score:
                  _fuzzyScore(name, search) *
                  0.8, // Slightly lower than branches
            ),
          );
        }
      }
    }
  } catch (_) {}

  suggestions.sort((a, b) => b.score.compareTo(a.score));
  return suggestions.take(15).toList();
}