Text-Treesitter

 view release on metacpan or  search on metacpan

examples/highlight.pl  view on Meta::CPAN

         die "TODO: This fold region starts earlier than the previous one"
            if @fold_regions and $startline < $fold_regions[-1][0];

         next if $startline == $endline;

         push @fold_regions, [ $startline, $endline ];

         pop @regions_applied while @regions_applied and $regions_applied[-1][1] < $startline;
         push @regions_applied, $fold_regions[-1];

         $fold_deepest = scalar @regions_applied if @regions_applied > $fold_deepest;
      }
   }

   if( $INJECTIONS and $query_injections ) {
      $qc->exec( $query_injections, $root );

      while( my $captures = $qc->next_match_captures ) {
         my $sublanguage;
         my $content;
         if( defined $captures->{'injection.language'} ) {
            $sublanguage = $captures->{'injection.language'};
            $content     = $captures->{'injection.content'};
         }
         elsif( defined $captures->{language} ) {
            $sublanguage = $captures->{language};
            $content     = $captures->{content};
         }
         elsif( keys $captures->%* > 1 ) {
            warn "This injection capture yielded more than one name key - ", join( ", ", sort keys $captures->%* ), "\n";
            next;
         }
         else {
            $sublanguage = ( keys $captures->%* )[0];
            $content     = $captures->{$sublanguage};
         }

         $sublanguage = $sublanguage->text if $sublanguage isa Text::Treesitter::Node;

         apply_language_highlights( $sublanguage,
            start_byte => $content->start_byte,
            end_byte   => $content->end_byte,
         );
      }
   }

   $qc->exec( $query_highlight, $root );

   while( my $captures = $qc->next_match_captures ) {
      CAPTURE: foreach my $capturename ( sort keys $captures->%* ) {
         # TODO: actually implement the priority logic
         next if $capturename eq "priority";

         my $node = $captures->{$capturename};

         my $start = $tree->byte_to_char( $node->start_byte );
         my $len   = $tree->byte_to_char( $node->end_byte ) - $start;

         $str->apply_tag( $start, $len, captures => $capturename ) if $CAPTURES;

         my @nameparts = split m/\./, $capturename;
         while( @nameparts ) {
            if( my $format = $FORMATS{ join ".", @nameparts } ) {
               $str->apply_tag( $start, $len, $_, $format->{$_} ) for keys %$format;
               next CAPTURE;
            }

            pop @nameparts;
         }

         $UNRECOGNISED_CAPTURES{ $capturename }++;
      }
   }
}

apply_language_highlights( $LANGUAGE );

foreach my ( $lnum, $line ) ( indexed $str->split( qr/\n/ ) ) {
   if( $FOLDING ) {
      my @regions = grep { $_->[0] <= $lnum and $lnum <= $_->[1] } @fold_regions;
      my $final_here;
      my $markers = join "", map {
         $_->[0] == $lnum ? ( $final_here = true, "┌" )[1] :
         $_->[1] == $lnum ? ( $final_here = true, "â””" )[1] :
                            "│";
      } @regions;

      $markers .= ( $final_here ? "─" : " " ) while length $markers < $fold_deepest;

      print $markers . " ";
   }
   String::Tagged::Terminal->new_from_formatting( $line )
      ->say_to_terminal;
   if( $CAPTURES ) {
      my @captures;
      $line->iter_extents_nooverlap( sub ( $e, %tags ) {
         my $captures = $tags{captures};
         push @captures, $captures // "*" if $e->substr =~ m/\S/;
      }, only => [qw( captures )]);
      print "# ", join( "|", @captures ), "\n" if @captures;
   }
}

if( $PRINT_UNRECOGNISED and keys %UNRECOGNISED_CAPTURES ) {
   print STDERR "-------\nUnrecognised:\n";
   foreach ( sort keys %UNRECOGNISED_CAPTURES ) {
      print STDERR "  $_\n";
   }
}



( run in 1.420 second using v1.01-cache-2.11-cpan-71847e10f99 )