@@ -15,6 +15,10 @@ macro_rules! contract_file {
1515
1616const GRAMMAR_PLUGIN : & str = contract_file ! ( "fixtures/languages/language-grammar.plugin.json" ) ;
1717const HTTP_GRAMMAR : & str = contract_file ! ( "fixtures/languages/grammars/http.tmLanguage.json" ) ;
18+ const FIRST_LINE_PLUGIN : & str = contract_file ! ( "fixtures/languages/first-line.plugin.json" ) ;
19+ const SAMPLE_GRAMMAR : & str = contract_file ! ( "fixtures/languages/grammars/sample.tmLanguage.json" ) ;
20+ const SAMPLE_FIRST_LINE : & str =
21+ contract_file ! ( "fixtures/languages/grammars/sample.first-line.json" ) ;
1822
1923fn manifest ( source : & str ) -> Value {
2024 serde_json:: from_str ( source) . expect ( "fixture manifest is JSON" )
@@ -379,3 +383,177 @@ fn configuration_only_contribution_decodes_without_grammar() {
379383 assert ! ( result[ "language" ] [ "grammar" ] . is_null( ) ) ;
380384 assert_eq ! ( result[ "language" ] [ "configuration" ] [ "lineComment" ] , "#" ) ;
381385}
386+
387+ #[ test]
388+ fn first_line_grammar_uses_the_existing_language_capability ( ) {
389+ let plugin = manifest ( FIRST_LINE_PLUGIN ) ;
390+ let admission = validate_command ( json ! ( {
391+ "manifest" : plugin,
392+ "hostCapabilities" : { "languages" : [ 0 ] } ,
393+ } ) )
394+ . unwrap ( ) ;
395+ assert_eq ! ( admission[ "status" ] , "ok" , "{admission}" ) ;
396+ let outcome = decode_grammar_command ( json ! ( {
397+ "manifest" : admission[ "plugin" ] ,
398+ "languageID" : "sample" ,
399+ "grammar" : SAMPLE_GRAMMAR ,
400+ } ) )
401+ . unwrap ( ) ;
402+ assert_eq ! ( outcome[ "status" ] , "ok" , "{outcome}" ) ;
403+ let language = & outcome[ "language" ] ;
404+ assert_eq ! ( language[ "fileExtensions" ] , json!( [ "sample" ] ) ) ;
405+ assert_eq ! ( language[ "configuration" ] [ "lineComment" ] , "//" ) ;
406+ assert_eq ! ( language[ "grammar" ] [ "scopeName" ] , "source.sample" ) ;
407+ assert_eq ! ( language[ "grammar" ] [ "source" ] , SAMPLE_GRAMMAR ) ;
408+ assert_eq ! (
409+ language[ "grammar" ] [ "firstLineMatch" ] ,
410+ manifest( SAMPLE_GRAMMAR ) [ "firstLineMatch" ]
411+ ) ;
412+ }
413+
414+ #[ test]
415+ fn standalone_first_line_metadata_keeps_the_existing_grammar ( ) {
416+ let mut plugin = manifest ( FIRST_LINE_PLUGIN ) ;
417+ plugin[ "languages" ] [ 0 ]
418+ . as_object_mut ( )
419+ . unwrap ( )
420+ . remove ( "grammar" ) ;
421+ let admission = validate_command ( json ! ( {
422+ "manifest" : plugin,
423+ "hostCapabilities" : { "languages" : [ 0 ] } ,
424+ } ) )
425+ . unwrap ( ) ;
426+ assert_eq ! ( admission[ "status" ] , "ok" , "{admission}" ) ;
427+ let decode = |source : & str | {
428+ decode_grammar_command ( json ! ( {
429+ "manifest" : admission[ "plugin" ] ,
430+ "languageID" : "sample" ,
431+ "grammar" : source,
432+ } ) )
433+ . unwrap ( )
434+ } ;
435+ let decoded = decode ( SAMPLE_FIRST_LINE ) ;
436+ assert_eq ! ( decoded[ "status" ] , "ok" , "{decoded}" ) ;
437+ assert ! ( decoded[ "language" ] [ "grammar" ] . is_null( ) ) ;
438+ assert_eq ! (
439+ decoded[ "language" ] [ "firstLineMatch" ] ,
440+ manifest( SAMPLE_FIRST_LINE ) [ "firstLineMatch" ]
441+ ) ;
442+ assert_eq ! ( decoded[ "language" ] [ "configuration" ] [ "lineComment" ] , "//" ) ;
443+ let unsupported = decode ( r#"{"firstLineMatch":"["}"# ) ;
444+ assert_eq ! ( unsupported[ "status" ] , "ok" , "{unsupported}" ) ;
445+ assert ! ( unsupported[ "language" ] [ "grammar" ] . is_null( ) ) ;
446+ assert ! ( !unsupported[ "language" ]
447+ . as_object( )
448+ . unwrap( )
449+ . contains_key( "firstLineMatch" ) ) ;
450+
451+ for source in [ "invalid JSON" , "[]" , & " " . repeat ( 4097 ) ] {
452+ let result = decode ( source) ;
453+ assert_eq ! ( result[ "status" ] , "rejected" , "{result}" ) ;
454+ assert_eq ! ( result[ "error" ] [ "code" ] , "plugin.grammar.invalid" ) ;
455+ }
456+ }
457+
458+ #[ test]
459+ fn unsupported_first_line_metadata_keeps_its_grammar ( ) {
460+ for pattern in [
461+ Value :: Null ,
462+ json ! ( 42 ) ,
463+ json ! ( "" ) ,
464+ json ! ( "a*" ) ,
465+ json ! ( "[" ) ,
466+ json ! ( r"(a)\1" ) ,
467+ json ! ( "a{10000000}" ) ,
468+ json ! ( "é" . repeat( 257 ) ) ,
469+ ] {
470+ let mut grammar = grammar_with ( json ! ( [ ] ) ) ;
471+ grammar[ "firstLineMatch" ] = pattern;
472+ let source = grammar. to_string ( ) ;
473+ let outcome = decode_grammar ( & source) ;
474+ assert_eq ! ( outcome[ "status" ] , "ok" , "{outcome}" ) ;
475+ let grammar = & outcome[ "language" ] [ "grammar" ] ;
476+ assert ! ( !grammar. as_object( ) . unwrap( ) . contains_key( "firstLineMatch" ) ) ;
477+ assert_eq ! ( grammar[ "scopeName" ] , "source.test" ) ;
478+ assert_eq ! ( grammar[ "source" ] , source) ;
479+ }
480+ }
481+
482+ #[ test]
483+ fn first_line_matching_keeps_registered_priority_and_never_reads_past_the_line ( ) {
484+ let specific = json ! ( {
485+ "languageID" : "sample" ,
486+ "firstLineMatch" : manifest( SAMPLE_GRAMMAR ) [ "firstLineMatch" ] ,
487+ } ) ;
488+ let generic = json ! ( { "languageID" : "other" , "firstLineMatch" : "#!" } ) ;
489+ let choose = |line : & str , candidates : & [ Value ] | {
490+ let response = lithe_plugin_manifest:: match_first_line (
491+ json ! ( { "firstLine" : line, "candidates" : candidates} ) ,
492+ )
493+ . unwrap ( ) ;
494+ response[ "languageID" ] . clone ( )
495+ } ;
496+ for line in [
497+ "#!/opt/tools/sample" ,
498+ "#! /usr/bin/env sample3" ,
499+ "#!/usr/bin/env -S sample3 -f" ,
500+ ] {
501+ assert_eq ! ( choose( line, & [ specific. clone( ) , generic. clone( ) ] ) , "sample" ) ;
502+ assert_eq ! ( choose( line, & [ generic. clone( ) , specific. clone( ) ] ) , "other" ) ;
503+ }
504+ for line in [
505+ "x#!/opt/tools/sample" ,
506+ "#!/opt/tools/sample-extra" ,
507+ "#!/opt/tools/sample\0 " ,
508+ "\n #!/opt/tools/sample" ,
509+ "#!/opt/tools/sample\r " ,
510+ ] {
511+ assert_eq ! ( choose( line, std:: slice:: from_ref( & specific) ) , Value :: Null ) ;
512+ }
513+ assert_eq ! (
514+ choose(
515+ & format!( "#!{}" , "é" . repeat( 511 ) ) ,
516+ std:: slice:: from_ref( & generic)
517+ ) ,
518+ "other"
519+ ) ;
520+ assert_eq ! (
521+ choose(
522+ & format!( "#!{}x" , "é" . repeat( 511 ) ) ,
523+ std:: slice:: from_ref( & generic)
524+ ) ,
525+ Value :: Null
526+ ) ;
527+ assert_eq ! ( choose( "#!/opt/tools/sample" , & [ ] ) , Value :: Null ) ;
528+ // A word boundary alone may match at the start of nonempty text but claims no bytes.
529+ assert_eq ! (
530+ choose(
531+ "word" ,
532+ & [ json!( { "languageID" : "other" , "firstLineMatch" : "\\ b" } ) ]
533+ ) ,
534+ Value :: Null
535+ ) ;
536+ }
537+
538+ #[ test]
539+ fn first_line_matching_validates_the_whole_candidate_batch ( ) {
540+ let entry = json ! ( { "languageID" : "sample" , "firstLineMatch" : "#!" } ) ;
541+ let call = |candidates : Vec < Value > | {
542+ lithe_plugin_manifest:: match_first_line ( json ! ( { "firstLine" : "#!" , "candidates" : candidates} ) )
543+ } ;
544+ assert_eq ! (
545+ call( vec![ entry. clone( ) ; 64 ] ) . unwrap( ) [ "languageID" ] ,
546+ "sample"
547+ ) ;
548+ assert ! ( call( vec![ entry. clone( ) ; 65 ] ) . is_err( ) ) ;
549+ assert ! ( call( vec![
550+ entry. clone( ) ,
551+ json!( { "languageID" : "UPPER" , "firstLineMatch" : "#!" } )
552+ ] )
553+ . is_err( ) ) ;
554+ assert ! ( call( vec![
555+ entry,
556+ json!( { "languageID" : "rest" , "firstLineMatch" : "(" } )
557+ ] )
558+ . is_err( ) ) ;
559+ }
0 commit comments